2023-03-02 18:23:33 +00:00
|
|
|
import type { Workflow, WorkflowsOnEventTypes, WorkflowStep } from "@prisma/client";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2023-07-18 20:27:54 +00:00
|
|
|
import { isWhatsappAction } from "@calcom/features/ee/workflows/lib/actionHelperFunctions";
|
2023-01-18 14:32:39 +00:00
|
|
|
import { SENDER_ID, SENDER_NAME } from "@calcom/lib/constants";
|
2023-07-31 17:35:48 +00:00
|
|
|
import { WorkflowMethods, WorkflowTriggerEvents } from "@calcom/prisma/enums";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { WorkflowActions } from "@calcom/prisma/enums";
|
2022-07-28 19:58:26 +00:00
|
|
|
import type { CalendarEvent } from "@calcom/types/Calendar";
|
|
|
|
|
2023-07-31 17:35:48 +00:00
|
|
|
import { scheduleEmailReminder, deleteScheduledEmailReminder } from "./emailReminderManager";
|
|
|
|
import { scheduleSMSReminder, deleteScheduledSMSReminder } from "./smsReminderManager";
|
|
|
|
import { scheduleWhatsappReminder, deleteScheduledWhatsappReminder } from "./whatsappReminderManager";
|
2023-07-11 15:48:44 +00:00
|
|
|
|
2023-04-18 10:08:09 +00:00
|
|
|
type ExtendedCalendarEvent = CalendarEvent & {
|
|
|
|
metadata?: { videoCallUrl: string | undefined };
|
|
|
|
eventType: { slug?: string };
|
|
|
|
};
|
|
|
|
|
2023-07-31 17:35:48 +00:00
|
|
|
type ProcessWorkflowStepParams = {
|
|
|
|
smsReminderNumber: string | null;
|
|
|
|
calendarEvent: ExtendedCalendarEvent;
|
|
|
|
emailAttendeeSendToOverride?: string;
|
|
|
|
hideBranding?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface ScheduleWorkflowRemindersArgs extends ProcessWorkflowStepParams {
|
2022-07-14 00:10:45 +00:00
|
|
|
workflows: (WorkflowsOnEventTypes & {
|
|
|
|
workflow: Workflow & {
|
|
|
|
steps: WorkflowStep[];
|
|
|
|
};
|
2023-04-13 19:03:08 +00:00
|
|
|
})[];
|
|
|
|
requiresConfirmation?: boolean;
|
|
|
|
isRescheduleEvent?: boolean;
|
|
|
|
isFirstRecurringEvent?: boolean;
|
|
|
|
}
|
|
|
|
|
2023-07-31 17:35:48 +00:00
|
|
|
const processWorkflowStep = async (
|
|
|
|
workflow: Workflow,
|
|
|
|
step: WorkflowStep,
|
|
|
|
{
|
|
|
|
smsReminderNumber,
|
|
|
|
calendarEvent: evt,
|
|
|
|
emailAttendeeSendToOverride,
|
|
|
|
hideBranding,
|
|
|
|
}: ProcessWorkflowStepParams
|
|
|
|
) => {
|
|
|
|
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,
|
|
|
|
step.template,
|
|
|
|
step.sender || SENDER_ID,
|
|
|
|
workflow.userId,
|
|
|
|
workflow.teamId,
|
|
|
|
step.numberVerificationPending
|
|
|
|
);
|
|
|
|
} else if (step.action === WorkflowActions.EMAIL_ATTENDEE || step.action === WorkflowActions.EMAIL_HOST) {
|
|
|
|
let sendTo: string[] = [];
|
|
|
|
|
|
|
|
switch (step.action) {
|
|
|
|
case WorkflowActions.EMAIL_HOST:
|
|
|
|
sendTo = [evt.organizer?.email || ""];
|
|
|
|
break;
|
|
|
|
case WorkflowActions.EMAIL_ATTENDEE:
|
|
|
|
const attendees = !!emailAttendeeSendToOverride
|
|
|
|
? [emailAttendeeSendToOverride]
|
|
|
|
: evt.attendees?.map((attendee) => attendee.email);
|
|
|
|
|
|
|
|
sendTo = attendees;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
await scheduleEmailReminder(
|
|
|
|
evt,
|
|
|
|
workflow.trigger,
|
|
|
|
step.action,
|
|
|
|
{
|
|
|
|
time: workflow.time,
|
|
|
|
timeUnit: workflow.timeUnit,
|
|
|
|
},
|
|
|
|
sendTo,
|
|
|
|
step.emailSubject || "",
|
|
|
|
step.reminderBody || "",
|
|
|
|
step.id,
|
|
|
|
step.template,
|
|
|
|
step.sender || SENDER_NAME,
|
|
|
|
hideBranding
|
|
|
|
);
|
|
|
|
} 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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
2023-07-31 17:35:48 +00:00
|
|
|
if (requiresConfirmation || !workflows.length) return;
|
|
|
|
|
|
|
|
for (const workflowReference of workflows) {
|
|
|
|
if (workflowReference.workflow.steps.length === 0) continue;
|
|
|
|
|
|
|
|
const workflow = workflowReference.workflow;
|
|
|
|
const isNotBeforeOrAfterEvent =
|
|
|
|
workflow.trigger !== WorkflowTriggerEvents.BEFORE_EVENT &&
|
|
|
|
workflow.trigger !== WorkflowTriggerEvents.AFTER_EVENT;
|
|
|
|
|
|
|
|
if (
|
|
|
|
isNotBeforeOrAfterEvent &&
|
|
|
|
// Check if the trigger is not a new event without a reschedule and is the first recurring event.
|
|
|
|
!(
|
|
|
|
workflow.trigger === WorkflowTriggerEvents.NEW_EVENT &&
|
|
|
|
!isRescheduleEvent &&
|
|
|
|
isFirstRecurringEvent
|
|
|
|
) &&
|
|
|
|
// Check if the trigger is not a rescheduled event that is rescheduled.
|
|
|
|
!(workflow.trigger === WorkflowTriggerEvents.RESCHEDULE_EVENT && isRescheduleEvent)
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const step of workflow.steps) {
|
|
|
|
await processWorkflowStep(workflow, step, {
|
|
|
|
calendarEvent: evt,
|
|
|
|
emailAttendeeSendToOverride,
|
|
|
|
smsReminderNumber,
|
|
|
|
hideBranding,
|
|
|
|
});
|
2023-04-13 19:03:08 +00:00
|
|
|
}
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-07-31 17:35:48 +00:00
|
|
|
const reminderMethods: { [x: string]: (id: number, referenceId: string | null) => void } = {
|
|
|
|
[WorkflowMethods.EMAIL]: deleteScheduledEmailReminder,
|
|
|
|
[WorkflowMethods.SMS]: deleteScheduledSMSReminder,
|
|
|
|
[WorkflowMethods.WHATSAPP]: deleteScheduledWhatsappReminder,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const cancelWorkflowReminders = async (
|
|
|
|
workflowReminders: { method: WorkflowMethods; id: number; referenceId: string | null }[]
|
|
|
|
) => {
|
|
|
|
await Promise.all(
|
|
|
|
workflowReminders.map((reminder) => {
|
|
|
|
return reminderMethods[reminder.method](reminder.id, reminder.referenceId);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2023-07-31 17:35:48 +00:00
|
|
|
if (!workflows.length) return;
|
|
|
|
|
|
|
|
for (const workflowRef of workflows) {
|
|
|
|
const { workflow } = workflowRef;
|
|
|
|
|
|
|
|
if (workflow.trigger !== WorkflowTriggerEvents.EVENT_CANCELLED) continue;
|
|
|
|
|
|
|
|
for (const step of workflow.steps) {
|
|
|
|
processWorkflowStep(workflow, step, {
|
|
|
|
smsReminderNumber,
|
|
|
|
hideBranding,
|
|
|
|
calendarEvent: evt,
|
|
|
|
});
|
2023-04-13 19:03:08 +00:00
|
|
|
}
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
};
|