perf: optimize fetch for primary google calendar (#22041)

* perf: optimize getting primary google calendar

* add test

* use calendars

* safer
This commit is contained in:
Benny Joo authored and GitHub committed 2025-06-25 23:10:25 +01:00
1 parent 29aad2c535
commit 801b0be414
5 files changed
+47 -51

No files matched your search

@@ -678,15 +678,6 @@ export default class GoogleCalendarService implements Calendar {
}
}
/**
* Better alternative to `getPrimaryCalendar` that doesn't require any argument
*/
async fetchPrimaryCalendar() {
const calendar = await this.authedCalendar();
const primaryCalendar = await this.getPrimaryCalendar(calendar);
return primaryCalendar;
}
async listCalendars(): Promise<IntegrationCalendar[]> {
this.log.debug("Listing calendars");
const calendar = await this.authedCalendar();
@@ -1009,39 +1000,16 @@ export default class GoogleCalendarService implements Calendar {
}
}
async getPrimaryCalendar(
calendar: calendar_v3.Calendar,
fields: string[] = ["id", "summary", "primary", "accessRole"]
): Promise<calendar_v3.Schema$CalendarListEntry | null> {
let pageToken: string | undefined;
let firstCalendar: calendar_v3.Schema$CalendarListEntry | undefined;
async getPrimaryCalendar(_calendar?: calendar_v3.Calendar): Promise<calendar_v3.Schema$Calendar | null> {
try {
do {
const response: any = await calendar.calendarList.list({
fields: `items(${fields.join(",")}),nextPageToken`,
pageToken,
maxResults: 250, // 250 is max
});
const cals = response.data.items ?? [];
const primaryCal = cals.find((cal: calendar_v3.Schema$CalendarListEntry) => cal.primary);
if (primaryCal) {
return primaryCal;
}
// Store the first calendar in case no primary is found
if (cals.length > 0 && !firstCalendar) {
firstCalendar = cals[0];
}
pageToken = response.data.nextPageToken;
} while (pageToken);
// should not be reached because Google Cal always has a primary cal
return firstCalendar ?? null;
const calendar = _calendar ?? (await this.authedCalendar());
const response = await calendar.calendars.get({
calendarId: "primary",
});
return response.data;
} catch (error) {
logger.error("Error in `getPrimaryCalendar`", { error });
// should not be reached because Google Cal always has a primary cal
logger.error("Error getting primary calendar", { error });
throw error;
}
}
@@ -3,6 +3,7 @@ import { vi } from "vitest";
const setCredentialsMock = vi.fn();
const freebusyQueryMock = vi.fn();
const calendarListMock = vi.fn();
const calendarsGetMock = vi.fn();
const calendarMockImplementation = {
channels: {
stop: vi.fn().mockResolvedValue(undefined),
@@ -10,6 +11,9 @@ const calendarMockImplementation = {
calendarList: {
list: calendarListMock,
},
calendars: {
get: calendarsGetMock,
},
events: {
watch: vi.fn().mockResolvedValue({
data: {
@@ -885,3 +885,27 @@ describe("getAvailability", () => {
expect(availabilityWithAllCalendarsAsFallback).toEqual([...mockedBusyTimes1, ...mockedBusyTimes2]);
});
});
describe("getPrimaryCalendar", () => {
test("should fetch primary calendar using 'primary' keyword", async () => {
const credential = await createCredentialForCalendarService();
const calendarService = new CalendarService(credential);
setFullMockOAuthManagerRequest();
const mockPrimaryCalendar = {
id: "[email protected]",
summary: "Primary Calendar",
description: "Primary calendar for [email protected]",
timeZone: "America/New_York",
};
const calendarsGetMock = vi.fn().mockResolvedValue({
data: mockPrimaryCalendar,
});
calendarMock.calendar_v3.Calendar().calendars.get = calendarsGetMock;
const result = await calendarService.getPrimaryCalendar();
expect(calendarsGetMock).toHaveBeenCalledTimes(1);
expect(calendarsGetMock).toHaveBeenCalledWith({
calendarId: "primary",
});
expect(result).toEqual(mockPrimaryCalendar);
});
});