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;
+ }),
});