2023-03-02 18:23:33 +00:00
|
|
|
import type { Workflow, WorkflowsOnEventTypes, WorkflowStep } from "@prisma/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
|
|
|
|
2023-01-18 14:32:39 +00:00
|
|
|
import { SENDER_ID, SENDER_NAME } from "@calcom/lib/constants";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { WorkflowTriggerEvents } from "@calcom/prisma/enums";
|
|
|
|
import { WorkflowActions } from "@calcom/prisma/enums";
|
2022-07-28 19:58:26 +00:00
|
|
|
import type { CalendarEvent } from "@calcom/types/Calendar";
|
|
|
|
|
|
|
|
import { scheduleEmailReminder } from "./emailReminderManager";
|
|
|
|
import { scheduleSMSReminder } from "./smsReminderManager";
|
2023-07-11 15:48:44 +00:00
|
|
|
import { scheduleWhatsappReminder } from "./whatsappReminderManager";
|
|
|
|
|
|
|
|
import { isWhatsappAction } from "@calcom/features/ee/workflows/lib/actionHelperFunctions";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2023-04-18 10:08:09 +00:00
|
|
|
type ExtendedCalendarEvent = CalendarEvent & {
|
|
|
|
metadata?: { videoCallUrl: string | undefined };
|
|
|
|
eventType: { slug?: string };
|
|
|
|
};
|
|
|
|
|
2023-04-13 19:03:08 +00:00
|
|
|
export interface ScheduleWorkflowRemindersArgs {
|
2022-07-14 00:10:45 +00:00
|
|
|
workflows: (WorkflowsOnEventTypes & {
|
|
|
|
workflow: Workflow & {
|
|
|
|
steps: WorkflowStep[];
|
|
|
|
};
|
2023-04-13 19:03:08 +00:00
|
|
|
})[];
|
|
|
|
smsReminderNumber: string | null;
|
2023-04-18 10:08:09 +00:00
|
|
|
calendarEvent: ExtendedCalendarEvent;
|
2023-04-13 19:03:08 +00:00
|
|
|
requiresConfirmation?: boolean;
|
|
|
|
isRescheduleEvent?: boolean;
|
|
|
|
isFirstRecurringEvent?: boolean;
|
|
|
|
emailAttendeeSendToOverride?: string;
|
2023-04-18 10:08:09 +00:00
|
|
|
hideBranding?: boolean;
|
2023-04-13 19:03:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const scheduleWorkflowReminders = async (args: ScheduleWorkflowRemindersArgs) => {
|
|
|
|
const {
|
|
|
|
workflows,
|
|
|
|
smsReminderNumber,
|
|
|
|
calendarEvent: evt,
|
|
|
|
requiresConfirmation = false,
|
|
|
|
isRescheduleEvent = false,
|
|
|
|
isFirstRecurringEvent = false,
|
|
|
|
emailAttendeeSendToOverride = "",
|
2023-04-18 10:08:09 +00:00
|
|
|
hideBranding,
|
2023-04-13 19:03:08 +00:00
|
|
|
} = args;
|
|
|
|
if (workflows.length > 0 && !requiresConfirmation) {
|
|
|
|
for (const workflowReference of workflows) {
|
|
|
|
if (workflowReference.workflow.steps.length === 0) continue;
|
2022-08-31 23:09:34 +00:00
|
|
|
|
|
|
|
const workflow = workflowReference.workflow;
|
|
|
|
if (
|
|
|
|
workflow.trigger === WorkflowTriggerEvents.BEFORE_EVENT ||
|
2022-11-23 16:38:13 +00:00
|
|
|
(workflow.trigger === WorkflowTriggerEvents.NEW_EVENT &&
|
|
|
|
!isRescheduleEvent &&
|
|
|
|
isFirstRecurringEvent) ||
|
2022-10-07 18:18:28 +00:00
|
|
|
(workflow.trigger === WorkflowTriggerEvents.RESCHEDULE_EVENT && isRescheduleEvent) ||
|
|
|
|
workflow.trigger === WorkflowTriggerEvents.AFTER_EVENT
|
2022-08-31 23:09:34 +00:00
|
|
|
) {
|
2023-04-13 19:03:08 +00:00
|
|
|
for (const step of workflow.steps) {
|
2022-08-31 23:09:34 +00:00
|
|
|
if (step.action === WorkflowActions.SMS_ATTENDEE || step.action === WorkflowActions.SMS_NUMBER) {
|
|
|
|
const sendTo = step.action === WorkflowActions.SMS_ATTENDEE ? smsReminderNumber : step.sendTo;
|
|
|
|
await scheduleSMSReminder(
|
|
|
|
evt,
|
|
|
|
sendTo,
|
|
|
|
workflow.trigger,
|
|
|
|
step.action,
|
|
|
|
{
|
|
|
|
time: workflow.time,
|
|
|
|
timeUnit: workflow.timeUnit,
|
|
|
|
},
|
|
|
|
step.reminderBody || "",
|
|
|
|
step.id,
|
2022-11-11 15:01:17 +00:00
|
|
|
step.template,
|
2022-12-15 21:54:40 +00:00
|
|
|
step.sender || SENDER_ID,
|
|
|
|
workflow.userId,
|
2023-02-27 07:24:43 +00:00
|
|
|
workflow.teamId,
|
2022-12-15 21:54:40 +00:00
|
|
|
step.numberVerificationPending
|
2022-08-31 23:09:34 +00:00
|
|
|
);
|
|
|
|
} else if (
|
|
|
|
step.action === WorkflowActions.EMAIL_ATTENDEE ||
|
2022-12-16 21:11:08 +00:00
|
|
|
step.action === WorkflowActions.EMAIL_HOST
|
2022-08-31 23:09:34 +00:00
|
|
|
) {
|
2023-05-09 17:08:14 +00:00
|
|
|
let sendTo: string[] = [];
|
2022-10-10 13:40:20 +00:00
|
|
|
|
|
|
|
switch (step.action) {
|
|
|
|
case WorkflowActions.EMAIL_HOST:
|
2023-05-09 17:08:14 +00:00
|
|
|
sendTo = [evt.organizer?.email || ""];
|
2022-10-10 13:40:20 +00:00
|
|
|
break;
|
|
|
|
case WorkflowActions.EMAIL_ATTENDEE:
|
2023-05-09 17:08:14 +00:00
|
|
|
const attendees = !!emailAttendeeSendToOverride
|
|
|
|
? [emailAttendeeSendToOverride]
|
|
|
|
: evt.attendees?.map((attendee) => attendee.email);
|
|
|
|
|
|
|
|
sendTo = attendees;
|
|
|
|
|
2022-10-10 13:40:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-12-18 02:04:06 +00:00
|
|
|
|
2023-04-13 19:03:08 +00:00
|
|
|
await scheduleEmailReminder(
|
2022-08-31 23:09:34 +00:00
|
|
|
evt,
|
|
|
|
workflow.trigger,
|
|
|
|
step.action,
|
|
|
|
{
|
|
|
|
time: workflow.time,
|
|
|
|
timeUnit: workflow.timeUnit,
|
|
|
|
},
|
|
|
|
sendTo,
|
|
|
|
step.emailSubject || "",
|
|
|
|
step.reminderBody || "",
|
|
|
|
step.id,
|
2023-01-18 14:32:39 +00:00
|
|
|
step.template,
|
2023-04-18 10:08:09 +00:00
|
|
|
step.sender || SENDER_NAME,
|
|
|
|
hideBranding
|
2022-08-31 23:09:34 +00:00
|
|
|
);
|
2023-07-11 15:48:44 +00:00
|
|
|
} else if (isWhatsappAction(step.action)) {
|
|
|
|
const sendTo = step.action === WorkflowActions.WHATSAPP_ATTENDEE ? smsReminderNumber : step.sendTo;
|
|
|
|
await scheduleWhatsappReminder(
|
|
|
|
evt,
|
|
|
|
sendTo,
|
|
|
|
workflow.trigger,
|
|
|
|
step.action,
|
|
|
|
{
|
|
|
|
time: workflow.time,
|
|
|
|
timeUnit: workflow.timeUnit,
|
|
|
|
},
|
|
|
|
step.reminderBody || "",
|
|
|
|
step.id,
|
|
|
|
step.template,
|
|
|
|
workflow.userId,
|
|
|
|
workflow.teamId,
|
|
|
|
step.numberVerificationPending
|
|
|
|
);
|
2022-08-31 23:09:34 +00:00
|
|
|
}
|
2023-04-13 19:03:08 +00:00
|
|
|
}
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
2023-04-13 19:03:08 +00:00
|
|
|
}
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-04-13 19:03:08 +00:00
|
|
|
export interface SendCancelledRemindersArgs {
|
2022-07-14 00:10:45 +00:00
|
|
|
workflows: (WorkflowsOnEventTypes & {
|
|
|
|
workflow: Workflow & {
|
|
|
|
steps: WorkflowStep[];
|
|
|
|
};
|
2023-04-13 19:03:08 +00:00
|
|
|
})[];
|
|
|
|
smsReminderNumber: string | null;
|
2023-04-18 10:08:09 +00:00
|
|
|
evt: ExtendedCalendarEvent;
|
|
|
|
hideBranding?: boolean;
|
2023-04-13 19:03:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const sendCancelledReminders = async (args: SendCancelledRemindersArgs) => {
|
2023-04-18 10:08:09 +00:00
|
|
|
const { workflows, smsReminderNumber, evt, hideBranding } = args;
|
2023-04-13 19:03:08 +00:00
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
if (workflows.length > 0) {
|
2023-04-13 19:03:08 +00:00
|
|
|
for (const workflowRef of workflows) {
|
|
|
|
const { workflow } = workflowRef;
|
|
|
|
|
|
|
|
if (workflow.trigger === WorkflowTriggerEvents.EVENT_CANCELLED) {
|
|
|
|
for (const step of workflow.steps) {
|
2022-07-14 00:10:45 +00:00
|
|
|
if (step.action === WorkflowActions.SMS_ATTENDEE || step.action === WorkflowActions.SMS_NUMBER) {
|
|
|
|
const sendTo = step.action === WorkflowActions.SMS_ATTENDEE ? smsReminderNumber : step.sendTo;
|
|
|
|
await scheduleSMSReminder(
|
|
|
|
evt,
|
|
|
|
sendTo,
|
|
|
|
workflow.trigger,
|
|
|
|
step.action,
|
|
|
|
{
|
|
|
|
time: workflow.time,
|
|
|
|
timeUnit: workflow.timeUnit,
|
|
|
|
},
|
|
|
|
step.reminderBody || "",
|
|
|
|
step.id,
|
2022-11-11 15:01:17 +00:00
|
|
|
step.template,
|
2022-12-15 21:54:40 +00:00
|
|
|
step.sender || SENDER_ID,
|
|
|
|
workflow.userId,
|
2023-02-27 07:24:43 +00:00
|
|
|
workflow.teamId,
|
2022-12-15 21:54:40 +00:00
|
|
|
step.numberVerificationPending
|
2022-07-14 00:10:45 +00:00
|
|
|
);
|
|
|
|
} else if (
|
|
|
|
step.action === WorkflowActions.EMAIL_ATTENDEE ||
|
2022-12-16 21:11:08 +00:00
|
|
|
step.action === WorkflowActions.EMAIL_HOST
|
2022-07-14 00:10:45 +00:00
|
|
|
) {
|
2023-04-13 19:03:08 +00:00
|
|
|
let sendTo: MailData["to"] = "";
|
2022-10-10 13:40:20 +00:00
|
|
|
|
|
|
|
switch (step.action) {
|
|
|
|
case WorkflowActions.EMAIL_HOST:
|
|
|
|
sendTo = evt.organizer.email;
|
|
|
|
break;
|
|
|
|
case WorkflowActions.EMAIL_ATTENDEE:
|
2023-04-13 19:03:08 +00:00
|
|
|
sendTo = evt.attendees.map((a) => a.email);
|
2022-10-10 13:40:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-04-13 19:03:08 +00:00
|
|
|
|
|
|
|
await scheduleEmailReminder(
|
2022-07-14 00:10:45 +00:00
|
|
|
evt,
|
|
|
|
workflow.trigger,
|
|
|
|
step.action,
|
|
|
|
{
|
|
|
|
time: workflow.time,
|
|
|
|
timeUnit: workflow.timeUnit,
|
|
|
|
},
|
|
|
|
sendTo,
|
|
|
|
step.emailSubject || "",
|
|
|
|
step.reminderBody || "",
|
|
|
|
step.id,
|
2023-01-18 14:32:39 +00:00
|
|
|
step.template,
|
2023-04-18 10:08:09 +00:00
|
|
|
step.sender || SENDER_NAME,
|
|
|
|
hideBranding
|
2022-07-14 00:10:45 +00:00
|
|
|
);
|
2023-07-11 15:48:44 +00:00
|
|
|
} else if (isWhatsappAction(step.action)) {
|
|
|
|
const sendTo = step.action === WorkflowActions.WHATSAPP_ATTENDEE ? smsReminderNumber : step.sendTo;
|
|
|
|
await scheduleWhatsappReminder(
|
|
|
|
evt,
|
|
|
|
sendTo,
|
|
|
|
workflow.trigger,
|
|
|
|
step.action,
|
|
|
|
{
|
|
|
|
time: workflow.time,
|
|
|
|
timeUnit: workflow.timeUnit,
|
|
|
|
},
|
|
|
|
step.reminderBody || "",
|
|
|
|
step.id,
|
|
|
|
step.template,
|
|
|
|
workflow.userId,
|
|
|
|
workflow.teamId,
|
|
|
|
step.numberVerificationPending
|
|
|
|
);
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
2023-04-13 19:03:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
};
|