-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathapi.v1.waitpoints.tokens.$waitpointFriendlyId.complete.ts
More file actions
81 lines (73 loc) · 2.49 KB
/
api.v1.waitpoints.tokens.$waitpointFriendlyId.complete.ts
File metadata and controls
81 lines (73 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { json } from "@remix-run/server-runtime";
import {
CompleteWaitpointTokenRequestBody,
type CompleteWaitpointTokenResponseBody,
stringifyIO,
} from "@trigger.dev/core/v3";
import { WaitpointId } from "@trigger.dev/core/v3/isomorphic";
import { z } from "zod";
import { $replica } from "~/db.server";
import { env } from "~/env.server";
import { logger } from "~/services/logger.server";
import { processWaitpointCompletionPacket } from "~/runEngine/concerns/waitpointCompletionPacket.server";
import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server";
import { engine } from "~/v3/runEngine.server";
const { action, loader } = createActionApiRoute(
{
params: z.object({
waitpointFriendlyId: z.string(),
}),
body: CompleteWaitpointTokenRequestBody,
maxContentLength: env.TASK_PAYLOAD_MAXIMUM_SIZE,
allowJWT: true,
authorization: {
action: "write",
resource: (params) => ({ waitpoints: params.waitpointFriendlyId }),
superScopes: ["write:waitpoints", "admin"],
},
corsStrategy: "all",
},
async ({ authentication, body, params }) => {
// Resume tokens are actually just waitpoints
const waitpointId = WaitpointId.toId(params.waitpointFriendlyId);
try {
//check permissions
const waitpoint = await $replica.waitpoint.findFirst({
where: {
id: waitpointId,
environmentId: authentication.environment.id,
},
});
if (!waitpoint) {
throw json({ error: "Waitpoint not found" }, { status: 404 });
}
if (waitpoint.status === "COMPLETED") {
return json<CompleteWaitpointTokenResponseBody>({
success: true,
});
}
const stringifiedData = await stringifyIO(body.data);
const finalData = await processWaitpointCompletionPacket(
stringifiedData,
authentication.environment,
`${WaitpointId.toFriendlyId(waitpointId)}/token`
);
const result = await engine.completeWaitpoint({
id: waitpointId,
output: finalData.data
? { type: finalData.dataType, value: finalData.data, isError: false }
: undefined,
});
return json<CompleteWaitpointTokenResponseBody>(
{
success: true,
},
{ status: 200 }
);
} catch (error) {
logger.error("Failed to complete waitpoint token", { error });
throw json({ error: "Failed to complete waitpoint token" }, { status: 500 });
}
}
);
export { action, loader };