2022-03-02 16:24:57 +00:00
|
|
|
import { BookingStatus, WebhookTriggerEvents } from "@prisma/client";
|
2021-09-22 19:52:38 +00:00
|
|
|
import async from "async";
|
2022-01-26 16:41:18 +00:00
|
|
|
import dayjs from "dayjs";
|
2021-10-26 16:17:24 +00:00
|
|
|
import { NextApiRequest, NextApiResponse } from "next";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
import { refund } from "@ee/lib/stripe/server";
|
2021-06-06 01:51:24 +00:00
|
|
|
|
2021-09-22 19:52:38 +00:00
|
|
|
import { asStringOrNull } from "@lib/asStringOrNull";
|
2021-09-28 19:12:48 +00:00
|
|
|
import { getSession } from "@lib/auth";
|
2021-11-26 11:03:43 +00:00
|
|
|
import { sendCancelledEmails } from "@lib/emails/email-manager";
|
2021-10-26 16:17:24 +00:00
|
|
|
import { FAKE_DAILY_CREDENTIAL } from "@lib/integrations/Daily/DailyVideoApiAdapter";
|
2022-01-06 17:28:31 +00:00
|
|
|
import { getCalendar } from "@lib/integrations/calendar/CalendarManager";
|
|
|
|
import { CalendarEvent } from "@lib/integrations/calendar/interfaces/Calendar";
|
2021-09-22 19:52:38 +00:00
|
|
|
import prisma from "@lib/prisma";
|
|
|
|
import { deleteMeeting } from "@lib/videoClient";
|
2021-10-07 15:14:47 +00:00
|
|
|
import sendPayload from "@lib/webhooks/sendPayload";
|
2021-11-22 11:37:07 +00:00
|
|
|
import getSubscribers from "@lib/webhooks/subscriptions";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
import { getTranslation } from "@server/lib/i18n";
|
2021-10-07 16:12:39 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-09-14 08:45:28 +00:00
|
|
|
// just bail if it not a DELETE
|
2021-10-07 15:14:47 +00:00
|
|
|
if (req.method !== "DELETE" && req.method !== "POST") {
|
2021-09-14 08:45:28 +00:00
|
|
|
return res.status(405).end();
|
|
|
|
}
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
const uid = asStringOrNull(req.body.uid) || "";
|
2022-01-28 17:40:29 +00:00
|
|
|
const cancellationReason = asStringOrNull(req.body.reason) || "";
|
2021-09-28 17:23:50 +00:00
|
|
|
const session = await getSession({ req: req });
|
2021-09-14 08:45:28 +00:00
|
|
|
|
|
|
|
const bookingToDelete = await prisma.booking.findUnique({
|
|
|
|
where: {
|
|
|
|
uid,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
2021-10-07 15:14:47 +00:00
|
|
|
userId: true,
|
2021-09-14 08:45:28 +00:00
|
|
|
user: {
|
|
|
|
select: {
|
2021-09-28 17:23:50 +00:00
|
|
|
id: true,
|
2021-09-14 08:45:28 +00:00
|
|
|
credentials: true,
|
2021-09-22 18:36:13 +00:00
|
|
|
email: true,
|
|
|
|
timeZone: true,
|
|
|
|
name: true,
|
2022-01-13 19:47:15 +00:00
|
|
|
destinationCalendar: true,
|
2021-06-09 19:46:41 +00:00
|
|
|
},
|
2021-09-14 08:45:28 +00:00
|
|
|
},
|
|
|
|
attendees: true,
|
2021-10-07 16:12:39 +00:00
|
|
|
location: true,
|
2021-09-14 08:45:28 +00:00
|
|
|
references: {
|
|
|
|
select: {
|
|
|
|
uid: true,
|
|
|
|
type: true,
|
2021-07-15 21:34:55 +00:00
|
|
|
},
|
|
|
|
},
|
2021-09-22 18:36:13 +00:00
|
|
|
payment: true,
|
|
|
|
paid: true,
|
|
|
|
title: true,
|
2022-01-21 12:06:45 +00:00
|
|
|
eventType: {
|
|
|
|
select: {
|
|
|
|
title: true,
|
|
|
|
},
|
|
|
|
},
|
2021-09-22 18:36:13 +00:00
|
|
|
description: true,
|
|
|
|
startTime: true,
|
|
|
|
endTime: true,
|
|
|
|
uid: true,
|
2021-10-07 15:14:47 +00:00
|
|
|
eventTypeId: true,
|
2022-01-21 21:35:31 +00:00
|
|
|
destinationCalendar: true,
|
2021-09-14 08:45:28 +00:00
|
|
|
},
|
|
|
|
});
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2021-09-28 17:23:50 +00:00
|
|
|
if (!bookingToDelete || !bookingToDelete.user) {
|
2021-09-14 08:45:28 +00:00
|
|
|
return res.status(404).end();
|
|
|
|
}
|
2021-09-13 08:57:56 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
if ((!session || session.user?.id !== bookingToDelete.user?.id) && bookingToDelete.startTime < new Date()) {
|
2021-09-28 17:23:50 +00:00
|
|
|
return res.status(403).json({ message: "Cannot cancel past events" });
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
if (!bookingToDelete.userId) {
|
|
|
|
return res.status(404).json({ message: "User not found" });
|
|
|
|
}
|
|
|
|
|
2021-10-07 15:14:47 +00:00
|
|
|
const organizer = await prisma.user.findFirst({
|
|
|
|
where: {
|
2021-10-26 16:17:24 +00:00
|
|
|
id: bookingToDelete.userId,
|
2021-10-07 15:14:47 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
name: true,
|
|
|
|
email: true,
|
|
|
|
timeZone: true,
|
2022-01-27 20:32:53 +00:00
|
|
|
locale: true,
|
2021-10-07 15:14:47 +00:00
|
|
|
},
|
2021-10-26 16:17:24 +00:00
|
|
|
rejectOnNotFound: true,
|
2021-10-07 15:14:47 +00:00
|
|
|
});
|
|
|
|
|
2022-01-27 20:32:53 +00:00
|
|
|
const attendeesListPromises = bookingToDelete.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);
|
|
|
|
const tOrganizer = await getTranslation(organizer.locale ?? "en", "common");
|
2021-10-26 16:17:24 +00:00
|
|
|
|
2021-10-07 15:14:47 +00:00
|
|
|
const evt: CalendarEvent = {
|
|
|
|
title: bookingToDelete?.title,
|
2022-01-21 12:06:45 +00:00
|
|
|
type: bookingToDelete?.eventType?.title as string,
|
2021-10-07 15:14:47 +00:00
|
|
|
description: bookingToDelete?.description || "",
|
2022-01-26 16:41:18 +00:00
|
|
|
startTime: bookingToDelete?.startTime ? dayjs(bookingToDelete.startTime).format() : "",
|
|
|
|
endTime: bookingToDelete?.endTime ? dayjs(bookingToDelete.endTime).format() : "",
|
2021-10-26 16:17:24 +00:00
|
|
|
organizer: {
|
|
|
|
email: organizer.email,
|
|
|
|
name: organizer.name ?? "Nameless",
|
|
|
|
timeZone: organizer.timeZone,
|
2022-01-27 20:32:53 +00:00
|
|
|
language: { translate: tOrganizer, locale: organizer.locale ?? "en" },
|
2021-10-26 16:17:24 +00:00
|
|
|
},
|
2022-01-27 20:32:53 +00:00
|
|
|
attendees: attendeesList,
|
2021-10-26 16:17:24 +00:00
|
|
|
uid: bookingToDelete?.uid,
|
2021-11-26 11:03:43 +00:00
|
|
|
location: bookingToDelete?.location,
|
2022-01-21 21:35:31 +00:00
|
|
|
destinationCalendar: bookingToDelete?.destinationCalendar || bookingToDelete?.user.destinationCalendar,
|
2022-01-28 17:40:29 +00:00
|
|
|
cancellationReason: cancellationReason,
|
2021-10-07 15:14:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Hook up the webhook logic here
|
2022-03-02 16:24:57 +00:00
|
|
|
const eventTrigger: WebhookTriggerEvents = "BOOKING_CANCELLED";
|
2021-10-07 15:14:47 +00:00
|
|
|
// Send Webhook call if hooked to BOOKING.CANCELLED
|
2022-03-02 16:24:57 +00:00
|
|
|
const subscriberOptions = {
|
|
|
|
userId: bookingToDelete.userId,
|
2022-03-11 09:00:25 +00:00
|
|
|
eventTypeId: (bookingToDelete.eventTypeId as number) || 0,
|
2022-03-02 16:24:57 +00:00
|
|
|
triggerEvent: eventTrigger,
|
|
|
|
};
|
|
|
|
const subscribers = await getSubscribers(subscriberOptions);
|
2021-11-22 11:37:07 +00:00
|
|
|
const promises = subscribers.map((sub) =>
|
|
|
|
sendPayload(eventTrigger, new Date().toISOString(), sub.subscriberUrl, evt, sub.payloadTemplate).catch(
|
|
|
|
(e) => {
|
|
|
|
console.error(`Error executing webhook for event: ${eventTrigger}, URL: ${sub.subscriberUrl}`, e);
|
|
|
|
}
|
|
|
|
)
|
2021-10-07 15:14:47 +00:00
|
|
|
);
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
// by cancelling first, and blocking whilst doing so; we can ensure a cancel
|
|
|
|
// action always succeeds even if subsequent integrations fail cancellation.
|
|
|
|
await prisma.booking.update({
|
|
|
|
where: {
|
|
|
|
uid,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
status: BookingStatus.CANCELLED,
|
2022-01-28 17:40:29 +00:00
|
|
|
cancellationReason: cancellationReason,
|
2021-09-14 08:45:28 +00:00
|
|
|
},
|
|
|
|
});
|
2021-09-13 08:57:56 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
if (bookingToDelete.location === "integrations:daily") {
|
|
|
|
bookingToDelete.user.credentials.push(FAKE_DAILY_CREDENTIAL);
|
|
|
|
}
|
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
const apiDeletes = async.mapLimit(bookingToDelete.user.credentials, 5, async (credential) => {
|
|
|
|
const bookingRefUid = bookingToDelete.references.filter((ref) => ref.type === credential.type)[0]?.uid;
|
|
|
|
if (bookingRefUid) {
|
|
|
|
if (credential.type.endsWith("_calendar")) {
|
2022-01-06 17:28:31 +00:00
|
|
|
const calendar = getCalendar(credential);
|
|
|
|
|
2022-01-13 19:47:15 +00:00
|
|
|
return calendar?.deleteEvent(bookingRefUid, evt);
|
2021-09-14 08:45:28 +00:00
|
|
|
} else if (credential.type.endsWith("_video")) {
|
2022-01-06 17:28:31 +00:00
|
|
|
return deleteMeeting(credential, bookingRefUid);
|
2021-06-14 17:00:17 +00:00
|
|
|
}
|
2021-09-14 08:45:28 +00:00
|
|
|
}
|
|
|
|
});
|
2021-09-13 08:57:56 +00:00
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
if (bookingToDelete && bookingToDelete.paid) {
|
|
|
|
const evt: CalendarEvent = {
|
2022-01-21 12:06:45 +00:00
|
|
|
type: bookingToDelete?.eventType?.title as string,
|
2021-09-22 18:36:13 +00:00
|
|
|
title: bookingToDelete.title,
|
|
|
|
description: bookingToDelete.description ?? "",
|
|
|
|
startTime: bookingToDelete.startTime.toISOString(),
|
|
|
|
endTime: bookingToDelete.endTime.toISOString(),
|
|
|
|
organizer: {
|
|
|
|
email: bookingToDelete.user?.email ?? "dev@calendso.com",
|
|
|
|
name: bookingToDelete.user?.name ?? "no user",
|
|
|
|
timeZone: bookingToDelete.user?.timeZone ?? "",
|
2022-01-27 20:32:53 +00:00
|
|
|
language: { translate: tOrganizer, locale: organizer.locale ?? "en" },
|
2021-09-22 18:36:13 +00:00
|
|
|
},
|
2022-01-27 20:32:53 +00:00
|
|
|
attendees: attendeesList,
|
2021-09-22 18:36:13 +00:00
|
|
|
location: bookingToDelete.location ?? "",
|
2021-10-26 16:17:24 +00:00
|
|
|
uid: bookingToDelete.uid ?? "",
|
2022-01-21 21:35:31 +00:00
|
|
|
destinationCalendar: bookingToDelete?.destinationCalendar || bookingToDelete?.user.destinationCalendar,
|
2021-09-22 18:36:13 +00:00
|
|
|
};
|
|
|
|
await refund(bookingToDelete, evt);
|
|
|
|
await prisma.booking.update({
|
|
|
|
where: {
|
|
|
|
id: bookingToDelete.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
rejected: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// We skip the deletion of the event, because that would also delete the payment reference, which we should keep
|
|
|
|
await apiDeletes;
|
|
|
|
return res.status(200).json({ message: "Booking successfully deleted." });
|
|
|
|
}
|
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
const attendeeDeletes = prisma.attendee.deleteMany({
|
|
|
|
where: {
|
|
|
|
bookingId: bookingToDelete.id,
|
|
|
|
},
|
|
|
|
});
|
2021-09-13 08:57:56 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
const bookingReferenceDeletes = prisma.bookingReference.deleteMany({
|
|
|
|
where: {
|
|
|
|
bookingId: bookingToDelete.id,
|
|
|
|
},
|
|
|
|
});
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
await Promise.all([apiDeletes, attendeeDeletes, bookingReferenceDeletes]);
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2021-11-26 11:03:43 +00:00
|
|
|
await sendCancelledEmails(evt);
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
res.status(204).end();
|
2021-07-15 21:34:55 +00:00
|
|
|
}
|