Fixes that workflow reminders of cancelled and rescheduled bookings are still sent#1
Fixes that workflow reminders of cancelled and rescheduled bookings are still sent#1sartorijr92 wants to merge 8 commits intobase/pr-7232from
Conversation
|
@kody start-review |
Kody Review CompleteGreat news! 🎉 Keep up the excellent work! 🚀 Kody Guide: Usage and ConfigurationInteracting with Kody
Current Kody ConfigurationReview OptionsThe following review options are enabled or disabled:
|
|
@kody review |
Code Review Completed! 🔥The code review was successfully completed based on your current configurations. Kody Guide: Usage and ConfigurationInteracting with Kody
Current Kody ConfigurationReview OptionsThe following review options are enabled or disabled:
|
| }, | ||
| }); | ||
|
|
||
| try { |
There was a problem hiding this comment.
Premature loop termination on client.request failure causes PrismaPromise deletions to be silently dropped and blocks subsequent reminder processing. Wrap each iteration in a try/catch and await prisma.workflowReminder.delete inline to ensure individual referenceId errors do not abort the batch.
for (const reminder of remindersToCancel) {
try {
await client.request({
url: "/v3/user/scheduled_sends",
method: "POST",
body: {
batch_id: reminder.referenceId,
status: "cancel",
},
});
await prisma.workflowReminder.delete({
where: {
id: reminder.id,
},
});
} catch (error) {
console.log(`Error cancelling scheduled Email for reminder ${reminder.id}: ${error}`);
}
}Prompt for LLM
File packages/features/ee/workflows/api/scheduleEmailReminders.ts:
Line 53:
WHAT: A single failing `client.request` inside the for-loop jumps to the catch block, leaving `await Promise.all(workflowRemindersToDelete)` unreachable; because Prisma's `prisma.workflowReminder.delete(...)` returns a lazy PrismaPromise that only executes when awaited, every delete collected for already-cancelled reminders is silently dropped. WHY: cancelled reminders stay in the DB with `cancelled: true`, and any reminder positioned after the failing one is never processed this run — if a particular referenceId consistently fails (stale or invalid SendGrid batch ID), all subsequent cancellations remain stuck forever and the emails they were meant to cancel will be sent. HOW: wrap each iteration in its own try/catch so one bad reminder cannot abort the batch, and only delete the DB row after its SendGrid cancel succeeded (await the prisma delete inline rather than relying on a deferred Promise.all that the catch can skip).
Suggested Code:
for (const reminder of remindersToCancel) {
try {
await client.request({
url: "/v3/user/scheduled_sends",
method: "POST",
body: {
batch_id: reminder.referenceId,
status: "cancel",
},
});
await prisma.workflowReminder.delete({
where: {
id: reminder.id,
},
});
} catch (error) {
console.log(`Error cancelling scheduled Email for reminder ${reminder.id}: ${error}`);
}
}
Talk to Kody by mentioning @kody
Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.
What does this PR do?
Fixes that workflow emails are still sent for cancelled and rescheduled bookings. The fix of PR calcom#6991 was wrong and didn't work as expected.
The SendGrid endpoint
DELETE /v3/user/scheduled_sends/${referenceId}was not used correctly. This endpoint deletes a cancelled scheduled email fromscheduled_sendsby removing the 'cancel' status (so email will again be scheduled).The reason why cancelling our scheduled emails stopped working is that all cancerlled emails are saved in
scheduled_sendsuntil the scheduled date but the max. of pending cancellations is 100. Once we reached 100 pending cancellations, new cancellations failed and emails were still sent out.The solution implemented in this PR:
cancelledis added to theworkflowRemindermodelscheduleEmailRemindersis responsible for the final cancellation of the scheduled email (runs every 15 mins)Fixes calcom#7225
Environment: Staging(main branch) / Production
Type of change