Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
chore: Clean Up Delete Credential Selected Calendar Error Message (ca…
Browse files Browse the repository at this point in the history
  • Loading branch information
joeauyeung authored Nov 22, 2023
1 parent 9a6683e commit 2498785
Showing 1 changed file with 24 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Prisma } from "@prisma/client";
import z from "zod";

import { getCalendar } from "@calcom/app-store/_utils/getCalendar";
Expand Down Expand Up @@ -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,
},
});
};

0 comments on commit 2498785

Please sign in to comment.