fix: cron scheduleEmailRemider time out (#12108)
Co-authored-by: CarinaWolli <wollencarina@gmail.com>pull/12095/head
parent
b9cef10ef2
commit
08d65c85de
|
@ -26,6 +26,7 @@ const sendgridAPIKey = process.env.SENDGRID_API_KEY as string;
|
||||||
const senderEmail = process.env.SENDGRID_EMAIL as string;
|
const senderEmail = process.env.SENDGRID_EMAIL as string;
|
||||||
|
|
||||||
sgMail.setApiKey(sendgridAPIKey);
|
sgMail.setApiKey(sendgridAPIKey);
|
||||||
|
client.setApiKey(sendgridAPIKey);
|
||||||
|
|
||||||
type Booking = Prisma.BookingGetPayload<{
|
type Booking = Prisma.BookingGetPayload<{
|
||||||
include: {
|
include: {
|
||||||
|
@ -106,6 +107,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
|
||||||
const pageSize = 90;
|
const pageSize = 90;
|
||||||
let pageNumber = 0;
|
let pageNumber = 0;
|
||||||
|
const deletePromises = [];
|
||||||
|
|
||||||
//delete batch_ids with already past scheduled date from scheduled_sends
|
//delete batch_ids with already past scheduled date from scheduled_sends
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -128,19 +130,25 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const reminder of remindersToDelete) {
|
deletePromises.push(
|
||||||
try {
|
remindersToDelete.map((reminder) =>
|
||||||
await client.request({
|
client.request({
|
||||||
url: `/v3/user/scheduled_sends/${reminder.referenceId}`,
|
url: `/v3/user/scheduled_sends/${reminder.referenceId}`,
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
});
|
})
|
||||||
} catch (error) {
|
)
|
||||||
console.log(`Error deleting batch id from scheduled_sends: ${error}`);
|
);
|
||||||
}
|
|
||||||
}
|
|
||||||
pageNumber++;
|
pageNumber++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Promise.allSettled(deletePromises).then((results) => {
|
||||||
|
results.forEach((result) => {
|
||||||
|
if (result.status === "rejected") {
|
||||||
|
console.log(`Error deleting batch id from scheduled_sends: ${result.reason}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
await prisma.workflowReminder.deleteMany({
|
await prisma.workflowReminder.deleteMany({
|
||||||
where: {
|
where: {
|
||||||
method: WorkflowMethods.EMAIL,
|
method: WorkflowMethods.EMAIL,
|
||||||
|
@ -153,6 +161,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;
|
pageNumber = 0;
|
||||||
|
|
||||||
|
const allPromisesCancelReminders = [];
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const remindersToCancel = await prisma.workflowReminder.findMany({
|
const remindersToCancel = await prisma.workflowReminder.findMany({
|
||||||
where: {
|
where: {
|
||||||
|
@ -175,8 +186,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const reminder of remindersToCancel) {
|
for (const reminder of remindersToCancel) {
|
||||||
try {
|
const cancelPromise = client.request({
|
||||||
await client.request({
|
|
||||||
url: "/v3/user/scheduled_sends",
|
url: "/v3/user/scheduled_sends",
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: {
|
body: {
|
||||||
|
@ -185,7 +195,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await prisma.workflowReminder.update({
|
const updatePromise = prisma.workflowReminder.update({
|
||||||
where: {
|
where: {
|
||||||
id: reminder.id,
|
id: reminder.id,
|
||||||
},
|
},
|
||||||
|
@ -193,14 +203,22 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
scheduled: false, // to know which reminder already got cancelled (to avoid error from cancelling the same reminders again)
|
scheduled: false, // to know which reminder already got cancelled (to avoid error from cancelling the same reminders again)
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
console.log(`Error cancelling scheduled Emails: ${error}`);
|
allPromisesCancelReminders.push(cancelPromise, updatePromise);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pageNumber++;
|
pageNumber++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Promise.allSettled(allPromisesCancelReminders).then((results) => {
|
||||||
|
results.forEach((result) => {
|
||||||
|
if (result.status === "rejected") {
|
||||||
|
console.log(`Error cancelling scheduled_sends: ${result.reason}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
pageNumber = 0;
|
pageNumber = 0;
|
||||||
|
const sendEmailPromises = [];
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
//find all unscheduled Email reminders
|
//find all unscheduled Email reminders
|
||||||
|
@ -390,7 +408,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const batchId = batchIdResponse[1].batch_id;
|
const batchId = batchIdResponse[1].batch_id;
|
||||||
|
|
||||||
if (reminder.workflowStep.action !== WorkflowActions.EMAIL_ADDRESS) {
|
if (reminder.workflowStep.action !== WorkflowActions.EMAIL_ADDRESS) {
|
||||||
await sgMail.send({
|
sendEmailPromises.push(
|
||||||
|
sgMail.send({
|
||||||
to: sendTo,
|
to: sendTo,
|
||||||
from: {
|
from: {
|
||||||
email: senderEmail,
|
email: senderEmail,
|
||||||
|
@ -417,7 +436,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
: undefined,
|
: undefined,
|
||||||
});
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await prisma.workflowReminder.update({
|
await prisma.workflowReminder.update({
|
||||||
|
@ -436,6 +456,15 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
}
|
}
|
||||||
pageNumber++;
|
pageNumber++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Promise.allSettled(sendEmailPromises).then((results) => {
|
||||||
|
results.forEach((result) => {
|
||||||
|
if (result.status === "rejected") {
|
||||||
|
console.log("Email sending failed", result.reason);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
res.status(200).json({ message: "Emails scheduled" });
|
res.status(200).json({ message: "Emails scheduled" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue