refactor(api): extract timeline shift mutation helpers
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import type { SplitAssignmentRecord } from "@capakraken/application";
|
||||
import { Prisma, type PrismaClient } from "@capakraken/db";
|
||||
import type { ShiftValidationResult, WeekdayAvailability } from "@capakraken/shared";
|
||||
import { calculateTimelineAllocationDailyCost } from "./timeline-cost-support.js";
|
||||
import type { TimelineShiftProjectRecord } from "./timeline-shift-support.js";
|
||||
|
||||
export function buildTimelineProjectDateRangeUpdate(input: {
|
||||
newStartDate: Date;
|
||||
newEndDate: Date;
|
||||
}) {
|
||||
return {
|
||||
startDate: input.newStartDate,
|
||||
endDate: input.newEndDate,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildTimelineShiftedAssignmentUpdate(input: {
|
||||
newStartDate: Date;
|
||||
newEndDate: Date;
|
||||
dailyCostCents: number | undefined;
|
||||
}) {
|
||||
return {
|
||||
...buildTimelineProjectDateRangeUpdate(input),
|
||||
...(input.dailyCostCents !== undefined ? { dailyCostCents: input.dailyCostCents } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function buildTimelineProjectShiftAuditChanges(input: {
|
||||
project: TimelineShiftProjectRecord;
|
||||
newStartDate: Date;
|
||||
newEndDate: Date;
|
||||
validation: ShiftValidationResult;
|
||||
}): Prisma.InputJsonValue {
|
||||
return {
|
||||
before: {
|
||||
startDate: input.project.startDate,
|
||||
endDate: input.project.endDate,
|
||||
},
|
||||
after: {
|
||||
startDate: input.newStartDate,
|
||||
endDate: input.newEndDate,
|
||||
},
|
||||
costImpact: input.validation.costImpact,
|
||||
} as unknown as Prisma.InputJsonValue;
|
||||
}
|
||||
|
||||
export async function recalculateShiftedAssignmentDailyCost(input: {
|
||||
db: PrismaClient;
|
||||
assignment: SplitAssignmentRecord;
|
||||
newStartDate: Date;
|
||||
newEndDate: Date;
|
||||
}): Promise<number | undefined> {
|
||||
if (!input.assignment.resourceId || !input.assignment.resource) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const metadata = (input.assignment.metadata as Record<string, unknown> | null | undefined) ?? {};
|
||||
const includeSaturday = (metadata.includeSaturday as boolean | undefined) ?? false;
|
||||
|
||||
return calculateTimelineAllocationDailyCost({
|
||||
db: input.db,
|
||||
resourceId: input.assignment.resourceId,
|
||||
lcrCents: input.assignment.resource.lcrCents,
|
||||
hoursPerDay: input.assignment.hoursPerDay,
|
||||
startDate: input.newStartDate,
|
||||
endDate: input.newEndDate,
|
||||
availability: input.assignment.resource.availability as WeekdayAvailability,
|
||||
includeSaturday,
|
||||
});
|
||||
}
|
||||
@@ -4,12 +4,17 @@ import {
|
||||
type SplitAssignmentRecord,
|
||||
type SplitDemandRequirementRecord,
|
||||
} from "@capakraken/application";
|
||||
import { Prisma, type PrismaClient } from "@capakraken/db";
|
||||
import type { PrismaClient } from "@capakraken/db";
|
||||
import { validateShift } from "@capakraken/engine";
|
||||
import type { ShiftValidationResult } from "@capakraken/shared";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { calculateTimelineAllocationDailyCost } from "./timeline-cost-support.js";
|
||||
import type { TimelineShiftPlan } from "./timeline-shift-planning.js";
|
||||
import {
|
||||
buildTimelineProjectDateRangeUpdate,
|
||||
buildTimelineProjectShiftAuditChanges,
|
||||
buildTimelineShiftedAssignmentUpdate,
|
||||
recalculateShiftedAssignmentDailyCost,
|
||||
} from "./timeline-shift-mutation-support.js";
|
||||
|
||||
export interface TimelineShiftProjectRecord {
|
||||
id: string;
|
||||
@@ -92,49 +97,6 @@ export function buildTimelineProjectShiftEventPayload(input: {
|
||||
};
|
||||
}
|
||||
|
||||
function buildTimelineProjectShiftAuditChanges(input: {
|
||||
project: TimelineShiftProjectRecord;
|
||||
newStartDate: Date;
|
||||
newEndDate: Date;
|
||||
validation: ShiftValidationResult;
|
||||
}): Prisma.InputJsonValue {
|
||||
return {
|
||||
before: {
|
||||
startDate: input.project.startDate,
|
||||
endDate: input.project.endDate,
|
||||
},
|
||||
after: {
|
||||
startDate: input.newStartDate,
|
||||
endDate: input.newEndDate,
|
||||
},
|
||||
costImpact: input.validation.costImpact,
|
||||
} as unknown as Prisma.InputJsonValue;
|
||||
}
|
||||
|
||||
async function recalculateShiftedAssignmentDailyCost(input: {
|
||||
db: PrismaClient;
|
||||
assignment: SplitAssignmentRecord;
|
||||
newStartDate: Date;
|
||||
newEndDate: Date;
|
||||
}): Promise<number | undefined> {
|
||||
if (!input.assignment.resourceId || !input.assignment.resource) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const metadata = (input.assignment.metadata as Record<string, unknown> | null | undefined) ?? {};
|
||||
const includeSaturday = (metadata.includeSaturday as boolean | undefined) ?? false;
|
||||
return calculateTimelineAllocationDailyCost({
|
||||
db: input.db,
|
||||
resourceId: input.assignment.resourceId,
|
||||
lcrCents: input.assignment.resource.lcrCents,
|
||||
hoursPerDay: input.assignment.hoursPerDay,
|
||||
startDate: input.newStartDate,
|
||||
endDate: input.newEndDate,
|
||||
availability: input.assignment.resource.availability as import("@capakraken/shared").WeekdayAvailability,
|
||||
includeSaturday,
|
||||
});
|
||||
}
|
||||
|
||||
export async function applyTimelineProjectShift(input: ApplyTimelineProjectShiftInput) {
|
||||
const validation = buildTimelineProjectShiftValidation({
|
||||
context: input.context,
|
||||
@@ -146,20 +108,14 @@ export async function applyTimelineProjectShift(input: ApplyTimelineProjectShift
|
||||
const updatedProject = await input.db.$transaction(async (tx) => {
|
||||
const projectRecord = await tx.project.update({
|
||||
where: { id: input.projectId },
|
||||
data: {
|
||||
startDate: input.newStartDate,
|
||||
endDate: input.newEndDate,
|
||||
},
|
||||
data: buildTimelineProjectDateRangeUpdate(input),
|
||||
});
|
||||
|
||||
for (const demandRequirement of input.context.demandRequirements) {
|
||||
await updateDemandRequirement(
|
||||
tx as unknown as Parameters<typeof updateDemandRequirement>[0],
|
||||
demandRequirement.id,
|
||||
{
|
||||
startDate: input.newStartDate,
|
||||
endDate: input.newEndDate,
|
||||
},
|
||||
buildTimelineProjectDateRangeUpdate(input),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -174,11 +130,11 @@ export async function applyTimelineProjectShift(input: ApplyTimelineProjectShift
|
||||
await updateAssignment(
|
||||
tx as unknown as Parameters<typeof updateAssignment>[0],
|
||||
assignment.id,
|
||||
{
|
||||
startDate: input.newStartDate,
|
||||
endDate: input.newEndDate,
|
||||
...(dailyCostCents !== undefined ? { dailyCostCents } : {}),
|
||||
},
|
||||
buildTimelineShiftedAssignmentUpdate({
|
||||
newStartDate: input.newStartDate,
|
||||
newEndDate: input.newEndDate,
|
||||
dailyCostCents,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user