refactor: Falling Back to `FirstCalendarCredential` (#11986)

12120-chore-upgrade-to-next-1400
Joe Au-Yeung 2023-10-27 11:52:56 -04:00 committed by GitHub
parent 426d31712e
commit 2831fb2b57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 25 deletions

View File

@ -489,6 +489,22 @@ export default class EventManager {
*/ */
private async createAllCalendarEvents(event: CalendarEvent) { private async createAllCalendarEvents(event: CalendarEvent) {
let createdEvents: EventResult<NewCalendarEventType>[] = []; let createdEvents: EventResult<NewCalendarEventType>[] = [];
const fallbackToFirstConnectedCalendar = async () => {
/**
* Not ideal but, if we don't find a destination calendar,
* fallback to the first connected calendar - Shouldn't be a CRM calendar
*/
const [credential] = this.calendarCredentials.filter((cred) => !cred.type.endsWith("other_calendar"));
if (credential) {
const createdEvent = await createEvent(credential, event);
log.silly("Created Calendar event", safeStringify({ createdEvent }));
if (createdEvent) {
createdEvents.push(createdEvent);
}
}
};
if (event.destinationCalendar && event.destinationCalendar.length > 0) { if (event.destinationCalendar && event.destinationCalendar.length > 0) {
// Since GCal pushes events to multiple calendars we only want to create one event per booking // Since GCal pushes events to multiple calendars we only want to create one event per booking
let gCalAdded = false; let gCalAdded = false;
@ -545,6 +561,14 @@ export default class EventManager {
); );
// It might not be the first connected calendar as it seems that the order is not guaranteed to be ascending of credentialId. // It might not be the first connected calendar as it seems that the order is not guaranteed to be ascending of credentialId.
const firstCalendarCredential = destinationCalendarCredentials[0]; const firstCalendarCredential = destinationCalendarCredentials[0];
if (!firstCalendarCredential) {
log.warn(
"No other credentials found of the same type as the destination calendar. Falling back to first connected calendar"
);
await fallbackToFirstConnectedCalendar();
}
log.warn( log.warn(
"No credentialId found for destination calendar, falling back to first found calendar", "No credentialId found for destination calendar, falling back to first found calendar",
safeStringify({ safeStringify({
@ -563,19 +587,7 @@ export default class EventManager {
calendarCredentials: this.calendarCredentials, calendarCredentials: this.calendarCredentials,
}) })
); );
await fallbackToFirstConnectedCalendar();
/**
* Not ideal but, if we don't find a destination calendar,
* fallback to the first connected calendar - Shouldn't be a CRM calendar
*/
const [credential] = this.calendarCredentials.filter((cred) => !cred.type.endsWith("other_calendar"));
if (credential) {
const createdEvent = await createEvent(credential, event);
log.silly("Created Calendar event", safeStringify({ createdEvent }));
if (createdEvent) {
createdEvents.push(createdEvent);
}
}
} }
// Taking care of non-traditional calendar integrations // Taking care of non-traditional calendar integrations

View File

@ -59,18 +59,12 @@ export function getPiiFreeBooking(booking: {
} }
export function getPiiFreeCredential(credential: Partial<Credential>) { export function getPiiFreeCredential(credential: Partial<Credential>) {
return { /**
id: credential.id, * Let's just get a boolean value for PII sensitive fields so that we atleast know if it's present or not
invalid: credential.invalid, */
appId: credential.appId, const booleanKeyStatus = getBooleanStatus(credential?.key);
userId: credential.userId,
type: credential.type, return { ...credential, key: booleanKeyStatus };
teamId: credential.teamId,
/**
* Let's just get a boolean value for PII sensitive fields so that we atleast know if it's present or not
*/
key: getBooleanStatus(credential.key),
};
} }
export function getPiiFreeSelectedCalendar(selectedCalendar: Partial<SelectedCalendar>) { export function getPiiFreeSelectedCalendar(selectedCalendar: Partial<SelectedCalendar>) {