Files
Nexus/packages/api/src/router/project-planning-read-model.ts
T
Hartmut ad0855902b 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>
2026-03-14 23:03:42 +01:00

113 lines
2.8 KiB
TypeScript

import { buildSplitAllocationReadModel } from "@planarchy/application";
import type { PrismaClient } from "@planarchy/db";
import { AllocationStatus } from "@planarchy/shared";
export const PROJECT_PLANNING_ALLOCATION_INCLUDE = {
resource: {
select: {
id: true,
displayName: true,
eid: true,
chapter: true,
lcrCents: true,
availability: true,
},
},
project: {
select: {
id: true,
name: true,
shortCode: true,
orderType: true,
budgetCents: true,
winProbability: true,
status: true,
startDate: true,
endDate: true,
staffingReqs: true,
responsiblePerson: true,
},
},
roleEntity: {
select: { id: true, name: true, color: true },
},
} as const;
export const PROJECT_PLANNING_DEMAND_INCLUDE = {
project: PROJECT_PLANNING_ALLOCATION_INCLUDE.project,
roleEntity: PROJECT_PLANNING_ALLOCATION_INCLUDE.roleEntity,
} as const;
export const PROJECT_PLANNING_ASSIGNMENT_INCLUDE = {
resource: PROJECT_PLANNING_ALLOCATION_INCLUDE.resource,
project: PROJECT_PLANNING_ALLOCATION_INCLUDE.project,
roleEntity: PROJECT_PLANNING_ALLOCATION_INCLUDE.roleEntity,
} as const;
/**
* Lighter resource select for timeline rendering (hot path).
* Omits `availability` which is only needed for budget/cost calculations.
*/
const TIMELINE_RESOURCE_SELECT = {
select: {
id: true,
displayName: true,
eid: true,
chapter: true,
lcrCents: true,
},
} as const;
export const TIMELINE_ASSIGNMENT_INCLUDE = {
resource: TIMELINE_RESOURCE_SELECT,
project: PROJECT_PLANNING_ALLOCATION_INCLUDE.project,
roleEntity: PROJECT_PLANNING_ALLOCATION_INCLUDE.roleEntity,
} as const;
type ProjectPlanningReadDbClient = Pick<
PrismaClient,
"demandRequirement" | "assignment"
>;
export interface LoadProjectPlanningReadModelInput {
projectId: string;
activeOnly?: boolean;
}
export async function loadProjectPlanningReadModel(
db: ProjectPlanningReadDbClient,
input: LoadProjectPlanningReadModelInput,
) {
const statusFilter = input.activeOnly
? { status: { not: AllocationStatus.CANCELLED } }
: {};
const [demandRequirements, assignments] = await Promise.all([
db.demandRequirement.findMany({
where: {
projectId: input.projectId,
...statusFilter,
},
include: PROJECT_PLANNING_DEMAND_INCLUDE,
orderBy: [{ startDate: "asc" }, { projectId: "asc" }],
}),
db.assignment.findMany({
where: {
projectId: input.projectId,
...statusFilter,
},
include: PROJECT_PLANNING_ASSIGNMENT_INCLUDE,
orderBy: [{ startDate: "asc" }, { resourceId: "asc" }],
}),
]);
return {
demandRequirements,
assignments,
readModel: buildSplitAllocationReadModel({
demandRequirements,
assignments,
}),
};
}