|
| 1 | +import { type ActionFunctionArgs, json } from "@remix-run/server-runtime"; |
| 2 | +import { AddTagsRequestBody, parsePacket, UpdateMetadataRequestBody } from "@trigger.dev/core/v3"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { prisma } from "~/db.server"; |
| 5 | +import { createTag, getTagsForRunId, MAX_TAGS_PER_RUN } from "~/models/taskRunTag.server"; |
| 6 | +import { authenticateApiRequest } from "~/services/apiAuth.server"; |
| 7 | +import { handleMetadataPacket } from "~/utils/packets"; |
| 8 | +import { ServiceValidationError } from "~/v3/services/baseService.server"; |
| 9 | +import { FINAL_RUN_STATUSES } from "~/v3/taskStatus"; |
| 10 | + |
| 11 | +const ParamsSchema = z.object({ |
| 12 | + runId: z.string(), |
| 13 | +}); |
| 14 | + |
| 15 | +export async function action({ request, params }: ActionFunctionArgs) { |
| 16 | + // Ensure this is a POST request |
| 17 | + if (request.method.toUpperCase() !== "PUT") { |
| 18 | + return { status: 405, body: "Method Not Allowed" }; |
| 19 | + } |
| 20 | + |
| 21 | + // Authenticate the request |
| 22 | + const authenticationResult = await authenticateApiRequest(request); |
| 23 | + if (!authenticationResult) { |
| 24 | + return json({ error: "Invalid or Missing API Key" }, { status: 401 }); |
| 25 | + } |
| 26 | + |
| 27 | + const parsedParams = ParamsSchema.safeParse(params); |
| 28 | + if (!parsedParams.success) { |
| 29 | + return json( |
| 30 | + { error: "Invalid request parameters", issues: parsedParams.error.issues }, |
| 31 | + { status: 400 } |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + try { |
| 36 | + const anyBody = await request.json(); |
| 37 | + |
| 38 | + const body = UpdateMetadataRequestBody.safeParse(anyBody); |
| 39 | + |
| 40 | + if (!body.success) { |
| 41 | + return json({ error: "Invalid request body", issues: body.error.issues }, { status: 400 }); |
| 42 | + } |
| 43 | + |
| 44 | + const metadataPacket = handleMetadataPacket( |
| 45 | + body.data.metadata, |
| 46 | + body.data.metadataType ?? "application/json" |
| 47 | + ); |
| 48 | + |
| 49 | + if (!metadataPacket) { |
| 50 | + return json({ error: "Invalid metadata" }, { status: 400 }); |
| 51 | + } |
| 52 | + |
| 53 | + const taskRun = await prisma.taskRun.findFirst({ |
| 54 | + where: { |
| 55 | + friendlyId: parsedParams.data.runId, |
| 56 | + runtimeEnvironmentId: authenticationResult.environment.id, |
| 57 | + }, |
| 58 | + select: { |
| 59 | + status: true, |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + if (!taskRun) { |
| 64 | + return json({ error: "Task Run not found" }, { status: 404 }); |
| 65 | + } |
| 66 | + |
| 67 | + if (FINAL_RUN_STATUSES.includes(taskRun.status)) { |
| 68 | + return json({ error: "Cannot update metadata for a completed run" }, { status: 400 }); |
| 69 | + } |
| 70 | + |
| 71 | + await prisma.taskRun.update({ |
| 72 | + where: { |
| 73 | + friendlyId: parsedParams.data.runId, |
| 74 | + runtimeEnvironmentId: authenticationResult.environment.id, |
| 75 | + }, |
| 76 | + data: { |
| 77 | + metadata: metadataPacket?.data, |
| 78 | + metadataType: metadataPacket?.dataType, |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + const parsedPacket = await parsePacket(metadataPacket); |
| 83 | + |
| 84 | + return json({ metadata: parsedPacket }, { status: 200 }); |
| 85 | + } catch (error) { |
| 86 | + if (error instanceof ServiceValidationError) { |
| 87 | + return json({ error: error.message }, { status: error.status ?? 422 }); |
| 88 | + } else { |
| 89 | + return json( |
| 90 | + { error: error instanceof Error ? error.message : "Internal Server Error" }, |
| 91 | + { status: 500 } |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments