Scheduling infrastructure for absolutely everyone.
fix: Cleanup after removing connected calendar (#10012)
Pradumn Kumarcde9b24a9bd7
diff --git a/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.ts b/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.tsindex adf1142c480..f57439d7b8b 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 }: DeleteCredentialOpdirName: 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 zapierif (credential.app?.slug === "zapier") {await prisma.apiKey.deleteMany({