Refactor cancel integrations logic (#3416)

* Refactor cancel integrations logic

* Add additional comments

Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Joe Au-Yeung
2022-07-19 16:00:01 -06:00
committed by GitHub
co-authored by Peer Richelsen kodiakhq[bot]
parent 3b7f18cb5d
commit 0e9d754f64
+51 -12
View File
@@ -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) {