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 <wollencarina@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
pull/3629/head
Carina Wollendorfer 2022-08-01 06:20:21 -04:00 committed by GitHub
parent 9346ae644f
commit b42f0ffadd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 21 deletions

View File

@ -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({

View File

@ -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({