2022-07-14 00:10:45 +00:00
|
|
|
/* Schedule any workflow reminder that falls within 7 days for SMS */
|
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import dayjs from "@calcom/dayjs";
|
2023-04-18 10:08:09 +00:00
|
|
|
import { getCalEventResponses } from "@calcom/features/bookings/lib/getCalEventResponses";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { defaultHandler } from "@calcom/lib/server";
|
2023-07-19 14:30:37 +00:00
|
|
|
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
|
2022-07-28 19:58:26 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { WorkflowActions, WorkflowMethods, WorkflowTemplates } from "@calcom/prisma/enums";
|
2022-12-18 02:04:06 +00:00
|
|
|
import { bookingMetadataSchema } from "@calcom/prisma/zod-utils";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-12-12 11:00:15 +00:00
|
|
|
import { getSenderId } from "../lib/alphanumericSenderIdSupport";
|
2022-07-28 19:58:26 +00:00
|
|
|
import * as twilio from "../lib/reminders/smsProviders/twilioProvider";
|
2023-03-10 14:28:42 +00:00
|
|
|
import type { VariablesType } from "../lib/reminders/templates/customTemplate";
|
|
|
|
import customTemplate from "../lib/reminders/templates/customTemplate";
|
2022-07-28 19:58:26 +00:00
|
|
|
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,
|
2022-12-02 13:43:07 +00:00
|
|
|
scheduledDate: {
|
|
|
|
lte: dayjs().add(7, "day").toISOString(),
|
|
|
|
},
|
2022-07-14 00:10:45 +00:00
|
|
|
},
|
|
|
|
include: {
|
|
|
|
workflowStep: true,
|
|
|
|
booking: {
|
|
|
|
include: {
|
|
|
|
eventType: true,
|
|
|
|
user: true,
|
|
|
|
attendees: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-07-14 17:02:06 +00:00
|
|
|
if (!unscheduledReminders.length) {
|
|
|
|
res.json({ ok: true });
|
|
|
|
return;
|
|
|
|
}
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-08-01 10:20:21 +00:00
|
|
|
for (const reminder of unscheduledReminders) {
|
2023-03-10 14:28:42 +00:00
|
|
|
if (!reminder.workflowStep || !reminder.booking) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-12-02 13:43:07 +00:00
|
|
|
try {
|
|
|
|
const sendTo =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.SMS_NUMBER
|
|
|
|
? reminder.workflowStep.sendTo
|
|
|
|
: reminder.booking?.smsReminderNumber;
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-12-02 13:43:07 +00:00
|
|
|
const userName =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.SMS_ATTENDEE
|
|
|
|
? reminder.booking?.attendees[0].name
|
|
|
|
: "";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-12-02 13:43:07 +00:00
|
|
|
const attendeeName =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.SMS_ATTENDEE
|
|
|
|
? reminder.booking?.user?.name
|
|
|
|
: reminder.booking?.attendees[0].name;
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-12-02 13:43:07 +00:00
|
|
|
const timeZone =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.SMS_ATTENDEE
|
|
|
|
? reminder.booking?.attendees[0].timeZone
|
|
|
|
: reminder.booking?.user?.timeZone;
|
2022-07-14 23:05:01 +00:00
|
|
|
|
2022-12-12 11:00:15 +00:00
|
|
|
const senderID = getSenderId(sendTo, reminder.workflowStep.sender);
|
|
|
|
|
2023-02-08 17:06:25 +00:00
|
|
|
const locale =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.EMAIL_ATTENDEE ||
|
|
|
|
reminder.workflowStep.action === WorkflowActions.SMS_ATTENDEE
|
|
|
|
? reminder.booking?.attendees[0].locale
|
|
|
|
: reminder.booking?.user?.locale;
|
|
|
|
|
2022-12-02 13:43:07 +00:00
|
|
|
let message: string | null = reminder.workflowStep.reminderBody;
|
2023-04-18 10:08:09 +00:00
|
|
|
|
|
|
|
if (reminder.workflowStep.reminderBody) {
|
|
|
|
const { responses } = getCalEventResponses({
|
|
|
|
bookingFields: reminder.booking.eventType?.bookingFields ?? null,
|
|
|
|
booking: reminder.booking,
|
|
|
|
});
|
|
|
|
|
|
|
|
const variables: VariablesType = {
|
|
|
|
eventName: reminder.booking?.eventType?.title,
|
|
|
|
organizerName: reminder.booking?.user?.name || "",
|
|
|
|
attendeeName: reminder.booking?.attendees[0].name,
|
|
|
|
attendeeEmail: reminder.booking?.attendees[0].email,
|
|
|
|
eventDate: dayjs(reminder.booking?.startTime).tz(timeZone),
|
|
|
|
eventEndTime: dayjs(reminder.booking?.endTime).tz(timeZone),
|
|
|
|
timeZone: timeZone,
|
|
|
|
location: reminder.booking?.location || "",
|
|
|
|
additionalNotes: reminder.booking?.description,
|
|
|
|
responses: responses,
|
|
|
|
meetingUrl: bookingMetadataSchema.parse(reminder.booking?.metadata || {})?.videoCallUrl,
|
|
|
|
cancelLink: `/booking/${reminder.booking.uid}?cancel=true`,
|
|
|
|
rescheduleLink: `/${reminder.booking.user?.username}/${reminder.booking.eventType?.slug}?rescheduleUid=${reminder.booking.uid}`,
|
|
|
|
};
|
|
|
|
const customMessage = customTemplate(
|
|
|
|
reminder.workflowStep.reminderBody || "",
|
|
|
|
variables,
|
2023-07-19 14:30:37 +00:00
|
|
|
locale || "en",
|
|
|
|
getTimeFormatStringFromUserTimeFormat(reminder.booking.user?.timeFormat)
|
2023-04-18 10:08:09 +00:00
|
|
|
);
|
|
|
|
message = customMessage.text;
|
|
|
|
} else if (reminder.workflowStep.template === WorkflowTemplates.REMINDER) {
|
|
|
|
message = smsReminderTemplate(
|
|
|
|
false,
|
|
|
|
reminder.workflowStep.action,
|
2023-07-19 14:30:37 +00:00
|
|
|
getTimeFormatStringFromUserTimeFormat(reminder.booking.user?.timeFormat),
|
2023-04-18 10:08:09 +00:00
|
|
|
reminder.booking?.startTime.toISOString() || "",
|
|
|
|
reminder.booking?.eventType?.title || "",
|
|
|
|
timeZone || "",
|
|
|
|
attendeeName || "",
|
|
|
|
userName
|
|
|
|
);
|
2022-12-02 13:43:07 +00:00
|
|
|
}
|
2023-04-18 10:08:09 +00:00
|
|
|
|
2023-08-10 18:52:36 +00:00
|
|
|
if (message?.length && message?.length > 0 && sendTo) {
|
2022-12-12 11:00:15 +00:00
|
|
|
const scheduledSMS = await twilio.scheduleSMS(sendTo, message, reminder.scheduledDate, senderID);
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-12-02 13:43:07 +00:00
|
|
|
await prisma.workflowReminder.update({
|
|
|
|
where: {
|
|
|
|
id: reminder.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
scheduled: true,
|
|
|
|
referenceId: scheduledSMS.sid,
|
|
|
|
},
|
|
|
|
});
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
2022-12-02 13:43:07 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error scheduling SMS with error ${error}`);
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
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 }),
|
|
|
|
});
|