From 374a6e087ae4b7c86659172c34601540b937f621 Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Fri, 17 Jun 2022 23:23:28 +0200 Subject: [PATCH] feat: remove datacredentials, move to deployment.databaseUrl --- lib/helpers/customPrisma.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/helpers/customPrisma.ts b/lib/helpers/customPrisma.ts index dc9bbeeaa3..f106ce36f2 100644 --- a/lib/helpers/customPrisma.ts +++ b/lib/helpers/customPrisma.ts @@ -7,27 +7,31 @@ import { prismaAdmin } from "@calcom/console/modules/common/utils/prisma"; import { asStringOrUndefined } from "@calcom/lib/asStringOrNull"; import { prisma, customPrisma } from "@calcom/prisma"; -// This replaces the prisma client for the cusotm one if the customCredentialsId is valid +// This replaces the prisma client for the cusotm one if the key is valid export const customPrismaClient: NextMiddleware = async (req, res, next) => { const { - query: { customCredentialsId }, - } = req; + query: { key }, + }: { query: { key?: string } } = req; // If no custom api Id is provided, attach to request the regular cal.com prisma client. - if (!customCredentialsId) { + if (!key) { req.prisma = prisma; await next(); } else { - const id = asStringOrUndefined(customCredentialsId); + const id = asStringOrUndefined(key); - // If we have a customCredentialsId, we check if it is valid. - const dataCredentials = await prismaAdmin.dataCredentials.findUnique({ - where: { id }, + // If we have a key, we check if it is valid. + const deployment = await prismaAdmin.deployment.findUnique({ + where: { key }, }); - if (!dataCredentials) { + if (!deployment) { res.status(400).json({ error: "Invalid custom credentials id" }); return; } - const credentials = dataCredentials?.credentials; + const credentials = deployment.databaseUrl; + if (!credentials) { + res.status(400).json({ error: "no databaseUrl set up at your instance yet" }); + return; + } const hashedUrl = await hash(credentials, 12); const cachedPrisma = cache.get(hashedUrl);