import { defineDurable } from "@zapier/zapier-durable"; import { createZapierSdk } from "@zapier/zapier-sdk"; import { TEMPLATE_DOC_ID, DOC_FOLDER, ORG_DOMAIN, TITLE_PREFIX, CLICKUP_WORKSPACE_ID, DRAFTER_EMAIL, } from "./config.ts"; const sdk = createZapierSdk(); // Email by Zapier sends from zapiermail.com. const COLLABORATOR_EMAILS = ["REPLACE_COLLABORATOR_EMAIL"]; // Ask the user who gets the ready email, one address per entry. // Days after the run date that the drafting task is due. const DUE_OFFSET_DAYS = 7; const MONTHS = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ]; const workflow = defineDurable( "create-monthly-investor-update-and-assign-drafting-task", async (ctx) => { const when = await ctx.step("capture-run-details", async () => { const runAt = new Date(); return { startedAt: runAt.toISOString(), label: `${MONTHS[runAt.getUTCMonth()]} ${runAt.getUTCFullYear()}`, dueAt: new Date( runAt.getTime() + DUE_OFFSET_DAYS * 86400000, ).toISOString(), }; }); const docTitle = `${TITLE_PREFIX}Investor update ${when.label}`; const taskTitle = `${TITLE_PREFIX}Draft investor update ${when.label}`; const created = await ctx.step("create-investor-update-doc", async () => sdk.runAction({ appKey: "google-docs", actionType: "write", actionKey: "create_from_template", connection: "google-docs", inputs: { file: TEMPLATE_DOC_ID, title: docTitle, folder: DOC_FOLDER, domain: ORG_DOMAIN, sharing_preference: "org_link_edit", empty_fields_preference: "keep", template_field__report_date: when.label, }, }), ); const doc = created.data[0] as { alternateLink?: string } | undefined; if (!doc?.alternateLink) { return { skipped: true, reason: "doc-not-created" }; } const existing = await ctx.step("find-drafting-task", async () => sdk.runAction({ appKey: "clickup", actionType: "write", actionKey: "ae_39348_clickup_find_task_by_name_custom_action", connection: "clickup", inputs: { taskName: taskTitle, workspaceId: CLICKUP_WORKSPACE_ID }, }), ); if (existing.data.length === 0) { await ctx.step("create-drafting-task", async () => sdk.runAction({ appKey: "clickup", actionType: "write", actionKey: "task", connection: "clickup", inputs: { name: taskTitle, content: doc.alternateLink, team_id: Number(CLICKUP_WORKSPACE_ID), due_date: when.dueAt, start_date: when.startedAt, assignees: [DRAFTER_EMAIL], }, }), ); } const email = await ctx.step("email-collaborators", async () => sdk.runAction({ appKey: "email", actionType: "write", actionKey: "outbound", inputs: { to: COLLABORATOR_EMAILS.join(","), subject: `${docTitle} is ready`, body: `
The ${when.label} investor update is ready: ${docTitle}
`, }, }), ); return { docTitle, docLink: doc.alternateLink, taskTitle, taskExisted: existing.data.length > 0, emailed: COLLABORATOR_EMAILS, emailResult: email.data[0] ?? null, }; }, ); export default workflow;