Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(ui): add deletion of organizations via org settings (#2922)

Marc Klingen2ec0ae8db2b3
diff --git a/web/src/components/SettingsDangerZone.tsx b/web/src/components/SettingsDangerZone.tsx
new file mode 100644
index 0000000000..cc74250080
--- /dev/null
+++ b/web/src/components/SettingsDangerZone.tsx
@@ -0,0 +1,30 @@
+import Header from "@/src/components/layouts/header";
+import React from "react";
+
+export const SettingsDangerZone: React.FC<{
+ items: {
+ title: string;
+ description: string;
+ button: React.ReactNode;
+ }[];
+}> = ({ items }) => {
+ return (
+ <div className="space-y-3">
+ <Header title="Danger Zone" level="h3" />
+ <div className="rounded border">
+ {items.map((item, index) => (
+ <div
+ key={index}
+ className="flex items-center justify-between gap-4 border-b p-4 last:border-b-0"
+ >
+ <div>
+ <h4 className="font-semibold">{item.title}</h4>
+ <p className="text-sm">{item.description}</p>
+ </div>
+ {item.button}
+ </div>
+ ))}
+ </div>
+ </div>
+ );
+};
diff --git a/web/src/components/layouts/header.tsx b/web/src/components/layouts/header.tsx
index e9ae85c69f..34fb3185a6 100644
--- a/web/src/components/layouts/header.tsx
+++ b/web/src/components/layouts/header.tsx
@@ -229,7 +229,6 @@ const BreadcrumbComponent = ({
<Button
variant="ghost"
size="xs"
- data-testid="create-project-btn"
className="h-8 w-full text-sm font-normal"
asChild
>
@@ -316,7 +315,6 @@ const BreadcrumbComponent = ({
<Button
variant="ghost"
size="xs"
- data-testid="create-project-btn"
className="h-8 w-full text-sm font-normal"
asChild
>
diff --git a/web/src/features/organizations/components/DeleteOrganizationButton.tsx b/web/src/features/organizations/components/DeleteOrganizationButton.tsx
new file mode 100644
index 0000000000..308fa49325
--- /dev/null
+++ b/web/src/features/organizations/components/DeleteOrganizationButton.tsx
@@ -0,0 +1,119 @@
+import { Button } from "@/src/components/ui/button";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "@/src/components/ui/dialog";
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormMessage,
+} from "@/src/components/ui/form";
+import { Input } from "@/src/components/ui/input";
+import { api } from "@/src/utils/api";
+import * as z from "zod";
+import { useForm } from "react-hook-form";
+import { zodResolver } from "@hookform/resolvers/zod";
+import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
+import { useQueryOrganization } from "@/src/features/organizations/hooks";
+import { useHasOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
+import { showSuccessToast } from "@/src/features/notifications/showSuccessToast"; // Import success toast function
+
+export function DeleteOrganizationButton() {
+ const capture = usePostHogClientCapture();
+
+ const organization = useQueryOrganization();
+ const confirmMessage =
+ organization?.name.replaceAll(" ", "-").toLowerCase() ?? "organization";
+
+ const formSchema = z.object({
+ name: z.string().includes(confirmMessage, {
+ message: `Please confirm with "${confirmMessage}"`,
+ }),
+ });
+
+ const hasAccess = useHasOrganizationAccess({
+ organizationId: organization?.id,
+ scope: "organization:delete",
+ });
+
+ const deleteOrganization = api.organizations.delete.useMutation();
+
+ const form = useForm<z.infer<typeof formSchema>>({
+ resolver: zodResolver(formSchema),
+ defaultValues: {
+ name: "",
+ },
+ });
+
+ const onSubmit = async () => {
+ if (!organization) return;
+ try {
+ await deleteOrganization.mutateAsync({
+ orgId: organization.id,
+ });
+ capture("organization_settings:delete_organization");
+ showSuccessToast({
+ title: "Organization Deleted",
+ description: "The organization has been successfully deleted.",
+ });
+ await new Promise((resolve) => setTimeout(resolve, 5000)); // Delay for 5 seconds
+ window.location.href = "/"; // Browser reload to refresh jwt
+ } catch (error) {
+ console.error(error);
+ }
+ };
+
+ return (
+ <Dialog>
+ <DialogTrigger asChild>
+ <Button variant="destructive-secondary" disabled={!hasAccess}>
+ Delete Organization
+ </Button>
+ </DialogTrigger>
+ <DialogContent className="sm:max-w-[425px]">
+ <DialogHeader>
+ <DialogTitle className="text-lg font-semibold">
+ Delete Organization
+ </DialogTitle>
+ <DialogDescription>
+ {`To confirm, type "${confirmMessage}" in the input box `}
+ </DialogDescription>
+ </DialogHeader>
+ <Form {...form}>
+ <form
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
+ onSubmit={form.handleSubmit(onSubmit)}
+ className="space-y-8"
+ >
+ <FormField
+ control={form.control}
+ name="name"
+ render={({ field }) => (
+ <FormItem>
+ <FormControl>
+ <Input placeholder={confirmMessage} {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <Button
+ type="submit"
+ variant="destructive"
+ loading={deleteOrganization.isLoading}
+ className="w-full"
+ >
+ Delete Organization
+ </Button>
+ </form>
+ </Form>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/web/src/features/organizations/components/RenameOrganization.tsx b/web/src/features/organizations/components/RenameOrganization.tsx
index 13042e6a6c..e370d445fb 100644
--- a/web/src/features/organizations/components/RenameOrganization.tsx
+++ b/web/src/features/organizations/components/RenameOrganization.tsx
@@ -26,7 +26,7 @@ export default function RenameOrganization() {
const organization = useQueryOrganization();
const hasAccess = useHasOrganizationAccess({
organizationId: organization?.id,
- scope: "organizations:update",
+ scope: "organization:update",
});
const orgName =
@@ -73,7 +73,7 @@ export default function RenameOrganization() {
<b>{form.watch().name}</b>&quot;.
</p>
) : (
- <p className="mb-4 text-sm" data-testid="organization-name">
+ <p className="mb-4 text-sm">
Your Organization is currently named &quot;<b>{orgName}</b>
&quot;.
</p>
@@ -83,7 +83,6 @@ export default function RenameOrganization() {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
onSubmit={form.handleSubmit(onSubmit)}
className="flex-1"
- data-testid="rename-organization-form"
id="rename-organization-form"
>
<FormField
@@ -97,7 +96,6 @@ export default function RenameOrganization() {
placeholder={orgName}
{...field}
className="flex-1"
- data-testid="new-organization-name-input"
disabled={!hasAccess}
/>
{!hasAccess && (
diff --git a/web/src/features/organizations/server/organizationRouter.ts b/web/src/features/organizations/server/organizationRouter.ts
index a4f5f2c67b..e63986f24c 100644
--- a/web/src/features/organizations/server/organizationRouter.ts
+++ b/web/src/features/organizations/server/organizationRouter.ts
@@ -56,7 +56,7 @@ export const organizationsRouter = createTRPCRouter({
throwIfNoOrganizationAccess({
session: ctx.session,
organizationId: input.orgId,
- scope: "organizations:update",
+ scope: "organization:update",
});
const beforeOrganization = await ctx.prisma.organization.findFirst({
where: {
@@ -81,6 +81,48 @@ export const organizationsRouter = createTRPCRouter({
after: afterOrganization,
});
+ return true;
+ }),
+ delete: protectedOrganizationProcedure
+ .input(
+ z.object({
+ orgId: z.string(),
+ }),
+ )
+ .mutation(async ({ input, ctx }) => {
+ throwIfNoOrganizationAccess({
+ session: ctx.session,
+ organizationId: input.orgId,
+ scope: "organization:delete",
+ });
+
+ const countProjects = await ctx.prisma.project.count({
+ where: {
+ orgId: input.orgId,
+ },
+ });
+ if (countProjects > 0) {
+ throw new TRPCError({
+ code: "FORBIDDEN",
+ message:
+ "Please delete or transfer all projects before deleting the organization.",
+ });
+ }
+
+ const organization = await ctx.prisma.organization.delete({
+ where: {
+ id: input.orgId,
+ },
+ });
+
+ await auditLog({
+ session: ctx.session,
+ resourceType: "organization",
+ resourceId: input.orgId,
+ action: "delete",
+ before: organization,
+ });
+
return true;
}),
});
diff --git a/web/src/features/posthog-analytics/usePostHogClientCapture.ts b/web/src/features/posthog-analytics/usePostHogClientCapture.ts
index a4799932db..d31876f5c0 100644
--- a/web/src/features/posthog-analytics/usePostHogClientCapture.ts
+++ b/web/src/features/posthog-analytics/usePostHogClientCapture.ts
@@ -123,6 +123,7 @@ const events = {
"delete_membership_invitation",
"delete_membership",
"pricing_dialog_opened",
+ "delete_organization",
],
help_popup: ["opened", "href_clicked"],
navigate_detail_pages: ["button_click_prev_or_next"],
diff --git a/web/src/features/projects/components/DeleteProjectButton.tsx b/web/src/features/projects/components/DeleteProjectButton.tsx
index acc2d7e619..860784661f 100644
--- a/web/src/features/projects/components/DeleteProjectButton.tsx
+++ b/web/src/features/projects/components/DeleteProjectButton.tsx
@@ -96,11 +96,7 @@ export function DeleteProjectButton() {
render={({ field }) => (
<FormItem>
<FormControl>
- <Input
- placeholder={confirmMessage}
- {...field}
- data-testid="new-project-name-input"
- />
+ <Input placeholder={confirmMessage} {...field} />
</FormControl>
<FormMessage />
</FormItem>
diff --git a/web/src/features/projects/components/RenameProject.tsx b/web/src/features/projects/components/RenameProject.tsx
index 5402489a68..31032fd609 100644
--- a/web/src/features/projects/components/RenameProject.tsx
+++ b/web/src/features/projects/components/RenameProject.tsx
@@ -70,7 +70,7 @@ export default function RenameProject() {
<b>{form.watch().name}</b>&quot;.
</p>
) : (
- <p className="mb-4 text-sm text-primary" data-testid="project-name">
+ <p className="mb-4 text-sm text-primary">
Your Project is currently named &quot;
<b>{project?.name ?? ""}</b>
&quot;.
@@ -81,7 +81,6 @@ export default function RenameProject() {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
onSubmit={form.handleSubmit(onSubmit)}
className="flex-1"
- data-testid="rename-project-form"
id="rename-project-form"
>
<FormField
@@ -95,7 +94,6 @@ export default function RenameProject() {
placeholder={project?.name ?? ""}
{...field}
className="flex-1"
- data-testid="new-project-name-input"
disabled={!hasAccess}
/>
{!hasAccess && (
diff --git a/web/src/features/projects/components/TransferProjectButton.tsx b/web/src/features/projects/components/TransferProjectButton.tsx
index b63d54e45e..880d1d50d0 100644
--- a/web/src/features/projects/components/TransferProjectButton.tsx
+++ b/web/src/features/projects/components/TransferProjectButton.tsx
@@ -174,11 +174,7 @@ export function TransferProjectButton() {
<FormItem>
<FormLabel>Confirm</FormLabel>
<FormControl>
- <Input
- placeholder={confirmMessage}
- {...field}
- data-testid="new-project-name-input"
- />
+ <Input placeholder={confirmMessage} {...field} />
</FormControl>
<FormDescription>
{`To confirm, type "${confirmMessage}" in the input box `}
diff --git a/web/src/features/rbac/constants/organizationAccessRights.ts b/web/src/features/rbac/constants/organizationAccessRights.ts
index c27614b58f..1b02e55901 100644
--- a/web/src/features/rbac/constants/organizationAccessRights.ts
+++ b/web/src/features/rbac/constants/organizationAccessRights.ts
@@ -3,8 +3,8 @@ import { type Role } from "@langfuse/shared/src/db";
const organizationScopes = [
"projects:create",
"projects:transfer_org",
- "organizations:update",
- "organizations:delete", // todo: not implemented yet
+ "organization:update",
+ "organization:delete",
"organizationMembers:read",
"organizationMembers:CUD",
] as const;
@@ -16,15 +16,15 @@ export const organizationRoleAccessRights: Record<Role, OrganizationScope[]> = {
OWNER: [
"projects:create",
"projects:transfer_org",
- "organizations:update",
- "organizations:delete",
+ "organization:update",
+ "organization:delete",
"organizationMembers:CUD",
"organizationMembers:read",
],
ADMIN: [
"projects:create",
"projects:transfer_org",
- "organizations:update",
+ "organization:update",
"organizationMembers:CUD",
"organizationMembers:read",
],
diff --git a/web/src/pages/organization/[organizationId]/settings/index.tsx b/web/src/pages/organization/[organizationId]/settings/index.tsx
index fd5bac7318..2dd9e91def 100644
--- a/web/src/pages/organization/[organizationId]/settings/index.tsx
+++ b/web/src/pages/organization/[organizationId]/settings/index.tsx
@@ -8,6 +8,8 @@ import RenameOrganization from "@/src/features/organizations/components/RenameOr
import { useQueryOrganization } from "@/src/features/organizations/hooks";
import { OrganizationUsageChart } from "@/src/features/usage-metering/OrganizationUsageChart";
import { useRouter } from "next/router";
+import { SettingsDangerZone } from "@/src/components/SettingsDangerZone";
+import { DeleteOrganizationButton } from "@/src/features/organizations/components/DeleteOrganizationButton";
const OrgSettingsPage = () => {
const organization = useQueryOrganization();
@@ -35,6 +37,16 @@ const OrgSettingsPage = () => {
json={{ name: organization.name, id: organization.id }}
/>
</div>
+ <SettingsDangerZone
+ items={[
+ {
+ title: "Delete this organization",
+ description:
+ "Once you delete an organization, there is no going back. Please be certain.",
+ button: <DeleteOrganizationButton />,
+ },
+ ]}
+ />
</div>
),
},
diff --git a/web/src/pages/project/[projectId]/settings/index.tsx b/web/src/pages/project/[projectId]/settings/index.tsx
index f7ff179924..c5e99044be 100644
--- a/web/src/pages/project/[projectId]/settings/index.tsx
+++ b/web/src/pages/project/[projectId]/settings/index.tsx
@@ -19,6 +19,7 @@ import { TransferProjectButton } from "@/src/features/projects/components/Transf
import { useHasOrgEntitlement } from "@/src/features/entitlements/hooks";
import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
import { useRouter } from "next/router";
+import { SettingsDangerZone } from "@/src/components/SettingsDangerZone";
export default function SettingsPage() {
const { project, organization } = useQueryProject();
@@ -47,36 +48,22 @@ export default function SettingsPage() {
}}
/>
</div>
- <div className="space-y-3">
- <Header title="Danger Zone" level="h3" />
- <div className="rounded border">
- {[
- {
- title: "Transfer ownership",
- description:
- "Transfer this project to another organization where you have the ability to create projects.",
- button: <TransferProjectButton />,
- },
- {
- title: "Delete this project",
- description:
- "Once you delete a project, there is no going back. Please be certain.",
- button: <DeleteProjectButton />,
- },
- ].map((item, index) => (
- <div
- key={index}
- className="flex items-center justify-between gap-4 border-b p-4 last:border-b-0"
- >
- <div>
- <h4 className="font-semibold">{item.title}</h4>
- <p className="text-sm">{item.description}</p>
- </div>
- {item.button}
- </div>
- ))}
- </div>
- </div>
+ <SettingsDangerZone
+ items={[
+ {
+ title: "Transfer ownership",
+ description:
+ "Transfer this project to another organization where you have the ability to create projects.",
+ button: <TransferProjectButton />,
+ },
+ {
+ title: "Delete this project",
+ description:
+ "Once you delete a project, there is no going back. Please be certain.",
+ button: <DeleteProjectButton />,
+ },
+ ]}
+ />
</div>
),
},