Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions packages/app-store/_utils/getCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
[
Expand 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,
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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);
});
});

Expand All @@ -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);
});
});
});
3 changes: 2 additions & 1 deletion packages/features/calendar-cache/lib/getShouldServeCache.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
6 changes: 3 additions & 3 deletions packages/features/calendars/lib/getCalendarsEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
);
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -215,7 +215,7 @@ function getServerUrlFromCredential(credential: CredentialForCalendarService): s

const url = new URL(decryptedData.url);
return `${url.protocol}//${url.host}`;
} catch (error) {
} catch {
return null;
}
}
Expand Down
Loading