Files
calendar/packages/app-store/routing-forms/trpc/deleteForm.handler.ts
T
Eunjae LeeGitHubhariom@cal.com <hariombalhara@gmail.com>Hariom BalharaDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
6fe6540bc6 fix: apply PBAC to routing form CRUD (#22859)
* fix: apply PBAC to routing form CRUD

* apply permission checks to the UI

* moving prisma call to repository [WIP]

* rename repository and fix type error

* update implementation

* fix formMutation handler

* remove unused import

* revert some rename

* add RolePermission for 'routingForm'

* Revert "revert some rename"

This reverts commit 0ef3114c0d27b821271b36296de531654b37749f.

* clean up PrismaRoutingFormRepository

* fix unit test

* remove no longer necessary code

* fix type definition

* explicit permission handling

* do not disable un-editable routing form

* fix: correct property name from readonly to readOnly in ListLinkItem

- Fix TypeScript error in routing forms component
- Change readonly={readOnly} to readOnly={readOnly} to match component interface
- Resolves type check failure in CI

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

---------

Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2025-08-08 13:18:18 +01:00

60 lines
1.6 KiB
TypeScript

import { entityPrismaWhereClause } from "@calcom/lib/entityPermissionUtils.server";
import type { PrismaClient } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
import { TRPCError } from "@trpc/server";
import getConnectedForms from "../lib/getConnectedForms";
import type { TDeleteFormInputSchema } from "./deleteForm.schema";
import { checkPermissionOnExistingRoutingForm } from "./permissions";
interface DeleteFormHandlerOptions {
ctx: {
prisma: PrismaClient;
user: NonNullable<TrpcSessionUser>;
};
input: TDeleteFormInputSchema;
}
export const deleteFormHandler = async ({ ctx, input }: DeleteFormHandlerOptions) => {
const { user, prisma } = ctx;
await checkPermissionOnExistingRoutingForm({
formId: input.id,
userId: user.id,
permission: "routingForm.delete",
fallbackRoles: [MembershipRole.ADMIN, MembershipRole.OWNER],
});
const areFormsUsingIt = (
await getConnectedForms(prisma, {
id: input.id,
userId: user.id,
})
).length;
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.",
});
}
const deletedRes = await prisma.app_RoutingForms_Form.deleteMany({
where: {
id: input.id,
...entityPrismaWhereClause({ userId: user.id }),
},
});
if (!deletedRes.count) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Form seems to be already deleted.",
});
}
return deletedRes;
};
export default deleteFormHandler;