Converts booking deletion to soft-delete + more robust cancellation (#581)

* Converts booking deletion to soft-delete + more robust cancellation

* Update pages/api/cancel.ts

infer type :)

Co-authored-by: Alex Johansson <[email protected]>

Co-authored-by: Alex Johansson <[email protected]>
Co-authored-by: Bailey Pumfleet <[email protected]>
This commit is contained in:
authored and GitHub committed 2021-09-13 09:57:56 +01:00
1 parent e48318a34b
commit 8ee68e2ace
4 files changed
+49 -19

No files matched your search

+26 -12
View File
@@ -1,15 +1,17 @@
import prisma from "../../lib/prisma";
import { deleteEvent } from "../../lib/calendarClient";
import prisma from "@lib/prisma";
import { deleteEvent } from "@lib/calendarClient";
import async from "async";
import { deleteMeeting } from "../../lib/videoClient";
import { deleteMeeting } from "@lib/videoClient";
import { asStringOrNull } from "@lib/asStringOrNull";
import { BookingStatus } from "@prisma/client";
export default async function handler(req, res) {
if (req.method == "POST") {
const uid = req.body.uid;
const uid = asStringOrNull(req.body.uid);
const bookingToDelete = await prisma.booking.findFirst({
const bookingToDelete = await prisma.booking.findUnique({
where: {
uid: uid,
uid,
},
select: {
id: true,
@@ -28,6 +30,21 @@ export default async function handler(req, res) {
},
});
if (!bookingToDelete) {
return res.status(404).end();
}
// 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,
},
});
const apiDeletes = async.mapLimit(bookingToDelete.user.credentials, 5, async (credential) => {
const bookingRefUid = bookingToDelete.references.filter((ref) => ref.type === credential.type)[0]?.uid;
if (bookingRefUid) {
@@ -38,23 +55,20 @@ export default async function handler(req, res) {
}
}
});
const attendeeDeletes = prisma.attendee.deleteMany({
where: {
bookingId: bookingToDelete.id,
},
});
const bookingReferenceDeletes = prisma.bookingReference.deleteMany({
where: {
bookingId: bookingToDelete.id,
},
});
const bookingDeletes = prisma.booking.delete({
where: {
id: bookingToDelete.id,
},
});
await Promise.all([apiDeletes, attendeeDeletes, bookingReferenceDeletes, bookingDeletes]);
await Promise.all([apiDeletes, attendeeDeletes, bookingReferenceDeletes]);
//TODO Perhaps send emails to user and client to tell about the cancellation