51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import type { Prisma } from "@prisma/client";
|
|
|
|
import { updateTriggerForExistingBookings } from "@calcom/features/webhooks/lib/scheduleTrigger";
|
|
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;
|
|
|
|
const where: Prisma.WebhookWhereInput = { AND: [{ id: id }] };
|
|
|
|
if (Array.isArray(where.AND)) {
|
|
if (input.eventTypeId) {
|
|
where.AND.push({ eventTypeId: input.eventTypeId });
|
|
} else if (input.teamId) {
|
|
where.AND.push({ teamId: input.teamId });
|
|
} else if (ctx.user.role == "ADMIN") {
|
|
where.AND.push({ OR: [{ platform: true }, { userId: ctx.user.id }] });
|
|
} else {
|
|
where.AND.push({ userId: ctx.user.id });
|
|
}
|
|
}
|
|
|
|
const webhookToDelete = await prisma.webhook.findFirst({
|
|
where,
|
|
});
|
|
|
|
if (webhookToDelete) {
|
|
await prisma.webhook.delete({
|
|
where: {
|
|
id: webhookToDelete.id,
|
|
},
|
|
});
|
|
|
|
await updateTriggerForExistingBookings(webhookToDelete, webhookToDelete.eventTriggers, []);
|
|
}
|
|
|
|
return {
|
|
id,
|
|
};
|
|
};
|