From 9a3624bb9a284b9b2260f2bf35c7dd8a2fc52bed Mon Sep 17 00:00:00 2001 From: alannnc Date: Mon, 27 Jun 2022 08:52:42 -0600 Subject: [PATCH] fix/request-reschedule-google-calendar-sync (#3157) * Pass calendarId as parameter to deleteEvent() * Updated deleteEvent param and default when sending calendarId --- apps/web/pages/api/book/request-reschedule.ts | 31 +++---------------- .../googlecalendar/lib/CalendarService.ts | 17 +++++++--- 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/apps/web/pages/api/book/request-reschedule.ts b/apps/web/pages/api/book/request-reschedule.ts index 6c7d7fc87a..8a54528127 100644 --- a/apps/web/pages/api/book/request-reschedule.ts +++ b/apps/web/pages/api/book/request-reschedule.ts @@ -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, diff --git a/packages/app-store/googlecalendar/lib/CalendarService.ts b/packages/app-store/googlecalendar/lib/CalendarService.ts index 78fcd7ce43..1240962576 100644 --- a/packages/app-store/googlecalendar/lib/CalendarService.ts +++ b/packages/app-store/googlecalendar/lib/CalendarService.ts @@ -216,7 +216,7 @@ export default class GoogleCalendarService implements Calendar { }); } - async deleteEvent(uid: string, event: CalendarEvent, externalCalendarId: string): Promise { + async deleteEvent(uid: string, event: CalendarEvent, externalCalendarId?: string | null): Promise { 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);