From 59a9a77e3fbf540974812611808389944cd8739a Mon Sep 17 00:00:00 2001 From: Leo Giovanetti Date: Tue, 10 Jan 2023 18:09:28 -0300 Subject: [PATCH] Fixes cancelling all recurring events (#6384) --- .../bookings/lib/handleCancelBooking.ts | 59 +++++++++++++++++-- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/packages/features/bookings/lib/handleCancelBooking.ts b/packages/features/bookings/lib/handleCancelBooking.ts index f8182ed25c..99a41e8769 100644 --- a/packages/features/bookings/lib/handleCancelBooking.ts +++ b/packages/features/bookings/lib/handleCancelBooking.ts @@ -198,6 +198,14 @@ async function handler(req: NextApiRequest & { userId?: number }) { uid: string; workflowReminders: WorkflowReminder[]; scheduledJobs: string[]; + references: { + type: string; + credentialId: number | null; + uid: string; + externalCalendarId: string | null; + }[]; + startTime: Date; + endTime: Date; }[] = []; // by cancelling first, and blocking whilst doing so; we can ensure a cancel @@ -225,6 +233,16 @@ async function handler(req: NextApiRequest & { userId?: number }) { }, }, select: { + startTime: true, + endTime: true, + references: { + select: { + uid: true, + type: true, + externalCalendarId: true, + credentialId: true, + }, + }, workflowReminders: true, uid: true, scheduledJobs: true, @@ -242,6 +260,16 @@ async function handler(req: NextApiRequest & { userId?: number }) { cancellationReason: cancellationReason, }, select: { + startTime: true, + endTime: true, + references: { + select: { + uid: true, + type: true, + externalCalendarId: true, + credentialId: true, + }, + }, workflowReminders: true, uid: true, scheduledJobs: true, @@ -271,15 +299,34 @@ async function handler(req: NextApiRequest & { userId?: number }) { ); if (calendarCredential) { const calendar = getCalendar(calendarCredential); - apiDeletes.push(calendar?.deleteEvent(uid, evt, externalCalendarId)); + if ( + bookingToDelete.eventType?.recurringEvent && + bookingToDelete.recurringEventId && + allRemainingBookings + ) { + bookingToDelete.user.credentials + .filter((credential) => credential.type.endsWith("_calendar")) + .forEach((credential) => { + const calendar = getCalendar(credential); + updatedBookings.forEach((updBooking) => { + const bookingRef = updBooking.references.find((ref) => ref.type.includes("_calendar")); + if (bookingRef) { + const { uid, externalCalendarId } = bookingRef; + apiDeletes.push(calendar?.deleteEvent(uid, evt, externalCalendarId) as Promise); + } + }); + }); + } else { + apiDeletes.push(calendar?.deleteEvent(uid, evt, externalCalendarId) as Promise); + } } - // For bookings made before the refactor we go through the old behaviour of running through each calendar credential } else { + // For bookings made before the refactor we go through the old behaviour of running through each calendar credential bookingToDelete.user.credentials .filter((credential) => credential.type.endsWith("_calendar")) .forEach((credential) => { const calendar = getCalendar(credential); - apiDeletes.push(calendar?.deleteEvent(uid, evt, externalCalendarId)); + apiDeletes.push(calendar?.deleteEvent(uid, evt, externalCalendarId) as Promise); }); } } @@ -382,7 +429,11 @@ async function handler(req: NextApiRequest & { userId?: number }) { }); }); - await Promise.all([apiDeletes, attendeeDeletes, bookingReferenceDeletes].concat(remindersToDelete)); + const prismaPromises: Promise[] = [attendeeDeletes, bookingReferenceDeletes].concat( + remindersToDelete + ); + + await Promise.all(prismaPromises.concat(apiDeletes)); await sendCancelledEmails(evt);