2023-03-02 18:23:33 +00:00
|
|
|
import type { Workflow, WorkflowsOnEventTypes, WorkflowStep } from "@prisma/client";
|
|
|
|
import { WorkflowActions, WorkflowTriggerEvents } from "@prisma/client";
|
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";
|
2022-07-28 19:58:26 +00:00
|
|
|
import type { CalendarEvent } from "@calcom/types/Calendar";
|
|
|
|
|
|
|
|
import { scheduleEmailReminder } from "./emailReminderManager";
|
|
|
|
import { scheduleSMSReminder } from "./smsReminderManager";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
export const scheduleWorkflowReminders = async (
|
|
|
|
workflows: (WorkflowsOnEventTypes & {
|
|
|
|
workflow: Workflow & {
|
|
|
|
steps: WorkflowStep[];
|
|
|
|
};
|
|
|
|
})[],
|
|
|
|
smsReminderNumber: string | null,
|
2022-12-18 02:04:06 +00:00
|
|
|
evt: CalendarEvent & { metadata?: { videoCallUrl: string } },
|
2022-08-31 23:09:34 +00:00
|
|
|
needsConfirmation: boolean,
|
2022-11-23 16:38:13 +00:00
|
|
|
isRescheduleEvent: boolean,
|
|
|
|
isFirstRecurringEvent: boolean
|
2022-07-14 00:10:45 +00:00
|
|
|
) => {
|
|
|
|
if (workflows.length > 0 && !needsConfirmation) {
|
|
|
|
workflows.forEach((workflowReference) => {
|
2022-08-31 23:09:34 +00:00
|
|
|
if (workflowReference.workflow.steps.length === 0) return;
|
|
|
|
|
|
|
|
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
|
|
|
) {
|
|
|
|
workflow.steps.forEach(async (step) => {
|
|
|
|
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
|
|
|
) {
|
2022-10-10 13:40:20 +00:00
|
|
|
let sendTo = "";
|
|
|
|
|
|
|
|
switch (step.action) {
|
|
|
|
case WorkflowActions.EMAIL_HOST:
|
|
|
|
sendTo = evt.organizer.email;
|
|
|
|
break;
|
|
|
|
case WorkflowActions.EMAIL_ATTENDEE:
|
|
|
|
sendTo = evt.attendees[0].email;
|
|
|
|
break;
|
|
|
|
}
|
2022-12-18 02:04:06 +00:00
|
|
|
|
2022-08-31 23:09:34 +00:00
|
|
|
scheduleEmailReminder(
|
|
|
|
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,
|
|
|
|
step.sender || SENDER_NAME
|
2022-08-31 23:09:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const sendCancelledReminders = async (
|
|
|
|
workflows: (WorkflowsOnEventTypes & {
|
|
|
|
workflow: Workflow & {
|
|
|
|
steps: WorkflowStep[];
|
|
|
|
};
|
|
|
|
})[],
|
|
|
|
smsReminderNumber: string | null,
|
|
|
|
evt: CalendarEvent
|
|
|
|
) => {
|
|
|
|
if (workflows.length > 0) {
|
|
|
|
workflows
|
|
|
|
.filter((workflowRef) => workflowRef.workflow.trigger === WorkflowTriggerEvents.EVENT_CANCELLED)
|
|
|
|
.forEach((workflowRef) => {
|
|
|
|
const workflow = workflowRef.workflow;
|
|
|
|
workflow.steps.forEach(async (step) => {
|
|
|
|
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
|
|
|
) {
|
2022-10-10 13:40:20 +00:00
|
|
|
let sendTo = "";
|
|
|
|
|
|
|
|
switch (step.action) {
|
|
|
|
case WorkflowActions.EMAIL_HOST:
|
|
|
|
sendTo = evt.organizer.email;
|
|
|
|
break;
|
|
|
|
case WorkflowActions.EMAIL_ATTENDEE:
|
|
|
|
sendTo = evt.attendees[0].email;
|
|
|
|
break;
|
|
|
|
}
|
2022-07-14 00:10:45 +00:00
|
|
|
scheduleEmailReminder(
|
|
|
|
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,
|
|
|
|
step.sender || SENDER_NAME
|
2022-07-14 00:10:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|