Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: sort calendar crendentials in event manager #15448

29 changes: 19 additions & 10 deletions packages/core/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -694,7 +703,7 @@ export default class EventManager {
calendarCredentials: this.calendarCredentials,
})
);
await fallbackToFirstConnectedCalendar();
await fallbackToFirstCalendarInTheList();
}

// Taking care of non-traditional calendar integrations
Expand Down
Loading