fix: scheduleEmailReminders cron job (#11929)
Co-authored-by: CarinaWolli <wollencarina@gmail.com>pull/11717/head
parent
4e4d67c8c0
commit
4b8bdeba74
|
@ -99,7 +99,11 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
|
||||||
const sandboxMode = process.env.NEXT_PUBLIC_IS_E2E ? true : false;
|
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
|
//delete batch_ids with already past scheduled date from scheduled_sends
|
||||||
|
while (true) {
|
||||||
const remindersToDelete = await prisma.workflowReminder.findMany({
|
const remindersToDelete = await prisma.workflowReminder.findMany({
|
||||||
where: {
|
where: {
|
||||||
method: WorkflowMethods.EMAIL,
|
method: WorkflowMethods.EMAIL,
|
||||||
|
@ -108,8 +112,14 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
lte: dayjs().toISOString(),
|
lte: dayjs().toISOString(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
skip: pageNumber * pageSize,
|
||||||
|
take: pageSize,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (remindersToDelete.length === 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
for (const reminder of remindersToDelete) {
|
for (const reminder of remindersToDelete) {
|
||||||
try {
|
try {
|
||||||
await client.request({
|
await client.request({
|
||||||
|
@ -120,6 +130,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
console.log(`Error deleting batch id from scheduled_sends: ${error}`);
|
console.log(`Error deleting batch id from scheduled_sends: ${error}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pageNumber++;
|
||||||
|
}
|
||||||
|
|
||||||
await prisma.workflowReminder.deleteMany({
|
await prisma.workflowReminder.deleteMany({
|
||||||
where: {
|
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
|
//cancel reminders for cancelled/rescheduled bookings that are scheduled within the next hour
|
||||||
|
|
||||||
|
pageNumber = 0;
|
||||||
|
while (true) {
|
||||||
const remindersToCancel = await prisma.workflowReminder.findMany({
|
const remindersToCancel = await prisma.workflowReminder.findMany({
|
||||||
where: {
|
where: {
|
||||||
cancelled: true,
|
cancelled: true,
|
||||||
|
@ -139,8 +154,14 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
lte: dayjs().add(1, "hour").toISOString(),
|
lte: dayjs().add(1, "hour").toISOString(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
skip: pageNumber * pageSize,
|
||||||
|
take: pageSize,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (remindersToCancel.length === 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
for (const reminder of remindersToCancel) {
|
for (const reminder of remindersToCancel) {
|
||||||
try {
|
try {
|
||||||
await client.request({
|
await client.request({
|
||||||
|
@ -164,7 +185,12 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
console.log(`Error cancelling scheduled Emails: ${error}`);
|
console.log(`Error cancelling scheduled Emails: ${error}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pageNumber++;
|
||||||
|
}
|
||||||
|
|
||||||
|
pageNumber = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
//find all unscheduled Email reminders
|
//find all unscheduled Email reminders
|
||||||
const unscheduledReminders = await prisma.workflowReminder.findMany({
|
const unscheduledReminders = await prisma.workflowReminder.findMany({
|
||||||
where: {
|
where: {
|
||||||
|
@ -175,6 +201,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
},
|
},
|
||||||
OR: [{ cancelled: false }, { cancelled: null }],
|
OR: [{ cancelled: false }, { cancelled: null }],
|
||||||
},
|
},
|
||||||
|
skip: pageNumber * pageSize,
|
||||||
|
take: pageSize,
|
||||||
include: {
|
include: {
|
||||||
workflowStep: true,
|
workflowStep: true,
|
||||||
booking: {
|
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" });
|
res.status(200).json({ message: "No Emails to schedule" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (unscheduledReminders.length === 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
for (const reminder of unscheduledReminders) {
|
for (const reminder of unscheduledReminders) {
|
||||||
if (!reminder.workflowStep || !reminder.booking) {
|
if (!reminder.workflowStep || !reminder.booking) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -233,7 +265,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
|
||||||
let emailContent = {
|
let emailContent = {
|
||||||
emailSubject: reminder.workflowStep.emailSubject || "",
|
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;
|
let emailBodyEmpty = false;
|
||||||
|
@ -351,6 +385,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
console.log(`Error scheduling Email with error ${error}`);
|
console.log(`Error scheduling Email with error ${error}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pageNumber++;
|
||||||
|
}
|
||||||
res.status(200).json({ message: "Emails scheduled" });
|
res.status(200).json({ message: "Emails scheduled" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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");
|
|
@ -820,6 +820,8 @@ model WorkflowReminder {
|
||||||
@@index([bookingUid])
|
@@index([bookingUid])
|
||||||
@@index([workflowStepId])
|
@@index([workflowStepId])
|
||||||
@@index([seatReferenceId])
|
@@index([seatReferenceId])
|
||||||
|
@@index([method, scheduled, scheduledDate])
|
||||||
|
@@index([cancelled, scheduledDate])
|
||||||
}
|
}
|
||||||
|
|
||||||
model WebhookScheduledTriggers {
|
model WebhookScheduledTriggers {
|
||||||
|
|
Loading…
Reference in New Issue