2021-09-13 08:57:56 +00:00
|
|
|
import { BookingStatus } from "@prisma/client";
|
2021-09-22 19:52:38 +00:00
|
|
|
import async from "async";
|
|
|
|
|
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-09-22 19:52:38 +00:00
|
|
|
import { CalendarEvent, deleteEvent } from "@lib/calendarClient";
|
|
|
|
import prisma from "@lib/prisma";
|
|
|
|
import { deleteMeeting } from "@lib/videoClient";
|
|
|
|
|
2021-06-06 01:51:24 +00:00
|
|
|
export default async function handler(req, res) {
|
2021-09-14 08:45:28 +00:00
|
|
|
// just bail if it not a DELETE
|
2021-10-05 01:30:00 +00:00
|
|
|
if (req.method !== "DELETE") {
|
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) || "";
|
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,
|
|
|
|
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,
|
2021-06-09 19:46:41 +00:00
|
|
|
},
|
2021-09-14 08:45:28 +00:00
|
|
|
},
|
|
|
|
attendees: true,
|
|
|
|
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,
|
|
|
|
location: true,
|
|
|
|
title: true,
|
|
|
|
description: true,
|
|
|
|
startTime: true,
|
|
|
|
endTime: true,
|
|
|
|
uid: 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-09-28 17:23:50 +00:00
|
|
|
if ((!session || session.user?.id != bookingToDelete.user?.id) && bookingToDelete.startTime < new Date()) {
|
|
|
|
return res.status(403).json({ message: "Cannot cancel past events" });
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
});
|
2021-09-13 08:57:56 +00:00
|
|
|
|
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")) {
|
|
|
|
return await deleteEvent(credential, bookingRefUid);
|
|
|
|
} else if (credential.type.endsWith("_video")) {
|
|
|
|
return await 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 = {
|
|
|
|
type: bookingToDelete.title,
|
|
|
|
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 ?? "",
|
|
|
|
},
|
|
|
|
attendees: bookingToDelete.attendees,
|
|
|
|
location: bookingToDelete.location ?? "",
|
|
|
|
};
|
|
|
|
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-09-14 08:45:28 +00:00
|
|
|
//TODO Perhaps send emails to user and client to tell about the cancellation
|
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
|
|
|
}
|