ad0855902b
Phase 1 — Quick Wins: centralize formatMoney/formatCents, extract findUniqueOrThrow helper (19 routers), shared Prisma select constants, useInvalidatePlanningViews hook, status badge consolidation, composite DB indexes. Phase 2 — Timeline Split: extract TimelineContext, TimelineResourcePanel, TimelineProjectPanel; split 28-dep useMemo into 3 focused memos. TimelineView.tsx reduced from 1,903 to 538 lines. Phase 3 — Query Performance: server-side filtering for getEntriesView, remove availability from timeline resource select, SSE event debouncing (50ms batch window). Phase 4 — Estimate Workspace: extract 7 tab components and 3 editor components. EstimateWorkspaceClient 1,298→306 lines, EstimateWorkspaceDraftEditor 1,205→581 lines. Phase 5 — Package Cleanup: split commit-dispo-import-batch (1,112→573 lines), extract shared pagination helper with 11 tests. All tests pass: 209 API, 254 engine, 67 application. Co-Authored-By: claude-flow <ruv@ruv.net>
145 lines
5.2 KiB
TypeScript
145 lines
5.2 KiB
TypeScript
import {
|
|
CreateManagementLevelGroupSchema,
|
|
CreateManagementLevelSchema,
|
|
UpdateManagementLevelGroupSchema,
|
|
UpdateManagementLevelSchema,
|
|
} from "@planarchy/shared";
|
|
import { TRPCError } from "@trpc/server";
|
|
import { z } from "zod";
|
|
import { findUniqueOrThrow } from "../db/helpers.js";
|
|
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 findUniqueOrThrow(
|
|
ctx.db.managementLevelGroup.findUnique({
|
|
where: { id: input.id },
|
|
include: {
|
|
levels: { orderBy: { name: "asc" } },
|
|
_count: { select: { resources: true } },
|
|
},
|
|
}),
|
|
"Management level group",
|
|
);
|
|
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 findUniqueOrThrow(
|
|
ctx.db.managementLevelGroup.findUnique({ where: { id: input.id } }),
|
|
"Group",
|
|
);
|
|
|
|
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 }) => {
|
|
await findUniqueOrThrow(
|
|
ctx.db.managementLevelGroup.findUnique({ where: { id: input.groupId } }),
|
|
"Group",
|
|
);
|
|
|
|
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 findUniqueOrThrow(
|
|
ctx.db.managementLevel.findUnique({ where: { id: input.id } }),
|
|
"Level",
|
|
);
|
|
|
|
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 findUniqueOrThrow(
|
|
ctx.db.managementLevel.findUnique({
|
|
where: { id: input.id },
|
|
include: { _count: { select: { resources: true } } },
|
|
}),
|
|
"Level",
|
|
);
|
|
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 };
|
|
}),
|
|
});
|