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
@@ -10,6 +10,7 @@ import {
} from "@planarchy/shared";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { findUniqueOrThrow } from "../db/helpers.js";
import { createTRPCRouter, controllerProcedure, managerProcedure } from "../trpc.js";
const ruleInclude = {
@@ -51,11 +52,13 @@ export const experienceMultiplierRouter = createTRPCRouter({
getById: controllerProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const set = await ctx.db.experienceMultiplierSet.findUnique({
where: { id: input.id },
include: ruleInclude,
});
if (!set) throw new TRPCError({ code: "NOT_FOUND", message: "Experience multiplier set not found" });
const set = await findUniqueOrThrow(
ctx.db.experienceMultiplierSet.findUnique({
where: { id: input.id },
include: ruleInclude,
}),
"Experience multiplier set",
);
return set;
}),
@@ -95,8 +98,10 @@ export const experienceMultiplierRouter = createTRPCRouter({
update: managerProcedure
.input(UpdateExperienceMultiplierSetSchema)
.mutation(async ({ ctx, input }) => {
const existing = await ctx.db.experienceMultiplierSet.findUnique({ where: { id: input.id } });
if (!existing) throw new TRPCError({ code: "NOT_FOUND", message: "Experience multiplier set not found" });
await findUniqueOrThrow(
ctx.db.experienceMultiplierSet.findUnique({ where: { id: input.id } }),
"Experience multiplier set",
);
if (input.isDefault) {
await ctx.db.experienceMultiplierSet.updateMany({
@@ -137,8 +142,10 @@ export const experienceMultiplierRouter = createTRPCRouter({
delete: managerProcedure
.input(z.object({ id: z.string() }))
.mutation(async ({ ctx, input }) => {
const existing = await ctx.db.experienceMultiplierSet.findUnique({ where: { id: input.id } });
if (!existing) throw new TRPCError({ code: "NOT_FOUND", message: "Experience multiplier set not found" });
await findUniqueOrThrow(
ctx.db.experienceMultiplierSet.findUnique({ where: { id: input.id } }),
"Experience multiplier set",
);
await ctx.db.experienceMultiplierSet.delete({ where: { id: input.id } });
return { id: input.id };
}),
@@ -151,25 +158,28 @@ export const experienceMultiplierRouter = createTRPCRouter({
}))
.query(async ({ ctx, input }) => {
const [estimate, multiplierSet] = await Promise.all([
ctx.db.estimate.findUnique({
where: { id: input.estimateId },
include: {
versions: {
orderBy: { versionNumber: "desc" },
take: 1,
include: { demandLines: { orderBy: { createdAt: "asc" } } },
findUniqueOrThrow(
ctx.db.estimate.findUnique({
where: { id: input.estimateId },
include: {
versions: {
orderBy: { versionNumber: "desc" },
take: 1,
include: { demandLines: { orderBy: { createdAt: "asc" } } },
},
},
},
}),
ctx.db.experienceMultiplierSet.findUnique({
where: { id: input.multiplierSetId },
include: ruleInclude,
}),
}),
"Estimate",
),
findUniqueOrThrow(
ctx.db.experienceMultiplierSet.findUnique({
where: { id: input.multiplierSetId },
include: ruleInclude,
}),
"Experience multiplier set",
),
]);
if (!estimate) throw new TRPCError({ code: "NOT_FOUND", message: "Estimate not found" });
if (!multiplierSet) throw new TRPCError({ code: "NOT_FOUND", message: "Experience multiplier set not found" });
const version = estimate.versions[0];
if (!version) throw new TRPCError({ code: "NOT_FOUND", message: "Estimate has no versions" });
@@ -227,29 +237,32 @@ export const experienceMultiplierRouter = createTRPCRouter({
}),
/** Apply multipliers to demand lines on the working version */
apply: managerProcedure
applyRules: managerProcedure
.input(ApplyExperienceMultipliersSchema)
.mutation(async ({ ctx, input }) => {
const [estimate, multiplierSet] = await Promise.all([
ctx.db.estimate.findUnique({
where: { id: input.estimateId },
include: {
versions: {
orderBy: { versionNumber: "desc" },
take: 1,
include: { demandLines: true },
findUniqueOrThrow(
ctx.db.estimate.findUnique({
where: { id: input.estimateId },
include: {
versions: {
orderBy: { versionNumber: "desc" },
take: 1,
include: { demandLines: true },
},
},
},
}),
ctx.db.experienceMultiplierSet.findUnique({
where: { id: input.multiplierSetId },
include: ruleInclude,
}),
}),
"Estimate",
),
findUniqueOrThrow(
ctx.db.experienceMultiplierSet.findUnique({
where: { id: input.multiplierSetId },
include: ruleInclude,
}),
"Experience multiplier set",
),
]);
if (!estimate) throw new TRPCError({ code: "NOT_FOUND", message: "Estimate not found" });
if (!multiplierSet) throw new TRPCError({ code: "NOT_FOUND", message: "Experience multiplier set not found" });
const version = estimate.versions[0];
if (!version) throw new TRPCError({ code: "NOT_FOUND", message: "Estimate has no versions" });
if (version.status !== "WORKING") {