refactor(api): extract timeline project context response support

This commit is contained in:
2026-03-31 17:26:42 +02:00
parent ad4b334f20
commit aa442829b9
5 changed files with 350 additions and 331 deletions
@@ -1,23 +1,17 @@
import { TRPCError } from "@trpc/server";
import {
buildTimelineProjectAssignmentConflicts,
type TimelineProjectAssignmentConflict,
} from "./timeline-project-conflict-support.js";
import {
formatHolidayOverlays,
loadTimelineHolidayOverlays,
summarizeHolidayOverlays,
} from "./timeline-holiday-read.js";
import {
anonymizeResourceOnEntry,
createTimelineDateRange,
fmtDate,
summarizeTimelineEntries,
toDate,
} from "./timeline-read-shared.js";
type TimelineAnonymizationDirectory = Parameters<typeof anonymizeResourceOnEntry>[1];
export interface ResolveTimelineProjectContextPeriodInput {
requestedStartDate?: string | undefined;
requestedEndDate?: string | undefined;
@@ -62,110 +56,6 @@ export function resolveTimelineProjectContextPeriod(
return { startDate, endDate };
}
export function buildTimelineProjectContextSummary(input: {
allocations: Array<{ projectId: string | null; resourceId: string | null }>;
demands: Array<{ projectId: string | null }>;
assignments: Array<{ projectId: string | null; resourceId: string | null }>;
resourceIds: string[];
allResourceAllocations: unknown[];
assignmentConflicts: Array<{ crossProjectOverlapCount: number }>;
holidayOverlays: ReturnType<typeof formatHolidayOverlays>;
}) {
return {
...summarizeTimelineEntries({
allocations: input.allocations,
demands: input.demands,
assignments: input.assignments,
}),
resourceIds: input.resourceIds.length,
allResourceAllocationCount: input.allResourceAllocations.length,
conflictedAssignmentCount: input.assignmentConflicts.filter(
(item) => item.crossProjectOverlapCount > 0,
).length,
...summarizeHolidayOverlays(input.holidayOverlays),
};
}
export function buildTimelineProjectContextResponse<
TProject,
TAllocation extends { resource?: { id: string } | null },
TDemand,
TAssignment extends { resource?: { id: string } | null },
TBooking extends { resource?: { id: string } | null },
>(input: {
project: TProject;
allocations: TAllocation[];
demands: TDemand[];
assignments: TAssignment[];
allResourceAllocations: TBooking[];
resourceIds: string[];
directory: TimelineAnonymizationDirectory;
}) {
return {
project: input.project,
allocations: input.allocations.map((allocation) =>
anonymizeResourceOnEntry(allocation, input.directory),
),
demands: input.demands,
assignments: input.assignments.map((assignment) =>
anonymizeResourceOnEntry(assignment, input.directory),
),
allResourceAllocations: input.allResourceAllocations.map((allocation) =>
anonymizeResourceOnEntry(allocation, input.directory),
),
resourceIds: input.resourceIds,
};
}
export function buildTimelineProjectContextDetailResponse<
TProject,
TAllocation extends { projectId: string | null; resourceId: string | null; resource?: { id: string } | null },
TDemand extends { projectId: string | null },
TAssignment extends { projectId: string | null; resourceId: string | null; resource?: { id: string } | null },
TBooking extends { resource?: { id: string } | null },
TConflict extends { crossProjectOverlapCount: number },
>(input: {
project: TProject;
period: { startDate: Date; endDate: Date };
allocations: TAllocation[];
demands: TDemand[];
assignments: TAssignment[];
allResourceAllocations: TBooking[];
resourceIds: string[];
assignmentConflicts: TConflict[];
holidayOverlays: ReturnType<typeof formatHolidayOverlays>;
directory: TimelineAnonymizationDirectory;
}) {
const base = buildTimelineProjectContextResponse({
project: input.project,
allocations: input.allocations,
demands: input.demands,
assignments: input.assignments,
allResourceAllocations: input.allResourceAllocations,
resourceIds: input.resourceIds,
directory: input.directory,
});
return {
...base,
period: {
startDate: fmtDate(input.period.startDate),
endDate: fmtDate(input.period.endDate),
},
summary: buildTimelineProjectContextSummary({
allocations: input.allocations,
demands: input.demands,
assignments: input.assignments,
resourceIds: input.resourceIds,
allResourceAllocations: input.allResourceAllocations,
assignmentConflicts: input.assignmentConflicts,
holidayOverlays: input.holidayOverlays,
}),
assignmentConflicts: input.assignmentConflicts,
holidayOverlays: input.holidayOverlays,
};
}
export async function loadTimelineProjectContextDetailArtifacts(
db: Parameters<typeof loadTimelineHolidayOverlays>[0],
input: {