2022-07-14 00:10:45 +00:00
|
|
|
import {
|
|
|
|
TimeUnit,
|
|
|
|
WorkflowTriggerEvents,
|
|
|
|
WorkflowTemplates,
|
|
|
|
WorkflowActions,
|
|
|
|
WorkflowMethods,
|
|
|
|
} from "@prisma/client";
|
|
|
|
import client from "@sendgrid/client";
|
|
|
|
import sgMail from "@sendgrid/mail";
|
|
|
|
|
|
|
|
import dayjs from "@calcom/dayjs";
|
|
|
|
import prisma from "@calcom/prisma";
|
2022-07-28 19:58:26 +00:00
|
|
|
|
|
|
|
import { BookingInfo, timeUnitLowerCase } from "./smsReminderManager";
|
2022-08-03 22:22:38 +00:00
|
|
|
import customTemplate, { VariablesType } from "./templates/customTemplate";
|
2022-07-28 19:58:26 +00:00
|
|
|
import emailReminderTemplate from "./templates/emailReminderTemplate";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
let sendgridAPIKey, senderEmail: string;
|
|
|
|
|
|
|
|
if (process.env.SENDGRID_API_KEY) {
|
|
|
|
sendgridAPIKey = process.env.SENDGRID_API_KEY as string;
|
|
|
|
senderEmail = process.env.SENDGRID_EMAIL as string;
|
|
|
|
|
|
|
|
sgMail.setApiKey(sendgridAPIKey);
|
|
|
|
client.setApiKey(sendgridAPIKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const scheduleEmailReminder = async (
|
|
|
|
evt: BookingInfo,
|
|
|
|
triggerEvent: WorkflowTriggerEvents,
|
|
|
|
action: WorkflowActions,
|
|
|
|
timeBefore: {
|
|
|
|
time: number | null;
|
|
|
|
timeUnit: TimeUnit | null;
|
|
|
|
},
|
|
|
|
sendTo: string,
|
|
|
|
emailSubject: string,
|
|
|
|
emailBody: string,
|
|
|
|
workflowStepId: number,
|
|
|
|
template: WorkflowTemplates
|
|
|
|
) => {
|
2022-08-04 08:52:05 +00:00
|
|
|
const { startTime, endTime } = evt;
|
2022-07-14 00:10:45 +00:00
|
|
|
const uid = evt.uid as string;
|
|
|
|
const currentDate = dayjs();
|
|
|
|
const timeUnit: timeUnitLowerCase | undefined =
|
|
|
|
timeBefore.timeUnit?.toLocaleLowerCase() as timeUnitLowerCase;
|
|
|
|
const scheduledDate =
|
|
|
|
timeBefore.time && timeUnit ? dayjs(startTime).subtract(timeBefore.time, timeUnit) : null;
|
|
|
|
|
2022-07-20 19:48:40 +00:00
|
|
|
if (!process.env.SENDGRID_API_KEY || !process.env.SENDGRID_EMAIL) {
|
|
|
|
console.error("Sendgrid credentials are missing from the .env file");
|
|
|
|
return;
|
|
|
|
}
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
const batchIdResponse = await client.request({
|
|
|
|
url: "/v3/mail/batch",
|
|
|
|
method: "POST",
|
|
|
|
});
|
|
|
|
|
|
|
|
const name = action === WorkflowActions.EMAIL_HOST ? evt.organizer.name : evt.attendees[0].name;
|
|
|
|
const attendeeName = action === WorkflowActions.EMAIL_HOST ? evt.attendees[0].name : evt.organizer.name;
|
2022-07-14 23:05:01 +00:00
|
|
|
const timeZone = action === WorkflowActions.EMAIL_HOST ? evt.organizer.timeZone : evt.attendees[0].timeZone;
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-07-21 18:56:20 +00:00
|
|
|
let emailContent = {
|
|
|
|
emailSubject,
|
|
|
|
emailBody: {
|
|
|
|
text: emailBody,
|
|
|
|
html: `<body style="white-space: pre-wrap;">${emailBody}</body>`,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
switch (template) {
|
|
|
|
case WorkflowTemplates.REMINDER:
|
2022-08-04 08:52:05 +00:00
|
|
|
emailContent = emailReminderTemplate(startTime, endTime, evt.title, timeZone, attendeeName, name);
|
2022-07-14 00:10:45 +00:00
|
|
|
break;
|
2022-08-03 22:22:38 +00:00
|
|
|
case WorkflowTemplates.CUSTOM:
|
|
|
|
const variables: VariablesType = {
|
|
|
|
eventName: evt.title || "",
|
|
|
|
organizerName: evt.organizer.name,
|
|
|
|
attendeeName: evt.attendees[0].name,
|
|
|
|
eventDate: dayjs(startTime).tz(timeZone),
|
|
|
|
eventTime: dayjs(startTime).tz(timeZone),
|
|
|
|
timeZone: timeZone,
|
|
|
|
location: evt.location,
|
|
|
|
additionalNotes: evt.additionalNotes,
|
|
|
|
customInputs: evt.customInputs,
|
|
|
|
};
|
|
|
|
|
|
|
|
const emailSubjectTemplate = await customTemplate(
|
|
|
|
emailSubject,
|
|
|
|
variables,
|
|
|
|
evt.organizer.language.locale
|
|
|
|
);
|
|
|
|
emailContent.emailSubject = emailSubjectTemplate.text;
|
|
|
|
emailContent.emailBody = await customTemplate(emailBody, variables, evt.organizer.language.locale);
|
|
|
|
break;
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
triggerEvent === WorkflowTriggerEvents.NEW_EVENT ||
|
2022-08-31 23:09:34 +00:00
|
|
|
triggerEvent === WorkflowTriggerEvents.EVENT_CANCELLED ||
|
|
|
|
triggerEvent === WorkflowTriggerEvents.RESCHEDULE_EVENT
|
2022-07-14 00:10:45 +00:00
|
|
|
) {
|
|
|
|
try {
|
2022-07-21 18:56:20 +00:00
|
|
|
await sgMail.send({
|
|
|
|
to: sendTo,
|
|
|
|
from: senderEmail,
|
|
|
|
subject: emailContent.emailSubject,
|
|
|
|
text: emailContent.emailBody.text,
|
|
|
|
html: emailContent.emailBody.html,
|
|
|
|
batchId: batchIdResponse[1].batch_id,
|
|
|
|
});
|
2022-07-14 00:10:45 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log("Error sending Email");
|
|
|
|
}
|
|
|
|
} else if (triggerEvent === WorkflowTriggerEvents.BEFORE_EVENT && scheduledDate) {
|
|
|
|
// Sendgrid to schedule emails
|
|
|
|
// Can only schedule at least 60 minutes and at most 72 hours in advance
|
|
|
|
if (
|
|
|
|
currentDate.isBefore(scheduledDate.subtract(1, "hour")) &&
|
|
|
|
!scheduledDate.isAfter(currentDate.add(72, "hour"))
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
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-07-14 00:10:45 +00:00
|
|
|
batchId: batchIdResponse[1].batch_id,
|
|
|
|
sendAt: scheduledDate.unix(),
|
|
|
|
});
|
|
|
|
|
|
|
|
await prisma.workflowReminder.create({
|
|
|
|
data: {
|
|
|
|
bookingUid: uid,
|
|
|
|
workflowStepId: workflowStepId,
|
|
|
|
method: WorkflowMethods.EMAIL,
|
|
|
|
scheduledDate: scheduledDate.toDate(),
|
|
|
|
scheduled: true,
|
|
|
|
referenceId: batchIdResponse[1].batch_id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error scheduling email with error ${error}`);
|
|
|
|
}
|
|
|
|
} else if (scheduledDate.isAfter(currentDate.add(72, "hour"))) {
|
|
|
|
// Write to DB and send to CRON if scheduled reminder date is past 72 hours
|
|
|
|
await prisma.workflowReminder.create({
|
|
|
|
data: {
|
|
|
|
bookingUid: uid,
|
|
|
|
workflowStepId: workflowStepId,
|
|
|
|
method: WorkflowMethods.EMAIL,
|
|
|
|
scheduledDate: scheduledDate.toDate(),
|
|
|
|
scheduled: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteScheduledEmailReminder = async (referenceId: string) => {
|
|
|
|
try {
|
|
|
|
await client.request({
|
|
|
|
url: "/v3/user/scheduled_sends",
|
|
|
|
method: "POST",
|
|
|
|
body: {
|
|
|
|
batch_id: referenceId,
|
|
|
|
status: "cancel",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error canceling reminder with error ${error}`);
|
|
|
|
}
|
|
|
|
};
|