Skip to content

Scheduling infrastructure for absolutely everyone.

chore: sort calendar crendentials in event manager (#15448)

Shaik-Sirajuddin58540af9ce2f
diff --git a/packages/core/EventManager.ts b/packages/core/EventManager.ts
index 82cd50a2888..d6ddcc2839b 100644
--- a/packages/core/EventManager.ts
+++ b/packages/core/EventManager.ts
@@ -40,6 +40,14 @@ export const isDedicatedIntegration = (location: string): boolean => {
return location !== MeetLocationType && location.includes("integrations:");
};
+interface HasId {
+ id: number;
+}
+
+const latestCredentialFirst = <T extends HasId>(a: T, b: T) => {
+ return b.id - a.id;
+};
+
export const getLocationRequestFromIntegration = (location: string) => {
const eventLocationType = getEventLocationTypeFromApp(location);
if (eventLocationType) {
@@ -98,18 +106,19 @@ export default class EventManager {
// This includes all calendar-related apps, traditional calendars such as Google Calendar
// (type google_calendar) and non-traditional calendars such as CRMs like Close.com
// (type closecom_other_calendar)
- this.calendarCredentials = appCredentials.filter(
- // Backwards compatibility until CRM manager is implemented
- (cred) => cred.type.endsWith("_calendar") && !cred.type.includes("other_calendar")
- );
+ this.calendarCredentials = appCredentials
+ .filter(
+ // Backwards compatibility until CRM manager is implemented
+ (cred) => cred.type.endsWith("_calendar") && !cred.type.includes("other_calendar")
+ )
+ //see https://github.com/calcom/cal.com/issues/11671#issue-1923600672
+ .sort(latestCredentialFirst);
this.videoCredentials = appCredentials
.filter((cred) => cred.type.endsWith("_video") || cred.type.endsWith("_conferencing"))
// Whenever a new video connection is added, latest credentials are added with the highest ID.
// Because you can't rely on having them in the highest first order here, ensure this by sorting in DESC order
// We also don't have updatedAt or createdAt dates on credentials so this is the best we can do
- .sort((a, b) => {
- return b.id - a.id;
- });
+ .sort(latestCredentialFirst);
this.crmCredentials = appCredentials.filter(
(cred) => cred.type.endsWith("_crm") || cred.type.endsWith("_other_calendar")
);
@@ -592,7 +601,7 @@ export default class EventManager {
private async createAllCalendarEvents(event: CalendarEvent) {
let createdEvents: EventResult<NewCalendarEventType>[] = [];
- const fallbackToFirstConnectedCalendar = async () => {
+ const fallbackToFirstCalendarInTheList = async () => {
/**
* Not ideal but, if we don't find a destination calendar,
* fallback to the first connected calendar - Shouldn't be a CRM calendar
@@ -673,7 +682,7 @@ export default class EventManager {
log.warn(
"No other credentials found of the same type as the destination calendar. Falling back to first connected calendar"
);
- await fallbackToFirstConnectedCalendar();
+ await fallbackToFirstCalendarInTheList();
} else {
log.warn(
"No credentialId found for destination calendar, falling back to first found calendar of same type as destination calendar",
@@ -694,7 +703,7 @@ export default class EventManager {
calendarCredentials: this.calendarCredentials,
})
);
- await fallbackToFirstConnectedCalendar();
+ await fallbackToFirstCalendarInTheList();
}
// Taking care of non-traditional calendar integrations