Files
calendar/packages/prisma/extensions/usage-tracking.ts
T
Volnei MunhozGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
e310931417 fix: remove circular dependency in packages/prisma (#24596)
- Replaced DeploymentRepository import with inline implementation
- Created InlineDeploymentRepository class implementing IDeploymentRepository
- Breaks circular dependency: deployment.ts -> prisma/index.ts -> usage-tracking.ts -> deployment.ts
- Verified with madge: no circular dependencies found

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2025-10-21 13:14:15 +00:00

56 lines
1.8 KiB
TypeScript

import { waitUntil } from "@vercel/functions";
import { UsageEvent, LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService";
import type { IDeploymentRepository } from "@calcom/lib/server/repository/deployment.interface";
import { Prisma } from "@calcom/prisma/client";
import type { PrismaClient } from "@calcom/prisma/client";
class InlineDeploymentRepository implements IDeploymentRepository {
constructor(private prisma: PrismaClient) {}
async getLicenseKeyWithId(id: number): Promise<string | null> {
const deployment = await this.prisma.deployment.findUnique({
where: { id },
select: { licenseKey: true },
});
return deployment?.licenseKey || null;
}
async getSignatureToken(id: number): Promise<string | null> {
const deployment = await this.prisma.deployment.findUnique({
where: { id },
select: { signatureTokenEncrypted: true },
});
return deployment?.signatureTokenEncrypted || null;
}
}
async function incrementUsage(prismaClient: PrismaClient, event?: UsageEvent) {
const deploymentRepo = new InlineDeploymentRepository(prismaClient);
try {
const licenseKeyService = await LicenseKeySingleton.getInstance(deploymentRepo);
await licenseKeyService.incrementUsage(event);
} catch (e) {
console.log(e);
}
}
export function usageTrackingExtention(prismaClient: PrismaClient) {
return Prisma.defineExtension({
query: {
booking: {
async create({ args, query }) {
waitUntil(incrementUsage(prismaClient, UsageEvent.BOOKING));
return query(args);
},
},
user: {
async create({ args, query }) {
waitUntil(incrementUsage(prismaClient, UsageEvent.USER));
return query(args);
},
},
},
});
}