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:
@@ -22,6 +22,7 @@ import {
|
||||
import {
|
||||
ApproveEstimateVersionSchema,
|
||||
CloneEstimateSchema,
|
||||
CommercialTermsSchema,
|
||||
CreateEstimateExportSchema,
|
||||
CreateEstimatePlanningHandoffSchema,
|
||||
CreateEstimateSchema,
|
||||
@@ -30,10 +31,12 @@ import {
|
||||
GenerateWeeklyPhasingSchema,
|
||||
PermissionKey,
|
||||
SubmitEstimateVersionSchema,
|
||||
UpdateCommercialTermsSchema,
|
||||
UpdateEstimateDraftSchema,
|
||||
} from "@planarchy/shared";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
import { findUniqueOrThrow } from "../db/helpers.js";
|
||||
import {
|
||||
controllerProcedure,
|
||||
createTRPCRouter,
|
||||
@@ -151,15 +154,14 @@ export const estimateRouter = createTRPCRouter({
|
||||
getById: controllerProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const estimate = await getEstimateById(
|
||||
ctx.db as unknown as Parameters<typeof getEstimateById>[0],
|
||||
input.id,
|
||||
const estimate = await findUniqueOrThrow(
|
||||
getEstimateById(
|
||||
ctx.db as unknown as Parameters<typeof getEstimateById>[0],
|
||||
input.id,
|
||||
),
|
||||
"Estimate",
|
||||
);
|
||||
|
||||
if (!estimate) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Estimate not found" });
|
||||
}
|
||||
|
||||
return estimate;
|
||||
}),
|
||||
|
||||
@@ -169,14 +171,13 @@ export const estimateRouter = createTRPCRouter({
|
||||
requirePermission(ctx, PermissionKey.MANAGE_PROJECTS);
|
||||
|
||||
if (input.projectId) {
|
||||
const project = await ctx.db.project.findUnique({
|
||||
where: { id: input.projectId },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Project not found" });
|
||||
}
|
||||
await findUniqueOrThrow(
|
||||
ctx.db.project.findUnique({
|
||||
where: { id: input.projectId },
|
||||
select: { id: true },
|
||||
}),
|
||||
"Project",
|
||||
);
|
||||
}
|
||||
|
||||
const estimate = await createEstimate(
|
||||
@@ -253,14 +254,13 @@ export const estimateRouter = createTRPCRouter({
|
||||
requirePermission(ctx, PermissionKey.MANAGE_PROJECTS);
|
||||
|
||||
if (input.projectId) {
|
||||
const project = await ctx.db.project.findUnique({
|
||||
where: { id: input.projectId },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Project not found" });
|
||||
}
|
||||
await findUniqueOrThrow(
|
||||
ctx.db.project.findUnique({
|
||||
where: { id: input.projectId },
|
||||
select: { id: true },
|
||||
}),
|
||||
"Project",
|
||||
);
|
||||
}
|
||||
|
||||
let estimate;
|
||||
@@ -592,15 +592,14 @@ export const estimateRouter = createTRPCRouter({
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
requirePermission(ctx, PermissionKey.MANAGE_PROJECTS);
|
||||
|
||||
const estimate = await getEstimateById(
|
||||
ctx.db as unknown as Parameters<typeof getEstimateById>[0],
|
||||
input.estimateId,
|
||||
const estimate = await findUniqueOrThrow(
|
||||
getEstimateById(
|
||||
ctx.db as unknown as Parameters<typeof getEstimateById>[0],
|
||||
input.estimateId,
|
||||
),
|
||||
"Estimate",
|
||||
);
|
||||
|
||||
if (!estimate) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Estimate not found" });
|
||||
}
|
||||
|
||||
const workingVersion = estimate.versions.find(
|
||||
(v) => v.status === "WORKING",
|
||||
);
|
||||
@@ -668,15 +667,14 @@ export const estimateRouter = createTRPCRouter({
|
||||
getWeeklyPhasing: controllerProcedure
|
||||
.input(z.object({ estimateId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const estimate = await getEstimateById(
|
||||
ctx.db as unknown as Parameters<typeof getEstimateById>[0],
|
||||
input.estimateId,
|
||||
const estimate = await findUniqueOrThrow(
|
||||
getEstimateById(
|
||||
ctx.db as unknown as Parameters<typeof getEstimateById>[0],
|
||||
input.estimateId,
|
||||
),
|
||||
"Estimate",
|
||||
);
|
||||
|
||||
if (!estimate) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Estimate not found" });
|
||||
}
|
||||
|
||||
// Get the latest version (first in the sorted array)
|
||||
const version = estimate.versions[0];
|
||||
|
||||
@@ -754,4 +752,89 @@ export const estimateRouter = createTRPCRouter({
|
||||
chapterAggregation,
|
||||
};
|
||||
}),
|
||||
|
||||
// ─── Commercial Terms ───────────────────────────────────────────────────
|
||||
|
||||
getCommercialTerms: controllerProcedure
|
||||
.input(z.object({ estimateId: z.string(), versionId: z.string().optional() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const estimate = await ctx.db.estimate.findUnique({
|
||||
where: { id: input.estimateId },
|
||||
include: {
|
||||
versions: {
|
||||
...(input.versionId
|
||||
? { where: { id: input.versionId } }
|
||||
: { orderBy: { versionNumber: "desc" as const }, take: 1 }),
|
||||
select: { id: true, commercialTerms: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!estimate || estimate.versions.length === 0) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Estimate version not found" });
|
||||
}
|
||||
|
||||
const version = estimate.versions[0]!;
|
||||
const raw = version.commercialTerms;
|
||||
|
||||
// Parse stored JSON through Zod for type safety, fall back to defaults
|
||||
const terms = raw
|
||||
? CommercialTermsSchema.parse(raw)
|
||||
: CommercialTermsSchema.parse({});
|
||||
|
||||
return { versionId: version.id, terms };
|
||||
}),
|
||||
|
||||
updateCommercialTerms: managerProcedure
|
||||
.input(UpdateCommercialTermsSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
requirePermission(ctx, PermissionKey.MANAGE_PROJECTS);
|
||||
|
||||
const estimate = await ctx.db.estimate.findUnique({
|
||||
where: { id: input.estimateId },
|
||||
include: {
|
||||
versions: {
|
||||
...(input.versionId
|
||||
? { where: { id: input.versionId } }
|
||||
: { where: { status: "WORKING" }, take: 1 }),
|
||||
select: { id: true, status: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!estimate || estimate.versions.length === 0) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Estimate version not found" });
|
||||
}
|
||||
|
||||
const version = estimate.versions[0]!;
|
||||
|
||||
if (version.status !== "WORKING") {
|
||||
throw new TRPCError({
|
||||
code: "PRECONDITION_FAILED",
|
||||
message: "Commercial terms can only be edited on working versions",
|
||||
});
|
||||
}
|
||||
|
||||
const validated = CommercialTermsSchema.parse(input.terms);
|
||||
|
||||
await ctx.db.estimateVersion.update({
|
||||
where: { id: version.id },
|
||||
data: { commercialTerms: validated as unknown as Prisma.InputJsonValue },
|
||||
});
|
||||
|
||||
await ctx.db.auditLog.create({
|
||||
data: {
|
||||
entityType: "Estimate",
|
||||
entityId: estimate.id,
|
||||
action: "UPDATE",
|
||||
...(ctx.dbUser?.id ? { userId: ctx.dbUser.id } : {}),
|
||||
changes: {
|
||||
field: "commercialTerms",
|
||||
after: validated,
|
||||
} as Prisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
|
||||
return { versionId: version.id, terms: validated };
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user