126 lines
3.3 KiB
TypeScript
126 lines
3.3 KiB
TypeScript
import { CreateRoleSchema } from "@capakraken/shared";
|
|
import { z } from "zod";
|
|
import { findUniqueOrThrow } from "../db/helpers.js";
|
|
import { RESOURCE_BRIEF_SELECT } from "../db/selects.js";
|
|
import {
|
|
createTRPCRouter,
|
|
managerProcedure,
|
|
planningReadProcedure,
|
|
protectedProcedure,
|
|
} from "../trpc.js";
|
|
import {
|
|
createRole,
|
|
deactivateRole,
|
|
deleteRole,
|
|
RoleIdInputSchema,
|
|
UpdateRoleProcedureInputSchema,
|
|
updateRole,
|
|
} from "./role-procedure-support.js";
|
|
import {
|
|
attachRolePlanningEntryCounts,
|
|
attachSingleRolePlanningEntryCount,
|
|
buildRoleListWhere,
|
|
findRoleByIdentifier,
|
|
} from "./role-support.js";
|
|
|
|
export const roleRouter = createTRPCRouter({
|
|
list: planningReadProcedure
|
|
.input(
|
|
z.object({
|
|
isActive: z.boolean().optional(),
|
|
search: z.string().optional(),
|
|
}),
|
|
)
|
|
.query(async ({ ctx, input }) => {
|
|
const roles = await ctx.db.role.findMany({
|
|
where: buildRoleListWhere(input),
|
|
include: {
|
|
_count: {
|
|
select: { resourceRoles: true },
|
|
},
|
|
},
|
|
orderBy: { name: "asc" },
|
|
});
|
|
|
|
return attachRolePlanningEntryCounts(ctx.db, roles);
|
|
}),
|
|
|
|
resolveByIdentifier: protectedProcedure
|
|
.input(z.object({ identifier: z.string().trim().min(1) }))
|
|
.query(async ({ ctx, input }) => {
|
|
const select = {
|
|
id: true,
|
|
name: true,
|
|
color: true,
|
|
isActive: true,
|
|
} as const;
|
|
|
|
return findRoleByIdentifier<{
|
|
id: string;
|
|
name: string;
|
|
color: string | null;
|
|
isActive: boolean;
|
|
}>(ctx.db, input.identifier, select);
|
|
}),
|
|
|
|
getByIdentifier: planningReadProcedure
|
|
.input(z.object({ identifier: z.string() }))
|
|
.query(async ({ ctx, input }) => {
|
|
const select = {
|
|
id: true,
|
|
name: true,
|
|
description: true,
|
|
color: true,
|
|
isActive: true,
|
|
_count: { select: { resourceRoles: true } },
|
|
} as const;
|
|
|
|
const role = await findRoleByIdentifier<{
|
|
id: string;
|
|
name: string;
|
|
description: string | null;
|
|
color: string | null;
|
|
isActive: boolean;
|
|
_count: { resourceRoles: number };
|
|
}>(ctx.db, input.identifier, select);
|
|
return attachSingleRolePlanningEntryCount(ctx.db, role);
|
|
}),
|
|
|
|
getById: planningReadProcedure
|
|
.input(z.object({ id: z.string() }))
|
|
.query(async ({ ctx, input }) => {
|
|
const role = await findUniqueOrThrow(
|
|
ctx.db.role.findUnique({
|
|
where: { id: input.id },
|
|
include: {
|
|
_count: { select: { resourceRoles: true } },
|
|
resourceRoles: {
|
|
include: {
|
|
resource: { select: RESOURCE_BRIEF_SELECT },
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
"Role",
|
|
);
|
|
|
|
return attachSingleRolePlanningEntryCount(ctx.db, role);
|
|
}),
|
|
|
|
create: managerProcedure
|
|
.input(CreateRoleSchema)
|
|
.mutation(({ ctx, input }) => createRole(ctx, input)),
|
|
|
|
update: managerProcedure
|
|
.input(UpdateRoleProcedureInputSchema)
|
|
.mutation(({ ctx, input }) => updateRole(ctx, input)),
|
|
|
|
delete: managerProcedure
|
|
.input(RoleIdInputSchema)
|
|
.mutation(({ ctx, input }) => deleteRole(ctx, input)),
|
|
|
|
deactivate: managerProcedure
|
|
.input(RoleIdInputSchema)
|
|
.mutation(({ ctx, input }) => deactivateRole(ctx, input)),
|
|
});
|