From a30555018fc0e0fa24e5eab35f75cc138a22e464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Omar=20L=C3=B3pez?= Date: Sat, 16 Mar 2024 12:26:13 -0700 Subject: [PATCH] fix: deprecated code in new credential sync endpoints (#14116) --- apps/api/pages/api/credential-sync/_delete.ts | 8 ++--- apps/api/pages/api/credential-sync/_get.ts | 3 +- apps/api/pages/api/credential-sync/_patch.ts | 3 +- apps/api/pages/api/credential-sync/_post.ts | 29 ++++++++++--------- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/apps/api/pages/api/credential-sync/_delete.ts b/apps/api/pages/api/credential-sync/_delete.ts index cfcbd719b0..7753110ffc 100644 --- a/apps/api/pages/api/credential-sync/_delete.ts +++ b/apps/api/pages/api/credential-sync/_delete.ts @@ -1,6 +1,7 @@ import type { NextApiRequest } from "next"; import { defaultResponder } from "@calcom/lib/server"; +import prisma from "@calcom/prisma"; import { schemaCredentialDeleteParams } from "~/lib/validations/credential-sync"; @@ -40,12 +41,7 @@ import { schemaCredentialDeleteParams } from "~/lib/validations/credential-sync" * description: Credential syncing not enabled */ async function handler(req: NextApiRequest) { - const { prisma } = req; - - const { userId: reqUserId, credentialId: reqCredentialId } = schemaCredentialDeleteParams.parse(req.query); - - const userId = parseInt(reqUserId); - const credentialId = parseInt(reqCredentialId); + const { userId, credentialId } = schemaCredentialDeleteParams.parse(req.query); const credential = await prisma.credential.delete({ where: { diff --git a/apps/api/pages/api/credential-sync/_get.ts b/apps/api/pages/api/credential-sync/_get.ts index b2d56102c4..d465d080fe 100644 --- a/apps/api/pages/api/credential-sync/_get.ts +++ b/apps/api/pages/api/credential-sync/_get.ts @@ -1,6 +1,7 @@ import type { NextApiRequest } from "next"; import { defaultResponder } from "@calcom/lib/server"; +import prisma from "@calcom/prisma"; import { schemaCredentialGetParams } from "~/lib/validations/credential-sync"; @@ -34,8 +35,6 @@ import { schemaCredentialGetParams } from "~/lib/validations/credential-sync"; * description: Credential syncing not enabled */ async function handler(req: NextApiRequest) { - const { prisma } = req; - const { appSlug, userId } = schemaCredentialGetParams.parse(req.query); let credentials = await prisma.credential.findMany({ diff --git a/apps/api/pages/api/credential-sync/_patch.ts b/apps/api/pages/api/credential-sync/_patch.ts index 3ccd20d35a..ac7aac2ecb 100644 --- a/apps/api/pages/api/credential-sync/_patch.ts +++ b/apps/api/pages/api/credential-sync/_patch.ts @@ -3,6 +3,7 @@ import type { NextApiRequest } from "next"; import { minimumTokenResponseSchema } from "@calcom/app-store/_utils/oauth/parseRefreshTokenResponse"; import { symmetricDecrypt } from "@calcom/lib/crypto"; import { defaultResponder } from "@calcom/lib/server"; +import prisma from "@calcom/prisma"; import { schemaCredentialPatchParams, schemaCredentialPatchBody } from "~/lib/validations/credential-sync"; @@ -54,8 +55,6 @@ import { schemaCredentialPatchParams, schemaCredentialPatchBody } from "~/lib/va * description: Credential syncing not enabled */ async function handler(req: NextApiRequest) { - const { prisma } = req; - const { userId, credentialId } = schemaCredentialPatchParams.parse(req.query); const { encryptedKey } = schemaCredentialPatchBody.parse(req.body); diff --git a/apps/api/pages/api/credential-sync/_post.ts b/apps/api/pages/api/credential-sync/_post.ts index a7e5b85137..6a6b7aebd9 100644 --- a/apps/api/pages/api/credential-sync/_post.ts +++ b/apps/api/pages/api/credential-sync/_post.ts @@ -6,8 +6,10 @@ import { appStoreMetadata } from "@calcom/app-store/appStoreMetaData"; import { symmetricDecrypt } from "@calcom/lib/crypto"; import { HttpError } from "@calcom/lib/http-error"; import { defaultResponder } from "@calcom/lib/server"; +import prisma from "@calcom/prisma"; +import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential"; -import { schemaCredentialPostParams, schemaCredentialPostBody } from "~/lib/validations/credential-sync"; +import { schemaCredentialPostBody, schemaCredentialPostParams } from "~/lib/validations/credential-sync"; /** * @swagger @@ -54,8 +56,6 @@ import { schemaCredentialPostParams, schemaCredentialPostBody } from "~/lib/vali * description: Credential syncing not enabled */ async function handler(req: NextApiRequest) { - const { prisma } = req; - if (!req.body) { throw new HttpError({ message: "Request body is missing", statusCode: 400 }); } @@ -88,26 +88,29 @@ async function handler(req: NextApiRequest) { const appMetadata = appStoreMetadata[app.dirName as keyof typeof appStoreMetadata]; - const credential = await prisma.credential.create({ + const createdcredential = await prisma.credential.create({ data: { userId, appId: appSlug, key, type: appMetadata.type, }, - include: { - user: { - select: { - email: true, - }, - }, - }, + select: credentialForCalendarServiceSelect, }); + // createdcredential.user.email; + // TODO: ^ Investigate why this select doesn't work. + const credential = await prisma.credential.findUniqueOrThrow({ + where: { + id: createdcredential.id, + }, + select: credentialForCalendarServiceSelect, + }); + // ^ Workaround for the select in `create` not working if (createCalendarResources) { const calendar = await getCalendar(credential); + if (!calendar) throw new HttpError({ message: "Calendar missing for credential", statusCode: 500 }); const calendars = await calendar.listCalendars(); - const calendarToCreate = calendars.find((calendar) => calendar.primary) || calendars[0]; if (createSelectedCalendar) { @@ -129,7 +132,7 @@ async function handler(req: NextApiRequest) { integration: appMetadata.type, externalId: calendarToCreate.externalId, credential: { connect: { id: credential.id } }, - primaryEmail: calendarToCreate.email || credential.user.email, + primaryEmail: calendarToCreate.email || credential.user?.email, user: { connect: { id: userId } }, }, });