2022-07-14 00:10:45 +00:00
|
|
|
/* Schedule any workflow reminder that falls within 7 days for SMS */
|
2022-07-28 19:58:26 +00:00
|
|
|
import { WorkflowActions, WorkflowMethods, WorkflowTemplates } from "@prisma/client";
|
2022-07-14 00:10:45 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import dayjs from "@calcom/dayjs";
|
|
|
|
import { defaultHandler } from "@calcom/lib/server";
|
2022-07-28 19:58:26 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
import * as twilio from "../lib/reminders/smsProviders/twilioProvider";
|
|
|
|
import smsReminderTemplate from "../lib/reminders/templates/smsReminderTemplate";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
const apiKey = req.headers.authorization || req.query.apiKey;
|
|
|
|
if (process.env.CRON_API_KEY !== apiKey) {
|
|
|
|
res.status(401).json({ message: "Not authenticated" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//delete all scheduled sms reminders where scheduled date is past current date
|
|
|
|
await prisma.workflowReminder.deleteMany({
|
|
|
|
where: {
|
|
|
|
method: WorkflowMethods.SMS,
|
|
|
|
scheduledDate: {
|
|
|
|
lte: dayjs().toISOString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
//find all unscheduled SMS reminders
|
|
|
|
const unscheduledReminders = await prisma.workflowReminder.findMany({
|
|
|
|
where: {
|
|
|
|
method: WorkflowMethods.SMS,
|
|
|
|
scheduled: false,
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
workflowStep: true,
|
|
|
|
booking: {
|
|
|
|
include: {
|
|
|
|
eventType: true,
|
|
|
|
user: true,
|
|
|
|
attendees: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!unscheduledReminders.length) res.json({ ok: true });
|
|
|
|
|
|
|
|
const dateInSevenDays = dayjs().add(7, "day");
|
|
|
|
|
2022-08-01 10:20:21 +00:00
|
|
|
for (const reminder of unscheduledReminders) {
|
2022-07-14 00:10:45 +00:00
|
|
|
if (dayjs(reminder.scheduledDate).isBefore(dateInSevenDays)) {
|
|
|
|
try {
|
|
|
|
const sendTo =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.SMS_NUMBER
|
|
|
|
? reminder.workflowStep.sendTo
|
|
|
|
: reminder.booking?.smsReminderNumber;
|
|
|
|
|
|
|
|
const userName =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.SMS_ATTENDEE
|
|
|
|
? reminder.booking?.attendees[0].name
|
|
|
|
: "";
|
|
|
|
|
|
|
|
const attendeeName =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.SMS_ATTENDEE
|
|
|
|
? reminder.booking?.user?.name
|
|
|
|
: reminder.booking?.attendees[0].name;
|
|
|
|
|
2022-07-14 23:05:01 +00:00
|
|
|
const timeZone =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.SMS_ATTENDEE
|
|
|
|
? reminder.booking?.attendees[0].timeZone
|
|
|
|
: reminder.booking?.user?.timeZone;
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
let message: string | null = reminder.workflowStep.reminderBody;
|
|
|
|
switch (reminder.workflowStep.template) {
|
|
|
|
case WorkflowTemplates.REMINDER:
|
|
|
|
message = smsReminderTemplate(
|
|
|
|
reminder.booking?.startTime.toISOString() || "",
|
|
|
|
reminder.booking?.eventType?.title || "",
|
2022-07-14 23:05:01 +00:00
|
|
|
timeZone || "",
|
2022-07-14 00:10:45 +00:00
|
|
|
attendeeName || "",
|
|
|
|
userName
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (message?.length && message?.length > 0 && sendTo) {
|
|
|
|
const scheduledSMS = await twilio.scheduleSMS(sendTo, message, reminder.scheduledDate);
|
|
|
|
|
2022-08-01 10:20:21 +00:00
|
|
|
await prisma.workflowReminder.update({
|
2022-07-14 00:10:45 +00:00
|
|
|
where: {
|
|
|
|
id: reminder.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
scheduled: true,
|
|
|
|
referenceId: scheduledSMS.sid,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error scheduling SMS with error ${error}`);
|
|
|
|
}
|
|
|
|
}
|
2022-08-01 10:20:21 +00:00
|
|
|
}
|
|
|
|
res.status(200).json({ message: "SMS scheduled" });
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultHandler({
|
|
|
|
POST: Promise.resolve({ default: handler }),
|
|
|
|
});
|