Files
calendar/packages/lib/server/repository/PrismaApiKeyRepository.ts
T
2df2868b20 fix: cal ai webhook (#24368)
* fix: cal ai email

* fix: remove

* fix: org

* replace Cal AI with Cal.ai

* fix: use

* fix: feedback

* fix: types

* fix: types

* fix: types

* fix: tests

* Merge branch 'main' into fix/cal-ai-credits

* refactor: feedback

* refactor: imporvement

* fix: type

* refactor: feedback

* fix: tests

* fix: use pbac

---------

Co-authored-by: CarinaWolli <wollencarina@gmail.com>
2025-11-05 13:31:55 +04:00

87 lines
1.9 KiB
TypeScript

import { v4 as uuidv4 } from "uuid";
import { generateUniqueAPIKey as generateHashedApiKey } from "@calcom/ee/api-keys/lib/apiKeys";
import type { PrismaClient } from "@calcom/prisma";
export class PrismaApiKeyRepository {
constructor(private prismaClient: PrismaClient) {}
static async withGlobalPrisma() {
return new PrismaApiKeyRepository((await import("@calcom/prisma")).prisma);
}
async findByHashedKey(hashedKey: string) {
return this.prismaClient.apiKey.findUnique({
where: { hashedKey },
select: {
id: true,
hashedKey: true,
userId: true,
expiresAt: true,
user: {
select: {
role: true,
locked: true,
email: true,
},
},
},
});
}
async findApiKeysFromUserId({ userId }: { userId: number }) {
const apiKeys = await this.prismaClient.apiKey.findMany({
where: {
userId,
OR: [
{
NOT: {
appId: "zapier",
},
},
{
appId: null,
},
],
},
orderBy: { createdAt: "desc" },
});
return apiKeys.filter((apiKey) => {
if (apiKey.note?.startsWith("Cal.ai Phone API Key")) {
return false;
}
return true;
});
}
async createApiKey({
userId,
teamId,
note,
expiresAt,
}: {
userId: number;
teamId?: number;
note?: string;
expiresAt?: Date | null;
}) {
const [hashedApiKey, apiKey] = generateHashedApiKey();
await this.prismaClient.apiKey.create({
data: {
id: uuidv4(),
userId,
teamId,
expiresAt,
hashedKey: hashedApiKey,
note: note,
},
});
const apiKeyPrefix = process.env.API_KEY_PREFIX ?? "cal_";
const prefixedApiKey = `${apiKeyPrefix}${apiKey}`;
return prefixedApiKey;
}
}