From b42f0ffadd525c8ed6d79e1034e8122dbe0e8036 Mon Sep 17 00:00:00 2001 From: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com> Date: Mon, 1 Aug 2022 06:20:21 -0400 Subject: [PATCH] Fixes unique constraint error of batchId when scheduling emails (#3542) * make batchid unique + return response status 200 * use correct batchId * change for loop and updateMany Co-authored-by: CarinaWolli Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../workflows/api/scheduleEmailReminders.ts | 43 +++++++++++-------- .../ee/workflows/api/scheduleSMSReminders.ts | 7 +-- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/packages/features/ee/workflows/api/scheduleEmailReminders.ts b/packages/features/ee/workflows/api/scheduleEmailReminders.ts index e4b6882867..4d92d23029 100644 --- a/packages/features/ee/workflows/api/scheduleEmailReminders.ts +++ b/packages/features/ee/workflows/api/scheduleEmailReminders.ts @@ -27,11 +27,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { return; } - const batchIdResponse = await client.request({ - url: "/v3/mail/batch", - method: "POST", - }); - //delete all scheduled email reminders where scheduled is past current date await prisma.workflowReminder.deleteMany({ where: { @@ -60,11 +55,14 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { }, }); - if (!unscheduledReminders.length) res.json({ ok: true }); + if (!unscheduledReminders.length) { + res.status(200).json({ message: "No Emails to schedule" }); + return; + } const dateInSeventyTwoHours = dayjs().add(72, "hour"); - unscheduledReminders.forEach(async (reminder) => { + for (const reminder of unscheduledReminders) { if (dayjs(reminder.scheduledDate).isBefore(dateInSeventyTwoHours)) { try { const sendTo = @@ -107,30 +105,39 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { break; } if (emailContent.emailSubject.length > 0 && emailContent.emailBody.text.length > 0 && sendTo) { + const batchIdResponse = await client.request({ + url: "/v3/mail/batch", + method: "POST", + }); + + const batchId = batchIdResponse[1].batch_id; + await sgMail.send({ to: sendTo, from: senderEmail, subject: emailContent.emailSubject, text: emailContent.emailBody.text, html: emailContent.emailBody.html, - batchId: batchIdResponse[1].batch_id, + batchId: batchId, sendAt: dayjs(reminder.scheduledDate).unix(), }); + + await prisma.workflowReminder.update({ + where: { + id: reminder.id, + }, + data: { + scheduled: true, + referenceId: batchId, + }, + }); } - await prisma.workflowReminder.updateMany({ - where: { - id: reminder.id, - }, - data: { - scheduled: true, - referenceId: batchIdResponse[1].batch_id, - }, - }); } catch (error) { console.log(`Error scheduling Email with error ${error}`); } } - }); + } + res.status(200).json({ message: "Emails scheduled" }); } export default defaultHandler({ diff --git a/packages/features/ee/workflows/api/scheduleSMSReminders.ts b/packages/features/ee/workflows/api/scheduleSMSReminders.ts index 8a0d7d0034..9afc0245e7 100644 --- a/packages/features/ee/workflows/api/scheduleSMSReminders.ts +++ b/packages/features/ee/workflows/api/scheduleSMSReminders.ts @@ -48,7 +48,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { const dateInSevenDays = dayjs().add(7, "day"); - unscheduledReminders.forEach(async (reminder) => { + for (const reminder of unscheduledReminders) { if (dayjs(reminder.scheduledDate).isBefore(dateInSevenDays)) { try { const sendTo = @@ -86,7 +86,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { if (message?.length && message?.length > 0 && sendTo) { const scheduledSMS = await twilio.scheduleSMS(sendTo, message, reminder.scheduledDate); - await prisma.workflowReminder.updateMany({ + await prisma.workflowReminder.update({ where: { id: reminder.id, }, @@ -100,7 +100,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { console.log(`Error scheduling SMS with error ${error}`); } } - }); + } + res.status(200).json({ message: "SMS scheduled" }); } export default defaultHandler({