Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ export const PersonalCalendarView = ({ userEmail }: PersonalCalendarViewProps) =
{t("back")}
</Button>
<div className="flex items-center gap-2">
<Button
color="minimal"
className="rounded-[10px]"
onClick={handleSkip}
disabled={queryIntegrations.isPending || isSubmitting}>
<Button color="minimal" className="rounded-[10px]" onClick={handleSkip} disabled={isSubmitting}>
{t("onboarding_skip_for_now")}
</Button>
<Button
Expand All @@ -122,7 +118,7 @@ export const PersonalCalendarView = ({ userEmail }: PersonalCalendarViewProps) =
className="rounded-[10px]"
onClick={handleContinue}
loading={isSubmitting}
disabled={queryIntegrations.isPending || isSubmitting}>
disabled={isSubmitting}>
{t("continue")}
</Button>
</div>
Expand Down
57 changes: 57 additions & 0 deletions packages/lib/apps/getInstallCountPerApp.test.ts
Original file line number Diff line number Diff line change
@@ -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 });
});
});
25 changes: 19 additions & 6 deletions packages/lib/apps/getInstallCountPerApp.ts
Original file line number Diff line number Diff line change
@@ -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<Record<string, number>> => {
const mostPopularApps = z.array(z.object({ appId: z.string(), installCount: z.number() })).parse(
Expand All @@ -28,11 +28,24 @@ const computeInstallCountsFromDB = async (): Promise<Record<string, number>> =>
);
};

// 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<Record<string, number>> } | null = null;

const getInstallCountPerApp = async (): Promise<Record<string, number>> => {
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;
Expand Down
Loading