2023-05-09 19:27:05 +00:00
|
|
|
import type { PrismaClient } from "@prisma/client";
|
|
|
|
|
2023-06-15 08:58:07 +00:00
|
|
|
import { entityPrismaWhereClause } from "@calcom/lib/entityPermissionUtils";
|
2023-05-09 19:27:05 +00:00
|
|
|
import { TRPCError } from "@calcom/trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
|
|
|
|
|
|
|
import getConnectedForms from "../lib/getConnectedForms";
|
2023-06-15 08:58:07 +00:00
|
|
|
import { isFormCreateEditAllowed } from "../lib/isFormCreateEditAllowed";
|
2023-05-09 19:27:05 +00:00
|
|
|
import type { TDeleteFormInputSchema } from "./deleteForm.schema";
|
|
|
|
|
|
|
|
interface DeleteFormHandlerOptions {
|
|
|
|
ctx: {
|
|
|
|
prisma: PrismaClient;
|
|
|
|
user: NonNullable<TrpcSessionUser>;
|
|
|
|
};
|
|
|
|
input: TDeleteFormInputSchema;
|
|
|
|
}
|
|
|
|
export const deleteFormHandler = async ({ ctx, input }: DeleteFormHandlerOptions) => {
|
|
|
|
const { user, prisma } = ctx;
|
2023-06-15 08:58:07 +00:00
|
|
|
if (!(await isFormCreateEditAllowed({ userId: user.id, formId: input.id, targetTeamId: null }))) {
|
2023-05-09 19:27:05 +00:00
|
|
|
throw new TRPCError({
|
|
|
|
code: "FORBIDDEN",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const areFormsUsingIt = (
|
|
|
|
await getConnectedForms(prisma, {
|
|
|
|
id: input.id,
|
|
|
|
userId: user.id,
|
|
|
|
})
|
|
|
|
).length;
|
2023-06-15 08:58:07 +00:00
|
|
|
|
2023-05-09 19:27:05 +00:00
|
|
|
if (areFormsUsingIt) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "BAD_REQUEST",
|
|
|
|
message: "This form is being used by other forms. Please remove it's usage from there first.",
|
|
|
|
});
|
|
|
|
}
|
2023-06-15 08:58:07 +00:00
|
|
|
|
|
|
|
const deletedRes = await prisma.app_RoutingForms_Form.deleteMany({
|
2023-05-09 19:27:05 +00:00
|
|
|
where: {
|
|
|
|
id: input.id,
|
2023-06-15 08:58:07 +00:00
|
|
|
...entityPrismaWhereClause({ userId: user.id }),
|
2023-05-09 19:27:05 +00:00
|
|
|
},
|
|
|
|
});
|
2023-06-15 08:58:07 +00:00
|
|
|
|
|
|
|
if (!deletedRes.count) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "BAD_REQUEST",
|
|
|
|
message: "Form seems to be already deleted.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return deletedRes;
|
2023-05-09 19:27:05 +00:00
|
|
|
};
|
2023-05-12 12:01:05 +00:00
|
|
|
|
|
|
|
export default deleteFormHandler;
|