fix/request-reschedule-google-calendar-sync (#3157)

* Pass calendarId as parameter to deleteEvent()

* Updated deleteEvent param and default when sending calendarId
This commit is contained in:
alannnc 2022-06-27 08:52:42 -06:00 committed by GitHub
parent 3449ad1a9f
commit 9a3624bb9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 31 deletions

View File

@ -193,38 +193,17 @@ const handler = async (
if (bookingRef.type.endsWith("_calendar")) {
const calendar = getCalendar(credentialsMap.get(bookingRef.type));
return calendar?.deleteEvent(bookingRef.uid, builder.calendarEvent);
return calendar?.deleteEvent(
bookingRef.uid,
builder.calendarEvent,
bookingRef.externalCalendarId
);
} else if (bookingRef.type.endsWith("_video")) {
return deleteMeeting(credentialsMap.get(bookingRef.type), bookingRef.uid);
}
}
});
// Updating attendee destinationCalendar if required
if (
bookingToReschedule.destinationCalendar &&
bookingToReschedule.destinationCalendar.userId &&
bookingToReschedule.destinationCalendar.integration.endsWith("_calendar")
) {
const { destinationCalendar } = bookingToReschedule;
if (destinationCalendar.userId) {
const bookingRefsFiltered: BookingReference[] = bookingToReschedule.references.filter(
(ref) => !!credentialsMap.get(ref.type)
);
const attendeeData = await findUserDataByUserId(destinationCalendar.userId);
const attendeeCredentialsMap = new Map();
attendeeData.credentials.forEach((credential) => {
attendeeCredentialsMap.set(credential.type, credential);
});
bookingRefsFiltered.forEach((bookingRef) => {
if (bookingRef.uid) {
const calendar = getCalendar(attendeeCredentialsMap.get(destinationCalendar.integration));
calendar?.deleteEvent(bookingRef.uid, builder.calendarEvent);
}
});
}
}
// Send emails
await sendRequestRescheduleEmail(builder.calendarEvent, {
rescheduleLink: builder.rescheduleLink,

View File

@ -216,7 +216,7 @@ export default class GoogleCalendarService implements Calendar {
});
}
async deleteEvent(uid: string, event: CalendarEvent, externalCalendarId: string): Promise<void> {
async deleteEvent(uid: string, event: CalendarEvent, externalCalendarId?: string | null): Promise<void> {
return new Promise(async (resolve, reject) => {
const auth = await this.auth;
const myGoogleAuth = await auth.getToken();
@ -224,20 +224,27 @@ export default class GoogleCalendarService implements Calendar {
version: "v3",
auth: myGoogleAuth,
});
const defaultCalendarId = "primary";
const calendarId = externalCalendarId ? externalCalendarId : event.destinationCalendar?.externalId;
calendar.events.delete(
{
auth: myGoogleAuth,
calendarId: externalCalendarId ? externalCalendarId : event.destinationCalendar?.externalId,
calendarId: calendarId ? calendarId : defaultCalendarId,
eventId: uid,
sendNotifications: true,
sendUpdates: "all",
},
function (err: GoogleCalError | null, event) {
if (err) {
/* 410 is when an event is already deleted on the Google cal before on cal.com
404 is when the event is on a different calendar */
/**
* 410 is when an event is already deleted on the Google cal before on cal.com
* 404 is when the event is on a different calendar
*/
if (err.code === 410) return resolve();
console.error("There was an error contacting google calendar service: ", err);
if (err.code === 410 || err.code === 404) return resolve();
if (err.code === 404) return resolve();
return reject(err);
}
return resolve(event?.data);