Files
calendar/packages/features/ee/common/server/checkLicense.ts
T
devin-ai-integration[bot]GitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>benny@cal.com <benny@cal.com>Anik Dhabal Babu
4920abdaf7 feat: optimize Prisma queries by replacing findFirst with findUnique where applicable (#21826)
* feat: optimize Prisma queries by replacing findFirst with findUnique where applicable

- Replace findFirst/findFirstOrThrow with findUnique/findUniqueOrThrow for queries using unique constraints
- Maintain existing functionality and error handling behavior
- Focus on queries using primary keys and unique index fields from schema
- Revert problematic changes that caused test failures to maintain stability

Co-Authored-By: benny@cal.com <benny@cal.com>

* revert: exclude API files from Prisma query optimizations per user request

- Reverted all 55 API-related files to their original state
- Kept all non-API Prisma query optimizations intact
- API files include apps/api/v1, apps/api/v2, apps/web/app/api, and packages/app-store/*/api
- Non-API optimizations remain for packages/lib, packages/features, apps/web (non-api), etc.

Co-Authored-By: benny@cal.com <benny@cal.com>

* feat: optimize membership query in attributeUtils to use findUnique with userId_teamId constraint

Co-Authored-By: benny@cal.com <benny@cal.com>

* revert: exclude test files from Prisma query optimizations per user request

Co-Authored-By: benny@cal.com <benny@cal.com>

* revert: revert attributeUtils.ts to use findFirst for test compatibility

Co-Authored-By: benny@cal.com <benny@cal.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: benny@cal.com <benny@cal.com>
Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
2025-06-17 09:52:02 +03:00

53 lines
1.8 KiB
TypeScript

import cache from "memory-cache";
import { z } from "zod";
import { CONSOLE_URL } from "@calcom/lib/constants";
import type { PrismaClient } from "@calcom/prisma";
const CACHING_TIME = 86400000; // 24 hours in milliseconds
const schemaLicenseKey = z
.string()
// .uuid() exists but I'd to fix the situation where the CALCOM_LICENSE_KEY is wrapped in quotes
.regex(/^\"?[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}\"?$/, {
message: "License key must follow UUID format: 8-4-4-4-12",
})
.transform((v) => {
// Remove the double quotes from the license key, as they 404 the fetch.
return v != null && v.length >= 2 && v.charAt(0) == '"' && v.charAt(v.length - 1) == '"'
? v.substring(1, v.length - 1)
: v;
});
async function checkLicense(
/** The prisma client to use (necessary for public API to handle custom prisma instances) */
prisma: PrismaClient
): Promise<boolean> {
/** We skip for E2E testing */
if (!!process.env.NEXT_PUBLIC_IS_E2E) return true;
/** We check first on env */
let licenseKey = process.env.CALCOM_LICENSE_KEY;
if (!licenseKey) {
/** We try to check on DB only if env is undefined */
const deployment = await prisma.deployment.findUnique({ where: { id: 1 } });
licenseKey = deployment?.licenseKey ?? undefined;
}
if (!licenseKey) return false;
const url = `${CONSOLE_URL}/api/license?key=${schemaLicenseKey.parse(licenseKey)}`;
const cachedResponse = cache.get(url);
if (cachedResponse) {
return cachedResponse;
} else {
try {
const response = await fetch(url, { mode: "cors" });
const data = await response.json();
cache.put(url, data.valid, CACHING_TIME);
return data.valid;
} catch (error) {
return false;
}
}
}
export default checkLicense;