Skip to content

Commit 91063c4

Browse files
authored
chore: Determine to serve cache high level (calcom#24756)
* chore: Determine to serve cache high level * Fix invalid import * fix: Cache test
1 parent 1e1298f commit 91063c4

4 files changed

Lines changed: 26 additions & 27 deletions

File tree

packages/app-store/_utils/getCalendar.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import { CalendarServiceMap } from "../calendar.services.generated";
1313
const log = logger.getSubLogger({ prefix: ["CalendarManager"] });
1414

1515
export const getCalendar = async (
16-
credential: CredentialForCalendarService | null
16+
credential: CredentialForCalendarService | null,
17+
shouldServeCache?: boolean,
1718
): Promise<Calendar | null> => {
1819
if (!credential || !credential.key) return null;
1920
let { type: calendarType } = credential;
@@ -41,12 +42,8 @@ export const getCalendar = async (
4142
log.warn(`calendar of type ${calendarType} is not implemented`);
4243
return null;
4344
}
44-
45-
// check if Calendar Cache is supported and enabled
46-
if (CalendarCacheEventService.isCalendarTypeSupported(calendarType)) {
47-
log.debug(
48-
`Using regular CalendarService for credential ${credential.id} (not Google or Office365 Calendar)`
49-
);
45+
// if shouldServeCache is not supplied, determine on the fly.
46+
if (typeof shouldServeCache === "undefined") {
5047
const featuresRepository = new FeaturesRepository(prisma);
5148
const [isCalendarSubscriptionCacheEnabled, isCalendarSubscriptionCacheEnabledForUser] = await Promise.all(
5249
[
@@ -59,19 +56,19 @@ export const getCalendar = async (
5956
),
6057
]
6158
);
62-
63-
if (isCalendarSubscriptionCacheEnabled && isCalendarSubscriptionCacheEnabledForUser) {
64-
log.debug(`Calendar Cache is enabled, using CalendarCacheService for credential ${credential.id}`);
65-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
66-
const originalCalendar = new CalendarService(credential as any);
67-
if (originalCalendar) {
68-
// return cacheable calendar
69-
const calendarCacheEventRepository = new CalendarCacheEventRepository(prisma);
70-
return new CalendarCacheWrapper({
71-
originalCalendar,
72-
calendarCacheEventRepository,
73-
});
74-
}
59+
shouldServeCache = isCalendarSubscriptionCacheEnabled && isCalendarSubscriptionCacheEnabledForUser;
60+
}
61+
if (CalendarCacheEventService.isCalendarTypeSupported(calendarType) && shouldServeCache) {
62+
log.debug(`Calendar Cache is enabled, using CalendarCacheService for credential ${credential.id}`);
63+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
64+
const originalCalendar = new CalendarService(credential as any);
65+
if (originalCalendar) {
66+
// return cacheable calendar
67+
const calendarCacheEventRepository = new CalendarCacheEventRepository(prisma);
68+
return new CalendarCacheWrapper({
69+
originalCalendar,
70+
calendarCacheEventRepository,
71+
});
7572
}
7673
}
7774

packages/features/calendar-cache/lib/getShouldServeCache.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
33
import type { IFeaturesRepository } from "@calcom/features/flags/features.repository.interface";
44

55
import { CacheService } from "./getShouldServeCache";
6+
import { CalendarSubscriptionService } from "@calcom/features/calendar-subscription/lib/CalendarSubscriptionService";
67

78
describe("CacheService.getShouldServeCache", () => {
89
const mockFeaturesRepository: IFeaturesRepository = {
@@ -57,7 +58,7 @@ describe("CacheService.getShouldServeCache", () => {
5758
const result = await cacheService.getShouldServeCache(undefined, 123);
5859

5960
expect(result).toBe(true);
60-
expect(mockFeaturesRepository.checkIfTeamHasFeature).toHaveBeenCalledWith(123, "calendar-cache-serve");
61+
expect(mockFeaturesRepository.checkIfTeamHasFeature).toHaveBeenCalledWith(123, CalendarSubscriptionService.CALENDAR_SUBSCRIPTION_CACHE_FEATURE);
6162
});
6263

6364
it("should check feature repository when teamId is provided and return false if feature is disabled", async () => {
@@ -66,7 +67,7 @@ describe("CacheService.getShouldServeCache", () => {
6667
const result = await cacheService.getShouldServeCache(undefined, 456);
6768

6869
expect(result).toBe(false);
69-
expect(mockFeaturesRepository.checkIfTeamHasFeature).toHaveBeenCalledWith(456, "calendar-cache-serve");
70+
expect(mockFeaturesRepository.checkIfTeamHasFeature).toHaveBeenCalledWith(456, CalendarSubscriptionService.CALENDAR_SUBSCRIPTION_CACHE_FEATURE);
7071
});
7172
});
7273

@@ -86,7 +87,7 @@ describe("CacheService.getShouldServeCache", () => {
8687
const result = await cacheService.getShouldServeCache(undefined, 999);
8788

8889
expect(result).toBe(true);
89-
expect(mockFeaturesRepository.checkIfTeamHasFeature).toHaveBeenCalledWith(999, "calendar-cache-serve");
90+
expect(mockFeaturesRepository.checkIfTeamHasFeature).toHaveBeenCalledWith(999, CalendarSubscriptionService.CALENDAR_SUBSCRIPTION_CACHE_FEATURE);
9091
});
9192
});
9293
});

packages/features/calendar-cache/lib/getShouldServeCache.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { IFeaturesRepository } from "@calcom/features/flags/features.repository.interface";
2+
import { CalendarSubscriptionService } from "@calcom/features/calendar-subscription/lib/CalendarSubscriptionService";
23

34
export interface ICacheService {
45
featuresRepository: IFeaturesRepository;
@@ -10,6 +11,6 @@ export class CacheService {
1011
async getShouldServeCache(shouldServeCache?: boolean | undefined, teamId?: number) {
1112
if (typeof shouldServeCache === "boolean") return shouldServeCache;
1213
if (!teamId) return false;
13-
return await this.dependencies.featuresRepository.checkIfTeamHasFeature(teamId, "calendar-cache-serve");
14+
return await this.dependencies.featuresRepository.checkIfTeamHasFeature(teamId, CalendarSubscriptionService.CALENDAR_SUBSCRIPTION_CACHE_FEATURE);
1415
}
1516
}

packages/features/calendars/lib/getCalendarsEvents.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const getCalendarsEvents = async (
9494

9595
const calendarAndCredentialPairs = await Promise.all(
9696
calendarCredentials.map(async (credential) => {
97-
const calendar = await getCalendar(credential);
97+
const calendar = await getCalendar(credential, shouldServeCache);
9898
return [calendar, credential] as const;
9999
})
100100
);
@@ -193,7 +193,7 @@ function getServerUrlFromCalendarExternalId(externalId: string): string | null {
193193
try {
194194
const url = new URL(externalId);
195195
return `${url.protocol}//${url.host}`;
196-
} catch (error) {
196+
} catch {
197197
return null;
198198
}
199199
}
@@ -215,7 +215,7 @@ function getServerUrlFromCredential(credential: CredentialForCalendarService): s
215215

216216
const url = new URL(decryptedData.url);
217217
return `${url.protocol}//${url.host}`;
218-
} catch (error) {
218+
} catch {
219219
return null;
220220
}
221221
}

0 commit comments

Comments
 (0)