Enhance booking and signature functionalities

- Updated MySignaturePage title to Signature
This commit is contained in:
Marshal
2026-08-01 22:03:18 +00:00
parent ea9df9abbe
commit 53d8655dc3
26 changed files with 1050 additions and 19 deletions

View File

@@ -3131,6 +3131,14 @@ export class BookingBatchService implements OnModuleInit {
async intercityCapacity(scheduleId: string): Promise<{
budget: CorridorBudget;
needFor: (booking: Booking) => Capacity;
/**
* Per-wagon-type split of `needFor(booking).wagons`, against THIS
* schedule's own wagon stock — so the same booking reads differently on a
* different train. Empty when the stock can't be resolved.
*/
breakdownFor: (
booking: Booking,
) => Array<{ wagonTypeId: string; code: string; wagons: number }>;
} | null> {
const schedule =
await this.trainSchedulesRepository.findByIdWithFullGraph(scheduleId);
@@ -3145,7 +3153,24 @@ export class BookingBatchService implements OnModuleInit {
// still accepts ride-alongs on its empty legs — that is the whole point
// of the ride-along flow.
const budget = await this.remainingBudget(schedule, limits, wagonDims);
return { budget, needFor: (booking) => this.needFor(booking, wagonDims) };
// Physical stock of THIS schedule's train (built consist, or the yard fleet
// it will draw from) — what makes the breakdown train-specific.
const stock = await this.trainSchedulingService.wagonStockForSchedule(
schedule.id,
schedule.originStationId,
budget.stops,
);
return {
budget,
needFor: (booking) => this.needFor(booking, wagonDims),
breakdownFor: (booking) =>
this.wagonBreakdownFor(
booking,
wagonDims,
stock.remainingByTypeId,
stock.codesByTypeId,
),
};
}
/**
@@ -4094,6 +4119,78 @@ export class BookingBatchService implements OnModuleInit {
};
}
/**
* The wagon count of {@link wagonsFor}, split across the wagon TYPES this
* particular train stocks — "3 × N35 + 1 × PW2" rather than a bare 4.
*
* `wagonsFor` sizes the booking on ONE representative type (the first the
* cargo type allows), which is all the abstract budget needs. Staff placing a
* ride-along need the physical picture: how many of each type this schedule
* must actually give up. So each allowed type is sized on its OWN capacity and
* items-fit, then filled greedily from the type with the largest per-wagon
* take, bounded by what the schedule has left of it.
*
* Because the stock is per-schedule, the same booking breaks down differently
* on a train stocking 60T N35s than on one stocking 40T PW2s. Returns [] when
* the booking's types are unconfigured or the train stocks none of them — the
* caller then shows the plain total.
*/
private wagonBreakdownFor(
booking: Booking,
wagonDims: WagonDims,
stockByTypeId: Map<string, number>,
codesByTypeId: Map<string, string>,
): Array<{ wagonTypeId: string; code: string; wagons: number }> {
const total = this.wagonsFor(booking, wagonDims);
if (total <= 0) return [];
// Per-wagon take of each allowed type ON THIS TRAIN, largest first: a type
// that swallows more of the booking per wagon needs fewer wagons.
const options = this.allowedDimsWithTypes(booking, wagonDims)
.filter((o) => o.wagonTypeId && (stockByTypeId.get(o.wagonTypeId) ?? 0) > 0)
.map((o) => {
const wagonTypeId = o.wagonTypeId as string;
const wagonsIfAlone = Math.max(
1,
bulkItemWagonsRequired(
booking,
o.dims.capacityTons,
bulkItemsFitFor(booking.cargoType, wagonTypeId),
) ||
(o.dims.capacityTons > 0
? Math.ceil(bookingCargoTons(booking) / o.dims.capacityTons)
: total),
);
return {
wagonTypeId,
code: codesByTypeId.get(wagonTypeId) ?? '—',
available: stockByTypeId.get(wagonTypeId) ?? 0,
// Share of the whole booking one wagon of this type carries.
takePerWagon: 1 / wagonsIfAlone,
};
})
.sort((a, b) => b.takePerWagon - a.takePerWagon);
if (!options.length) return [];
// Fill greedily by take, capped by stock; `remaining` is the fraction of the
// booking still unplaced, so a wagon of any type covers `takePerWagon` of it.
const out: Array<{ wagonTypeId: string; code: string; wagons: number }> = [];
let remaining = 1;
for (const option of options) {
if (remaining <= 1e-9) break;
const wagons = Math.min(
option.available,
Math.ceil(remaining / option.takePerWagon),
);
if (wagons <= 0) continue;
out.push({ wagonTypeId: option.wagonTypeId, code: option.code, wagons });
remaining -= wagons * option.takePerWagon;
}
// The train cannot hold the whole booking in the types it stocks — the
// `fits` check already fails it; report only what it CAN take.
return out;
}
private fits(need: Capacity, budget: Capacity): boolean {
return (
need.wagons <= budget.wagons &&