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 <me@alexvanandel.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 <me@alexvanandel.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
parent
f00f80ed26
commit
ccf99fbb6b
@@ -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 };
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user