2023-07-11 15:48:44 +00:00
|
|
|
/* Schedule any workflow reminder that falls within 7 days for WHATSAPP */
|
2023-07-14 17:02:06 +00:00
|
|
|
import { WorkflowActions, WorkflowMethods } from "@prisma/client";
|
2023-07-11 15:48:44 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import dayjs from "@calcom/dayjs";
|
|
|
|
import { defaultHandler } from "@calcom/lib/server";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
|
|
|
import { getWhatsappTemplateFunction } from "../lib/actionHelperFunctions";
|
2023-07-14 17:02:06 +00:00
|
|
|
import * as twilio from "../lib/reminders/smsProviders/twilioProvider";
|
2023-07-11 15:48:44 +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 whatsapp reminders where scheduled date is past current date
|
|
|
|
await prisma.workflowReminder.deleteMany({
|
|
|
|
where: {
|
|
|
|
method: WorkflowMethods.WHATSAPP,
|
|
|
|
scheduledDate: {
|
|
|
|
lte: dayjs().toISOString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
//find all unscheduled WHATSAPP reminders
|
|
|
|
const unscheduledReminders = await prisma.workflowReminder.findMany({
|
|
|
|
where: {
|
|
|
|
method: WorkflowMethods.WHATSAPP,
|
|
|
|
scheduled: false,
|
|
|
|
scheduledDate: {
|
|
|
|
lte: dayjs().add(7, "day").toISOString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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;
|
|
|
|
}
|
2023-07-11 15:48:44 +00:00
|
|
|
|
|
|
|
for (const reminder of unscheduledReminders) {
|
|
|
|
if (!reminder.workflowStep || !reminder.booking) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const sendTo =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.WHATSAPP_NUMBER
|
|
|
|
? reminder.workflowStep.sendTo
|
|
|
|
: reminder.booking?.smsReminderNumber;
|
|
|
|
|
|
|
|
const userName =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.WHATSAPP_ATTENDEE
|
|
|
|
? reminder.booking?.attendees[0].name
|
|
|
|
: "";
|
|
|
|
|
|
|
|
const attendeeName =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.WHATSAPP_ATTENDEE
|
|
|
|
? reminder.booking?.user?.name
|
|
|
|
: reminder.booking?.attendees[0].name;
|
|
|
|
|
|
|
|
const timeZone =
|
|
|
|
reminder.workflowStep.action === WorkflowActions.WHATSAPP_ATTENDEE
|
|
|
|
? reminder.booking?.attendees[0].timeZone
|
|
|
|
: reminder.booking?.user?.timeZone;
|
|
|
|
|
2023-07-14 17:02:06 +00:00
|
|
|
const templateFunction = getWhatsappTemplateFunction(reminder.workflowStep.template);
|
2023-07-11 15:48:44 +00:00
|
|
|
const message = templateFunction(
|
|
|
|
false,
|
|
|
|
reminder.workflowStep.action,
|
|
|
|
reminder.booking?.startTime.toISOString() || "",
|
|
|
|
reminder.booking?.eventType?.title || "",
|
|
|
|
timeZone || "",
|
|
|
|
attendeeName || "",
|
|
|
|
userName
|
|
|
|
);
|
|
|
|
|
|
|
|
if (message?.length && message?.length > 0 && sendTo) {
|
|
|
|
const scheduledSMS = await twilio.scheduleSMS(sendTo, message, reminder.scheduledDate, "", true);
|
|
|
|
|
|
|
|
await prisma.workflowReminder.update({
|
|
|
|
where: {
|
|
|
|
id: reminder.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
scheduled: true,
|
|
|
|
referenceId: scheduledSMS.sid,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error scheduling WHATSAPP with error ${error}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).json({ message: "WHATSAPP scheduled" });
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultHandler({
|
|
|
|
POST: Promise.resolve({ default: handler }),
|
|
|
|
});
|