From ccf99fbb6bccd77370bb5dba63a5ea71feb1beb5 Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Wed, 21 Jan 2026 11:30:20 -0300 Subject: [PATCH] refactor: remove Object.freeze hack in getConnectedCalendars (#27070) * refactor: remove Object.freeze hack in getConnectedCalendars The previous implementation used Object.freeze to prevent the destinationCalendar object from being mutated when looping through multiple calendar credentials. This was a workaround for a bug where the wrong calendar's primaryEmail and integrationTitle would be set when multiple credentials existed. This refactor properly fixes the issue by: 1. Only setting destinationCalendar once (when first found) 2. Using a flag to track when the destination calendar is found in the current credential 3. Only setting primaryEmail and integrationTitle for the credential that contains the destination calendar Fixes the hack mentioned in: https://github.com/calcom/cal.com/pull/7644 Co-Authored-By: alex@cal.com * refactor: move destinationCalendar enrichment outside the loop Per feedback, moved the destinationCalendar finding and enrichment completely outside the Promise.all loop. After all connected calendars are fetched, we search through the results to find and enrich the destination calendar with primaryEmail and integrationTitle. Co-Authored-By: alex@cal.com --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../features/calendars/lib/CalendarManager.ts | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) 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 }; };