From cde9b24a9bd7d30730c922879fcd2b46cfb2874b Mon Sep 17 00:00:00 2001 From: Pradumn Kumar Date: Wed, 9 Aug 2023 19:52:51 +0530 Subject: [PATCH] fix: Cleanup after removing connected calendar (#10012) Co-authored-by: Leo Giovanetti Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com> Co-authored-by: Keith Williams Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> --- .../deleteCredential.handler.ts | 72 +++++++++++-------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.ts b/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.ts index adf1142c48..f57439d7b8 100644 --- a/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.ts +++ b/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.ts @@ -1,5 +1,6 @@ import z from "zod"; +import { getCalendar } from "@calcom/app-store/_utils/getCalendar"; import { cancelScheduledJobs } from "@calcom/app-store/zapier/lib/nodeScheduler"; import { DailyLocationType } from "@calcom/core/location"; import { sendCancelledEmails } from "@calcom/emails"; @@ -26,7 +27,8 @@ type DeleteCredentialOptions = { }; export const deleteCredentialHandler = async ({ ctx, input }: DeleteCredentialOptions) => { - const { id, externalId, teamId } = input; + const { user } = ctx; + const { id, teamId } = input; const credential = await prisma.credential.findFirst({ where: { @@ -43,6 +45,11 @@ export const deleteCredentialHandler = async ({ ctx, input }: DeleteCredentialOp dirName: true, }, }, + id: true, + type: true, + userId: true, + teamId: true, + invalid: true, }, }); @@ -107,40 +114,22 @@ export const deleteCredentialHandler = async ({ ctx, input }: DeleteCredentialOp } // If it's a calendar, remove the destination calendar from the event type - if (credential.app?.categories.includes(AppCategories.calendar)) { - if (eventType.destinationCalendar?.credential?.appId === credential.appId) { - const destinationCalendar = await prisma.destinationCalendar.findFirst({ - where: { - id: eventType.destinationCalendar?.id, - }, - }); - if (destinationCalendar) { - await prisma.destinationCalendar.delete({ - where: { - id: destinationCalendar.id, - }, - }); - } - } + if ( + credential.app?.categories.includes(AppCategories.calendar) && + eventType.destinationCalendar?.credential?.appId === credential.appId + ) { + const destinationCalendar = await prisma.destinationCalendar.findFirst({ + where: { + id: eventType.destinationCalendar?.id, + }, + }); - if (externalId) { - const existingSelectedCalendar = await prisma.selectedCalendar.findFirst({ + if (destinationCalendar) { + await prisma.destinationCalendar.delete({ where: { - externalId: externalId, + id: destinationCalendar.id, }, }); - // @TODO: SelectedCalendar doesn't have unique ID so we should only delete one item - if (existingSelectedCalendar) { - await prisma.selectedCalendar.delete({ - where: { - userId_integration_externalId: { - userId: existingSelectedCalendar.userId, - externalId: existingSelectedCalendar.externalId, - integration: existingSelectedCalendar.integration, - }, - }, - }); - } } } @@ -313,6 +302,27 @@ export const deleteCredentialHandler = async ({ ctx, input }: DeleteCredentialOp } } + // If it's a calendar remove it from the SelectedCalendars + if (credential.app?.categories.includes(AppCategories.calendar)) { + const calendar = await getCalendar(credential); + + const calendars = await calendar?.listCalendars(); + + if (calendars && calendars.length > 0) { + calendars.map(async (cal) => { + await prisma.selectedCalendar.delete({ + where: { + userId_integration_externalId: { + userId: user.id, + externalId: cal.externalId, + integration: cal.integration as string, + }, + }, + }); + }); + } + } + // if zapier get disconnected, delete zapier apiKey, delete zapier webhooks and cancel all scheduled jobs from zapier if (credential.app?.slug === "zapier") { await prisma.apiKey.deleteMany({