2023-03-10 14:28:42 +00:00
|
|
|
import type { TimeUnit } from "@prisma/client";
|
2023-04-18 10:08:09 +00:00
|
|
|
import { WorkflowTemplates } from "@prisma/client";
|
|
|
|
import { WorkflowTriggerEvents, WorkflowActions, WorkflowMethods } from "@prisma/client";
|
2022-07-14 00:10:45 +00:00
|
|
|
import client from "@sendgrid/client";
|
2023-04-13 19:03:08 +00:00
|
|
|
import type { MailData } from "@sendgrid/helpers/classes/mail";
|
2022-07-14 00:10:45 +00:00
|
|
|
import sgMail from "@sendgrid/mail";
|
|
|
|
|
|
|
|
import dayjs from "@calcom/dayjs";
|
2023-04-18 10:08:09 +00:00
|
|
|
import logger from "@calcom/lib/logger";
|
2022-07-14 00:10:45 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-12-18 02:04:06 +00:00
|
|
|
import { bookingMetadataSchema } from "@calcom/prisma/zod-utils";
|
2022-07-28 19:58:26 +00:00
|
|
|
|
2023-03-10 14:28:42 +00:00
|
|
|
import type { BookingInfo, timeUnitLowerCase } from "./smsReminderManager";
|
|
|
|
import type { VariablesType } from "./templates/customTemplate";
|
|
|
|
import customTemplate 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;
|
|
|
|
|
2023-04-18 10:08:09 +00:00
|
|
|
const log = logger.getChildLogger({ prefix: ["[emailReminderManager]"] });
|
2022-07-14 00:10:45 +00:00
|
|
|
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,
|
2022-10-07 18:18:28 +00:00
|
|
|
timeSpan: {
|
2022-07-14 00:10:45 +00:00
|
|
|
time: number | null;
|
|
|
|
timeUnit: TimeUnit | null;
|
|
|
|
},
|
2023-04-13 19:03:08 +00:00
|
|
|
sendTo: MailData["to"],
|
2022-07-14 00:10:45 +00:00
|
|
|
emailSubject: string,
|
|
|
|
emailBody: string,
|
|
|
|
workflowStepId: number,
|
2023-01-18 14:32:39 +00:00
|
|
|
template: WorkflowTemplates,
|
2023-04-18 10:08:09 +00:00
|
|
|
sender: string,
|
|
|
|
hideBranding?: boolean
|
2022-07-14 00:10:45 +00:00
|
|
|
) => {
|
2022-12-16 21:11:08 +00:00
|
|
|
if (action === WorkflowActions.EMAIL_ADDRESS) return;
|
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();
|
2022-10-07 18:18:28 +00:00
|
|
|
const timeUnit: timeUnitLowerCase | undefined = timeSpan.timeUnit?.toLocaleLowerCase() as timeUnitLowerCase;
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-10-07 18:18:28 +00:00
|
|
|
let scheduledDate = null;
|
|
|
|
|
|
|
|
if (triggerEvent === WorkflowTriggerEvents.BEFORE_EVENT) {
|
|
|
|
scheduledDate = timeSpan.time && timeUnit ? dayjs(startTime).subtract(timeSpan.time, timeUnit) : null;
|
|
|
|
} else if (triggerEvent === WorkflowTriggerEvents.AFTER_EVENT) {
|
|
|
|
scheduledDate = timeSpan.time && timeUnit ? dayjs(endTime).add(timeSpan.time, timeUnit) : null;
|
|
|
|
}
|
2023-04-18 10:08:09 +00:00
|
|
|
|
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
|
|
|
|
2022-10-10 13:40:20 +00:00
|
|
|
let name = "";
|
|
|
|
let attendeeName = "";
|
|
|
|
let timeZone = "";
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case WorkflowActions.EMAIL_HOST:
|
|
|
|
name = evt.organizer.name;
|
|
|
|
attendeeName = evt.attendees[0].name;
|
|
|
|
timeZone = evt.organizer.timeZone;
|
|
|
|
break;
|
|
|
|
case WorkflowActions.EMAIL_ATTENDEE:
|
|
|
|
name = evt.attendees[0].name;
|
|
|
|
attendeeName = evt.organizer.name;
|
|
|
|
timeZone = evt.attendees[0].timeZone;
|
|
|
|
break;
|
|
|
|
}
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-07-21 18:56:20 +00:00
|
|
|
let emailContent = {
|
|
|
|
emailSubject,
|
2023-04-18 10:08:09 +00:00
|
|
|
emailBody: `<body style="white-space: pre-wrap;">${emailBody}</body>`,
|
2022-07-21 18:56:20 +00:00
|
|
|
};
|
2023-04-18 10:08:09 +00:00
|
|
|
if (emailBody) {
|
|
|
|
const variables: VariablesType = {
|
|
|
|
eventName: evt.title || "",
|
|
|
|
organizerName: evt.organizer.name,
|
|
|
|
attendeeName: evt.attendees[0].name,
|
|
|
|
attendeeEmail: evt.attendees[0].email,
|
|
|
|
eventDate: dayjs(startTime).tz(timeZone),
|
|
|
|
eventEndTime: dayjs(endTime).tz(timeZone),
|
|
|
|
timeZone: timeZone,
|
|
|
|
location: evt.location,
|
|
|
|
additionalNotes: evt.additionalNotes,
|
|
|
|
responses: evt.responses,
|
|
|
|
meetingUrl: bookingMetadataSchema.parse(evt.metadata || {})?.videoCallUrl,
|
|
|
|
cancelLink: `/booking/${evt.uid}?cancel=true`,
|
|
|
|
rescheduleLink: `/${evt.organizer.username}/${evt.eventType.slug}?rescheduleUid=${evt.uid}`,
|
|
|
|
};
|
|
|
|
|
|
|
|
const locale =
|
|
|
|
action === WorkflowActions.EMAIL_ATTENDEE || action === WorkflowActions.SMS_ATTENDEE
|
|
|
|
? evt.attendees[0].language?.locale
|
|
|
|
: evt.organizer.language.locale;
|
|
|
|
|
|
|
|
const emailSubjectTemplate = customTemplate(emailSubject, variables, locale);
|
|
|
|
emailContent.emailSubject = emailSubjectTemplate.text;
|
|
|
|
emailContent.emailBody = customTemplate(emailBody, variables, locale, hideBranding).html;
|
|
|
|
} else if (template === WorkflowTemplates.REMINDER) {
|
|
|
|
emailContent = emailReminderTemplate(
|
|
|
|
false,
|
|
|
|
action,
|
|
|
|
startTime,
|
|
|
|
endTime,
|
|
|
|
evt.title,
|
|
|
|
timeZone,
|
|
|
|
attendeeName,
|
|
|
|
name
|
|
|
|
);
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:08:09 +00:00
|
|
|
// Allows debugging generated email content without waiting for sendgrid to send emails
|
|
|
|
log.debug(`Sending Email for trigger ${triggerEvent}`, JSON.stringify(emailContent));
|
|
|
|
|
|
|
|
const batchIdResponse = await client.request({
|
|
|
|
url: "/v3/mail/batch",
|
|
|
|
method: "POST",
|
|
|
|
});
|
|
|
|
|
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,
|
2023-01-18 14:32:39 +00:00
|
|
|
from: {
|
|
|
|
email: senderEmail,
|
|
|
|
name: sender,
|
|
|
|
},
|
2022-07-21 18:56:20 +00:00
|
|
|
subject: emailContent.emailSubject,
|
2023-04-18 10:08:09 +00:00
|
|
|
html: emailContent.emailBody,
|
2022-07-21 18:56:20 +00:00
|
|
|
batchId: batchIdResponse[1].batch_id,
|
2022-10-27 07:05:11 +00:00
|
|
|
replyTo: evt.organizer.email,
|
2022-07-21 18:56:20 +00:00
|
|
|
});
|
2022-07-14 00:10:45 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log("Error sending Email");
|
|
|
|
}
|
2022-10-07 18:18:28 +00:00
|
|
|
} else if (
|
|
|
|
(triggerEvent === WorkflowTriggerEvents.BEFORE_EVENT ||
|
|
|
|
triggerEvent === WorkflowTriggerEvents.AFTER_EVENT) &&
|
|
|
|
scheduledDate
|
|
|
|
) {
|
2022-07-14 00:10:45 +00:00
|
|
|
// 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,
|
2023-01-18 14:32:39 +00:00
|
|
|
from: {
|
|
|
|
email: senderEmail,
|
|
|
|
name: sender,
|
|
|
|
},
|
2022-07-21 18:56:20 +00:00
|
|
|
subject: emailContent.emailSubject,
|
2023-04-18 10:08:09 +00:00
|
|
|
html: emailContent.emailBody,
|
2022-07-14 00:10:45 +00:00
|
|
|
batchId: batchIdResponse[1].batch_id,
|
|
|
|
sendAt: scheduledDate.unix(),
|
2022-10-27 07:05:11 +00:00
|
|
|
replyTo: evt.organizer.email,
|
2022-07-14 00:10:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-10 14:28:42 +00:00
|
|
|
export const deleteScheduledEmailReminder = async (reminderId: number, referenceId: string | null) => {
|
2022-07-14 00:10:45 +00:00
|
|
|
try {
|
2023-02-20 17:40:08 +00:00
|
|
|
if (!referenceId) {
|
|
|
|
await prisma.workflowReminder.delete({
|
|
|
|
where: {
|
|
|
|
id: reminderId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-02-13 12:39:06 +00:00
|
|
|
|
2023-02-20 17:40:08 +00:00
|
|
|
await prisma.workflowReminder.update({
|
|
|
|
where: {
|
|
|
|
id: reminderId,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
cancelled: true,
|
|
|
|
},
|
2023-02-13 12:39:06 +00:00
|
|
|
});
|
2022-07-14 00:10:45 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error canceling reminder with error ${error}`);
|
|
|
|
}
|
|
|
|
};
|