153 lines
4.5 KiB
TypeScript
153 lines
4.5 KiB
TypeScript
|
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";
|
||
|
|
||
|
import { BookingInfo, timeUnitLowerCase } from "./smsReminderManager";
|
||
|
import emailReminderTemplate from "./templates/emailReminderTemplate";
|
||
|
|
||
|
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
|
||
|
) => {
|
||
|
const { startTime } = evt;
|
||
|
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;
|
||
|
|
||
|
if (!process.env.SENDGRID_API_KEY || !process.env.SENDGRID_EMAIL) {
|
||
|
console.error("Sendgrid credentials are missing from the .env file");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
const timeZone = action === WorkflowActions.EMAIL_HOST ? evt.organizer.timeZone : evt.attendees[0].timeZone;
|
||
|
|
||
|
let emailContent = {
|
||
|
emailSubject,
|
||
|
emailBody: {
|
||
|
text: emailBody,
|
||
|
html: `<body style="white-space: pre-wrap;">${emailBody}</body>`,
|
||
|
},
|
||
|
};
|
||
|
|
||
|
switch (template) {
|
||
|
case WorkflowTemplates.REMINDER:
|
||
|
emailContent = emailReminderTemplate(startTime, evt.title, timeZone, attendeeName, name);
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (
|
||
|
triggerEvent === WorkflowTriggerEvents.NEW_EVENT ||
|
||
|
triggerEvent === WorkflowTriggerEvents.EVENT_CANCELLED
|
||
|
) {
|
||
|
try {
|
||
|
await sgMail.send({
|
||
|
to: sendTo,
|
||
|
from: senderEmail,
|
||
|
subject: emailContent.emailSubject,
|
||
|
text: emailContent.emailBody.text,
|
||
|
html: emailContent.emailBody.html,
|
||
|
batchId: batchIdResponse[1].batch_id,
|
||
|
});
|
||
|
} 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,
|
||
|
subject: emailContent.emailSubject,
|
||
|
text: emailContent.emailBody.text,
|
||
|
html: emailContent.emailBody.html,
|
||
|
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}`);
|
||
|
}
|
||
|
};
|