2023-04-25 22:39:47 +00:00
|
|
|
import { prisma } from "@calcom/prisma";
|
|
|
|
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
|
|
|
|
|
|
|
import type { TDeleteInputSchema } from "./delete.schema";
|
|
|
|
|
|
|
|
type DeleteOptions = {
|
|
|
|
ctx: {
|
|
|
|
user: NonNullable<TrpcSessionUser>;
|
|
|
|
};
|
|
|
|
input: TDeleteInputSchema;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteHandler = async ({ ctx, input }: DeleteOptions) => {
|
|
|
|
const { id } = input;
|
2023-05-23 01:15:29 +00:00
|
|
|
|
|
|
|
const andCondition: Partial<{ id: string; eventTypeId: number; teamId: number; userId: number }>[] = [
|
|
|
|
{ id: id },
|
|
|
|
];
|
|
|
|
|
|
|
|
if (input.eventTypeId) {
|
|
|
|
andCondition.push({ eventTypeId: input.eventTypeId });
|
|
|
|
} else if (input.teamId) {
|
|
|
|
andCondition.push({ teamId: input.teamId });
|
|
|
|
} else {
|
|
|
|
andCondition.push({ userId: ctx.user.id });
|
|
|
|
}
|
|
|
|
|
|
|
|
const webhookToDelete = await prisma.webhook.findFirst({
|
|
|
|
where: {
|
|
|
|
AND: andCondition,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (webhookToDelete) {
|
|
|
|
await prisma.webhook.delete({
|
|
|
|
where: {
|
|
|
|
id: webhookToDelete.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2023-04-25 22:39:47 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
};
|
|
|
|
};
|