chore(repo): initialize planarchy workspace
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
import {
|
||||
CreateManagementLevelGroupSchema,
|
||||
CreateManagementLevelSchema,
|
||||
UpdateManagementLevelGroupSchema,
|
||||
UpdateManagementLevelSchema,
|
||||
} from "@planarchy/shared";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
import { adminProcedure, createTRPCRouter, protectedProcedure } from "../trpc.js";
|
||||
|
||||
export const managementLevelRouter = createTRPCRouter({
|
||||
// ─── Groups ─────────────────────────────────────────────
|
||||
|
||||
listGroups: protectedProcedure.query(async ({ ctx }) => {
|
||||
return ctx.db.managementLevelGroup.findMany({
|
||||
include: { levels: { orderBy: { name: "asc" } } },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
}),
|
||||
|
||||
getGroupById: protectedProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const group = await ctx.db.managementLevelGroup.findUnique({
|
||||
where: { id: input.id },
|
||||
include: {
|
||||
levels: { orderBy: { name: "asc" } },
|
||||
_count: { select: { resources: true } },
|
||||
},
|
||||
});
|
||||
if (!group) throw new TRPCError({ code: "NOT_FOUND", message: "Management level group not found" });
|
||||
return group;
|
||||
}),
|
||||
|
||||
createGroup: adminProcedure
|
||||
.input(CreateManagementLevelGroupSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const existing = await ctx.db.managementLevelGroup.findUnique({ where: { name: input.name } });
|
||||
if (existing) {
|
||||
throw new TRPCError({ code: "CONFLICT", message: `Group "${input.name}" already exists` });
|
||||
}
|
||||
return ctx.db.managementLevelGroup.create({
|
||||
data: {
|
||||
name: input.name,
|
||||
targetPercentage: input.targetPercentage,
|
||||
sortOrder: input.sortOrder,
|
||||
},
|
||||
include: { levels: true },
|
||||
});
|
||||
}),
|
||||
|
||||
updateGroup: adminProcedure
|
||||
.input(z.object({ id: z.string(), data: UpdateManagementLevelGroupSchema }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const existing = await ctx.db.managementLevelGroup.findUnique({ where: { id: input.id } });
|
||||
if (!existing) throw new TRPCError({ code: "NOT_FOUND", message: "Group not found" });
|
||||
|
||||
if (input.data.name && input.data.name !== existing.name) {
|
||||
const conflict = await ctx.db.managementLevelGroup.findUnique({ where: { name: input.data.name } });
|
||||
if (conflict) {
|
||||
throw new TRPCError({ code: "CONFLICT", message: `Group "${input.data.name}" already exists` });
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.db.managementLevelGroup.update({
|
||||
where: { id: input.id },
|
||||
data: {
|
||||
...(input.data.name !== undefined ? { name: input.data.name } : {}),
|
||||
...(input.data.targetPercentage !== undefined ? { targetPercentage: input.data.targetPercentage } : {}),
|
||||
...(input.data.sortOrder !== undefined ? { sortOrder: input.data.sortOrder } : {}),
|
||||
},
|
||||
include: { levels: true },
|
||||
});
|
||||
}),
|
||||
|
||||
// ─── Levels ─────────────────────────────────────────────
|
||||
|
||||
createLevel: adminProcedure
|
||||
.input(CreateManagementLevelSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const group = await ctx.db.managementLevelGroup.findUnique({ where: { id: input.groupId } });
|
||||
if (!group) throw new TRPCError({ code: "NOT_FOUND", message: "Group not found" });
|
||||
|
||||
const existing = await ctx.db.managementLevel.findUnique({ where: { name: input.name } });
|
||||
if (existing) {
|
||||
throw new TRPCError({ code: "CONFLICT", message: `Level "${input.name}" already exists` });
|
||||
}
|
||||
|
||||
return ctx.db.managementLevel.create({
|
||||
data: { name: input.name, groupId: input.groupId },
|
||||
});
|
||||
}),
|
||||
|
||||
updateLevel: adminProcedure
|
||||
.input(z.object({ id: z.string(), data: UpdateManagementLevelSchema }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const existing = await ctx.db.managementLevel.findUnique({ where: { id: input.id } });
|
||||
if (!existing) throw new TRPCError({ code: "NOT_FOUND", message: "Level not found" });
|
||||
|
||||
if (input.data.name && input.data.name !== existing.name) {
|
||||
const conflict = await ctx.db.managementLevel.findUnique({ where: { name: input.data.name } });
|
||||
if (conflict) {
|
||||
throw new TRPCError({ code: "CONFLICT", message: `Level "${input.data.name}" already exists` });
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.db.managementLevel.update({
|
||||
where: { id: input.id },
|
||||
data: {
|
||||
...(input.data.name !== undefined ? { name: input.data.name } : {}),
|
||||
...(input.data.groupId !== undefined ? { groupId: input.data.groupId } : {}),
|
||||
},
|
||||
});
|
||||
}),
|
||||
|
||||
deleteLevel: adminProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const level = await ctx.db.managementLevel.findUnique({
|
||||
where: { id: input.id },
|
||||
include: { _count: { select: { resources: true } } },
|
||||
});
|
||||
if (!level) throw new TRPCError({ code: "NOT_FOUND", message: "Level not found" });
|
||||
if (level._count.resources > 0) {
|
||||
throw new TRPCError({
|
||||
code: "PRECONDITION_FAILED",
|
||||
message: `Cannot delete level assigned to ${level._count.resources} resource(s)`,
|
||||
});
|
||||
}
|
||||
await ctx.db.managementLevel.delete({ where: { id: input.id } });
|
||||
return { success: true };
|
||||
}),
|
||||
});
|
||||
Reference in New Issue
Block a user