2023-07-11 15:48:44 +00:00
|
|
|
import type { TimeUnit } from "@prisma/client";
|
|
|
|
|
|
|
|
import dayjs from "@calcom/dayjs";
|
|
|
|
import logger from "@calcom/lib/logger";
|
2023-07-18 20:27:54 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2023-07-31 17:45:48 +00:00
|
|
|
import {
|
|
|
|
WorkflowTriggerEvents,
|
|
|
|
WorkflowTemplates,
|
|
|
|
WorkflowActions,
|
|
|
|
WorkflowMethods,
|
|
|
|
} from "@calcom/prisma/enums";
|
2023-07-11 15:48:44 +00:00
|
|
|
|
|
|
|
import * as twilio from "./smsProviders/twilioProvider";
|
2023-07-19 14:30:37 +00:00
|
|
|
import type { BookingInfo, timeUnitLowerCase } from "./smsReminderManager";
|
|
|
|
import { deleteScheduledSMSReminder } from "./smsReminderManager";
|
2023-07-18 20:27:54 +00:00
|
|
|
import {
|
|
|
|
whatsappEventCancelledTemplate,
|
|
|
|
whatsappEventCompletedTemplate,
|
|
|
|
whatsappEventRescheduledTemplate,
|
|
|
|
whatsappReminderTemplate,
|
|
|
|
} from "./templates/whatsapp";
|
2023-07-11 15:48:44 +00:00
|
|
|
|
|
|
|
const log = logger.getChildLogger({ prefix: ["[whatsappReminderManager]"] });
|
|
|
|
|
|
|
|
export const scheduleWhatsappReminder = async (
|
|
|
|
evt: BookingInfo,
|
|
|
|
reminderPhone: string | null,
|
|
|
|
triggerEvent: WorkflowTriggerEvents,
|
|
|
|
action: WorkflowActions,
|
|
|
|
timeSpan: {
|
|
|
|
time: number | null;
|
|
|
|
timeUnit: TimeUnit | null;
|
|
|
|
},
|
|
|
|
message: string,
|
|
|
|
workflowStepId: number,
|
|
|
|
template: WorkflowTemplates,
|
|
|
|
userId?: number | null,
|
|
|
|
teamId?: number | null,
|
2023-08-01 14:13:28 +00:00
|
|
|
isVerificationPending = false,
|
|
|
|
seatReferenceUid?: string
|
2023-07-11 15:48:44 +00:00
|
|
|
) => {
|
|
|
|
const { startTime, endTime } = evt;
|
|
|
|
const uid = evt.uid as string;
|
|
|
|
const currentDate = dayjs();
|
|
|
|
const timeUnit: timeUnitLowerCase | undefined = timeSpan.timeUnit?.toLocaleLowerCase() as timeUnitLowerCase;
|
|
|
|
let scheduledDate = null;
|
|
|
|
|
|
|
|
//WHATSAPP_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.WHATSAPP_ATTENDEE) return true;
|
|
|
|
const verifiedNumber = await prisma.verifiedNumber.findFirst({
|
|
|
|
where: {
|
|
|
|
OR: [{ userId }, { teamId }],
|
|
|
|
phoneNumber: reminderPhone || "",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!!verifiedNumber) return true;
|
|
|
|
return isVerificationPending;
|
|
|
|
}
|
|
|
|
const isNumberVerified = await getIsNumberVerified();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = action === WorkflowActions.WHATSAPP_ATTENDEE ? evt.attendees[0].name : evt.organizer.name;
|
2023-07-18 20:27:54 +00:00
|
|
|
const attendeeName =
|
|
|
|
action === WorkflowActions.WHATSAPP_ATTENDEE ? evt.organizer.name : evt.attendees[0].name;
|
2023-07-11 15:48:44 +00:00
|
|
|
const timeZone =
|
|
|
|
action === WorkflowActions.WHATSAPP_ATTENDEE ? evt.attendees[0].timeZone : evt.organizer.timeZone;
|
|
|
|
|
2023-07-18 20:27:54 +00:00
|
|
|
switch (template) {
|
2023-07-11 15:48:44 +00:00
|
|
|
case WorkflowTemplates.REMINDER:
|
2023-07-18 20:27:54 +00:00
|
|
|
message =
|
2023-07-19 14:30:37 +00:00
|
|
|
whatsappReminderTemplate(
|
|
|
|
false,
|
|
|
|
action,
|
|
|
|
evt.organizer.timeFormat,
|
|
|
|
evt.startTime,
|
|
|
|
evt.title,
|
|
|
|
timeZone,
|
|
|
|
attendeeName,
|
|
|
|
name
|
|
|
|
) || message;
|
2023-07-11 15:48:44 +00:00
|
|
|
break;
|
|
|
|
case WorkflowTemplates.CANCELLED:
|
2023-07-18 20:27:54 +00:00
|
|
|
message =
|
|
|
|
whatsappEventCancelledTemplate(
|
|
|
|
false,
|
|
|
|
action,
|
2023-07-19 14:30:37 +00:00
|
|
|
evt.organizer.timeFormat,
|
2023-07-18 20:27:54 +00:00
|
|
|
evt.startTime,
|
|
|
|
evt.title,
|
|
|
|
timeZone,
|
|
|
|
attendeeName,
|
|
|
|
name
|
|
|
|
) || message;
|
|
|
|
break;
|
2023-07-11 15:48:44 +00:00
|
|
|
case WorkflowTemplates.RESCHEDULED:
|
2023-07-18 20:27:54 +00:00
|
|
|
message =
|
|
|
|
whatsappEventRescheduledTemplate(
|
|
|
|
false,
|
|
|
|
action,
|
2023-07-19 14:30:37 +00:00
|
|
|
evt.organizer.timeFormat,
|
2023-07-18 20:27:54 +00:00
|
|
|
evt.startTime,
|
|
|
|
evt.title,
|
|
|
|
timeZone,
|
|
|
|
attendeeName,
|
|
|
|
name
|
|
|
|
) || message;
|
2023-07-11 15:48:44 +00:00
|
|
|
break;
|
|
|
|
case WorkflowTemplates.COMPLETED:
|
2023-07-18 20:27:54 +00:00
|
|
|
message =
|
|
|
|
whatsappEventCompletedTemplate(
|
|
|
|
false,
|
|
|
|
action,
|
2023-07-19 14:30:37 +00:00
|
|
|
evt.organizer.timeFormat,
|
2023-07-18 20:27:54 +00:00
|
|
|
evt.startTime,
|
|
|
|
evt.title,
|
|
|
|
timeZone,
|
|
|
|
attendeeName,
|
|
|
|
name
|
|
|
|
) || message;
|
|
|
|
break;
|
2023-07-11 15:48:44 +00:00
|
|
|
default:
|
2023-07-18 20:27:54 +00:00
|
|
|
message =
|
2023-07-19 14:30:37 +00:00
|
|
|
whatsappReminderTemplate(
|
|
|
|
false,
|
|
|
|
action,
|
|
|
|
evt.organizer.timeFormat,
|
|
|
|
evt.startTime,
|
|
|
|
evt.title,
|
|
|
|
timeZone,
|
|
|
|
attendeeName,
|
|
|
|
name
|
|
|
|
) || message;
|
2023-07-11 15:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allows debugging generated whatsapp content without waiting for twilio to send whatsapp messages
|
|
|
|
log.debug(`Sending Whatsapp for trigger ${triggerEvent}`, message);
|
2023-07-31 17:45:48 +00:00
|
|
|
if (
|
|
|
|
message.length > 0 &&
|
|
|
|
reminderPhone &&
|
|
|
|
isNumberVerified &&
|
|
|
|
action !== WorkflowActions.WHATSAPP_ATTENDEE
|
|
|
|
) {
|
2023-07-11 15:48:44 +00:00
|
|
|
//send WHATSAPP when event is booked/cancelled/rescheduled
|
|
|
|
if (
|
|
|
|
triggerEvent === WorkflowTriggerEvents.NEW_EVENT ||
|
|
|
|
triggerEvent === WorkflowTriggerEvents.EVENT_CANCELLED ||
|
|
|
|
triggerEvent === WorkflowTriggerEvents.RESCHEDULE_EVENT
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
await twilio.sendSMS(reminderPhone, message, "", true);
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error sending WHATSAPP with error ${error}`);
|
|
|
|
}
|
|
|
|
} else if (
|
|
|
|
(triggerEvent === WorkflowTriggerEvents.BEFORE_EVENT ||
|
|
|
|
triggerEvent === WorkflowTriggerEvents.AFTER_EVENT) &&
|
|
|
|
scheduledDate
|
|
|
|
) {
|
|
|
|
// 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 {
|
|
|
|
const scheduledWHATSAPP = await twilio.scheduleSMS(
|
|
|
|
reminderPhone,
|
|
|
|
message,
|
|
|
|
scheduledDate.toDate(),
|
|
|
|
"",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
await prisma.workflowReminder.create({
|
|
|
|
data: {
|
|
|
|
bookingUid: uid,
|
|
|
|
workflowStepId: workflowStepId,
|
|
|
|
method: WorkflowMethods.WHATSAPP,
|
|
|
|
scheduledDate: scheduledDate.toDate(),
|
|
|
|
scheduled: true,
|
|
|
|
referenceId: scheduledWHATSAPP.sid,
|
2023-08-01 14:13:28 +00:00
|
|
|
seatReferenceId: seatReferenceUid,
|
2023-07-11 15:48:44 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error scheduling WHATSAPP 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.WHATSAPP,
|
|
|
|
scheduledDate: scheduledDate.toDate(),
|
|
|
|
scheduled: false,
|
2023-08-01 14:13:28 +00:00
|
|
|
seatReferenceId: seatReferenceUid,
|
2023-07-11 15:48:44 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteScheduledWhatsappReminder = deleteScheduledSMSReminder;
|