From 2498785c49b44e13862f75346c245701792200f3 Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Date: Wed, 22 Nov 2023 08:22:03 -0500 Subject: [PATCH] chore: Clean Up Delete Credential Selected Calendar Error Message (#12353) --- .../deleteCredential.handler.ts | 64 +++++++------------ 1 file changed, 24 insertions(+), 40 deletions(-) diff --git a/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.ts b/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.ts index fb716afb841f35..e396d5e0d454d8 100644 --- a/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.ts +++ b/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.ts @@ -1,4 +1,3 @@ -import { Prisma } from "@prisma/client"; import z from "zod"; import { getCalendar } from "@calcom/app-store/_utils/getCalendar"; @@ -328,52 +327,37 @@ export const deleteCredentialHandler = async ({ ctx, input }: DeleteCredentialOp } } - // Validated that credential is user's above - await prisma.credential.delete({ - where: { - id: id, - }, - }); - // Backwards compatibility. Selected calendars cascade on delete when deleting a credential // If it's a calendar remove it from the SelectedCalendars if (credential.app?.categories.includes(AppCategories.calendar)) { - const selectedCalendars = await prisma.selectedCalendar.findMany({ - where: { - userId: user.id, - integration: credential.type as string, - }, - }); - - if (selectedCalendars.length) { + try { const calendar = await getCalendar(credential); const calendars = await calendar?.listCalendars(); - if (calendars && calendars.length > 0) { - calendars.map(async (cal) => { - prisma.selectedCalendar - .delete({ - where: { - userId_integration_externalId: { - userId: user.id, - externalId: cal.externalId, - integration: cal.integration as string, - }, - }, - }) - .catch((error) => { - if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2025") { - console.log( - `Error deleting selected calendars for user ${user.id} and calendar ${credential.appId}. Could not find selected calendar.` - ); - } - console.log( - `Error deleting selected calendars for user ${user.id} and calendar ${credential.appId} with error: ${error}` - ); - }); - }); - } + const calendarIds = calendars?.map((cal) => cal.externalId); + + await prisma.selectedCalendar.deleteMany({ + where: { + userId: user.id, + integration: credential.type as string, + externalId: { + in: calendarIds, + }, + }, + }); + } catch (error) { + console.warn( + `Error deleting selected calendars for userId: ${user.id} integration: ${credential.type}`, + error + ); } } + + // Validated that credential is user's above + await prisma.credential.delete({ + where: { + id: id, + }, + }); };