2022-06-06 16:54:47 +00:00
|
|
|
import { BookingStatus, ReminderType } from "@prisma/client";
|
2021-09-22 19:52:38 +00:00
|
|
|
import dayjs from "dayjs";
|
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2022-06-06 17:49:56 +00:00
|
|
|
import { sendOrganizerRequestReminderEmail } from "@calcom/emails";
|
2022-05-18 21:05:49 +00:00
|
|
|
import { isPrismaObjOrUndefined } from "@calcom/lib";
|
|
|
|
import prisma, { bookingMinimalSelect } from "@calcom/prisma";
|
2022-03-23 22:00:30 +00:00
|
|
|
import type { CalendarEvent } from "@calcom/types/Calendar";
|
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
import { getTranslation } from "@server/lib/i18n";
|
|
|
|
|
2022-02-10 17:42:06 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-10-25 15:16:42 +00:00
|
|
|
const apiKey = req.headers.authorization || req.query.apiKey;
|
|
|
|
if (process.env.CRON_API_KEY !== apiKey) {
|
|
|
|
res.status(401).json({ message: "Not authenticated" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (req.method !== "POST") {
|
|
|
|
res.status(405).json({ message: "Invalid method" });
|
|
|
|
return;
|
2021-07-18 19:12:35 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 15:16:42 +00:00
|
|
|
const reminderIntervalMinutes = [48 * 60, 24 * 60, 3 * 60];
|
|
|
|
let notificationsSent = 0;
|
|
|
|
for (const interval of reminderIntervalMinutes) {
|
|
|
|
const bookings = await prisma.booking.findMany({
|
|
|
|
where: {
|
2022-06-06 16:54:47 +00:00
|
|
|
status: BookingStatus.PENDING,
|
2021-10-25 15:16:42 +00:00
|
|
|
createdAt: {
|
|
|
|
lte: dayjs().add(-interval, "minutes").toDate(),
|
2021-07-18 19:12:35 +00:00
|
|
|
},
|
2022-06-08 08:37:06 +00:00
|
|
|
// Only send reminders if the event hasn't finished
|
|
|
|
endTime: { gte: new Date() },
|
2021-10-25 15:16:42 +00:00
|
|
|
},
|
|
|
|
select: {
|
2022-05-18 21:05:49 +00:00
|
|
|
...bookingMinimalSelect,
|
2021-10-26 16:17:24 +00:00
|
|
|
location: true,
|
2021-10-28 22:58:26 +00:00
|
|
|
user: {
|
|
|
|
select: {
|
|
|
|
email: true,
|
|
|
|
name: true,
|
|
|
|
username: true,
|
|
|
|
locale: true,
|
|
|
|
timeZone: true,
|
2022-01-21 21:35:31 +00:00
|
|
|
destinationCalendar: true,
|
2021-10-28 22:58:26 +00:00
|
|
|
},
|
|
|
|
},
|
2021-10-25 15:16:42 +00:00
|
|
|
uid: true,
|
2022-01-21 21:35:31 +00:00
|
|
|
destinationCalendar: true,
|
2021-10-25 15:16:42 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const reminders = await prisma.reminderMail.findMany({
|
|
|
|
where: {
|
|
|
|
reminderType: ReminderType.PENDING_BOOKING_CONFIRMATION,
|
|
|
|
referenceId: {
|
|
|
|
in: bookings.map((b) => b.id),
|
2021-07-18 19:12:35 +00:00
|
|
|
},
|
2021-10-25 15:16:42 +00:00
|
|
|
elapsedMinutes: {
|
|
|
|
gte: interval,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const booking of bookings.filter((b) => !reminders.some((r) => r.referenceId == b.id))) {
|
|
|
|
const { user } = booking;
|
|
|
|
const name = user?.name || user?.username;
|
|
|
|
if (!user || !name || !user.timeZone) {
|
|
|
|
console.error(`Booking ${booking.id} is missing required properties for booking reminder`, { user });
|
|
|
|
continue;
|
|
|
|
}
|
2021-10-28 22:58:26 +00:00
|
|
|
|
2022-01-27 20:32:53 +00:00
|
|
|
const tOrganizer = await getTranslation(user.locale ?? "en", "common");
|
|
|
|
|
|
|
|
const attendeesListPromises = booking.attendees.map(async (attendee) => {
|
|
|
|
return {
|
|
|
|
name: attendee.name,
|
|
|
|
email: attendee.email,
|
|
|
|
timeZone: attendee.timeZone,
|
|
|
|
language: {
|
|
|
|
translate: await getTranslation(attendee.locale ?? "en", "common"),
|
|
|
|
locale: attendee.locale ?? "en",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const attendeesList = await Promise.all(attendeesListPromises);
|
2021-10-28 22:58:26 +00:00
|
|
|
|
2021-10-25 15:16:42 +00:00
|
|
|
const evt: CalendarEvent = {
|
|
|
|
type: booking.title,
|
|
|
|
title: booking.title,
|
|
|
|
description: booking.description || undefined,
|
2022-05-18 21:05:49 +00:00
|
|
|
customInputs: isPrismaObjOrUndefined(booking.customInputs),
|
2021-10-26 16:17:24 +00:00
|
|
|
location: booking.location ?? "",
|
2021-10-25 15:16:42 +00:00
|
|
|
startTime: booking.startTime.toISOString(),
|
|
|
|
endTime: booking.endTime.toISOString(),
|
|
|
|
organizer: {
|
|
|
|
email: user.email,
|
|
|
|
name,
|
|
|
|
timeZone: user.timeZone,
|
2022-01-27 20:32:53 +00:00
|
|
|
language: { translate: tOrganizer, locale: user.locale ?? "en" },
|
2021-10-25 15:16:42 +00:00
|
|
|
},
|
2022-01-27 20:32:53 +00:00
|
|
|
attendees: attendeesList,
|
2021-10-26 16:17:24 +00:00
|
|
|
uid: booking.uid,
|
2022-01-21 21:35:31 +00:00
|
|
|
destinationCalendar: booking.destinationCalendar || user.destinationCalendar,
|
2021-10-25 15:16:42 +00:00
|
|
|
};
|
2021-07-18 19:12:35 +00:00
|
|
|
|
2021-11-26 11:03:43 +00:00
|
|
|
await sendOrganizerRequestReminderEmail(evt);
|
|
|
|
|
2021-10-25 15:16:42 +00:00
|
|
|
await prisma.reminderMail.create({
|
|
|
|
data: {
|
|
|
|
referenceId: booking.id,
|
2021-07-18 19:12:35 +00:00
|
|
|
reminderType: ReminderType.PENDING_BOOKING_CONFIRMATION,
|
2021-10-25 15:16:42 +00:00
|
|
|
elapsedMinutes: interval,
|
2021-07-18 19:12:35 +00:00
|
|
|
},
|
|
|
|
});
|
2021-10-25 15:16:42 +00:00
|
|
|
notificationsSent++;
|
2021-07-18 19:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-25 15:16:42 +00:00
|
|
|
res.status(200).json({ notificationsSent });
|
2021-07-18 19:12:35 +00:00
|
|
|
}
|