2022-07-14 00:10:45 +00:00
|
|
|
/* Schedule any workflow reminder that falls within 72 hours for email */
|
2022-07-28 19:58:26 +00:00
|
|
|
import { WorkflowActions, WorkflowMethods, WorkflowTemplates } from "@prisma/client";
|
2022-07-14 00:10:45 +00:00
|
|
|
import client from "@sendgrid/client";
|
|
|
|
import sgMail from "@sendgrid/mail";
|
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import dayjs from "@calcom/dayjs";
|
|
|
|
import { defaultHandler } from "@calcom/lib/server";
|
2022-07-28 19:58:26 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
import emailReminderTemplate from "../lib/reminders/templates/emailReminderTemplate";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
const sendgridAPIKey = process.env.SENDGRID_API_KEY as string;
|
|
|
|
const senderEmail = process.env.SENDGRID_EMAIL as string;
|
|
|
|
|
|
|
|
sgMail.setApiKey(sendgridAPIKey);
|
|
|
|
|
|
|
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
const apiKey = req.headers.authorization || req.query.apiKey;
|
|
|
|
if (process.env.CRON_API_KEY !== apiKey) {
|
|
|
|
res.status(401).json({ message: "Not authenticated" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!process.env.SENDGRID_API_KEY || !process.env.SENDGRID_EMAIL) {
|
|
|
|
res.status(405).json({ message: "No SendGrid API key or email" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//delete all scheduled email reminders where scheduled is past current date
|
|
|
|
await prisma.workflowReminder.deleteMany({
|
|
|
|
where: {
|
|
|
|
method: WorkflowMethods.EMAIL,
|
|
|
|
scheduledDate: {
|
|
|
|
lte: dayjs().toISOString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
//find all unscheduled Email reminders
|
|
|
|
const unscheduledReminders = await prisma.workflowReminder.findMany({
|
|
|
|
where: {
|
|
|
|
method: WorkflowMethods.EMAIL,
|
|
|
|
scheduled: false,
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
workflowStep: true,
|
|
|
|
booking: {
|
|
|
|
include: {
|
|
|
|
eventType: true,
|
|
|
|
user: true,
|
|
|
|
attendees: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-08-01 10:20:21 +00:00
|
|
|
if (!unscheduledReminders.length) {
|
|
|
|
res.status(200).json({ message: "No Emails to schedule" });
|
|
|
|
return;
|
|
|
|
}
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
const dateInSeventyTwoHours = dayjs().add(72, "hour");
|
|
|
|
|
2022-08-01 10:20:21 +00:00
|
|
|
for (const reminder of unscheduledReminders) {
|
2022-07-14 00:10:45 +00:00
|
|
|
if (dayjs(reminder.scheduledDate).isBefore(dateInSeventyTwoHours)) {
|
|
|
|
try {
|
|
|
|
const sendTo =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.EMAIL_HOST
|
|
|
|
? reminder.booking?.user?.email
|
|
|
|
: reminder.booking?.attendees[0].email;
|
|
|
|
|
|
|
|
const name =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.EMAIL_ATTENDEE
|
|
|
|
? reminder.booking?.attendees[0].name
|
|
|
|
: reminder.booking?.user?.name;
|
|
|
|
|
|
|
|
const attendeeName =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.EMAIL_ATTENDEE
|
|
|
|
? reminder.booking?.user?.name
|
|
|
|
: reminder.booking?.attendees[0].name;
|
|
|
|
|
2022-07-14 23:05:01 +00:00
|
|
|
const timeZone =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.EMAIL_ATTENDEE
|
|
|
|
? reminder.booking?.attendees[0].timeZone
|
|
|
|
: reminder.booking?.user?.timeZone;
|
|
|
|
|
2022-07-21 18:56:20 +00:00
|
|
|
let emailContent = {
|
|
|
|
emailSubject: reminder.workflowStep.emailSubject || "",
|
|
|
|
emailBody: {
|
|
|
|
text: reminder.workflowStep.reminderBody || "",
|
|
|
|
html: `<body style="white-space: pre-wrap;">${reminder.workflowStep.reminderBody || ""}</body>`,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
switch (reminder.workflowStep.template) {
|
|
|
|
case WorkflowTemplates.REMINDER:
|
2022-07-21 18:56:20 +00:00
|
|
|
emailContent = emailReminderTemplate(
|
2022-07-14 00:10:45 +00:00
|
|
|
reminder.booking?.startTime.toISOString() || "",
|
|
|
|
reminder.booking?.eventType?.title || "",
|
2022-07-14 23:05:01 +00:00
|
|
|
timeZone || "",
|
2022-07-14 00:10:45 +00:00
|
|
|
attendeeName || "",
|
|
|
|
name || ""
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2022-07-21 18:56:20 +00:00
|
|
|
if (emailContent.emailSubject.length > 0 && emailContent.emailBody.text.length > 0 && sendTo) {
|
2022-08-01 10:20:21 +00:00
|
|
|
const batchIdResponse = await client.request({
|
|
|
|
url: "/v3/mail/batch",
|
|
|
|
method: "POST",
|
|
|
|
});
|
|
|
|
|
|
|
|
const batchId = batchIdResponse[1].batch_id;
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
await sgMail.send({
|
|
|
|
to: sendTo,
|
|
|
|
from: senderEmail,
|
2022-07-21 18:56:20 +00:00
|
|
|
subject: emailContent.emailSubject,
|
|
|
|
text: emailContent.emailBody.text,
|
|
|
|
html: emailContent.emailBody.html,
|
2022-08-01 10:20:21 +00:00
|
|
|
batchId: batchId,
|
2022-07-14 00:10:45 +00:00
|
|
|
sendAt: dayjs(reminder.scheduledDate).unix(),
|
|
|
|
});
|
2022-08-01 10:20:21 +00:00
|
|
|
|
|
|
|
await prisma.workflowReminder.update({
|
|
|
|
where: {
|
|
|
|
id: reminder.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
scheduled: true,
|
|
|
|
referenceId: batchId,
|
|
|
|
},
|
|
|
|
});
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error scheduling Email with error ${error}`);
|
|
|
|
}
|
|
|
|
}
|
2022-08-01 10:20:21 +00:00
|
|
|
}
|
|
|
|
res.status(200).json({ message: "Emails scheduled" });
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultHandler({
|
|
|
|
POST: Promise.resolve({ default: handler }),
|
|
|
|
});
|