From 13eccaf8d9bb0562373f3ee3fb377a4480b391e0 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sat, 1 Mar 2025 22:14:12 -0600 Subject: [PATCH] feat(user): add organization count check before user deletion --- .../dashboard/settings/users/show-users.tsx | 30 +++++++++++++++++++ apps/dokploy/server/api/routers/user.ts | 14 +++++++++ 2 files changed, 44 insertions(+) diff --git a/apps/dokploy/components/dashboard/settings/users/show-users.tsx b/apps/dokploy/components/dashboard/settings/users/show-users.tsx index 6847558b..9580240d 100644 --- a/apps/dokploy/components/dashboard/settings/users/show-users.tsx +++ b/apps/dokploy/components/dashboard/settings/users/show-users.tsx @@ -36,6 +36,7 @@ export const ShowUsers = () => { const { data: isCloud } = api.settings.isCloud.useQuery(); const { data, isLoading, refetch } = api.user.all.useQuery(); const { mutateAsync } = api.user.remove.useMutation(); + const utils = api.useUtils(); return (
@@ -172,6 +173,35 @@ export const ShowUsers = () => { description="Are you sure you want to unlink this user?" type="destructive" onClick={async () => { + if (!isCloud) { + const orgCount = + await utils.user.checkUserOrganizations.fetch( + { + userId: member.user.id, + }, + ); + + console.log(orgCount); + + if (orgCount === 1) { + await mutateAsync({ + userId: member.user.id, + }) + .then(() => { + toast.success( + "User deleted successfully", + ); + refetch(); + }) + .catch(() => { + toast.error( + "Error deleting user", + ); + }); + return; + } + } + const { error } = await authClient.organization.removeMember( { diff --git a/apps/dokploy/server/api/routers/user.ts b/apps/dokploy/server/api/routers/user.ts index 5a84742a..0b740ab7 100644 --- a/apps/dokploy/server/api/routers/user.ts +++ b/apps/dokploy/server/api/routers/user.ts @@ -313,4 +313,18 @@ export const userRouter = createTRPCRouter({ const apiKey = await createApiKey(ctx.user.id, input); return apiKey; }), + + checkUserOrganizations: protectedProcedure + .input( + z.object({ + userId: z.string(), + }), + ) + .query(async ({ input }) => { + const organizations = await db.query.member.findMany({ + where: eq(member.userId, input.userId), + }); + + return organizations.length; + }), });