|
| 1 | +"use server"; |
| 2 | + |
| 3 | +import type { articleWorkflow } from "@/trigger/articleWorkflow"; |
| 4 | +import type { ReviewPayload } from "@/trigger/reviewSummary"; |
| 5 | +import { tasks, wait } from "@trigger.dev/sdk/v3"; |
| 6 | + |
| 7 | +// A user identifier that could be fetched from your auth mechanism. |
| 8 | +// This is out of scope for this example, so we just hardcode it. |
| 9 | +const user = "reactflowtest"; |
| 10 | +const userTag = `user_${user}`; |
| 11 | + |
| 12 | +const randomStr = (length: number) => |
| 13 | + [...Array(length)] |
| 14 | + .map( |
| 15 | + () => |
| 16 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"[ |
| 17 | + Math.floor(Math.random() * 62) |
| 18 | + ] |
| 19 | + ) |
| 20 | + .join(""); |
| 21 | + |
| 22 | +export async function triggerArticleWorkflow(prevState: any, formData: FormData) { |
| 23 | + const articleUrl = formData.get("articleUrl") as string; |
| 24 | + const uniqueTag = `reactflow_${randomStr(20)}`; |
| 25 | + |
| 26 | + const reviewWaitpointToken = await wait.createToken({ |
| 27 | + tags: [uniqueTag, userTag], |
| 28 | + timeout: "1h", |
| 29 | + idempotencyKey: `review-summary-${uniqueTag}`, |
| 30 | + }); |
| 31 | + |
| 32 | + const handle = await tasks.trigger<typeof articleWorkflow>( |
| 33 | + "article-workflow", |
| 34 | + { |
| 35 | + articleUrl, |
| 36 | + approvalWaitpointTokenId: reviewWaitpointToken.id, |
| 37 | + }, |
| 38 | + { |
| 39 | + tags: [uniqueTag, userTag], |
| 40 | + } |
| 41 | + ); |
| 42 | + |
| 43 | + return { |
| 44 | + articleUrl, |
| 45 | + runId: handle.id, |
| 46 | + runTag: uniqueTag, |
| 47 | + reviewWaitpointTokenId: reviewWaitpointToken.id, |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +export async function approveArticleSummary(tokenId: string) { |
| 52 | + await wait.completeToken<ReviewPayload>( |
| 53 | + { id: tokenId }, |
| 54 | + { |
| 55 | + approved: true, |
| 56 | + approvedAt: new Date(), |
| 57 | + approvedBy: user, |
| 58 | + } |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +export async function rejectArticleSummary(tokenId: string) { |
| 63 | + await wait.completeToken<ReviewPayload>( |
| 64 | + { id: tokenId }, |
| 65 | + { |
| 66 | + approved: false, |
| 67 | + rejectedAt: new Date(), |
| 68 | + rejectedBy: user, |
| 69 | + reason: "It's no good", |
| 70 | + } |
| 71 | + ); |
| 72 | +} |
0 commit comments