diff --git a/packages/features/calendars/lib/CalendarManager.ts b/packages/features/calendars/lib/CalendarManager.ts index 278f89ce99..e3d63da85e 100644 --- a/packages/features/calendars/lib/CalendarManager.ts +++ b/packages/features/calendars/lib/CalendarManager.ts @@ -54,7 +54,6 @@ export const getConnectedCalendars = async ( selectedCalendars: { externalId: string }[], destinationCalendarExternalId?: string ) => { - let destinationCalendar: IntegrationCalendar | undefined; const connectedCalendars = await Promise.all( calendarCredentials.map(async (item) => { try { @@ -85,7 +84,6 @@ export const getConnectedCalendars = async ( const cals = await calendarInstance.listCalendars(); const calendars = sortBy( cals.map((cal: IntegrationCalendar) => { - if (cal.externalId === destinationCalendarExternalId) destinationCalendar = cal; return { ...cal, readOnly: cal.readOnly || false, @@ -107,12 +105,6 @@ export const getConnectedCalendars = async ( }, }; } - // HACK https://github.com/calcom/cal.com/pull/7644/files#r1131508414 - if (destinationCalendar && !Object.isFrozen(destinationCalendar)) { - destinationCalendar.primaryEmail = primary.email; - destinationCalendar.integrationTitle = integration.title; - destinationCalendar = Object.freeze(destinationCalendar); - } return { integration: safeToSendIntegration, @@ -149,6 +141,27 @@ export const getConnectedCalendars = async ( }) ); + let destinationCalendar: IntegrationCalendar | undefined; + if (destinationCalendarExternalId) { + for (const connectedCalendar of connectedCalendars) { + if (!("calendars" in connectedCalendar) || !connectedCalendar.calendars) { + continue; + } + const calendar = connectedCalendar.calendars.find( + (cal) => cal.externalId === destinationCalendarExternalId + ); + if (calendar) { + destinationCalendar = { + ...calendar, + primary: calendar.primary ?? undefined, + primaryEmail: connectedCalendar.primary?.email, + integrationTitle: connectedCalendar.integration?.title, + }; + break; + } + } + } + return { connectedCalendars, destinationCalendar }; };