|
| 1 | +import { json, type LoaderFunctionArgs } from "@remix-run/server-runtime"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { $replica, prisma } from "~/db.server"; |
| 4 | +import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server"; |
| 5 | +import { findCurrentWorkerFromEnvironment } from "~/v3/models/workerDeployment.server"; |
| 6 | +import { getEnvironmentFromEnv } from "./api.v1.projects.$projectRef.$env"; |
| 7 | +import { GetWorkerByTagResponse } from "@trigger.dev/core/v3/schemas"; |
| 8 | + |
| 9 | +const ParamsSchema = z.object({ |
| 10 | + projectRef: z.string(), |
| 11 | + tagName: z.string(), |
| 12 | + env: z.enum(["dev", "staging", "prod", "preview"]), |
| 13 | +}); |
| 14 | + |
| 15 | +type ParamsSchema = z.infer<typeof ParamsSchema>; |
| 16 | + |
| 17 | +export async function loader({ request, params }: LoaderFunctionArgs) { |
| 18 | + const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request); |
| 19 | + |
| 20 | + if (!authenticationResult) { |
| 21 | + return json({ error: "Invalid or Missing Access Token" }, { status: 401 }); |
| 22 | + } |
| 23 | + |
| 24 | + const parsedParams = ParamsSchema.safeParse(params); |
| 25 | + |
| 26 | + if (!parsedParams.success) { |
| 27 | + return json({ error: "Invalid Params" }, { status: 400 }); |
| 28 | + } |
| 29 | + |
| 30 | + const { projectRef, env } = parsedParams.data; |
| 31 | + |
| 32 | + const project = await prisma.project.findFirst({ |
| 33 | + where: { |
| 34 | + externalRef: projectRef, |
| 35 | + organization: { |
| 36 | + members: { |
| 37 | + some: { |
| 38 | + userId: authenticationResult.userId, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + if (!project) { |
| 46 | + return json({ error: "Project not found" }, { status: 404 }); |
| 47 | + } |
| 48 | + |
| 49 | + const envResult = await getEnvironmentFromEnv({ |
| 50 | + projectId: project.id, |
| 51 | + userId: authenticationResult.userId, |
| 52 | + env, |
| 53 | + }); |
| 54 | + |
| 55 | + if (!envResult.success) { |
| 56 | + return json({ error: envResult.error }, { status: 404 }); |
| 57 | + } |
| 58 | + |
| 59 | + const runtimeEnv = envResult.environment; |
| 60 | + |
| 61 | + const currentWorker = await findCurrentWorkerFromEnvironment( |
| 62 | + { |
| 63 | + id: runtimeEnv.id, |
| 64 | + type: runtimeEnv.type, |
| 65 | + }, |
| 66 | + $replica, |
| 67 | + params.tagName |
| 68 | + ); |
| 69 | + |
| 70 | + if (!currentWorker) { |
| 71 | + return json({ error: "Worker not found" }, { status: 404 }); |
| 72 | + } |
| 73 | + |
| 74 | + const tasks = await $replica.backgroundWorkerTask.findMany({ |
| 75 | + where: { |
| 76 | + workerId: currentWorker.id, |
| 77 | + }, |
| 78 | + select: { |
| 79 | + friendlyId: true, |
| 80 | + slug: true, |
| 81 | + filePath: true, |
| 82 | + triggerSource: true, |
| 83 | + createdAt: true, |
| 84 | + payloadSchema: true, |
| 85 | + }, |
| 86 | + orderBy: { |
| 87 | + slug: "asc", |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + // Prepare the response object |
| 92 | + const response: GetWorkerByTagResponse = { |
| 93 | + worker: { |
| 94 | + id: currentWorker.friendlyId, |
| 95 | + version: currentWorker.version, |
| 96 | + engine: currentWorker.engine, |
| 97 | + sdkVersion: currentWorker.sdkVersion, |
| 98 | + cliVersion: currentWorker.cliVersion, |
| 99 | + tasks: tasks.map((task) => ({ |
| 100 | + id: task.friendlyId, |
| 101 | + slug: task.slug, |
| 102 | + filePath: task.filePath, |
| 103 | + triggerSource: task.triggerSource, |
| 104 | + createdAt: task.createdAt, |
| 105 | + payloadSchema: task.payloadSchema, |
| 106 | + })), |
| 107 | + }, |
| 108 | + }; |
| 109 | + |
| 110 | + // Optionally validate the response before returning (for type safety) |
| 111 | + // WorkerResponseSchema.parse(response); |
| 112 | + |
| 113 | + return json(response); |
| 114 | +} |
0 commit comments