diff --git a/apps/web/modules/onboarding/personal/calendar/personal-calendar-view.tsx b/apps/web/modules/onboarding/personal/calendar/personal-calendar-view.tsx index d90f1aa84d22..c73a55f7b3c6 100644 --- a/apps/web/modules/onboarding/personal/calendar/personal-calendar-view.tsx +++ b/apps/web/modules/onboarding/personal/calendar/personal-calendar-view.tsx @@ -109,11 +109,7 @@ export const PersonalCalendarView = ({ userEmail }: PersonalCalendarViewProps) = {t("back")}
-
diff --git a/packages/lib/apps/getInstallCountPerApp.test.ts b/packages/lib/apps/getInstallCountPerApp.test.ts new file mode 100644 index 000000000000..b557da5b890b --- /dev/null +++ b/packages/lib/apps/getInstallCountPerApp.test.ts @@ -0,0 +1,57 @@ +import prisma from "@calcom/prisma"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import getInstallCountPerApp, { resetInstallCountCache } from "./getInstallCountPerApp"; + +vi.mock("@calcom/prisma", () => ({ + default: { $queryRaw: vi.fn() }, +})); + +const queryRaw = vi.mocked(prisma.$queryRaw); + +describe("getInstallCountPerApp", () => { + beforeEach(() => { + vi.useRealTimers(); + queryRaw.mockReset(); + resetInstallCountCache(); + }); + + it("maps install counts by app slug", async () => { + queryRaw.mockResolvedValueOnce([ + { appId: "google-calendar", installCount: 5 }, + { appId: "office365-calendar", installCount: 2 }, + ]); + + await expect(getInstallCountPerApp()).resolves.toEqual({ + "google-calendar": 5, + "office365-calendar": 2, + }); + }); + + it("serves repeat calls from cache within the TTL", async () => { + queryRaw.mockResolvedValue([{ appId: "google-calendar", installCount: 1 }]); + + await getInstallCountPerApp(); + await getInstallCountPerApp(); + + expect(queryRaw).toHaveBeenCalledTimes(1); + }); + + it("queries again after the TTL expires", async () => { + vi.useFakeTimers(); + queryRaw.mockResolvedValue([{ appId: "google-calendar", installCount: 1 }]); + + await getInstallCountPerApp(); + vi.advanceTimersByTime(5 * 60 * 1000 + 1); + await getInstallCountPerApp(); + + expect(queryRaw).toHaveBeenCalledTimes(2); + }); + + it("does not cache a failed query", async () => { + queryRaw.mockRejectedValueOnce(new Error("db down")); + queryRaw.mockResolvedValueOnce([{ appId: "google-calendar", installCount: 3 }]); + + await expect(getInstallCountPerApp()).rejects.toThrow("db down"); + await expect(getInstallCountPerApp()).resolves.toEqual({ "google-calendar": 3 }); + }); +}); diff --git a/packages/lib/apps/getInstallCountPerApp.ts b/packages/lib/apps/getInstallCountPerApp.ts index ed95234875f5..febcc7c20a32 100644 --- a/packages/lib/apps/getInstallCountPerApp.ts +++ b/packages/lib/apps/getInstallCountPerApp.ts @@ -1,7 +1,7 @@ -import { unstable_cache } from "next/cache"; +import prisma from "@calcom/prisma"; import { z } from "zod"; -import prisma from "@calcom/prisma"; +const CACHE_TTL_MS = 5 * 60 * 1000; const computeInstallCountsFromDB = async (): Promise> => { const mostPopularApps = z.array(z.object({ appId: z.string(), installCount: z.number() })).parse( @@ -28,11 +28,24 @@ const computeInstallCountsFromDB = async (): Promise> => ); }; +// An in-process cache instead of Next's `unstable_cache`: this is called from Pages Router API +// routes (tRPC `apps.integrations`), where `unstable_cache` throws "incrementalCache missing". +let cached: { expiresAt: number; counts: Promise> } | null = null; + const getInstallCountPerApp = async (): Promise> => { - return unstable_cache(async () => computeInstallCountsFromDB(), ["app-install-counts"], { - revalidate: 300, - tags: ["app-install-counts"], - })(); + const now = Date.now(); + if (cached && cached.expiresAt > now) return cached.counts; + + const counts = computeInstallCountsFromDB(); + cached = { expiresAt: now + CACHE_TTL_MS, counts }; + counts.catch(() => { + if (cached?.counts === counts) cached = null; + }); + return counts; +}; + +export const resetInstallCountCache = () => { + cached = null; }; export default getInstallCountPerApp;