2023-03-02 18:23:33 +00:00
|
|
|
import type { TimeUnit } from "@prisma/client";
|
|
|
|
import { WorkflowTriggerEvents, WorkflowTemplates, WorkflowActions, WorkflowMethods } from "@prisma/client";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
import dayjs from "@calcom/dayjs";
|
|
|
|
import prisma from "@calcom/prisma";
|
2023-03-02 18:23:33 +00:00
|
|
|
import type { Prisma } from "@calcom/prisma/client";
|
2022-12-18 02:04:06 +00:00
|
|
|
import { bookingMetadataSchema } from "@calcom/prisma/zod-utils";
|
2022-07-28 19:58:26 +00:00
|
|
|
|
2022-12-12 11:00:15 +00:00
|
|
|
import { getSenderId } from "../alphanumericSenderIdSupport";
|
2022-07-28 19:58:26 +00:00
|
|
|
import * as twilio from "./smsProviders/twilioProvider";
|
2023-03-02 18:23:33 +00:00
|
|
|
import type { VariablesType } from "./templates/customTemplate";
|
|
|
|
import customTemplate from "./templates/customTemplate";
|
2022-07-28 19:58:26 +00:00
|
|
|
import smsReminderTemplate from "./templates/smsReminderTemplate";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
export enum timeUnitLowerCase {
|
|
|
|
DAY = "day",
|
|
|
|
MINUTE = "minute",
|
|
|
|
YEAR = "year",
|
|
|
|
}
|
|
|
|
|
|
|
|
export type BookingInfo = {
|
|
|
|
uid?: string | null;
|
2023-02-08 17:06:25 +00:00
|
|
|
attendees: { name: string; email: string; timeZone: string; language: { locale: string } }[];
|
2022-08-03 22:22:38 +00:00
|
|
|
organizer: {
|
|
|
|
language: { locale: string };
|
|
|
|
name: string;
|
|
|
|
email: string;
|
|
|
|
timeZone: string;
|
|
|
|
};
|
2022-07-14 00:10:45 +00:00
|
|
|
startTime: string;
|
2022-08-04 08:52:05 +00:00
|
|
|
endTime: string;
|
2022-07-14 00:10:45 +00:00
|
|
|
title: string;
|
2022-08-03 22:22:38 +00:00
|
|
|
location?: string | null;
|
|
|
|
additionalNotes?: string | null;
|
2023-03-20 19:08:14 +00:00
|
|
|
responses?: Prisma.JsonValue;
|
2022-12-18 02:04:06 +00:00
|
|
|
metadata?: Prisma.JsonValue;
|
2022-07-14 00:10:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const scheduleSMSReminder = async (
|
|
|
|
evt: BookingInfo,
|
|
|
|
reminderPhone: string | null,
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
message: string,
|
|
|
|
workflowStepId: number,
|
2022-11-11 15:01:17 +00:00
|
|
|
template: WorkflowTemplates,
|
2022-12-15 21:54:40 +00:00
|
|
|
sender: string,
|
2023-02-27 07:24:43 +00:00
|
|
|
userId?: number | null,
|
|
|
|
teamId?: number | null,
|
2022-12-15 21:54:40 +00:00
|
|
|
isVerificationPending = false
|
2022-07-14 00:10:45 +00:00
|
|
|
) => {
|
2022-10-07 18:18:28 +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;
|
|
|
|
let scheduledDate = null;
|
|
|
|
|
2022-12-12 11:00:15 +00:00
|
|
|
const senderID = getSenderId(reminderPhone, sender);
|
|
|
|
|
2022-12-15 21:54:40 +00:00
|
|
|
//SMS_ATTENDEE action does not need to be verified
|
|
|
|
//isVerificationPending is from all already existing workflows (once they edit their workflow, they will also have to verify the number)
|
|
|
|
async function getIsNumberVerified() {
|
|
|
|
if (action === WorkflowActions.SMS_ATTENDEE) return true;
|
|
|
|
const verifiedNumber = await prisma.verifiedNumber.findFirst({
|
2023-02-27 07:24:43 +00:00
|
|
|
where: {
|
|
|
|
OR: [{ userId }, { teamId }],
|
|
|
|
phoneNumber: reminderPhone || "",
|
|
|
|
},
|
2022-12-15 21:54:40 +00:00
|
|
|
});
|
|
|
|
if (!!verifiedNumber) return true;
|
|
|
|
return isVerificationPending;
|
|
|
|
}
|
|
|
|
const isNumberVerified = await getIsNumberVerified();
|
|
|
|
|
2022-10-07 18:18:28 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
const name = action === WorkflowActions.SMS_ATTENDEE ? evt.attendees[0].name : "";
|
|
|
|
const attendeeName = action === WorkflowActions.SMS_ATTENDEE ? evt.organizer.name : evt.attendees[0].name;
|
2022-07-14 23:05:01 +00:00
|
|
|
const timeZone =
|
|
|
|
action === WorkflowActions.SMS_ATTENDEE ? evt.attendees[0].timeZone : evt.organizer.timeZone;
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2023-02-08 17:06:25 +00:00
|
|
|
const locale =
|
|
|
|
action === WorkflowActions.EMAIL_ATTENDEE || action === WorkflowActions.SMS_ATTENDEE
|
|
|
|
? evt.attendees[0].language?.locale
|
|
|
|
: evt.organizer.language.locale;
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
switch (template) {
|
|
|
|
case WorkflowTemplates.REMINDER:
|
2022-07-14 23:05:01 +00:00
|
|
|
message = smsReminderTemplate(evt.startTime, evt.title, timeZone, attendeeName, name) || message;
|
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,
|
2022-10-28 09:53:06 +00:00
|
|
|
attendeeEmail: evt.attendees[0].email,
|
2022-08-03 22:22:38 +00:00
|
|
|
eventDate: dayjs(evt.startTime).tz(timeZone),
|
|
|
|
eventTime: dayjs(evt.startTime).tz(timeZone),
|
|
|
|
timeZone: timeZone,
|
|
|
|
location: evt.location,
|
|
|
|
additionalNotes: evt.additionalNotes,
|
2023-03-20 19:08:14 +00:00
|
|
|
responses: evt.responses,
|
2022-12-18 02:04:06 +00:00
|
|
|
meetingUrl: bookingMetadataSchema.parse(evt.metadata || {})?.videoCallUrl,
|
2022-08-03 22:22:38 +00:00
|
|
|
};
|
2023-02-08 17:06:25 +00:00
|
|
|
const customMessage = await customTemplate(message, variables, locale);
|
2022-08-03 22:22:38 +00:00
|
|
|
message = customMessage.text;
|
|
|
|
break;
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 21:54:40 +00:00
|
|
|
if (message.length > 0 && reminderPhone && isNumberVerified) {
|
2022-10-18 12:47:15 +00:00
|
|
|
//send SMS when event is booked/cancelled/rescheduled
|
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-12-12 11:00:15 +00:00
|
|
|
await twilio.sendSMS(reminderPhone, message, senderID);
|
2022-07-14 00:10:45 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error sending SMS with error ${error}`);
|
|
|
|
}
|
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
|
|
|
// Can only schedule at least 60 minutes in advance and at most 7 days in advance
|
|
|
|
if (
|
|
|
|
currentDate.isBefore(scheduledDate.subtract(1, "hour")) &&
|
|
|
|
!scheduledDate.isAfter(currentDate.add(7, "day"))
|
|
|
|
) {
|
|
|
|
try {
|
2022-11-11 15:01:17 +00:00
|
|
|
const scheduledSMS = await twilio.scheduleSMS(
|
|
|
|
reminderPhone,
|
|
|
|
message,
|
|
|
|
scheduledDate.toDate(),
|
2022-12-12 11:00:15 +00:00
|
|
|
senderID
|
2022-11-11 15:01:17 +00:00
|
|
|
);
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
await prisma.workflowReminder.create({
|
|
|
|
data: {
|
|
|
|
bookingUid: uid,
|
|
|
|
workflowStepId: workflowStepId,
|
|
|
|
method: WorkflowMethods.SMS,
|
|
|
|
scheduledDate: scheduledDate.toDate(),
|
|
|
|
scheduled: true,
|
|
|
|
referenceId: scheduledSMS.sid,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error scheduling SMS with error ${error}`);
|
|
|
|
}
|
|
|
|
} else if (scheduledDate.isAfter(currentDate.add(7, "day"))) {
|
|
|
|
// Write to DB and send to CRON if scheduled reminder date is past 7 days
|
|
|
|
await prisma.workflowReminder.create({
|
|
|
|
data: {
|
|
|
|
bookingUid: uid,
|
|
|
|
workflowStepId: workflowStepId,
|
|
|
|
method: WorkflowMethods.SMS,
|
|
|
|
scheduledDate: scheduledDate.toDate(),
|
|
|
|
scheduled: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-20 17:40:08 +00:00
|
|
|
export const deleteScheduledSMSReminder = 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 twilio.cancelSMS(referenceId);
|
|
|
|
}
|
|
|
|
await prisma.workflowReminder.delete({
|
|
|
|
where: {
|
|
|
|
id: reminderId,
|
|
|
|
},
|
|
|
|
});
|
2022-07-14 00:10:45 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error canceling reminder with error ${error}`);
|
|
|
|
}
|
|
|
|
};
|