Scheduling infrastructure for absolutely everyone.
Fixes unique constraint error of batchId when scheduling emails (#3542)
Carina Wollendorferb42f0ffadd52
diff --git a/packages/features/ee/workflows/api/scheduleEmailReminders.ts b/packages/features/ee/workflows/api/scheduleEmailReminders.tsindex e4b6882867a..4d92d230291 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 dateawait 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.tsindex 8a0d7d00340..9afc0245e7c 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({