Skip to content

Commit

Permalink
fix: cancellation email for seated event for 2nd attendee onwards (ca…
Browse files Browse the repository at this point in the history
…lcom#10177)

Co-authored-by: Joe Au-Yeung <[email protected]>
Co-authored-by: Carina Wollendorfer <[email protected]>
  • Loading branch information
3 people authored Aug 4, 2023
1 parent 123ecf3 commit d22d809
Showing 1 changed file with 54 additions and 80 deletions.
134 changes: 54 additions & 80 deletions packages/features/bookings/lib/handleCancelBooking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,86 +259,6 @@ async function handler(req: CustomRequest) {
},
});

/* If there are references then we should update them as well */
const lastAttendee =
bookingToDelete.attendees.filter((bookingAttendee) => attendee.email !== bookingAttendee.email).length <
0;

const integrationsToDelete = [];

for (const reference of bookingToDelete.references) {
if (reference.credentialId) {
const credential = await prisma.credential.findUnique({
where: {
id: reference.credentialId,
},
});

if (credential) {
if (lastAttendee) {
if (reference.type.includes("_video")) {
integrationsToDelete.push(deleteMeeting(credential, reference.uid));
}
if (reference.type.includes("_calendar")) {
const calendar = await getCalendar(credential);
if (calendar) {
integrationsToDelete.push(
calendar?.deleteEvent(reference.uid, evt, reference.externalCalendarId)
);
}
}
} else {
const updatedEvt = {
...evt,
attendees: evt.attendees.filter((evtAttendee) => attendee.email !== evtAttendee.email),
};
if (reference.type.includes("_video")) {
integrationsToDelete.push(
updateMeeting(
{ ...credential, appName: evt.location?.replace("integrations:", "") || "" },
updatedEvt,
reference
)
);
}
if (reference.type.includes("_calendar")) {
const calendar = await getCalendar(credential);
if (calendar) {
integrationsToDelete.push(
calendar?.updateEvent(reference.uid, updatedEvt, reference.externalCalendarId)
);
}
}
}
}
}
}

try {
await Promise.all(integrationsToDelete).then(async () => {
if (lastAttendee) {
await prisma.booking.update({
where: {
id: bookingToDelete.id,
},
data: {
status: BookingStatus.CANCELLED,
},
});
}
});
} catch (error) {
// Shouldn't stop code execution if integrations fail
// as integrations was already deleted
}

const tAttendees = await getTranslation(attendee.locale ?? "en", "common");

await sendCancelledSeatEmails(evt, {
...attendee,
language: { translate: tAttendees, locale: attendee.locale ?? "en" },
});

req.statusCode = 200;
return { message: "No longer attending event" };
}
Expand Down Expand Up @@ -739,6 +659,60 @@ async function handleSeatedEventCancellation(

const attendee = bookingToDelete?.attendees.find((attendee) => attendee.id === seatReference.attendeeId);

if (attendee) {
/* If there are references then we should update them as well */

const integrationsToUpdate = [];

for (const reference of bookingToDelete.references) {
if (reference.credentialId) {
const credential = await prisma.credential.findUnique({
where: {
id: reference.credentialId,
},
});

if (credential) {
const updatedEvt = {
...evt,
attendees: evt.attendees.filter((evtAttendee) => attendee.email !== evtAttendee.email),
};
if (reference.type.includes("_video")) {
integrationsToUpdate.push(
updateMeeting(
{ ...credential, appName: evt.location?.replace("integrations:", "") || "" },
updatedEvt,
reference
)
);
}
if (reference.type.includes("_calendar")) {
const calendar = await getCalendar(credential);
if (calendar) {
integrationsToUpdate.push(
calendar?.updateEvent(reference.uid, updatedEvt, reference.externalCalendarId)
);
}
}
}
}
}

try {
await Promise.all(integrationsToUpdate);
} catch (error) {
// Shouldn't stop code execution if integrations fail
// as integrations was already updated
}

const tAttendees = await getTranslation(attendee.locale ?? "en", "common");

await sendCancelledSeatEmails(evt, {
...attendee,
language: { translate: tAttendees, locale: attendee.locale ?? "en" },
});
}

evt.attendees = attendee
? [
{
Expand Down

0 comments on commit d22d809

Please sign in to comment.