-
Notifications
You must be signed in to change notification settings - Fork 12
Comprehensive workflow reminder management for booking lifecycle events #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: workflow-queue-base
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ import type { NextApiRequest, NextApiResponse } from "next"; | |||||
| import dayjs from "@calcom/dayjs"; | ||||||
| import { defaultHandler } from "@calcom/lib/server"; | ||||||
| import prisma from "@calcom/prisma"; | ||||||
| import { Prisma, WorkflowReminder } from "@calcom/prisma/client"; | ||||||
| import { bookingMetadataSchema } from "@calcom/prisma/zod-utils"; | ||||||
|
|
||||||
| import customTemplate, { VariablesType } from "../lib/reminders/templates/customTemplate"; | ||||||
|
|
@@ -39,6 +40,42 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { | |||||
| }, | ||||||
| }); | ||||||
|
|
||||||
| //cancel reminders for cancelled/rescheduled bookings that are scheduled within the next hour | ||||||
| const remindersToCancel = await prisma.workflowReminder.findMany({ | ||||||
| where: { | ||||||
| cancelled: true, | ||||||
| scheduledDate: { | ||||||
| lte: dayjs().add(1, "hour").toISOString(), | ||||||
| }, | ||||||
| }, | ||||||
| }); | ||||||
|
|
||||||
| try { | ||||||
| const workflowRemindersToDelete: Prisma.Prisma__WorkflowReminderClient<WorkflowReminder, never>[] = []; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. syntax: Incorrect type definition. Should be
Suggested change
|
||||||
|
|
||||||
| for (const reminder of remindersToCancel) { | ||||||
| await client.request({ | ||||||
| url: "/v3/user/scheduled_sends", | ||||||
| method: "POST", | ||||||
| body: { | ||||||
| batch_id: reminder.referenceId, | ||||||
| status: "cancel", | ||||||
| }, | ||||||
| }); | ||||||
|
Comment on lines
+56
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Missing null check for |
||||||
|
|
||||||
| const workflowReminderToDelete = prisma.workflowReminder.delete({ | ||||||
| where: { | ||||||
| id: reminder.id, | ||||||
| }, | ||||||
| }); | ||||||
|
|
||||||
| workflowRemindersToDelete.push(workflowReminderToDelete); | ||||||
| } | ||||||
| await Promise.all(workflowRemindersToDelete); | ||||||
| } catch (error) { | ||||||
| console.log(`Error cancelling scheduled Emails: ${error}`); | ||||||
| } | ||||||
|
|
||||||
| //find all unscheduled Email reminders | ||||||
| const unscheduledReminders = await prisma.workflowReminder.findMany({ | ||||||
| where: { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -194,20 +194,41 @@ export const scheduleEmailReminder = async ( | |
| } | ||
| }; | ||
|
|
||
| export const deleteScheduledEmailReminder = async (referenceId: string) => { | ||
| export const deleteScheduledEmailReminder = async ( | ||
| reminderId: number, | ||
| referenceId: string | null, | ||
| immediateDelete?: boolean | ||
| ) => { | ||
| try { | ||
| await client.request({ | ||
| url: "/v3/user/scheduled_sends", | ||
| method: "POST", | ||
| body: { | ||
| batch_id: referenceId, | ||
| status: "cancel", | ||
| }, | ||
| }); | ||
| if (!referenceId) { | ||
| await prisma.workflowReminder.delete({ | ||
| where: { | ||
| id: reminderId, | ||
| }, | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| await client.request({ | ||
| url: `/v3/user/scheduled_sends/${referenceId}`, | ||
| method: "DELETE", | ||
| if (immediateDelete) { | ||
| await client.request({ | ||
| url: "/v3/user/scheduled_sends", | ||
| method: "POST", | ||
| body: { | ||
| batch_id: referenceId, | ||
| status: "cancel", | ||
| }, | ||
| }); | ||
| return; | ||
|
Comment on lines
+213
to
+222
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: When |
||
| } | ||
|
|
||
| await prisma.workflowReminder.update({ | ||
| where: { | ||
| id: reminderId, | ||
| }, | ||
| data: { | ||
| cancelled: true, | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| console.log(`Error canceling reminder with error ${error}`); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| -- AlterTable | ||
| ALTER TABLE "WorkflowReminder" ADD COLUMN "cancelled" BOOLEAN; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider adding a default value (likely FALSE) to ensure consistent behavior for existing and new records |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: The reminder cancellation logic should await the deletion operations to ensure they complete before proceeding with the reschedule