refactor: replace shouldServeCache with mode parameter for calendar cache control (#26539)

* refactor: replace shouldServeCache with mode parameter for calendar cache control

Replace the boolean shouldServeCache parameter with a new CalendarFetchMode type
that can have values 'slots', 'overlay', and 'booking'. This provides better
control over when to serve cache vs relay on calendar providers.

- 'slots' mode: Check feature flags and use cache when available (for getting
  actual calendar availability)
- 'overlay' mode: Don't use cache (for overlay calendar availability)
- 'booking' mode: Don't use cache (for booking confirmation)
- undefined: Same as 'slots' for backwards compatibility

The cache decision logic is now centralized in getCalendar.ts based on the mode
parameter.

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: update CalendarService.test.ts to use mode parameter

Co-Authored-By: Volnei Munhoz <[email protected]>

* refactor: use shared GetAvailabilityParams type across all calendar services

- Import GetAvailabilityParams and GetAvailabilityWithTimeZonesParams from @calcom/types/Calendar
- Replace inline type definitions with shared types in all calendar service implementations
- Update BaseCalendarService, CalendarCacheWrapper, and CalendarTelemetryWrapper
- Ensures consistent typing and follows DRY principles

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: add missing IntegrationCalendar import and update mock getAvailability signature

- Add IntegrationCalendar back to sendgrid CalendarService imports
- Update bookingScenario mock getAvailability to use typed params object
- Add listCalendars method to mock Calendar object

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: update test files to use typed params object for getAvailability methods

- Update CalendarCacheWrapper.test.ts to call getAvailability/getAvailabilityWithTimeZones with params object
- Update getCalendarsEvents.test.ts toHaveBeenCalledWith assertions to expect params object
- All 32 tests now pass

Co-Authored-By: Volnei Munhoz <[email protected]>

* refactor: consolidate to single GetAvailabilityParams type for both getAvailability methods

- Remove GetAvailabilityWithTimeZonesParams, use GetAvailabilityParams for both methods
- Add mode parameter to getAvailabilityWithTimeZones calls
- Update wrapper classes to pass mode through to underlying calendar
- Update test files to include mode in getAvailabilityWithTimeZones calls and assertions

Co-Authored-By: Volnei Munhoz <[email protected]>

* refactor: use EventBusyDate with optional timeZone for getAvailabilityWithTimeZones

- Add optional timeZone field to EventBusyDate type
- Update getAvailabilityWithTimeZones return type to use EventBusyDate[]
- Update Google Calendar service and wrapper classes to use the new type

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: type errors and remove unused calendar watching methods

- Fix type errors in CalendarCacheWrapper.ts (convert null to undefined for timeZone)
- Fix type errors in getCalendarsEvents.ts (ensure timeZone is always present)
- Remove unused watchCalendar/unwatchCalendar from Calendar interface
- Remove unused startWatchingCalendarsInGoogle/stopWatchingCalendarsInGoogle from GoogleCalendarService
- Remove unused imports (uuid, uniqueBy, GOOGLE_WEBHOOK_URL, ONE_MONTH_IN_MS)

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: remove watchCalendar/unwatchCalendar from CalendarTelemetryWrapper

Co-Authored-By: Volnei Munhoz <[email protected]>

* refactor: remove verbose JSDoc param comments from wrapper classes

Co-Authored-By: Volnei Munhoz <[email protected]>

* refactor: make mode parameter required in getCalendar

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: add required mode parameter to all getCalendar callers

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: provide default mode value in getBusyCalendarTimes

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: add mode parameter to remaining getCalendar callers

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: add mode parameter to vital and wipemycalother reschedule

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: add mode parameter to credential-sync API endpoint

Co-Authored-By: Volnei Munhoz <[email protected]>

* refactor: add 'none' mode to CalendarFetchMode and use as default

Co-Authored-By: Volnei Munhoz <[email protected]>

* test: add mode parameter to getCalendarsEvents test calls

Co-Authored-By: Volnei Munhoz <[email protected]>

* Update packages/app-store/delegationCredential.ts

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-01-08 09:07:41 -03:00
1 parent 64828abad2
commit a62eb2bdac
37 files changed
+356 -369

No files matched your search

@@ -38,7 +38,7 @@ import type {
} from "@calcom/prisma/client";
import { SchedulingType } from "@calcom/prisma/enums";
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
import type { EventBusyDetails, IntervalLimitUnit } from "@calcom/types/Calendar";
import type { CalendarFetchMode, EventBusyDetails, IntervalLimitUnit } from "@calcom/types/Calendar";
import type { TimeRange } from "@calcom/types/schedule";
import { findUsersForAvailabilityCheck } from "./findUsersForAvailabilityCheck";
@@ -58,7 +58,7 @@ const availabilitySchema = z
returnDateOverrides: z.boolean(),
bypassBusyCalendarTimes: z.boolean().optional(),
silentlyHandleCalendarFailures: z.boolean().optional(),
shouldServeCache: z.boolean().optional(),
mode: z.enum(["slots", "overlay", "booking"]).optional(),
})
.refine((data) => !!data.username || !!data.userId, "Either username or userId should be filled in.");
@@ -115,7 +115,7 @@ type GetUserAvailabilityQuery = {
returnDateOverrides: boolean;
bypassBusyCalendarTimes: boolean;
silentlyHandleCalendarFailures?: boolean;
shouldServeCache?: boolean;
mode?: CalendarFetchMode;
};
export type CurrentSeats = Awaited<
@@ -205,7 +205,7 @@ export class UserAvailabilityService {
for (const credential of delegatedCredentials) {
try {
const calendar = await getCalendar(credential);
const calendar = await getCalendar(credential, "slots");
if (calendar && "getMainTimeZone" in calendar && typeof calendar.getMainTimeZone === "function") {
const timezone = await calendar.getMainTimeZone();
if (timezone && timezone !== "UTC") {
@@ -304,7 +304,7 @@ export class UserAvailabilityService {
returnDateOverrides,
bypassBusyCalendarTimes = false,
silentlyHandleCalendarFailures = false,
shouldServeCache,
mode,
} = availabilitySchema.parse(query);
log.debug(
@@ -467,7 +467,7 @@ export class UserAvailabilityService {
currentBookings: initialData?.currentBookings,
bypassBusyCalendarTimes,
silentlyHandleCalendarFailures,
shouldServeCache,
mode,
});
} catch (error) {
log.error(`Error fetching busy times for user ${username}:`, error);
@@ -1160,7 +1160,7 @@ export default class EventManager {
const calendarCredential = await CredentialRepository.findCredentialForCalendarServiceById({
id: oldCalendarEvent.credentialId,
});
const calendar = await getCalendar(calendarCredential);
const calendar = await getCalendar(calendarCredential, "booking");
await calendar?.deleteEvent(oldCalendarEvent.uid, event, oldCalendarEvent.externalCalendarId);
}
}
@@ -687,7 +687,7 @@ export class LuckyUserService implements ILuckyUserService {
getIntervalStartDate({ interval, rrTimestampBasis, meetingStartTime }).toISOString(),
getIntervalEndDate({ interval, rrTimestampBasis, meetingStartTime }).toISOString(),
user.userLevelSelectedCalendars,
true,
"slots",
true
)
)
@@ -13,6 +13,7 @@ import { getPiiFreeUser } from "@calcom/lib/piiFreeData";
import { safeStringify } from "@calcom/lib/safeStringify";
import { withReporting } from "@calcom/lib/sentryWrapper";
import prisma from "@calcom/prisma";
import type { CalendarFetchMode } from "@calcom/types/Calendar";
import type { getEventTypeResponse } from "./getEventTypesFromDB";
import type { BookingType } from "./originalRescheduledBookingUtils";
@@ -59,7 +60,7 @@ const _ensureAvailableUsers = async (
},
input: { dateFrom: string; dateTo: string; timeZone: string; originalRescheduledBooking?: BookingType },
loggerWithEventDetails: Logger<unknown>,
shouldServeCache?: boolean
mode?: CalendarFetchMode
// ReturnType hint of at least one IsFixedAwareUser, as it's made sure at least one entry exists
): Promise<[IsFixedAwareUser, ...IsFixedAwareUser[]]> => {
const userAvailabilityService = getUserAvailabilityService();
@@ -102,7 +103,7 @@ const _ensureAvailableUsers = async (
beforeEventBuffer: eventType.beforeEventBuffer,
afterEventBuffer: eventType.afterEventBuffer,
bypassBusyCalendarTimes: false,
shouldServeCache,
mode,
withSource: true,
},
initialData: {
@@ -117,7 +117,7 @@ async function cancelAttendeeSeat(
integrationsToUpdate.push(updateMeeting(credential, updatedEvt, reference));
}
if (reference.type.includes("_calendar")) {
const calendar = await getCalendar(credential);
const calendar = await getCalendar(credential, "booking");
if (calendar) {
integrationsToUpdate.push(
calendar?.updateEvent(reference.uid, updatedEvt, reference.externalCalendarId)
@@ -43,7 +43,7 @@ const lastAttendeeDeleteBooking = async (
integrationsToDelete.push(deleteMeeting(credential, reference.uid));
}
if (reference.type.includes("_calendar") && originalBookingEvt) {
const calendar = await getCalendar(credential);
const calendar = await getCalendar(credential, "booking");
if (calendar) {
integrationsToDelete.push(
calendar?.deleteEvent(reference.uid, originalBookingEvt, reference.externalCalendarId)
@@ -795,7 +795,8 @@ async function handler(
const isTeamEventType =
!!eventType.schedulingType && ["COLLECTIVE", "ROUND_ROBIN"].includes(eventType.schedulingType);
const shouldServeCache = false;
// Use "booking" mode to bypass cache for booking confirmation
const calendarFetchMode = "booking" as const;
tracingLogger.info(
`Booking eventType ${eventTypeId} started`,
@@ -1002,7 +1003,7 @@ async function handler(
originalRescheduledBooking: originalRescheduledBooking ?? null,
},
tracingLogger,
shouldServeCache
calendarFetchMode
);
}
}
@@ -1017,7 +1018,7 @@ async function handler(
originalRescheduledBooking,
},
tracingLogger,
shouldServeCache
calendarFetchMode
);
}
}
@@ -1036,7 +1037,7 @@ async function handler(
originalRescheduledBooking,
},
tracingLogger,
shouldServeCache
calendarFetchMode
);
} else {
availableUsers = [...qualifiedRRUsers, ...fixedUsers] as IsFixedAwareUser[];
@@ -1065,7 +1066,7 @@ async function handler(
originalRescheduledBooking,
},
tracingLogger,
shouldServeCache
calendarFetchMode
);
} else {
availableUsers = [...additionalFallbackRRUsers, ...fixedUsers] as IsFixedAwareUser[];
@@ -1178,7 +1179,7 @@ async function handler(
originalRescheduledBooking,
},
tracingLogger,
shouldServeCache
calendarFetchMode
);
}
}
@@ -15,7 +15,7 @@ import type { Booking, EventType } from "@calcom/prisma/client";
import type { Prisma } from "@calcom/prisma/client";
import type { SelectedCalendar } from "@calcom/prisma/client";
import { BookingStatus } from "@calcom/prisma/enums";
import type { EventBusyDetails } from "@calcom/types/Calendar";
import type { CalendarFetchMode, EventBusyDetails } from "@calcom/types/Calendar";
import type { CredentialForCalendarService } from "@calcom/types/Credential";
export interface IBusyTimesService {
@@ -52,7 +52,7 @@ export class BusyTimesService {
| null;
bypassBusyCalendarTimes: boolean;
silentlyHandleCalendarFailures?: boolean;
shouldServeCache?: boolean;
mode?: CalendarFetchMode;
}) {
const {
credentials,
@@ -70,7 +70,7 @@ export class BusyTimesService {
duration,
bypassBusyCalendarTimes = false,
silentlyHandleCalendarFailures = false,
shouldServeCache,
mode,
} = params;
logger.silly(
@@ -194,7 +194,7 @@ export class BusyTimesService {
startTime,
endTime,
selectedCalendars,
shouldServeCache
mode
);
if (!calendarBusyTimesQuery.success) {
@@ -6,9 +6,9 @@ import type {
CalendarEvent,
CalendarServiceEvent,
EventBusyDate,
GetAvailabilityParams,
IntegrationCalendar,
NewCalendarEventType,
SelectedCalendarEventTypeIds,
} from "@calcom/types/Calendar";
const log = logger.getSubLogger({ prefix: ["CachedCalendarWrapper"] });
@@ -56,17 +56,9 @@ export class CalendarCacheWrapper implements Calendar {
* - Calendars **with** both `syncToken` and `syncSubscribedAt` → fetched from cache.
* - Calendars **without** one of them → fetched directly from the original calendar.
* - Results are merged into a single array.
*
* @param dateFrom - Start date (ISO string)
* @param dateTo - End date (ISO string)
* @param selectedCalendars - List of calendars to retrieve availability from
* @returns Combined array of busy date ranges
*/
async getAvailability(
dateFrom: string,
dateTo: string,
selectedCalendars: IntegrationCalendar[]
): Promise<EventBusyDate[]> {
async getAvailability(params: GetAvailabilityParams): Promise<EventBusyDate[]> {
const { dateFrom, dateTo, selectedCalendars } = params;
return withSpan(
{
name: "CalendarCacheWrapper.getAvailability",
@@ -100,7 +92,12 @@ export class CalendarCacheWrapper implements Calendar {
new Date(dateTo)
);
const cacheDurationMs = performance.now() - cacheStartTime;
results.push(...cached);
results.push(
...cached.map((event) => ({
...event,
timeZone: event.timeZone ?? undefined,
}))
);
span.setAttribute("cachedCalendarCount", withSync.length);
span.setAttribute("cacheFetchDurationMs", cacheDurationMs);
@@ -115,7 +112,13 @@ export class CalendarCacheWrapper implements Calendar {
// Fetch from original calendar for unsynced ones
if (withoutSync.length) {
const originalStartTime = performance.now();
const original = await this.deps.originalCalendar.getAvailability(dateFrom, dateTo, withoutSync);
const original = await this.deps.originalCalendar.getAvailability({
dateFrom,
dateTo,
selectedCalendars: withoutSync,
mode: params.mode,
fallbackToPrimary: params.fallbackToPrimary,
});
const originalDurationMs = performance.now() - originalStartTime;
results.push(...original);
@@ -143,17 +146,9 @@ export class CalendarCacheWrapper implements Calendar {
* - Calendars **with** both `syncToken` and `syncSubscribedAt` → fetched from cache.
* - Calendars **without** one of them → fetched directly from the original calendar.
* - Results are merged into a single array with `{ start, end, timeZone }` format.
*
* @param dateFrom - Start date (ISO string)
* @param dateTo - End date (ISO string)
* @param selectedCalendars - List of calendars to retrieve availability from
* @returns Combined array of time-zone-aware availability ranges
*/
async getAvailabilityWithTimeZones(
dateFrom: string,
dateTo: string,
selectedCalendars: IntegrationCalendar[]
): Promise<{ start: Date | string; end: Date | string; timeZone: string }[]> {
async getAvailabilityWithTimeZones(params: GetAvailabilityParams): Promise<EventBusyDate[]> {
const { dateFrom, dateTo, selectedCalendars } = params;
return withSpan(
{
name: "CalendarCacheWrapper.getAvailabilityWithTimeZones",
@@ -175,7 +170,7 @@ export class CalendarCacheWrapper implements Calendar {
const withSync = selectedCalendars.filter((c) => c.syncToken && c.syncSubscribedAt);
const withoutSync = selectedCalendars.filter((c) => !c.syncToken || !c.syncSubscribedAt);
const results: { start: Date | string; end: Date | string; timeZone: string }[] = [];
const results: EventBusyDate[] = [];
// Fetch from cache for synced calendars
if (withSync.length) {
@@ -208,11 +203,13 @@ export class CalendarCacheWrapper implements Calendar {
// Fetch from original calendar for unsynced ones
if (withoutSync.length) {
const originalStartTime = performance.now();
const original = await this.deps.originalCalendar.getAvailabilityWithTimeZones?.(
const original = await this.deps.originalCalendar.getAvailabilityWithTimeZones?.({
dateFrom,
dateTo,
withoutSync
);
selectedCalendars: withoutSync,
mode: params.mode,
fallbackToPrimary: params.fallbackToPrimary,
});
const originalDurationMs = performance.now() - originalStartTime;
if (original?.length) results.push(...original);
@@ -245,18 +242,4 @@ export class CalendarCacheWrapper implements Calendar {
testDelegationCredentialSetup?(): Promise<boolean> {
return this.deps.originalCalendar.testDelegationCredentialSetup?.() || Promise.resolve(false);
}
watchCalendar?(options: {
calendarId: string;
eventTypeIds: SelectedCalendarEventTypeIds;
}): Promise<unknown> {
return this.deps.originalCalendar.watchCalendar?.(options) || Promise.resolve();
}
unwatchCalendar?(options: {
calendarId: string;
eventTypeIds: SelectedCalendarEventTypeIds;
}): Promise<void> {
return this.deps.originalCalendar.unwatchCalendar?.(options) || Promise.resolve();
}
}
@@ -41,7 +41,12 @@ describe("CalendarCacheWrapper", () => {
describe("getAvailability", () => {
it("should return empty array when no calendars provided", async () => {
const result = await wrapper.getAvailability("2025-01-01", "2025-01-02", []);
const result = await wrapper.getAvailability({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars: [],
mode: "slots",
});
expect(result).toEqual([]);
expect(mockRepository.findAllBySelectedCalendarIdsBetween).not.toHaveBeenCalled();
@@ -73,7 +78,12 @@ describe("CalendarCacheWrapper", () => {
vi.mocked(mockRepository.findAllBySelectedCalendarIdsBetween).mockResolvedValue(cachedEvents);
const result = await wrapper.getAvailability("2025-01-01", "2025-01-02", selectedCalendars);
const result = await wrapper.getAvailability({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars,
mode: "slots",
});
expect(result).toEqual(cachedEvents);
expect(mockRepository.findAllBySelectedCalendarIdsBetween).toHaveBeenCalledWith(
@@ -108,15 +118,22 @@ describe("CalendarCacheWrapper", () => {
vi.mocked(mockOriginalCalendar.getAvailability).mockResolvedValue(originalEvents);
const result = await wrapper.getAvailability("2025-01-01", "2025-01-02", selectedCalendars);
const result = await wrapper.getAvailability({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars,
mode: "slots",
});
expect(result).toEqual(originalEvents);
expect(mockRepository.findAllBySelectedCalendarIdsBetween).not.toHaveBeenCalled();
expect(mockOriginalCalendar.getAvailability).toHaveBeenCalledWith(
"2025-01-01",
"2025-01-02",
selectedCalendars
);
expect(mockOriginalCalendar.getAvailability).toHaveBeenCalledWith({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars,
mode: "slots",
fallbackToPrimary: undefined,
});
});
it("should fetch from both cache and original calendar when calendars are mixed", async () => {
@@ -148,7 +165,12 @@ describe("CalendarCacheWrapper", () => {
vi.mocked(mockRepository.findAllBySelectedCalendarIdsBetween).mockResolvedValue(cachedEvents);
vi.mocked(mockOriginalCalendar.getAvailability).mockResolvedValue(originalEvents);
const result = await wrapper.getAvailability("2025-01-01", "2025-01-02", selectedCalendars);
const result = await wrapper.getAvailability({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars,
mode: "slots",
});
expect(result).toEqual([...cachedEvents, ...originalEvents]);
expect(mockRepository.findAllBySelectedCalendarIdsBetween).toHaveBeenCalledWith(
@@ -156,9 +178,13 @@ describe("CalendarCacheWrapper", () => {
new Date("2025-01-01"),
new Date("2025-01-02")
);
expect(mockOriginalCalendar.getAvailability).toHaveBeenCalledWith("2025-01-01", "2025-01-02", [
selectedCalendars[1],
]);
expect(mockOriginalCalendar.getAvailability).toHaveBeenCalledWith({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars: [selectedCalendars[1]],
mode: "slots",
fallbackToPrimary: undefined,
});
});
it("should filter out calendars without id when fetching from cache", async () => {
@@ -179,7 +205,12 @@ describe("CalendarCacheWrapper", () => {
},
];
await wrapper.getAvailability("2025-01-01", "2025-01-02", selectedCalendars);
await wrapper.getAvailability({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars,
mode: "slots",
});
expect(mockRepository.findAllBySelectedCalendarIdsBetween).toHaveBeenCalledWith(
["cal-1"], // Only cal-1 has a valid id
@@ -191,7 +222,12 @@ describe("CalendarCacheWrapper", () => {
describe("getAvailabilityWithTimeZones", () => {
it("should return empty array when no calendars provided", async () => {
const result = await wrapper.getAvailabilityWithTimeZones("2025-01-01", "2025-01-02", []);
const result = await wrapper.getAvailabilityWithTimeZones({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars: [],
mode: "slots",
});
expect(result).toEqual([]);
expect(mockRepository.findAllBySelectedCalendarIdsBetween).not.toHaveBeenCalled();
@@ -224,7 +260,12 @@ describe("CalendarCacheWrapper", () => {
vi.mocked(mockRepository.findAllBySelectedCalendarIdsBetween).mockResolvedValue(cachedEvents);
const result = await wrapper.getAvailabilityWithTimeZones("2025-01-01", "2025-01-02", selectedCalendars);
const result = await wrapper.getAvailabilityWithTimeZones({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars,
mode: "slots",
});
expect(result).toEqual([
{
@@ -262,15 +303,22 @@ describe("CalendarCacheWrapper", () => {
vi.mocked(mockOriginalCalendar.getAvailabilityWithTimeZones).mockResolvedValue(originalEvents);
const result = await wrapper.getAvailabilityWithTimeZones("2025-01-01", "2025-01-02", selectedCalendars);
const result = await wrapper.getAvailabilityWithTimeZones({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars,
mode: "slots",
});
expect(result).toEqual(originalEvents);
expect(mockRepository.findAllBySelectedCalendarIdsBetween).not.toHaveBeenCalled();
expect(mockOriginalCalendar.getAvailabilityWithTimeZones).toHaveBeenCalledWith(
"2025-01-01",
"2025-01-02",
selectedCalendars
);
expect(mockOriginalCalendar.getAvailabilityWithTimeZones).toHaveBeenCalledWith({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars,
mode: "slots",
fallbackToPrimary: undefined,
});
});
it("should fetch from both cache and original calendar when calendars are mixed", async () => {
@@ -310,7 +358,12 @@ describe("CalendarCacheWrapper", () => {
vi.mocked(mockRepository.findAllBySelectedCalendarIdsBetween).mockResolvedValue(cachedEvents);
vi.mocked(mockOriginalCalendar.getAvailabilityWithTimeZones).mockResolvedValue(originalEvents);
const result = await wrapper.getAvailabilityWithTimeZones("2025-01-01", "2025-01-02", selectedCalendars);
const result = await wrapper.getAvailabilityWithTimeZones({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars,
mode: "slots",
});
expect(result).toEqual([
{
@@ -351,7 +404,12 @@ describe("CalendarCacheWrapper", () => {
vi.mocked(mockRepository.findAllBySelectedCalendarIdsBetween).mockResolvedValue(cachedEvents);
mockOriginalCalendar.getAvailabilityWithTimeZones = undefined;
const result = await wrapper.getAvailabilityWithTimeZones("2025-01-01", "2025-01-02", selectedCalendars);
const result = await wrapper.getAvailabilityWithTimeZones({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars,
mode: "slots",
});
expect(result).toEqual([
{
@@ -375,7 +433,12 @@ describe("CalendarCacheWrapper", () => {
vi.mocked(mockOriginalCalendar.getAvailabilityWithTimeZones).mockResolvedValue([]);
const result = await wrapper.getAvailabilityWithTimeZones("2025-01-01", "2025-01-02", selectedCalendars);
const result = await wrapper.getAvailabilityWithTimeZones({
dateFrom: "2025-01-01",
dateTo: "2025-01-02",
selectedCalendars,
mode: "slots",
});
expect(result).toEqual([]);
});
@@ -5,9 +5,9 @@ import type {
CalendarEvent,
CalendarServiceEvent,
EventBusyDate,
GetAvailabilityParams,
IntegrationCalendar,
NewCalendarEventType,
SelectedCalendarEventTypeIds,
} from "@calcom/types/Calendar";
const log = logger.getSubLogger({ prefix: ["CalendarTelemetryWrapper"] });
@@ -53,19 +53,8 @@ export class CalendarTelemetryWrapper implements Calendar {
return this.deps.originalCalendar.deleteEvent(uid, event, externalCalendarId);
}
/**
* Retrieves availability with telemetry tracking when cache is disabled.
*
* @param dateFrom - Start date (ISO string)
* @param dateTo - End date (ISO string)
* @param selectedCalendars - List of calendars to retrieve availability from
* @returns Array of busy date ranges
*/
async getAvailability(
dateFrom: string,
dateTo: string,
selectedCalendars: IntegrationCalendar[]
): Promise<EventBusyDate[]> {
async getAvailability(params: GetAvailabilityParams): Promise<EventBusyDate[]> {
const { dateFrom, dateTo, selectedCalendars } = params;
return withSpan(
{
name: "CalendarTelemetryWrapper.getAvailability",
@@ -90,7 +79,13 @@ export class CalendarTelemetryWrapper implements Calendar {
if (!selectedCalendars?.length) return [];
const startTime = performance.now();
const results = await this.deps.originalCalendar.getAvailability(dateFrom, dateTo, selectedCalendars);
const results = await this.deps.originalCalendar.getAvailability({
dateFrom,
dateTo,
selectedCalendars,
mode: params.mode,
fallbackToPrimary: params.fallbackToPrimary,
});
const totalFetchDurationMs = performance.now() - startTime;
span.setAttribute("totalFetchDurationMs", totalFetchDurationMs);
@@ -109,19 +104,8 @@ export class CalendarTelemetryWrapper implements Calendar {
);
}
/**
* Retrieves availability with time zones and telemetry tracking when cache is disabled.
*
* @param dateFrom - Start date (ISO string)
* @param dateTo - End date (ISO string)
* @param selectedCalendars - List of calendars to retrieve availability from
* @returns Array of time-zone-aware availability ranges
*/
async getAvailabilityWithTimeZones(
dateFrom: string,
dateTo: string,
selectedCalendars: IntegrationCalendar[]
): Promise<{ start: Date | string; end: Date | string; timeZone: string }[]> {
async getAvailabilityWithTimeZones(params: GetAvailabilityParams): Promise<EventBusyDate[]> {
const { dateFrom, dateTo, selectedCalendars } = params;
// Check if the original calendar supports this method
if (!this.deps.originalCalendar.getAvailabilityWithTimeZones) {
return [];
@@ -151,11 +135,13 @@ export class CalendarTelemetryWrapper implements Calendar {
if (!selectedCalendars?.length) return [];
const startTime = performance.now();
const results = await this.deps.originalCalendar.getAvailabilityWithTimeZones?.(
const results = await this.deps.originalCalendar.getAvailabilityWithTimeZones?.({
dateFrom,
dateTo,
selectedCalendars
);
selectedCalendars,
mode: params.mode,
fallbackToPrimary: params.fallbackToPrimary,
});
const totalFetchDurationMs = performance.now() - startTime;
span.setAttribute("totalFetchDurationMs", totalFetchDurationMs);
@@ -185,18 +171,4 @@ export class CalendarTelemetryWrapper implements Calendar {
testDelegationCredentialSetup?(): Promise<boolean> {
return this.deps.originalCalendar.testDelegationCredentialSetup?.() || Promise.resolve(false);
}
watchCalendar?(options: {
calendarId: string;
eventTypeIds: SelectedCalendarEventTypeIds;
}): Promise<unknown> {
return this.deps.originalCalendar.watchCalendar?.(options) || Promise.resolve();
}
unwatchCalendar?(options: {
calendarId: string;
eventTypeIds: SelectedCalendarEventTypeIds;
}): Promise<void> {
return this.deps.originalCalendar.unwatchCalendar?.(options) || Promise.resolve();
}
}
@@ -18,6 +18,7 @@ import { getPiiFreeCalendarEvent, getPiiFreeCredential } from "@calcom/lib/piiFr
import { safeStringify } from "@calcom/lib/safeStringify";
import type {
CalendarEvent,
CalendarFetchMode,
CalendarServiceEvent,
EventBusyDate,
IntegrationCalendar,
@@ -34,7 +35,7 @@ export const getCalendarCredentials = (credentials: Array<CredentialForCalendarS
.filter((app) => app.type.endsWith("_calendar"))
.flatMap((app) => {
const credentials = app.credentials.flatMap((credential) => {
const calendar = () => getCalendar(credential);
const calendar = () => getCalendar(credential, "slots");
return app.variant === "calendar" ? [{ integration: app, credential, calendar }] : [];
});
@@ -236,7 +237,7 @@ export const getBusyCalendarTimes = async (
dateFrom: string,
dateTo: string,
selectedCalendars: SelectedCalendar[],
shouldServeCache?: boolean,
mode?: CalendarFetchMode,
includeTimeZone?: boolean
) => {
let results: (EventBusyDate & { timeZone?: string })[][] = [];
@@ -274,7 +275,7 @@ export const getBusyCalendarTimes = async (
startDate,
endDate,
selectedCalendars,
shouldServeCache
mode ?? "slots"
);
}
} catch (e) {
@@ -295,7 +296,7 @@ export const createEvent = async (
// Some calendar libraries may edit the original event so let's clone it
const formattedEvent = formatCalEvent(originalEvent);
const uid: string = getUid(formattedEvent);
const calendar = await getCalendar(credential);
const calendar = await getCalendar(credential, "booking");
let success = true;
let calError: string | undefined = undefined;
@@ -389,7 +390,7 @@ export const updateEvent = async (
const formattedEvent = formatCalEvent(rawCalEvent);
const calEvent = processEvent(formattedEvent);
const uid = getUid(calEvent);
const calendar = await getCalendar(credential);
const calendar = await getCalendar(credential, "booking");
let success = false;
let calError: string | undefined = undefined;
let calWarnings: string[] | undefined = [];
@@ -472,7 +473,7 @@ export const deleteEvent = async ({
event: CalendarEvent;
externalCalendarId?: string | null;
}): Promise<unknown> => {
const calendar = await getCalendar(credential);
const calendar = await getCalendar(credential, "booking");
log.debug(
"Deleting calendar event",
safeStringify({
@@ -190,7 +190,8 @@ describe("getCalendarsEvents", () => {
],
"2010-12-01",
"2010-12-02",
[]
[],
"slots"
);
expect(result).toEqual([]);
@@ -206,7 +207,8 @@ describe("getCalendarsEvents", () => {
],
"2010-12-01",
"2010-12-02",
[]
[],
"slots"
);
expect(result).toEqual([[]]);
@@ -229,7 +231,8 @@ describe("getCalendarsEvents", () => {
],
"2010-12-01",
"2010-12-02",
[selectedCalendar]
[selectedCalendar],
"slots"
);
expect(result).toEqual([[]]);
@@ -267,15 +270,17 @@ describe("getCalendarsEvents", () => {
],
"2010-12-01",
"2010-12-04",
[selectedCalendar]
[selectedCalendar],
"slots"
);
expect(getAvailabilitySpy).toHaveBeenCalledWith(
"2010-12-01",
"2010-12-04",
[selectedCalendar],
false
);
expect(getAvailabilitySpy).toHaveBeenCalledWith({
dateFrom: "2010-12-01",
dateTo: "2010-12-04",
selectedCalendars: [selectedCalendar],
mode: "slots",
fallbackToPrimary: false,
});
expect(result).toEqual([
availability.map((av) => ({
...av,
@@ -337,21 +342,24 @@ describe("getCalendarsEvents", () => {
],
"2010-12-01",
"2010-12-04",
[selectedGoogleCalendar, selectedOfficeCalendar]
[selectedGoogleCalendar, selectedOfficeCalendar],
"slots"
);
expect(getGoogleAvailabilitySpy).toHaveBeenCalledWith(
"2010-12-01",
"2010-12-04",
[selectedGoogleCalendar],
false
);
expect(getOfficeAvailabilitySpy).toHaveBeenCalledWith(
"2010-12-01",
"2010-12-04",
[selectedOfficeCalendar],
false
);
expect(getGoogleAvailabilitySpy).toHaveBeenCalledWith({
dateFrom: "2010-12-01",
dateTo: "2010-12-04",
selectedCalendars: [selectedGoogleCalendar],
mode: "slots",
fallbackToPrimary: false,
});
expect(getOfficeAvailabilitySpy).toHaveBeenCalledWith({
dateFrom: "2010-12-01",
dateTo: "2010-12-04",
selectedCalendars: [selectedOfficeCalendar],
mode: "slots",
fallbackToPrimary: false,
});
expect(result).toEqual([
googleAvailability.map((av) => ({
...av,
@@ -373,7 +381,8 @@ describe("getCalendarsEvents", () => {
[buildRegularCredential(credential)],
"2010-12-01",
"2010-12-02",
[]
[],
"slots"
);
expect(getAvailabilitySpy).not.toHaveBeenCalled();
@@ -391,9 +400,15 @@ describe("getCalendarsEvents", () => {
.spyOn(GoogleCalendarService.prototype, "getAvailability")
.mockReturnValue(Promise.resolve([]));
const result = await getCalendarsEvents(credentials, startDate, endDate, []);
const result = await getCalendarsEvents(credentials, startDate, endDate, [], "slots");
expect(getAvailabilitySpy).toHaveBeenCalledWith(startDate, endDate, [], true);
expect(getAvailabilitySpy).toHaveBeenCalledWith({
dateFrom: startDate,
dateTo: endDate,
selectedCalendars: [],
mode: "slots",
fallbackToPrimary: true,
});
expect(result).toEqual([[]]);
});
});
@@ -523,12 +538,13 @@ describe("getCalendarsEventsWithTimezones", () => {
[selectedCalendar]
);
expect(getAvailabilityWithTimezonesSpy).toHaveBeenCalledWith(
"2010-12-01",
"2010-12-04",
[selectedCalendar],
false
);
expect(getAvailabilityWithTimezonesSpy).toHaveBeenCalledWith({
dateFrom: "2010-12-01",
dateTo: "2010-12-04",
selectedCalendars: [selectedCalendar],
mode: "slots",
fallbackToPrimary: false,
});
expect(result).toEqual([
availability.map((av) => ({
...av,
@@ -565,7 +581,13 @@ describe("getCalendarsEventsWithTimezones", () => {
const result = await getCalendarsEventsWithTimezones(credentials, startDate, endDate, []);
expect(getAvailabilityWithTimezonesSpy).toHaveBeenCalledWith(startDate, endDate, [], true);
expect(getAvailabilityWithTimezonesSpy).toHaveBeenCalledWith({
dateFrom: startDate,
dateTo: endDate,
selectedCalendars: [],
mode: "slots",
fallbackToPrimary: true,
});
expect(result).toEqual([[]]);
});
});
@@ -5,7 +5,7 @@ import logger from "@calcom/lib/logger";
import { getPiiFreeSelectedCalendar, getPiiFreeCredential } from "@calcom/lib/piiFreeData";
import { safeStringify } from "@calcom/lib/safeStringify";
import { performance } from "@calcom/lib/server/perfObserver";
import type { EventBusyDate, SelectedCalendar } from "@calcom/types/Calendar";
import type { CalendarFetchMode, EventBusyDate, SelectedCalendar } from "@calcom/types/Calendar";
import type { CredentialForCalendarService } from "@calcom/types/Credential";
const log = logger.getSubLogger({ prefix: ["getCalendarsEvents"] });
@@ -25,7 +25,7 @@ export const getCalendarsEventsWithTimezones = async (
const calendarAndCredentialPairs = await Promise.all(
calendarCredentials.map(async (credential) => {
const calendar = await getCalendar(credential);
const calendar = await getCalendar(credential, "slots");
return [calendar, credential] as const;
})
);
@@ -67,14 +67,18 @@ export const getCalendarsEventsWithTimezones = async (
}
/** We extract external Ids so we don't cache too much */
const eventBusyDates =
(await c.getAvailabilityWithTimeZones?.(
(await c.getAvailabilityWithTimeZones?.({
dateFrom,
dateTo,
passedSelectedCalendars,
allowFallbackToPrimary
)) || [];
selectedCalendars: passedSelectedCalendars,
mode: "slots",
fallbackToPrimary: allowFallbackToPrimary,
})) || [];
return eventBusyDates;
return eventBusyDates.map((event) => ({
...event,
timeZone: event.timeZone || "UTC",
}));
});
const awaitedResults = await Promise.all(results);
return awaitedResults;
@@ -85,7 +89,7 @@ const getCalendarsEvents = async (
dateFrom: string,
dateTo: string,
selectedCalendars: SelectedCalendar[],
shouldServeCache?: boolean
mode: CalendarFetchMode
): Promise<EventBusyDate[][]> => {
const calendarCredentials = withCredentials
.filter((credential) => credential.type.endsWith("_calendar"))
@@ -94,7 +98,7 @@ const getCalendarsEvents = async (
const calendarAndCredentialPairs = await Promise.all(
calendarCredentials.map(async (credential) => {
const calendar = await getCalendar(credential, shouldServeCache);
const calendar = await getCalendar(credential, mode);
return [calendar, credential] as const;
})
);
@@ -147,12 +151,13 @@ const getCalendarsEvents = async (
selectedCalendars: passedSelectedCalendars.map(getPiiFreeSelectedCalendar),
})
);
const eventBusyDates = await calendarService.getAvailability(
const eventBusyDates = await calendarService.getAvailability({
dateFrom,
dateTo,
passedSelectedCalendars,
allowFallbackToPrimary
);
selectedCalendars: passedSelectedCalendars,
mode,
fallbackToPrimary: allowFallbackToPrimary,
});
performance.mark("eventBusyDatesEnd");
performance.measure(
`[getAvailability for ${selectedCalendarIds.join(", ")}][$1]'`,
@@ -455,7 +455,7 @@ const handleDeleteCredential = async ({
// If it's a calendar remove it from the SelectedCalendars
if (credential.app?.categories.includes(AppCategories.calendar)) {
try {
const calendar = await getCalendar(buildNonDelegationCredential(credential));
const calendar = await getCalendar(buildNonDelegationCredential(credential), "slots");
const calendars = await calendar?.listCalendars();