|
| 1 | +import { type ActionFunctionArgs, json } from "@remix-run/server-runtime"; |
| 2 | +import { |
| 3 | + type CompleteWaitpointTokenResponseBody, |
| 4 | + conditionallyExportPacket, |
| 5 | + stringifyIO, |
| 6 | +} from "@trigger.dev/core/v3"; |
| 7 | +import { WaitpointId } from "@trigger.dev/core/v3/isomorphic"; |
| 8 | +import { z } from "zod"; |
| 9 | +import { $replica } from "~/db.server"; |
| 10 | +import { logger } from "~/services/logger.server"; |
| 11 | +import { engine } from "~/v3/runEngine.server"; |
| 12 | + |
| 13 | +const paramsSchema = z.object({ |
| 14 | + waitpointFriendlyId: z.string(), |
| 15 | +}); |
| 16 | + |
| 17 | +export async function action({ request, params }: ActionFunctionArgs) { |
| 18 | + if (request.method.toUpperCase() !== "POST") { |
| 19 | + return json({ error: "Method not allowed" }, { status: 405, headers: { Allow: "POST" } }); |
| 20 | + } |
| 21 | + |
| 22 | + const { waitpointFriendlyId } = paramsSchema.parse(params); |
| 23 | + const waitpointId = WaitpointId.toId(waitpointFriendlyId); |
| 24 | + |
| 25 | + try { |
| 26 | + //check permissions |
| 27 | + const waitpoint = await $replica.waitpoint.findFirst({ |
| 28 | + where: { |
| 29 | + id: waitpointId, |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
| 33 | + if (!waitpoint) { |
| 34 | + throw json({ error: "Waitpoint not found" }, { status: 404 }); |
| 35 | + } |
| 36 | + |
| 37 | + if (waitpoint.status === "COMPLETED") { |
| 38 | + return json<CompleteWaitpointTokenResponseBody>({ |
| 39 | + success: true, |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + const body = await request.json(); |
| 44 | + |
| 45 | + const stringifiedData = await stringifyIO(body); |
| 46 | + const finalData = await conditionallyExportPacket( |
| 47 | + stringifiedData, |
| 48 | + `${waitpointId}/waitpoint/http-callback` |
| 49 | + ); |
| 50 | + |
| 51 | + const result = await engine.completeWaitpoint({ |
| 52 | + id: waitpointId, |
| 53 | + output: finalData.data |
| 54 | + ? { type: finalData.dataType, value: finalData.data, isError: false } |
| 55 | + : undefined, |
| 56 | + }); |
| 57 | + |
| 58 | + return json<CompleteWaitpointTokenResponseBody>( |
| 59 | + { |
| 60 | + success: true, |
| 61 | + }, |
| 62 | + { status: 200 } |
| 63 | + ); |
| 64 | + } catch (error) { |
| 65 | + logger.error("Failed to complete waitpoint token", { error }); |
| 66 | + throw json({ error: "Failed to complete waitpoint token" }, { status: 500 }); |
| 67 | + } |
| 68 | +} |
0 commit comments