fix: Cleanup after removing connected calendar (#10012)

Co-authored-by: Leo Giovanetti <hello@leog.me>
Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com>
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
pull/10662/head^2
Pradumn Kumar 2023-08-09 19:52:51 +05:30 committed by GitHub
parent cf89598279
commit cde9b24a9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 31 deletions

View File

@ -1,5 +1,6 @@
import z from "zod"; import z from "zod";
import { getCalendar } from "@calcom/app-store/_utils/getCalendar";
import { cancelScheduledJobs } from "@calcom/app-store/zapier/lib/nodeScheduler"; import { cancelScheduledJobs } from "@calcom/app-store/zapier/lib/nodeScheduler";
import { DailyLocationType } from "@calcom/core/location"; import { DailyLocationType } from "@calcom/core/location";
import { sendCancelledEmails } from "@calcom/emails"; import { sendCancelledEmails } from "@calcom/emails";
@ -26,7 +27,8 @@ type DeleteCredentialOptions = {
}; };
export const deleteCredentialHandler = async ({ ctx, input }: 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({ const credential = await prisma.credential.findFirst({
where: { where: {
@ -43,6 +45,11 @@ export const deleteCredentialHandler = async ({ ctx, input }: DeleteCredentialOp
dirName: true, 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 it's a calendar, remove the destination calendar from the event type
if (credential.app?.categories.includes(AppCategories.calendar)) { if (
if (eventType.destinationCalendar?.credential?.appId === credential.appId) { credential.app?.categories.includes(AppCategories.calendar) &&
const destinationCalendar = await prisma.destinationCalendar.findFirst({ eventType.destinationCalendar?.credential?.appId === credential.appId
where: { ) {
id: eventType.destinationCalendar?.id, const destinationCalendar = await prisma.destinationCalendar.findFirst({
}, where: {
}); id: eventType.destinationCalendar?.id,
if (destinationCalendar) { },
await prisma.destinationCalendar.delete({ });
where: {
id: destinationCalendar.id,
},
});
}
}
if (externalId) { if (destinationCalendar) {
const existingSelectedCalendar = await prisma.selectedCalendar.findFirst({ await prisma.destinationCalendar.delete({
where: { 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 zapier get disconnected, delete zapier apiKey, delete zapier webhooks and cancel all scheduled jobs from zapier
if (credential.app?.slug === "zapier") { if (credential.app?.slug === "zapier") {
await prisma.apiKey.deleteMany({ await prisma.apiKey.deleteMany({