refactor(api): extract system role config procedures
This commit is contained in:
@@ -0,0 +1,50 @@
|
|||||||
|
import { createAuditEntry } from "../lib/audit.js";
|
||||||
|
import type { TRPCContext } from "../trpc.js";
|
||||||
|
import { invalidateRoleDefaultsCache } from "../trpc.js";
|
||||||
|
import {
|
||||||
|
buildSystemRoleConfigUpdateData,
|
||||||
|
systemRoleConfigUpdateInputSchema,
|
||||||
|
type SystemRoleConfigUpdateInput,
|
||||||
|
} from "./system-role-config-support.js";
|
||||||
|
|
||||||
|
type SystemRoleConfigProcedureContext = Pick<TRPCContext, "db" | "dbUser">;
|
||||||
|
|
||||||
|
export { systemRoleConfigUpdateInputSchema };
|
||||||
|
|
||||||
|
export async function listSystemRoleConfigs(
|
||||||
|
ctx: SystemRoleConfigProcedureContext,
|
||||||
|
) {
|
||||||
|
return ctx.db.systemRoleConfig.findMany({
|
||||||
|
orderBy: { sortOrder: "asc" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateSystemRoleConfig(
|
||||||
|
ctx: SystemRoleConfigProcedureContext,
|
||||||
|
input: SystemRoleConfigUpdateInput,
|
||||||
|
) {
|
||||||
|
const existing = await ctx.db.systemRoleConfig.findUnique({
|
||||||
|
where: { role: input.role as never },
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await ctx.db.systemRoleConfig.update({
|
||||||
|
where: { role: input.role as never },
|
||||||
|
data: buildSystemRoleConfigUpdateData(input),
|
||||||
|
});
|
||||||
|
|
||||||
|
invalidateRoleDefaultsCache();
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "SystemRoleConfig",
|
||||||
|
entityId: input.role,
|
||||||
|
entityName: result.label,
|
||||||
|
action: "UPDATE",
|
||||||
|
userId: ctx.dbUser?.id,
|
||||||
|
before: (existing ?? {}) as unknown as Record<string, unknown>,
|
||||||
|
after: result as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
@@ -1,46 +1,16 @@
|
|||||||
import { adminProcedure, createTRPCRouter, invalidateRoleDefaultsCache } from "../trpc.js";
|
import { adminProcedure, createTRPCRouter } from "../trpc.js";
|
||||||
import { createAuditEntry } from "../lib/audit.js";
|
|
||||||
import {
|
import {
|
||||||
buildSystemRoleConfigUpdateData,
|
listSystemRoleConfigs,
|
||||||
systemRoleConfigUpdateInputSchema,
|
systemRoleConfigUpdateInputSchema,
|
||||||
} from "./system-role-config-support.js";
|
updateSystemRoleConfig,
|
||||||
|
} from "./system-role-config-procedure-support.js";
|
||||||
|
|
||||||
export const systemRoleConfigRouter = createTRPCRouter({
|
export const systemRoleConfigRouter = createTRPCRouter({
|
||||||
/** List all role configs (sorted by sortOrder) */
|
/** List all role configs (sorted by sortOrder) */
|
||||||
list: adminProcedure.query(async ({ ctx }) => {
|
list: adminProcedure.query(({ ctx }) => listSystemRoleConfigs(ctx)),
|
||||||
return ctx.db.systemRoleConfig.findMany({
|
|
||||||
orderBy: { sortOrder: "asc" },
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
|
|
||||||
/** Update a role's default permissions, label, description, and color */
|
/** Update a role's default permissions, label, description, and color */
|
||||||
update: adminProcedure
|
update: adminProcedure
|
||||||
.input(systemRoleConfigUpdateInputSchema)
|
.input(systemRoleConfigUpdateInputSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => updateSystemRoleConfig(ctx, input)),
|
||||||
const existing = await ctx.db.systemRoleConfig.findUnique({
|
|
||||||
where: { role: input.role as never },
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await ctx.db.systemRoleConfig.update({
|
|
||||||
where: { role: input.role as never },
|
|
||||||
data: buildSystemRoleConfigUpdateData(input),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Invalidate cached role defaults so changes take effect immediately
|
|
||||||
invalidateRoleDefaultsCache();
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "SystemRoleConfig",
|
|
||||||
entityId: input.role,
|
|
||||||
entityName: result.label,
|
|
||||||
action: "UPDATE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
before: (existing ?? {}) as unknown as Record<string, unknown>,
|
|
||||||
after: result as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user