refactor(api): extract timeline project load support

This commit is contained in:
2026-03-31 17:32:43 +02:00
parent acb4ec5243
commit 1bdae5816f
4 changed files with 263 additions and 90 deletions
@@ -4,10 +4,13 @@ import { ShiftProjectSchema } from "@capakraken/shared";
import { z } from "zod";
import { getAnonymizationDirectory } from "../lib/anonymization.js";
import { controllerProcedure } from "../trpc.js";
import { loadProjectPlanningReadModel } from "./project-planning-read-model.js";
import {
loadTimelineProjectContextDetailArtifacts,
} from "./timeline-project-context-support.js";
import {
loadTimelineProjectContext,
previewTimelineProjectShift,
} from "./timeline-project-load-support.js";
import {
buildTimelineProjectContextDetailResponse,
buildTimelineProjectContextResponse,
@@ -16,100 +19,12 @@ import {
buildTimelineBudgetStatusAllocations,
buildTimelineBudgetStatusResponse,
buildTimelineShiftPreviewDetailResponse,
buildTimelineShiftValidationBookings,
} from "./timeline-project-read-support.js";
import {
findTimelineProjectOrThrow,
projectShiftContextSelect,
timelineBudgetStatusProjectSelect,
timelineProjectContextSelect,
timelineShiftPreviewProjectSelect,
} from "./timeline-project-query-support.js";
import { buildTimelineShiftPlan } from "./timeline-shift-planning.js";
import { getAssignmentResourceIds, ShiftDbClient } from "./timeline-read-shared.js";
import { buildTimelineProjectShiftValidation } from "./timeline-shift-support.js";
export async function loadProjectShiftContext(db: ShiftDbClient, projectId: string) {
const [project, planningRead] = await Promise.all([
findTimelineProjectOrThrow(db, {
projectId,
select: projectShiftContextSelect,
}),
loadProjectPlanningReadModel(db, { projectId, activeOnly: true }),
]);
const { demandRequirements, assignments, readModel: projectReadModel } = planningRead;
const resourceIds = getAssignmentResourceIds(projectReadModel);
const allAssignmentWindows =
resourceIds.length === 0
? []
: buildTimelineShiftValidationBookings(
await listAssignmentBookings(db, {
resourceIds,
}),
);
const shiftPlan = buildTimelineShiftPlan({
demandRequirements,
assignments,
allAssignmentWindows,
});
return {
project,
demandRequirements,
assignments,
shiftPlan,
};
}
export async function loadTimelineProjectContext(db: ShiftDbClient, projectId: string) {
const [project, planningRead] = await Promise.all([
findTimelineProjectOrThrow(db, {
projectId,
select: timelineProjectContextSelect,
}),
loadProjectPlanningReadModel(db, {
projectId,
activeOnly: true,
}),
]);
const resourceIds = getAssignmentResourceIds(planningRead.readModel);
const allResourceAllocations =
resourceIds.length === 0
? []
: await listAssignmentBookings(db, {
resourceIds,
});
return {
project,
allocations: planningRead.readModel.allocations,
demands: planningRead.readModel.demands,
assignments: planningRead.readModel.assignments,
allResourceAllocations,
resourceIds,
};
}
export async function previewTimelineProjectShift(
db: ShiftDbClient,
input: {
projectId: string;
newStartDate: Date;
newEndDate: Date;
},
) {
const context = await loadProjectShiftContext(db, input.projectId);
return buildTimelineProjectShiftValidation({
context,
newStartDate: input.newStartDate,
newEndDate: input.newEndDate,
});
}
export const timelineProjectReadProcedures = {
getProjectContext: controllerProcedure