Skip to content

Scheduling infrastructure for absolutely everyone.

Refactor cancel integrations logic (#3416)

Joe Au-Yeung0e9d754f64ff
diff --git a/apps/web/pages/api/cancel.ts b/apps/web/pages/api/cancel.ts
index 79fdeefe9da..2f606d9da4a 100644
--- a/apps/web/pages/api/cancel.ts
+++ b/apps/web/pages/api/cancel.ts
@@ -64,6 +64,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
uid: true,
type: true,
externalCalendarId: true,
+ credentialId: true,
},
},
payment: true,
@@ -206,21 +207,59 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
bookingToDelete.user.credentials.push(FAKE_DAILY_CREDENTIAL);
}
- const apiDeletes = async.mapLimit(bookingToDelete.user.credentials, 5, async (credential: Credential) => {
- const bookingRefUid = bookingToDelete.references.filter((ref) => ref.type === credential.type)[0]?.uid;
- const bookingExternalCalendarId = bookingToDelete.references.filter(
- (ref) => ref.type === credential.type
- )[0]?.externalCalendarId;
- if (bookingRefUid) {
- if (credential.type.endsWith("_calendar")) {
- const calendar = getCalendar(credential);
+ const apiDeletes = [];
- return calendar?.deleteEvent(bookingRefUid, evt, bookingExternalCalendarId);
- } else if (credential.type.endsWith("_video")) {
- return deleteMeeting(credential, bookingRefUid);
+ const bookingCalendarReference = bookingToDelete.references.find((reference) =>
+ reference.type.includes("_calendar")
+ );
+
+ if (bookingCalendarReference) {
+ const { credentialId, uid, externalCalendarId } = bookingCalendarReference;
+ // If the booking calendar reference contains a credentialId
+ if (credentialId) {
+ // Find the correct calendar credential under user credentials
+ const calendarCredential = bookingToDelete.user.credentials.find(
+ (credential) => credential.id === credentialId
+ );
+ if (calendarCredential) {
+ const calendar = getCalendar(calendarCredential);
+ apiDeletes.push(calendar?.deleteEvent(uid, evt, externalCalendarId));
}
+ // For bookings made before the refactor we go through the old behaviour of running through each calendar credential
+ } else {
+ bookingToDelete.user.credentials
+ .filter((credential) => credential.type.endsWith("_calendar"))
+ .forEach((credential) => {
+ const calendar = getCalendar(credential);
+ apiDeletes.push(calendar?.deleteEvent(uid, evt, externalCalendarId));
+ });
}
- });
+ }
+
+ const bookingVideoReference = bookingToDelete.references.find((reference) =>
+ reference.type.includes("_video")
+ );
+
+ // If the video reference has a credentialId find the specific credential
+ if (bookingVideoReference && bookingVideoReference.credentialId) {
+ const { credentialId, uid } = bookingVideoReference;
+ if (credentialId) {
+ const videoCredential = bookingToDelete.user.credentials.find(
+ (credential) => credential.id === credentialId
+ );
+
+ if (videoCredential) {
+ apiDeletes.push(deleteMeeting(videoCredential, uid));
+ }
+ }
+ // For bookings made before this refactor we go through the old behaviour of running through each video credential
+ } else {
+ bookingToDelete.user.credentials
+ .filter((credential) => credential.type.endsWith("_video"))
+ .forEach((credential) => {
+ apiDeletes.push(deleteMeeting(credential, uid));
+ });
+ }
// Avoiding taking care of recurrence for now as Payments are not supported with Recurring Events at the moment
if (bookingToDelete && bookingToDelete.paid) {