Scheduling infrastructure for absolutely everyone.
chore: Determine to serve cache high level (#24756)
Alex van Andel91063c41f828
diff --git a/packages/app-store/_utils/getCalendar.ts b/packages/app-store/_utils/getCalendar.tsindex 8c9d1afb031..cde662a559f 100644--- a/packages/app-store/_utils/getCalendar.ts+++ b/packages/app-store/_utils/getCalendar.ts@@ -13,7 +13,8 @@ import { CalendarServiceMap } from "../calendar.services.generated";const log = logger.getSubLogger({ prefix: ["CalendarManager"] });export const getCalendar = async (- credential: CredentialForCalendarService | null+ credential: CredentialForCalendarService | null,+ shouldServeCache?: boolean,): Promise<Calendar | null> => {if (!credential || !credential.key) return null;let { type: calendarType } = credential;@@ -41,12 +42,8 @@ export const getCalendar = async (log.warn(`calendar of type ${calendarType} is not implemented`);return null;}-- // check if Calendar Cache is supported and enabled- if (CalendarCacheEventService.isCalendarTypeSupported(calendarType)) {- log.debug(- `Using regular CalendarService for credential ${credential.id} (not Google or Office365 Calendar)`- );+ // if shouldServeCache is not supplied, determine on the fly.+ if (typeof shouldServeCache === "undefined") {const featuresRepository = new FeaturesRepository(prisma);const [isCalendarSubscriptionCacheEnabled, isCalendarSubscriptionCacheEnabledForUser] = await Promise.all([@@ -59,19 +56,19 @@ export const getCalendar = async (),]);-- if (isCalendarSubscriptionCacheEnabled && isCalendarSubscriptionCacheEnabledForUser) {- log.debug(`Calendar Cache is enabled, using CalendarCacheService for credential ${credential.id}`);- // eslint-disable-next-line @typescript-eslint/no-explicit-any- const originalCalendar = new CalendarService(credential as any);- if (originalCalendar) {- // return cacheable calendar- const calendarCacheEventRepository = new CalendarCacheEventRepository(prisma);- return new CalendarCacheWrapper({- originalCalendar,- calendarCacheEventRepository,- });- }+ shouldServeCache = isCalendarSubscriptionCacheEnabled && isCalendarSubscriptionCacheEnabledForUser;+ }+ if (CalendarCacheEventService.isCalendarTypeSupported(calendarType) && shouldServeCache) {+ log.debug(`Calendar Cache is enabled, using CalendarCacheService for credential ${credential.id}`);+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ const originalCalendar = new CalendarService(credential as any);+ if (originalCalendar) {+ // return cacheable calendar+ const calendarCacheEventRepository = new CalendarCacheEventRepository(prisma);+ return new CalendarCacheWrapper({+ originalCalendar,+ calendarCacheEventRepository,+ });}}diff --git a/packages/features/calendar-cache/lib/getShouldServeCache.test.ts b/packages/features/calendar-cache/lib/getShouldServeCache.test.tsindex 4d2d844f171..3779f9c469c 100644--- a/packages/features/calendar-cache/lib/getShouldServeCache.test.ts+++ b/packages/features/calendar-cache/lib/getShouldServeCache.test.ts@@ -3,6 +3,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";import type { IFeaturesRepository } from "@calcom/features/flags/features.repository.interface";import { CacheService } from "./getShouldServeCache";+import { CalendarSubscriptionService } from "@calcom/features/calendar-subscription/lib/CalendarSubscriptionService";describe("CacheService.getShouldServeCache", () => {const mockFeaturesRepository: IFeaturesRepository = {@@ -57,7 +58,7 @@ describe("CacheService.getShouldServeCache", () => {const result = await cacheService.getShouldServeCache(undefined, 123);expect(result).toBe(true);- expect(mockFeaturesRepository.checkIfTeamHasFeature).toHaveBeenCalledWith(123, "calendar-cache-serve");+ expect(mockFeaturesRepository.checkIfTeamHasFeature).toHaveBeenCalledWith(123, CalendarSubscriptionService.CALENDAR_SUBSCRIPTION_CACHE_FEATURE);});it("should check feature repository when teamId is provided and return false if feature is disabled", async () => {@@ -66,7 +67,7 @@ describe("CacheService.getShouldServeCache", () => {const result = await cacheService.getShouldServeCache(undefined, 456);expect(result).toBe(false);- expect(mockFeaturesRepository.checkIfTeamHasFeature).toHaveBeenCalledWith(456, "calendar-cache-serve");+ expect(mockFeaturesRepository.checkIfTeamHasFeature).toHaveBeenCalledWith(456, CalendarSubscriptionService.CALENDAR_SUBSCRIPTION_CACHE_FEATURE);});});@@ -86,7 +87,7 @@ describe("CacheService.getShouldServeCache", () => {const result = await cacheService.getShouldServeCache(undefined, 999);expect(result).toBe(true);- expect(mockFeaturesRepository.checkIfTeamHasFeature).toHaveBeenCalledWith(999, "calendar-cache-serve");+ expect(mockFeaturesRepository.checkIfTeamHasFeature).toHaveBeenCalledWith(999, CalendarSubscriptionService.CALENDAR_SUBSCRIPTION_CACHE_FEATURE);});});});diff --git a/packages/features/calendar-cache/lib/getShouldServeCache.ts b/packages/features/calendar-cache/lib/getShouldServeCache.tsindex 73d4e86687b..c98400863cd 100644--- a/packages/features/calendar-cache/lib/getShouldServeCache.ts+++ b/packages/features/calendar-cache/lib/getShouldServeCache.ts@@ -1,4 +1,5 @@import type { IFeaturesRepository } from "@calcom/features/flags/features.repository.interface";+import { CalendarSubscriptionService } from "@calcom/features/calendar-subscription/lib/CalendarSubscriptionService";export interface ICacheService {featuresRepository: IFeaturesRepository;@@ -10,6 +11,6 @@ export class CacheService {async getShouldServeCache(shouldServeCache?: boolean | undefined, teamId?: number) {if (typeof shouldServeCache === "boolean") return shouldServeCache;if (!teamId) return false;- return await this.dependencies.featuresRepository.checkIfTeamHasFeature(teamId, "calendar-cache-serve");+ return await this.dependencies.featuresRepository.checkIfTeamHasFeature(teamId, CalendarSubscriptionService.CALENDAR_SUBSCRIPTION_CACHE_FEATURE);}}diff --git a/packages/features/calendars/lib/getCalendarsEvents.ts b/packages/features/calendars/lib/getCalendarsEvents.tsindex 0acd3969253..64d950f1597 100644--- a/packages/features/calendars/lib/getCalendarsEvents.ts+++ b/packages/features/calendars/lib/getCalendarsEvents.ts@@ -94,7 +94,7 @@ const getCalendarsEvents = async (const calendarAndCredentialPairs = await Promise.all(calendarCredentials.map(async (credential) => {- const calendar = await getCalendar(credential);+ const calendar = await getCalendar(credential, shouldServeCache);return [calendar, credential] as const;}));@@ -193,7 +193,7 @@ function getServerUrlFromCalendarExternalId(externalId: string): string | null {try {const url = new URL(externalId);return `${url.protocol}//${url.host}`;- } catch (error) {+ } catch {return null;}}@@ -215,7 +215,7 @@ function getServerUrlFromCredential(credential: CredentialForCalendarService): sconst url = new URL(decryptedData.url);return `${url.protocol}//${url.host}`;- } catch (error) {+ } catch {return null;}}