refactor: update custom role handling in API

- Replaced the delete operation with an update for organization roles, ensuring existing roles are modified instead of removed.
- Adjusted the return value to reflect the updated role instead of a newly created entry.
- Reintroduced the audit logging functionality for role updates.
This commit is contained in:
Mauricio Siu
2026-03-15 23:33:20 -06:00
parent 5ffd664570
commit 1e7a6f2071
@@ -1,7 +1,6 @@
import { db } from "@dokploy/server/db"; import { db } from "@dokploy/server/db";
import { member, organizationRole, user } from "@dokploy/server/db/schema"; import { member, organizationRole, user } from "@dokploy/server/db/schema";
import { statements } from "@dokploy/server/lib/access-control"; import { statements } from "@dokploy/server/lib/access-control";
import { audit } from "../../utils/audit";
import { TRPCError } from "@trpc/server"; import { TRPCError } from "@trpc/server";
import { and, count, eq } from "drizzle-orm"; import { and, count, eq } from "drizzle-orm";
import { z } from "zod"; import { z } from "zod";
@@ -10,6 +9,7 @@ import {
enterpriseProcedure, enterpriseProcedure,
protectedProcedure, protectedProcedure,
} from "../../trpc"; } from "../../trpc";
import { audit } from "../../utils/audit";
const permissionsSchema = z.record(z.string(), z.array(z.string())); const permissionsSchema = z.record(z.string(), z.array(z.string()));
@@ -182,8 +182,12 @@ export const customRoleRouter = createTRPCRouter({
validatePermissions(input.permissions); validatePermissions(input.permissions);
await db const [updated] = await db
.delete(organizationRole) .update(organizationRole)
.set({
role: effectiveRoleName,
permission: JSON.stringify(input.permissions),
})
.where( .where(
and( and(
eq( eq(
@@ -192,15 +196,7 @@ export const customRoleRouter = createTRPCRouter({
), ),
eq(organizationRole.role, input.roleName), eq(organizationRole.role, input.roleName),
), ),
); )
const [created] = await db
.insert(organizationRole)
.values({
organizationId: ctx.session.activeOrganizationId,
role: effectiveRoleName,
permission: JSON.stringify(input.permissions),
})
.returning(); .returning();
await audit(ctx, { await audit(ctx, {
@@ -208,7 +204,7 @@ export const customRoleRouter = createTRPCRouter({
resourceType: "customRole", resourceType: "customRole",
resourceName: effectiveRoleName, resourceName: effectiveRoleName,
}); });
return created; return updated;
}), }),
remove: enterpriseProcedure remove: enterpriseProcedure