refactor: complete v2 refactoring plan (Phases 1-5)

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>
This commit is contained in:
2026-03-14 23:03:42 +01:00
parent 4dabb9d4ce
commit ad0855902b
65 changed files with 7108 additions and 4740 deletions
+20 -13
View File
@@ -1,6 +1,7 @@
import { CreateClientSchema, UpdateClientSchema } from "@planarchy/shared";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { findUniqueOrThrow } from "../db/helpers.js";
import { createTRPCRouter, managerProcedure, protectedProcedure } from "../trpc.js";
import type { ClientTree } from "@planarchy/shared";
@@ -64,15 +65,17 @@ export const clientRouter = createTRPCRouter({
getById: protectedProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const client = await ctx.db.client.findUnique({
where: { id: input.id },
include: {
parent: true,
children: { orderBy: { sortOrder: "asc" } },
_count: { select: { projects: true, children: true } },
},
});
if (!client) throw new TRPCError({ code: "NOT_FOUND", message: "Client not found" });
const client = await findUniqueOrThrow(
ctx.db.client.findUnique({
where: { id: input.id },
include: {
parent: true,
children: { orderBy: { sortOrder: "asc" } },
_count: { select: { projects: true, children: true } },
},
}),
"Client",
);
return client;
}),
@@ -80,8 +83,10 @@ export const clientRouter = createTRPCRouter({
.input(CreateClientSchema)
.mutation(async ({ ctx, input }) => {
if (input.parentId) {
const parent = await ctx.db.client.findUnique({ where: { id: input.parentId } });
if (!parent) throw new TRPCError({ code: "NOT_FOUND", message: "Parent client not found" });
await findUniqueOrThrow(
ctx.db.client.findUnique({ where: { id: input.parentId } }),
"Parent client",
);
}
if (input.code) {
@@ -104,8 +109,10 @@ export const clientRouter = createTRPCRouter({
update: managerProcedure
.input(z.object({ id: z.string(), data: UpdateClientSchema }))
.mutation(async ({ ctx, input }) => {
const existing = await ctx.db.client.findUnique({ where: { id: input.id } });
if (!existing) throw new TRPCError({ code: "NOT_FOUND", message: "Client not found" });
const existing = await findUniqueOrThrow(
ctx.db.client.findUnique({ where: { id: input.id } }),
"Client",
);
if (input.data.code && input.data.code !== existing.code) {
const conflict = await ctx.db.client.findUnique({ where: { code: input.data.code } });