2021-09-13 08:57:56 +00:00
|
|
|
import prisma from "@lib/prisma";
|
|
|
|
import { deleteEvent } from "@lib/calendarClient";
|
|
|
|
import { deleteMeeting } from "@lib/videoClient";
|
2021-09-14 08:45:28 +00:00
|
|
|
import async from "async";
|
2021-09-13 08:57:56 +00:00
|
|
|
import { BookingStatus } from "@prisma/client";
|
2021-09-14 08:45:28 +00:00
|
|
|
import { asStringOrNull } from "@lib/asStringOrNull";
|
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
|
|
|
|
if (req.method !== "DELETE") {
|
|
|
|
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) || "";
|
|
|
|
|
|
|
|
const bookingToDelete = await prisma.booking.findUnique({
|
|
|
|
where: {
|
|
|
|
uid,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
user: {
|
|
|
|
select: {
|
|
|
|
credentials: 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-14 08:45:28 +00:00
|
|
|
},
|
|
|
|
});
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
if (!bookingToDelete) {
|
|
|
|
return res.status(404).end();
|
|
|
|
}
|
2021-09-13 08:57:56 +00:00
|
|
|
|
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-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
|
|
|
}
|