fix: scheduleEmailReminders cron job (#11929)

Co-authored-by: CarinaWolli <wollencarina@gmail.com>
This commit is contained in:
Carina Wollendorfer 2023-10-17 20:19:39 +02:00 committed by GitHub
parent 4e4d67c8c0
commit 4b8bdeba74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 264 additions and 221 deletions

View File

@ -99,7 +99,11 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const sandboxMode = process.env.NEXT_PUBLIC_IS_E2E ? true : false;
const pageSize = 90;
let pageNumber = 0;
//delete batch_ids with already past scheduled date from scheduled_sends
while (true) {
const remindersToDelete = await prisma.workflowReminder.findMany({
where: {
method: WorkflowMethods.EMAIL,
@ -108,8 +112,14 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
lte: dayjs().toISOString(),
},
},
skip: pageNumber * pageSize,
take: pageSize,
});
if (remindersToDelete.length === 0) {
break;
}
for (const reminder of remindersToDelete) {
try {
await client.request({
@ -120,6 +130,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
console.log(`Error deleting batch id from scheduled_sends: ${error}`);
}
}
pageNumber++;
}
await prisma.workflowReminder.deleteMany({
where: {
@ -131,6 +143,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
});
//cancel reminders for cancelled/rescheduled bookings that are scheduled within the next hour
pageNumber = 0;
while (true) {
const remindersToCancel = await prisma.workflowReminder.findMany({
where: {
cancelled: true,
@ -139,8 +154,14 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
lte: dayjs().add(1, "hour").toISOString(),
},
},
skip: pageNumber * pageSize,
take: pageSize,
});
if (remindersToCancel.length === 0) {
break;
}
for (const reminder of remindersToCancel) {
try {
await client.request({
@ -164,7 +185,12 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
console.log(`Error cancelling scheduled Emails: ${error}`);
}
}
pageNumber++;
}
pageNumber = 0;
while (true) {
//find all unscheduled Email reminders
const unscheduledReminders = await prisma.workflowReminder.findMany({
where: {
@ -175,6 +201,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
},
OR: [{ cancelled: false }, { cancelled: null }],
},
skip: pageNumber * pageSize,
take: pageSize,
include: {
workflowStep: true,
booking: {
@ -187,11 +215,15 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
},
});
if (!unscheduledReminders.length) {
if (!unscheduledReminders.length && pageNumber === 0) {
res.status(200).json({ message: "No Emails to schedule" });
return;
}
if (unscheduledReminders.length === 0) {
break;
}
for (const reminder of unscheduledReminders) {
if (!reminder.workflowStep || !reminder.booking) {
continue;
@ -233,7 +265,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
let emailContent = {
emailSubject: reminder.workflowStep.emailSubject || "",
emailBody: `<body style="white-space: pre-wrap;">${reminder.workflowStep.reminderBody || ""}</body>`,
emailBody: `<body style="white-space: pre-wrap;">${
reminder.workflowStep.reminderBody || ""
}</body>`,
};
let emailBodyEmpty = false;
@ -351,6 +385,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
console.log(`Error scheduling Email with error ${error}`);
}
}
pageNumber++;
}
res.status(200).json({ message: "Emails scheduled" });
}

View File

@ -0,0 +1,5 @@
-- CreateIndex
CREATE INDEX "WorkflowReminder_method_scheduled_scheduledDate_idx" ON "WorkflowReminder"("method", "scheduled", "scheduledDate");
-- CreateIndex
CREATE INDEX "WorkflowReminder_cancelled_scheduledDate_idx" ON "WorkflowReminder"("cancelled", "scheduledDate");

View File

@ -820,6 +820,8 @@ model WorkflowReminder {
@@index([bookingUid])
@@index([workflowStepId])
@@index([seatReferenceId])
@@index([method, scheduled, scheduledDate])
@@index([cancelled, scheduledDate])
}
model WebhookScheduledTriggers {