Files
formbricks/apps/web/modules/projects/settings/general/actions.ts
T
Piyush Gupta 35b2d12e18 feat: Product Model Revamp (#4353)
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com>
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com>
2024-12-03 04:34:09 +00:00

40 lines
1.3 KiB
TypeScript

"use server";
import { authenticatedActionClient } from "@/lib/utils/action-client";
import { checkAuthorizationUpdated } from "@/lib/utils/action-client-middleware";
import { getOrganizationIdFromProjectId } from "@/lib/utils/helper";
import { deleteProject } from "@/modules/projects/settings/lib/project";
import { z } from "zod";
import { getUserProjects } from "@formbricks/lib/project/service";
import { ZId } from "@formbricks/types/common";
const ZProjectDeleteAction = z.object({
projectId: ZId,
});
export const deleteProjectAction = authenticatedActionClient
.schema(ZProjectDeleteAction)
.action(async ({ ctx, parsedInput }) => {
const organizationId = await getOrganizationIdFromProjectId(parsedInput.projectId);
await checkAuthorizationUpdated({
userId: ctx.user.id,
organizationId: organizationId,
access: [
{
type: "organization",
roles: ["owner", "manager"],
},
],
});
const availableProjects = (await getUserProjects(ctx.user.id, organizationId)) ?? null;
if (!!availableProjects && availableProjects?.length <= 1) {
throw new Error("You can't delete the last project in the environment.");
}
// delete project
return await deleteProject(parsedInput.projectId);
});