refactor(api): extract timeline project context support
This commit is contained in:
@@ -1,24 +1,25 @@
|
||||
import { listAssignmentBookings } from "@capakraken/application";
|
||||
import { computeBudgetStatus, validateShift } from "@capakraken/engine";
|
||||
import { computeBudgetStatus } from "@capakraken/engine";
|
||||
import { ShiftProjectSchema } from "@capakraken/shared";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
import { findUniqueOrThrow } from "../db/helpers.js";
|
||||
import { getAnonymizationDirectory } from "../lib/anonymization.js";
|
||||
import { controllerProcedure } from "../trpc.js";
|
||||
import { loadProjectPlanningReadModel } from "./project-planning-read-model.js";
|
||||
import {
|
||||
buildTimelineProjectAssignmentConflicts,
|
||||
buildTimelineProjectContextSummary,
|
||||
resolveTimelineProjectContextPeriod,
|
||||
} from "./timeline-project-context-support.js";
|
||||
import { buildTimelineShiftPlan } from "./timeline-shift-planning.js";
|
||||
import { loadTimelineHolidayOverlays, formatHolidayOverlays, summarizeHolidayOverlays } from "./timeline-holiday-read.js";
|
||||
import { loadTimelineHolidayOverlays, formatHolidayOverlays } from "./timeline-holiday-read.js";
|
||||
import {
|
||||
anonymizeResourceOnEntry,
|
||||
createTimelineDateRange,
|
||||
fmtDate,
|
||||
getAssignmentResourceIds,
|
||||
rangesOverlap,
|
||||
ShiftDbClient,
|
||||
summarizeTimelineEntries,
|
||||
toDate,
|
||||
} 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([
|
||||
@@ -124,19 +125,12 @@ export async function previewTimelineProjectShift(
|
||||
newEndDate: Date;
|
||||
},
|
||||
) {
|
||||
const { project, shiftPlan } = await loadProjectShiftContext(db, input.projectId);
|
||||
const context = await loadProjectShiftContext(db, input.projectId);
|
||||
|
||||
return validateShift({
|
||||
project: {
|
||||
id: project.id,
|
||||
budgetCents: project.budgetCents,
|
||||
winProbability: project.winProbability,
|
||||
startDate: project.startDate,
|
||||
endDate: project.endDate,
|
||||
},
|
||||
return buildTimelineProjectShiftValidation({
|
||||
context,
|
||||
newStartDate: input.newStartDate,
|
||||
newEndDate: input.newEndDate,
|
||||
allocations: shiftPlan.validationAllocations,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -182,94 +176,46 @@ export const timelineProjectReadProcedures = {
|
||||
.query(async ({ ctx, input }) => {
|
||||
const projectContext = await loadTimelineProjectContext(ctx.db, input.projectId);
|
||||
const directory = await getAnonymizationDirectory(ctx.db);
|
||||
|
||||
const derivedStartDate = input.startDate
|
||||
? createTimelineDateRange({ startDate: input.startDate, durationDays: 1 }).startDate
|
||||
: projectContext.project.startDate
|
||||
?? projectContext.assignments[0]?.startDate
|
||||
?? projectContext.demands[0]?.startDate
|
||||
?? createTimelineDateRange({ durationDays: 1 }).startDate;
|
||||
const derivedEndDate = input.endDate
|
||||
? createTimelineDateRange({ startDate: fmtDate(derivedStartDate) ?? undefined, endDate: input.endDate }).endDate
|
||||
: projectContext.project.endDate
|
||||
?? createTimelineDateRange({
|
||||
startDate: fmtDate(derivedStartDate) ?? undefined,
|
||||
durationDays: input.durationDays ?? 21,
|
||||
}).endDate;
|
||||
|
||||
if (derivedEndDate < derivedStartDate) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "endDate must be on or after startDate.",
|
||||
});
|
||||
}
|
||||
const period = resolveTimelineProjectContextPeriod({
|
||||
requestedStartDate: input.startDate,
|
||||
requestedEndDate: input.endDate,
|
||||
durationDays: input.durationDays,
|
||||
projectStartDate: projectContext.project.startDate,
|
||||
projectEndDate: projectContext.project.endDate,
|
||||
firstAssignmentStartDate: projectContext.assignments[0]?.startDate,
|
||||
firstDemandStartDate: projectContext.demands[0]?.startDate,
|
||||
});
|
||||
|
||||
const holidayOverlays = projectContext.resourceIds.length > 0
|
||||
? await loadTimelineHolidayOverlays(ctx.db, {
|
||||
startDate: derivedStartDate,
|
||||
endDate: derivedEndDate,
|
||||
startDate: period.startDate,
|
||||
endDate: period.endDate,
|
||||
resourceIds: projectContext.resourceIds,
|
||||
projectIds: [input.projectId],
|
||||
})
|
||||
: [];
|
||||
const formattedHolidayOverlays = formatHolidayOverlays(holidayOverlays);
|
||||
|
||||
const assignmentConflicts = projectContext.assignments
|
||||
.filter((assignment) => assignment.resourceId && assignment.resource)
|
||||
.map((assignment) => {
|
||||
const overlaps = projectContext.allResourceAllocations
|
||||
.filter((booking) => (
|
||||
booking.resourceId === assignment.resourceId
|
||||
&& booking.id !== assignment.id
|
||||
&& rangesOverlap(
|
||||
toDate(booking.startDate),
|
||||
toDate(booking.endDate),
|
||||
toDate(assignment.startDate),
|
||||
toDate(assignment.endDate),
|
||||
)
|
||||
))
|
||||
.map((booking) => ({
|
||||
id: booking.id,
|
||||
projectId: booking.projectId,
|
||||
projectName: booking.project?.name ?? null,
|
||||
projectShortCode: booking.project?.shortCode ?? null,
|
||||
startDate: fmtDate(toDate(booking.startDate)),
|
||||
endDate: fmtDate(toDate(booking.endDate)),
|
||||
hoursPerDay: booking.hoursPerDay,
|
||||
status: booking.status,
|
||||
sameProject: booking.projectId === input.projectId,
|
||||
}));
|
||||
|
||||
return {
|
||||
assignmentId: assignment.id,
|
||||
resourceId: assignment.resourceId!,
|
||||
resourceName: assignment.resource?.displayName ?? null,
|
||||
startDate: fmtDate(toDate(assignment.startDate)),
|
||||
endDate: fmtDate(toDate(assignment.endDate)),
|
||||
hoursPerDay: assignment.hoursPerDay,
|
||||
overlapCount: overlaps.length,
|
||||
crossProjectOverlapCount: overlaps.filter((booking) => !booking.sameProject).length,
|
||||
overlaps,
|
||||
};
|
||||
});
|
||||
const assignmentConflicts = buildTimelineProjectAssignmentConflicts({
|
||||
projectId: input.projectId,
|
||||
assignments: projectContext.assignments,
|
||||
allResourceAllocations: projectContext.allResourceAllocations,
|
||||
});
|
||||
|
||||
return {
|
||||
project: projectContext.project,
|
||||
period: {
|
||||
startDate: fmtDate(derivedStartDate),
|
||||
endDate: fmtDate(derivedEndDate),
|
||||
},
|
||||
summary: {
|
||||
...summarizeTimelineEntries({
|
||||
allocations: projectContext.allocations,
|
||||
demands: projectContext.demands,
|
||||
assignments: projectContext.assignments,
|
||||
}),
|
||||
resourceIds: projectContext.resourceIds.length,
|
||||
allResourceAllocationCount: projectContext.allResourceAllocations.length,
|
||||
conflictedAssignmentCount: assignmentConflicts.filter((item) => item.crossProjectOverlapCount > 0).length,
|
||||
...summarizeHolidayOverlays(formattedHolidayOverlays),
|
||||
startDate: fmtDate(period.startDate),
|
||||
endDate: fmtDate(period.endDate),
|
||||
},
|
||||
summary: buildTimelineProjectContextSummary({
|
||||
allocations: projectContext.allocations,
|
||||
demands: projectContext.demands,
|
||||
assignments: projectContext.assignments,
|
||||
resourceIds: projectContext.resourceIds,
|
||||
allResourceAllocations: projectContext.allResourceAllocations,
|
||||
assignmentConflicts,
|
||||
holidayOverlays: formattedHolidayOverlays,
|
||||
}),
|
||||
allocations: projectContext.allocations.map((allocation) =>
|
||||
anonymizeResourceOnEntry(allocation, directory),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user