mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 21:08:12 +00:00
Add tareWeightTons property to CreateWagonTypeDto and update related components
This commit is contained in:
@@ -24,3 +24,13 @@ export const DEFAULT_CONTAINER_WAGON_LENGTH_METERS = 14;
|
||||
|
||||
/** Default CW3 covered wagon length for bulk bookings (m). */
|
||||
export const DEFAULT_BULK_WAGON_LENGTH_METERS = 14;
|
||||
|
||||
/**
|
||||
* Fallback tare weights (T) matching the length fallbacks above. The locomotive
|
||||
* pull limit is a GROSS limit, so a booking's weight budget must include the
|
||||
* empty weight of every wagon it occupies — not just its cargo.
|
||||
*/
|
||||
export const DEFAULT_CONTAINER_WAGON_TARE_TONS = 22.4;
|
||||
|
||||
/** Default CW3 gondola tare for bulk bookings (T). */
|
||||
export const DEFAULT_BULK_WAGON_TARE_TONS = 23.4;
|
||||
|
||||
@@ -31,6 +31,7 @@ describe('BookingBatchService — PAID reconcile', () => {
|
||||
createMany: jest.Mock;
|
||||
};
|
||||
let trainSchedulesRepository: {
|
||||
findById: jest.Mock;
|
||||
findByIdWithFullGraph: jest.Mock;
|
||||
findAll: jest.Mock;
|
||||
};
|
||||
@@ -65,6 +66,11 @@ describe('BookingBatchService — PAID reconcile', () => {
|
||||
createMany: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
trainSchedulesRepository = {
|
||||
findById: jest.fn().mockResolvedValue({
|
||||
id: scheduleId,
|
||||
bookingWindowStatus: 'OPEN',
|
||||
windowPhase: null,
|
||||
}),
|
||||
findByIdWithFullGraph: jest.fn().mockResolvedValue({
|
||||
id: scheduleId,
|
||||
maxWagons: 10,
|
||||
@@ -407,6 +413,49 @@ describe('BookingBatchService — PAID reconcile', () => {
|
||||
// Never reserved — waits for its partner in a later cycle.
|
||||
expect(notifier.payNow).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('clears a stale FULL flag and fills a train whose bookings all expired', async () => {
|
||||
// The deadlock: train A filled once, every booking then expired, but
|
||||
// bookingWindowStatus stayed FULL. isFillable() rejects FULL before it ever
|
||||
// reads the budget, so the batch skipped the train forever — it just cycled
|
||||
// PRE_WINDOW→DOC_REVIEW→PAYMENT with an empty consist, and only the odd
|
||||
// already-pinned booking got settled, one per cycle.
|
||||
const staleFull = {
|
||||
id: trainA,
|
||||
maxWagons: 1,
|
||||
bookingWindowStatus: 'FULL',
|
||||
// The batch runs while the customer window is closed.
|
||||
windowPhase: 'PAYMENT',
|
||||
direction: 'IMPORT',
|
||||
trainSetId: `set-${trainA}`,
|
||||
trainSet: { locomotive: smallLoco },
|
||||
scheduleBookings: [],
|
||||
scheduledDepartureDate: new Date('2026-06-20T06:00:00.000Z'),
|
||||
originStationId: originYardId,
|
||||
destinationStationId: destinationYardId,
|
||||
};
|
||||
trainSchedulesRepository.findAll.mockResolvedValue([{ ...staleFull }]);
|
||||
// Live capacity says the train is empty: 1 free wagon, nothing allocated.
|
||||
trainSchedulesRepository.findByIdWithFullGraph.mockResolvedValue(staleFull);
|
||||
// refreshWindowStatus writes CLOSED (mid-PAYMENT, not a customer-open phase);
|
||||
// the re-read reports it, and isFillable() admits CLOSED during PAYMENT.
|
||||
trainSchedulesRepository.findById.mockResolvedValue({
|
||||
id: trainA,
|
||||
bookingWindowStatus: 'CLOSED',
|
||||
windowPhase: 'PAYMENT',
|
||||
});
|
||||
bookingsRepository.findBatchPoolByCorridorDay.mockResolvedValue([
|
||||
commercial('waiting', 30),
|
||||
]);
|
||||
|
||||
const touched = await service.fillRouteDay(originYardId, destinationYardId, day);
|
||||
|
||||
// The train was reopened to the batch and actually filled, not skipped.
|
||||
expect(touched).toEqual([trainA]);
|
||||
expect(notifier.payNow).toHaveBeenCalledTimes(1);
|
||||
expect((notifier.payNow.mock.calls[0][0] as Booking).id).toBe('waiting');
|
||||
expect(notifier.unplaced).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('expireUnacceptedForRouteDay — doc-review sweep', () => {
|
||||
|
||||
@@ -30,12 +30,17 @@ import { BillingService } from "../billing/billing.service";
|
||||
|
||||
import {
|
||||
DEFAULT_BULK_WAGON_LENGTH_METERS,
|
||||
DEFAULT_BULK_WAGON_TARE_TONS,
|
||||
DEFAULT_CONTAINER_WAGON_LENGTH_METERS,
|
||||
DEFAULT_CONTAINER_WAGON_TARE_TONS,
|
||||
DEFAULT_WAGONS_PER_BOOKING,
|
||||
} from "./booking-batch.constants";
|
||||
import {
|
||||
WagonTypeDimensions,
|
||||
bookingGrossWeightTons,
|
||||
bookingTrainLengthMeters,
|
||||
deriveTrainCapacityFromLocomotive,
|
||||
trainHardCaps,
|
||||
wagonTypeDimensionsFromEntity,
|
||||
} from './train-capacity.util';
|
||||
import { WagonType } from '../wagon-types/entities/wagon-type.entity';
|
||||
@@ -63,7 +68,14 @@ interface RouteDayGroup {
|
||||
day: string;
|
||||
}
|
||||
|
||||
type WagonLengths = { container: number; bulk: number };
|
||||
/**
|
||||
* Per-freight-type wagon dimensions used to size a booking's capacity draw:
|
||||
* its length on the train and the tare it adds to the locomotive's gross load.
|
||||
*/
|
||||
type WagonDims = {
|
||||
container: { lengthMeters: number; tareWeightTons: number };
|
||||
bulk: { lengthMeters: number; tareWeightTons: number };
|
||||
};
|
||||
|
||||
export type BatchBoardBookingState =
|
||||
| "ALLOCATED"
|
||||
@@ -501,8 +513,8 @@ export class BookingBatchService implements OnModuleInit {
|
||||
}
|
||||
|
||||
const rules = await this.loadGlobalRules();
|
||||
const wagonLengths = await this.loadWagonLengths();
|
||||
const required = need ?? this.needFor(booking, wagonLengths);
|
||||
const wagonDims = await this.loadWagonDims();
|
||||
const required = need ?? this.needFor(booking, wagonDims);
|
||||
let corridorMatched = false;
|
||||
for (const candidate of candidates) {
|
||||
const schedule = await this.trainSchedulesRepository.findByIdWithFullGraph(
|
||||
@@ -511,7 +523,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
const locomotive = schedule?.trainSet?.locomotive;
|
||||
if (!schedule || !locomotive) continue;
|
||||
const limits = await this.capacityLimits(locomotive, rules);
|
||||
const budget = await this.remainingBudget(schedule, limits, wagonLengths);
|
||||
const budget = await this.remainingBudget(schedule, limits, wagonDims);
|
||||
const leg = budget.legOf(booking.originYardId, booking.destinationYardId);
|
||||
if (!leg) continue; // this train's route doesn't carry the booking's leg
|
||||
corridorMatched = true;
|
||||
@@ -549,8 +561,8 @@ export class BookingBatchService implements OnModuleInit {
|
||||
if (!partner || partner.status !== 'FULLY_EXECUTED') {
|
||||
return;
|
||||
}
|
||||
const wagonLengths = await this.loadWagonLengths();
|
||||
const need = this.combinedNeed(booking, partner, wagonLengths);
|
||||
const wagonDims = await this.loadWagonDims();
|
||||
const need = this.combinedNeed(booking, partner, wagonDims);
|
||||
const scheduleId = await this.pickExportSchedule(booking, need);
|
||||
await this.reserveOnExport([booking, partner], scheduleId);
|
||||
}
|
||||
@@ -620,7 +632,8 @@ export class BookingBatchService implements OnModuleInit {
|
||||
order: { scheduledDepartureDate: "ASC" },
|
||||
});
|
||||
|
||||
const wagonLengths = await this.loadWagonLengths();
|
||||
const wagonDims = await this.loadWagonDims();
|
||||
const rules = await this.loadGlobalRules();
|
||||
const linkRepo = this.dataSource.getRepository(TrainScheduleBooking);
|
||||
|
||||
const board: BatchBoardSchedule[] = [];
|
||||
@@ -635,7 +648,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
const bookings = await this.bookingsRepository.findAllBySchedule(s.id);
|
||||
|
||||
const items: BatchBoardBooking[] = bookings.map((b) => {
|
||||
const need = this.needFor(b, wagonLengths);
|
||||
const need = this.needFor(b, wagonDims);
|
||||
return {
|
||||
id: b.id,
|
||||
reference: b.reference ?? b.id.slice(0, 8),
|
||||
@@ -655,7 +668,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
};
|
||||
});
|
||||
|
||||
board.push(this.buildScheduleSummary(s, items));
|
||||
board.push(this.buildScheduleSummary(s, items, rules));
|
||||
}
|
||||
return board;
|
||||
}
|
||||
@@ -678,7 +691,8 @@ export class BookingBatchService implements OnModuleInit {
|
||||
);
|
||||
}
|
||||
|
||||
const wagonLengths = await this.loadWagonLengths();
|
||||
const wagonDims = await this.loadWagonDims();
|
||||
const rules = await this.loadGlobalRules();
|
||||
const linkRepo = this.dataSource.getRepository(TrainScheduleBooking);
|
||||
const links = await linkRepo.find({ where: { trainScheduleId: s.id } });
|
||||
const linkedIds = new Set(links.map((l) => l.bookingId));
|
||||
@@ -724,7 +738,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
}
|
||||
|
||||
const items: BatchBoardBookingDetail[] = bookings.map((b) => {
|
||||
const need = this.needFor(b, wagonLengths);
|
||||
const need = this.needFor(b, wagonDims);
|
||||
const alloc = allocationByBooking.get(b.id);
|
||||
return {
|
||||
id: b.id,
|
||||
@@ -871,7 +885,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
maxTrainLengthMeters: Number(loco.maxTrainLengthMeters),
|
||||
}
|
||||
: null,
|
||||
capacity: this.computeBoardCapacity(items, loco, s.maxWagons ?? null),
|
||||
capacity: this.computeBoardCapacity(items, loco, s.maxWagons ?? null, rules),
|
||||
counts: {
|
||||
allocated: items.filter((i) => i.state === "ALLOCATED").length,
|
||||
selectedForBatch: items.filter((i) => i.state === "SELECTED_FOR_BATCH")
|
||||
@@ -902,6 +916,13 @@ export class BookingBatchService implements OnModuleInit {
|
||||
return this.trainSchedulingService.tryAutoWagonAllocation(scheduleId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Board capacity figures. `usedWeightTons` is GROSS (each item's weight already
|
||||
* includes the tare of the wagons it occupies), so the ceiling it is measured
|
||||
* against must be the same one the fill loop spends from: the locomotive floored
|
||||
* by the global rule caps and widened by its overage tolerance. Reading the raw
|
||||
* `loco.maxPullWeightTons` here showed staff a ceiling the batch engine did not use.
|
||||
*/
|
||||
private computeBoardCapacity(
|
||||
items: Array<{
|
||||
state: BatchBoardBookingState;
|
||||
@@ -911,22 +932,40 @@ export class BookingBatchService implements OnModuleInit {
|
||||
}>,
|
||||
loco: Locomotive | null,
|
||||
maxWagons: number | null,
|
||||
rules: TrainSchedulingGlobalRules | null,
|
||||
): BatchBoardSchedule["capacity"] {
|
||||
const allocated = items.filter((i) => i.state === "ALLOCATED");
|
||||
const committed = items.filter(
|
||||
(i) => i.state === "ALLOCATED" || i.state === "SELECTED_FOR_BATCH",
|
||||
);
|
||||
const caps = loco
|
||||
? trainHardCaps(
|
||||
{
|
||||
maxPullWeightTons: Number(loco.maxPullWeightTons),
|
||||
maxTrainLengthMeters: Number(loco.maxTrainLengthMeters),
|
||||
overageToleranceTons: Number(loco.overageToleranceTons) || 0,
|
||||
overageToleranceMeters: Number(loco.overageToleranceMeters) || 0,
|
||||
},
|
||||
{
|
||||
maxTrainWeightTons: rules?.maxTrainWeightTons
|
||||
? Number(rules.maxTrainWeightTons)
|
||||
: undefined,
|
||||
maxTrainLengthMeters: rules?.maxTrainLengthMeters
|
||||
? Number(rules.maxTrainLengthMeters)
|
||||
: undefined,
|
||||
},
|
||||
)
|
||||
: null;
|
||||
const round2 = (value: number) => Math.round(value * 100) / 100;
|
||||
|
||||
return {
|
||||
allocatedWagons: allocated.reduce((sum, i) => sum + i.wagons, 0),
|
||||
allocatedLengthMeters:
|
||||
Math.round(
|
||||
allocated.reduce((sum, i) => sum + i.lengthMeters, 0) * 100,
|
||||
) / 100,
|
||||
maxLengthMeters: loco ? Number(loco.maxTrainLengthMeters) : null,
|
||||
usedWeightTons:
|
||||
Math.round(committed.reduce((sum, i) => sum + i.weightTons, 0) * 100) /
|
||||
100,
|
||||
maxWeightTons: loco ? Number(loco.maxPullWeightTons) : null,
|
||||
allocatedLengthMeters: round2(
|
||||
allocated.reduce((sum, i) => sum + i.lengthMeters, 0),
|
||||
),
|
||||
maxLengthMeters: caps ? caps.maxLengthMeters : null,
|
||||
usedWeightTons: round2(committed.reduce((sum, i) => sum + i.weightTons, 0)),
|
||||
maxWeightTons: caps ? caps.maxWeightTons : null,
|
||||
maxWagons: maxWagons ?? null,
|
||||
};
|
||||
}
|
||||
@@ -934,6 +973,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
private buildScheduleSummary(
|
||||
s: TrainSchedule,
|
||||
items: BatchBoardBooking[],
|
||||
rules: TrainSchedulingGlobalRules | null,
|
||||
): BatchBoardSchedule {
|
||||
const loco = s.trainSet?.locomotive ?? null;
|
||||
|
||||
@@ -966,7 +1006,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
maxTrainLengthMeters: Number(loco.maxTrainLengthMeters),
|
||||
}
|
||||
: null,
|
||||
capacity: this.computeBoardCapacity(items, loco, s.maxWagons ?? null),
|
||||
capacity: this.computeBoardCapacity(items, loco, s.maxWagons ?? null, rules),
|
||||
counts: {
|
||||
allocated: items.filter((i) => i.state === "ALLOCATED").length,
|
||||
selectedForBatch: items.filter((i) => i.state === "SELECTED_FOR_BATCH")
|
||||
@@ -1036,10 +1076,10 @@ export class BookingBatchService implements OnModuleInit {
|
||||
}
|
||||
|
||||
const rules = await this.loadGlobalRules();
|
||||
const wagonLengths = await this.loadWagonLengths();
|
||||
const wagonDims = await this.loadWagonDims();
|
||||
const limits = await this.capacityLimits(locomotive, rules);
|
||||
await this.syncScheduleMaxWagons(schedule, locomotive, rules);
|
||||
const budget = await this.remainingBudget(schedule, limits, wagonLengths);
|
||||
const budget = await this.remainingBudget(schedule, limits, wagonDims);
|
||||
if (budget.maxRemaining().wagons <= 0) {
|
||||
await this.setWindow(scheduleId, "FULL");
|
||||
return 0;
|
||||
@@ -1064,8 +1104,8 @@ export class BookingBatchService implements OnModuleInit {
|
||||
const { primary: booking, partner } = unit;
|
||||
const isPair = partner != null;
|
||||
const need = isPair
|
||||
? this.combinedNeed(booking, partner, wagonLengths)
|
||||
: this.needFor(booking, wagonLengths);
|
||||
? this.combinedNeed(booking, partner, wagonDims)
|
||||
: this.needFor(booking, wagonDims);
|
||||
const isGov = booking.isGovernment || (partner?.isGovernment ?? false);
|
||||
// Consolidated partners always share one corridor, so the primary's leg
|
||||
// stands for the pair.
|
||||
@@ -1084,7 +1124,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
need,
|
||||
leg,
|
||||
budget,
|
||||
wagonLengths,
|
||||
wagonDims,
|
||||
);
|
||||
if (!freed) continue; // still doesn't fit even after preempt
|
||||
} else {
|
||||
@@ -1151,6 +1191,42 @@ export class BookingBatchService implements OnModuleInit {
|
||||
destinationYardId: string,
|
||||
day: string,
|
||||
): Promise<string[]> {
|
||||
const { scheduleIds } = await this.fillRouteDayInternal(
|
||||
originYardId,
|
||||
destinationYardId,
|
||||
day,
|
||||
);
|
||||
return scheduleIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Route-day top-up for a single schedule: re-run the DAY pool over the whole
|
||||
* corridor the schedule belongs to, and report how many commercial units got a
|
||||
* fresh pay window.
|
||||
*
|
||||
* `fillSchedule` cannot do this job. Its pool (`findBatchPool`) is keyed on
|
||||
* `booking.train_schedule_id = :scheduleId`, but under day-level pooling a
|
||||
* booking that has not been reserved yet has a NULL `train_schedule_id` — it is
|
||||
* only pinned by `reserve()`. So the schedule-scoped top-up returned zero rows
|
||||
* and the waiting list never boarded after an expiry freed capacity; bookings
|
||||
* trickled in one per window cycle instead.
|
||||
*/
|
||||
private async topUpFill(scheduleId: string): Promise<number> {
|
||||
const schedule = await this.trainSchedulesRepository.findById(scheduleId);
|
||||
if (!schedule?.scheduledDepartureDate) return 0;
|
||||
const { commercialReserved } = await this.fillRouteDayInternal(
|
||||
schedule.originStationId,
|
||||
schedule.destinationStationId,
|
||||
eatDay(schedule.scheduledDepartureDate),
|
||||
);
|
||||
return commercialReserved;
|
||||
}
|
||||
|
||||
private async fillRouteDayInternal(
|
||||
originYardId: string,
|
||||
destinationYardId: string,
|
||||
day: string,
|
||||
): Promise<{ scheduleIds: string[]; commercialReserved: number }> {
|
||||
// The day's fillable schedules on this exact corridor, earliest first. Fillable
|
||||
// covers legacy OPEN trains and window-cycle trains in DOC_REVIEW/PAYMENT —
|
||||
// the batch must run while the customer window is closed.
|
||||
@@ -1168,23 +1244,36 @@ export class BookingBatchService implements OnModuleInit {
|
||||
},
|
||||
],
|
||||
});
|
||||
const scheduleIds = corridor
|
||||
const onDay = corridor
|
||||
.filter(
|
||||
(s) =>
|
||||
s.scheduledDepartureDate != null &&
|
||||
eatDay(s.scheduledDepartureDate) === day &&
|
||||
this.isFillable(s),
|
||||
eatDay(s.scheduledDepartureDate) === day,
|
||||
)
|
||||
.sort(
|
||||
(a, b) =>
|
||||
a.scheduledDepartureDate.getTime() - b.scheduledDepartureDate.getTime(),
|
||||
)
|
||||
.map((s) => s.id);
|
||||
);
|
||||
|
||||
if (scheduleIds.length === 0) return [];
|
||||
// A schedule flagged FULL is rejected by isFillable() before its budget is
|
||||
// ever consulted. Re-derive that flag from live capacity first, so a train
|
||||
// whose bookings all expired is not skipped forever with an empty consist.
|
||||
for (const s of onDay) {
|
||||
if (s.bookingWindowStatus === "FULL") {
|
||||
await this.refreshWindowStatus(s.id);
|
||||
const fresh = await this.trainSchedulesRepository.findById(s.id);
|
||||
if (fresh) s.bookingWindowStatus = fresh.bookingWindowStatus;
|
||||
}
|
||||
}
|
||||
|
||||
const scheduleIds = onDay.filter((s) => this.isFillable(s)).map((s) => s.id);
|
||||
|
||||
if (scheduleIds.length === 0) {
|
||||
return { scheduleIds: [], commercialReserved: 0 };
|
||||
}
|
||||
|
||||
const rules = await this.loadGlobalRules();
|
||||
const wagonLengths = await this.loadWagonLengths();
|
||||
const wagonDims = await this.loadWagonDims();
|
||||
|
||||
// Live per-schedule corridor budget + arm flag, in departure order.
|
||||
const trains: Array<{ id: string; budget: CorridorBudget; armed: boolean }> = [];
|
||||
@@ -1200,10 +1289,10 @@ export class BookingBatchService implements OnModuleInit {
|
||||
}
|
||||
const limits = await this.capacityLimits(locomotive, rules);
|
||||
await this.syncScheduleMaxWagons(schedule, locomotive, rules);
|
||||
const budget = await this.remainingBudget(schedule, limits, wagonLengths);
|
||||
const budget = await this.remainingBudget(schedule, limits, wagonDims);
|
||||
trains.push({ id, budget, armed: false });
|
||||
}
|
||||
if (trains.length === 0) return [];
|
||||
if (trains.length === 0) return { scheduleIds, commercialReserved: 0 };
|
||||
|
||||
// The day pool covers every booking whose leg lies somewhere on one of the
|
||||
// day's corridors — full-route AND sub-corridor (e.g. Dire→Djibouti on an
|
||||
@@ -1225,13 +1314,14 @@ export class BookingBatchService implements OnModuleInit {
|
||||
`poolSize=${pool.length} units=${units.length}`,
|
||||
);
|
||||
let reservedThisPass = 0;
|
||||
let commercialReserved = 0;
|
||||
|
||||
for (const unit of units) {
|
||||
const { primary: booking, partner } = unit;
|
||||
const isPair = partner != null;
|
||||
const need = isPair
|
||||
? this.combinedNeed(booking, partner, wagonLengths)
|
||||
: this.needFor(booking, wagonLengths);
|
||||
? this.combinedNeed(booking, partner, wagonDims)
|
||||
: this.needFor(booking, wagonDims);
|
||||
const isGov = booking.isGovernment || (partner?.isGovernment ?? false);
|
||||
|
||||
const legOn = (t: { budget: CorridorBudget }): CorridorLeg | null =>
|
||||
@@ -1267,7 +1357,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
need,
|
||||
leg,
|
||||
t.budget,
|
||||
wagonLengths,
|
||||
wagonDims,
|
||||
);
|
||||
if (freed) {
|
||||
target = t;
|
||||
@@ -1284,7 +1374,12 @@ export class BookingBatchService implements OnModuleInit {
|
||||
// non-import never split — isSplitEligible guards that. Passing the live
|
||||
// `trains` entries lets maybeOfferPartial mutate the chosen budget/armed.
|
||||
const offered = await this.maybeOfferPartial(booking, isPair, trains, need);
|
||||
if (offered) continue;
|
||||
if (offered) {
|
||||
// A partial offer opens a real commercial pay window, same as reserve().
|
||||
commercialReserved += 1;
|
||||
reservedThisPass += 1;
|
||||
continue;
|
||||
}
|
||||
// Stays in the pool, retried next batch/window cycle.
|
||||
this.notifier.unplaced(booking, day);
|
||||
if (partner) this.notifier.unplaced(partner, day);
|
||||
@@ -1305,6 +1400,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
await this.reserve(booking, target.id);
|
||||
if (partner) await this.reserve(partner, target.id);
|
||||
target.armed = true;
|
||||
commercialReserved += 1;
|
||||
}
|
||||
target.budget.subtract(need, legOn(target)!);
|
||||
reservedThisPass += 1;
|
||||
@@ -1326,7 +1422,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
void this.triggerWagonAllocation(t.id);
|
||||
}
|
||||
|
||||
return trains.map((t) => t.id);
|
||||
return { scheduleIds: trains.map((t) => t.id), commercialReserved };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1396,7 +1492,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
if (booking.consolidationPartnerId) return null;
|
||||
if (await this.splitService.findOpenOffer(booking.id)) return null;
|
||||
|
||||
const wagonLengths = await this.loadWagonLengths();
|
||||
const wagonDims = await this.loadWagonDims();
|
||||
const bulkCapacityTons = await this.loadBulkWagonCapacityTons();
|
||||
const sized = await this.splitService.sizeOffer(
|
||||
booking,
|
||||
@@ -1408,11 +1504,16 @@ export class BookingBatchService implements OnModuleInit {
|
||||
|
||||
const offeredNeed: Capacity = {
|
||||
wagons: sized.offeredWagons,
|
||||
weightTons: sized.offeredWeightTons,
|
||||
lengthMeters: bookingTrainLengthMeters(booking.freightType, sized.offeredWagons, {
|
||||
container: wagonLengths.container,
|
||||
bulk: wagonLengths.bulk,
|
||||
}),
|
||||
weightTons: bookingGrossWeightTons(
|
||||
sized.offeredWeightTons,
|
||||
sized.offeredWagons,
|
||||
this.tareFor(booking.freightType, wagonDims),
|
||||
),
|
||||
lengthMeters: bookingTrainLengthMeters(
|
||||
booking.freightType,
|
||||
sized.offeredWagons,
|
||||
this.lengthsOf(wagonDims),
|
||||
),
|
||||
};
|
||||
if (!this.fits(offeredNeed, budget)) return null;
|
||||
|
||||
@@ -1510,7 +1611,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
this.logger.log(
|
||||
`[BATCH] settle changed state on ${scheduleId} — running top-up fill for the waiting list`,
|
||||
);
|
||||
const topUpReserved = await this.fillSchedule(scheduleId);
|
||||
const topUpReserved = await this.topUpFill(scheduleId);
|
||||
// A top-up opened a fresh pay window for waiting bookings — push the
|
||||
// schedule's PAYMENT phase out so the window tick's concludeCycle doesn't
|
||||
// fire before those customers' new deadlines and expire them prematurely.
|
||||
@@ -1526,7 +1627,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
async settleBatch(scheduleId: string): Promise<void> {
|
||||
this.removeTimeout(scheduleId);
|
||||
await this.settleReserved(scheduleId, true);
|
||||
const topUpReserved = await this.fillSchedule(scheduleId);
|
||||
const topUpReserved = await this.topUpFill(scheduleId);
|
||||
if (topUpReserved > 0) {
|
||||
await this.extendPaymentPhaseForTopUp(scheduleId);
|
||||
}
|
||||
@@ -1632,11 +1733,14 @@ export class BookingBatchService implements OnModuleInit {
|
||||
.getRepository(Booking)
|
||||
.findOne({ where: { id: bookingId } });
|
||||
if (!booking) throw new NotFoundException(`Booking ${bookingId} not found`);
|
||||
// Capture the train before expire() detaches the booking from it — the
|
||||
// top-up has to run against the schedule whose wagons were just freed.
|
||||
const freedScheduleId = booking.trainScheduleId;
|
||||
await this.expire(booking);
|
||||
if (booking.trainScheduleId) {
|
||||
const topUpReserved = await this.fillSchedule(booking.trainScheduleId);
|
||||
if (freedScheduleId) {
|
||||
const topUpReserved = await this.topUpFill(freedScheduleId);
|
||||
if (topUpReserved > 0) {
|
||||
await this.extendPaymentPhaseForTopUp(booking.trainScheduleId);
|
||||
await this.extendPaymentPhaseForTopUp(freedScheduleId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1659,10 +1763,10 @@ export class BookingBatchService implements OnModuleInit {
|
||||
const locomotive = schedule?.trainSet?.locomotive;
|
||||
if (!schedule || !locomotive) return null;
|
||||
const rules = await this.loadGlobalRules();
|
||||
const wagonLengths = await this.loadWagonLengths();
|
||||
const wagonDims = await this.loadWagonDims();
|
||||
const limits = await this.capacityLimits(locomotive, rules);
|
||||
const budget = await this.remainingBudget(schedule, limits, wagonLengths);
|
||||
return { budget, needFor: (booking) => this.needFor(booking, wagonLengths) };
|
||||
const budget = await this.remainingBudget(schedule, limits, wagonDims);
|
||||
return { budget, needFor: (booking) => this.needFor(booking, wagonDims) };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1827,6 +1931,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
* it failed to pay for — it's back in the day pool for staff to act on.
|
||||
*/
|
||||
private async expire(booking: Booking): Promise<void> {
|
||||
const freedScheduleId = booking.trainScheduleId;
|
||||
await this.bookingsRepository.update(booking.id, {
|
||||
trainScheduleId: null,
|
||||
status: "EXPIRED",
|
||||
@@ -1835,6 +1940,9 @@ export class BookingBatchService implements OnModuleInit {
|
||||
selectedForBatchAt: null,
|
||||
} as never);
|
||||
booking.trainScheduleId = null;
|
||||
// The wagons this reservation held are back — a schedule parked at FULL
|
||||
// because of it must reopen, or it can never be filled again.
|
||||
if (freedScheduleId) await this.refreshWindowStatus(freedScheduleId);
|
||||
// An unpaid partial offer dies with the reservation — the booking stays whole.
|
||||
if (this.splitService) {
|
||||
await this.splitService.expireOpenOffer(booking.id);
|
||||
@@ -1939,7 +2047,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
need: Capacity,
|
||||
leg: CorridorLeg,
|
||||
budget: CorridorBudget,
|
||||
wagonLengths: WagonLengths,
|
||||
wagonDims: WagonDims,
|
||||
): Promise<boolean> {
|
||||
if (budget.fits(need, leg)) return true;
|
||||
const reservedCommercial = (
|
||||
@@ -1987,7 +2095,10 @@ export class BookingBatchService implements OnModuleInit {
|
||||
);
|
||||
});
|
||||
this.notifier.displaced(victim);
|
||||
budget.add(this.needFor(victim, wagonLengths), victimLeg);
|
||||
budget.add(this.needFor(victim, wagonDims), victimLeg);
|
||||
// Displacing frees wagons the same way an expiry does — don't leave the
|
||||
// schedule stuck at FULL.
|
||||
await this.refreshWindowStatus(scheduleId);
|
||||
}
|
||||
return budget.fits(need, leg);
|
||||
}
|
||||
@@ -2041,7 +2152,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
private combinedNeed(
|
||||
primary: Booking,
|
||||
partner: Booking,
|
||||
wagonLengths: WagonLengths,
|
||||
wagonDims: WagonDims,
|
||||
): Capacity {
|
||||
const containers = (b: Booking): number =>
|
||||
(b.bookingContainers ?? []).reduce((sum, c) => sum + Number(c.quantity ?? 0), 0);
|
||||
@@ -2050,15 +2161,36 @@ export class BookingBatchService implements OnModuleInit {
|
||||
totalContainers > 0
|
||||
? Math.ceil(totalContainers / MAX_TEU_SLOTS_PER_WAGON)
|
||||
: this.wagonsFor(primary) + this.wagonsFor(partner);
|
||||
const weightTons =
|
||||
const cargoTons =
|
||||
Number(primary.cargoTotalWeightVgm ?? 0) + Number(partner.cargoTotalWeightVgm ?? 0);
|
||||
return {
|
||||
wagons: sharedWagons,
|
||||
weightTons,
|
||||
lengthMeters: bookingTrainLengthMeters(primary.freightType, sharedWagons, {
|
||||
container: wagonLengths.container,
|
||||
bulk: wagonLengths.bulk,
|
||||
}),
|
||||
// Consolidation saves tare as well as slots: the pair rides `sharedWagons`
|
||||
// wagons, so it is charged `sharedWagons` tares, not one per booking.
|
||||
weightTons: bookingGrossWeightTons(
|
||||
cargoTons,
|
||||
sharedWagons,
|
||||
this.tareFor(primary.freightType, wagonDims),
|
||||
),
|
||||
lengthMeters: bookingTrainLengthMeters(
|
||||
primary.freightType,
|
||||
sharedWagons,
|
||||
this.lengthsOf(wagonDims),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
/** Per-wagon tare for the wagon type this freight rides on. */
|
||||
private tareFor(freightType: string | null | undefined, wagonDims: WagonDims): number {
|
||||
return freightType === 'BULK'
|
||||
? wagonDims.bulk.tareWeightTons
|
||||
: wagonDims.container.tareWeightTons;
|
||||
}
|
||||
|
||||
private lengthsOf(wagonDims: WagonDims): { container: number; bulk: number } {
|
||||
return {
|
||||
container: wagonDims.container.lengthMeters,
|
||||
bulk: wagonDims.bulk.lengthMeters,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2077,16 +2209,28 @@ export class BookingBatchService implements OnModuleInit {
|
||||
return Math.max(DEFAULT_WAGONS_PER_BOOKING, fromContainers);
|
||||
}
|
||||
|
||||
/** What one booking consumes along all three capacity axes. */
|
||||
private needFor(booking: Booking, wagonLengths: WagonLengths): Capacity {
|
||||
/**
|
||||
* What one booking consumes along all three capacity axes.
|
||||
*
|
||||
* The weight axis is GROSS — cargo plus the tare of every wagon the booking
|
||||
* occupies — because it is spent against the locomotive's pull limit, which
|
||||
* governs the whole train and not just its payload. Charging cargo alone let a
|
||||
* 37-wagon box-wagon train read 2590T when it really weighed 3522T.
|
||||
*/
|
||||
private needFor(booking: Booking, wagonDims: WagonDims): Capacity {
|
||||
const wagons = this.wagonsFor(booking);
|
||||
return {
|
||||
wagons,
|
||||
weightTons: Number(booking.cargoTotalWeightVgm ?? 0),
|
||||
lengthMeters: bookingTrainLengthMeters(booking.freightType, wagons, {
|
||||
container: wagonLengths.container,
|
||||
bulk: wagonLengths.bulk,
|
||||
}),
|
||||
weightTons: bookingGrossWeightTons(
|
||||
Number(booking.cargoTotalWeightVgm ?? 0),
|
||||
wagons,
|
||||
this.tareFor(booking.freightType, wagonDims),
|
||||
),
|
||||
lengthMeters: bookingTrainLengthMeters(
|
||||
booking.freightType,
|
||||
wagons,
|
||||
this.lengthsOf(wagonDims),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2098,7 +2242,11 @@ export class BookingBatchService implements OnModuleInit {
|
||||
);
|
||||
}
|
||||
|
||||
/** Locomotive + wagon-type-derived caps (weight, length, wagon slots — not a fixed 53). */
|
||||
/**
|
||||
* Hard caps for a schedule's train: gross pull weight, train length, and the
|
||||
* length-derived wagon slot count (never a fixed 53). Bookings spend against
|
||||
* these via {@link needFor}, whose weight axis is gross.
|
||||
*/
|
||||
private async capacityLimits(
|
||||
locomotive: Locomotive,
|
||||
rules: TrainSchedulingGlobalRules | null,
|
||||
@@ -2143,31 +2291,48 @@ export class BookingBatchService implements OnModuleInit {
|
||||
}
|
||||
}
|
||||
|
||||
private async loadWagonTypeDimensions(): Promise<
|
||||
Array<{ lengthMeters: number; capacityTons: number }>
|
||||
> {
|
||||
const types = await this.dataSource.getRepository(WagonType).find({
|
||||
where: [{ code: "NW5" }, { code: "CW3" }],
|
||||
});
|
||||
/**
|
||||
* Every active wagon type, so the slot count is derived from the shortest wagon
|
||||
* the fleet can actually marshal rather than from an arbitrary two-code sample.
|
||||
*/
|
||||
private async loadWagonTypeDimensions(): Promise<WagonTypeDimensions[]> {
|
||||
const types = await this.dataSource
|
||||
.getRepository(WagonType)
|
||||
.find({ where: { isActive: true } });
|
||||
if (types.length) return types.map(wagonTypeDimensionsFromEntity);
|
||||
return [
|
||||
{ lengthMeters: DEFAULT_CONTAINER_WAGON_LENGTH_METERS, capacityTons: 70 },
|
||||
{ lengthMeters: DEFAULT_BULK_WAGON_LENGTH_METERS, capacityTons: 60 },
|
||||
{
|
||||
lengthMeters: DEFAULT_CONTAINER_WAGON_LENGTH_METERS,
|
||||
capacityTons: 70,
|
||||
tareWeightTons: DEFAULT_CONTAINER_WAGON_TARE_TONS,
|
||||
},
|
||||
{
|
||||
lengthMeters: DEFAULT_BULK_WAGON_LENGTH_METERS,
|
||||
capacityTons: 60,
|
||||
tareWeightTons: DEFAULT_BULK_WAGON_TARE_TONS,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
private async loadWagonLengths(): Promise<WagonLengths> {
|
||||
/** Representative wagon per freight type: NW5 flat for containers, CW3 gondola for bulk. */
|
||||
private async loadWagonDims(): Promise<WagonDims> {
|
||||
const types = await this.dataSource.getRepository(WagonType).find({
|
||||
where: [{ code: "NW5" }, { code: "CW3" }],
|
||||
});
|
||||
const byCode = new Map(
|
||||
types.map((t) => [t.code, wagonTypeDimensionsFromEntity(t)]),
|
||||
);
|
||||
const nw5 = byCode.get("NW5");
|
||||
const cw3 = byCode.get("CW3");
|
||||
return {
|
||||
container:
|
||||
byCode.get("NW5")?.lengthMeters ??
|
||||
DEFAULT_CONTAINER_WAGON_LENGTH_METERS,
|
||||
bulk: byCode.get("CW3")?.lengthMeters ?? DEFAULT_BULK_WAGON_LENGTH_METERS,
|
||||
container: {
|
||||
lengthMeters: nw5?.lengthMeters ?? DEFAULT_CONTAINER_WAGON_LENGTH_METERS,
|
||||
tareWeightTons: nw5?.tareWeightTons ?? DEFAULT_CONTAINER_WAGON_TARE_TONS,
|
||||
},
|
||||
bulk: {
|
||||
lengthMeters: cw3?.lengthMeters ?? DEFAULT_BULK_WAGON_LENGTH_METERS,
|
||||
tareWeightTons: cw3?.tareWeightTons ?? DEFAULT_BULK_WAGON_TARE_TONS,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2204,7 +2369,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
private async remainingBudget(
|
||||
schedule: TrainSchedule,
|
||||
limits: Capacity,
|
||||
wagonLengths: WagonLengths,
|
||||
wagonDims: WagonDims,
|
||||
): Promise<CorridorBudget> {
|
||||
const stops = await this.stopsForSchedule(schedule);
|
||||
const budget = new CorridorBudget(stops, limits);
|
||||
@@ -2216,7 +2381,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
);
|
||||
for (const b of [...allocated, ...reserved]) {
|
||||
budget.subtract(
|
||||
this.needFor(b, wagonLengths),
|
||||
this.needFor(b, wagonDims),
|
||||
budget.legForYards(b.originYardId, b.destinationYardId),
|
||||
);
|
||||
}
|
||||
@@ -2228,7 +2393,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
* ≤ 0 means no leg can take another booking — the train-wide FULL signal.
|
||||
*/
|
||||
private async remainingWagons(schedule: TrainSchedule): Promise<number> {
|
||||
const wagonLengths = await this.loadWagonLengths();
|
||||
const wagonDims = await this.loadWagonDims();
|
||||
const budget = await this.remainingBudget(
|
||||
schedule,
|
||||
{
|
||||
@@ -2236,7 +2401,7 @@ export class BookingBatchService implements OnModuleInit {
|
||||
weightTons: Number.POSITIVE_INFINITY,
|
||||
lengthMeters: Number.POSITIVE_INFINITY,
|
||||
},
|
||||
wagonLengths,
|
||||
wagonDims,
|
||||
);
|
||||
return budget.maxRemaining().wagons;
|
||||
}
|
||||
@@ -2269,6 +2434,35 @@ export class BookingBatchService implements OnModuleInit {
|
||||
return (await this.remainingWagons(schedule)) <= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-derive `bookingWindowStatus` from live capacity after wagons were freed
|
||||
* (a reservation expired, a booking was displaced, a link was removed).
|
||||
*
|
||||
* FULL used to be a one-way door: `isFillable()` rejects a FULL schedule before
|
||||
* it ever looks at the budget, and the only writers of OPEN skip a FULL row. So
|
||||
* a train that filled once and then lost every booking to expiry stayed FULL
|
||||
* with all its wagons free — permanently unfillable, cycling PRE_WINDOW→PAYMENT
|
||||
* forever while `concludeCycle` (which reads real capacity, not the flag) kept
|
||||
* reopening it. Clearing FULL here is what lets the next batch actually run.
|
||||
*
|
||||
* Only the customer-facing OPEN phases may go back to OPEN; a schedule mid
|
||||
* DOC_REVIEW/PAYMENT drops to CLOSED, which `isFillable()` still admits.
|
||||
*/
|
||||
async refreshWindowStatus(scheduleId: string): Promise<void> {
|
||||
const schedule =
|
||||
await this.trainSchedulesRepository.findByIdWithFullGraph(scheduleId);
|
||||
if (!schedule || schedule.bookingWindowStatus !== "FULL") return;
|
||||
if ((await this.remainingWagons(schedule)) <= 0) return;
|
||||
|
||||
const customerWindowOpen =
|
||||
schedule.windowPhase == null || schedule.windowPhase === "OPEN";
|
||||
await this.setWindow(scheduleId, customerWindowOpen ? "OPEN" : "CLOSED");
|
||||
this.logger.log(
|
||||
`[BATCH] ${scheduleId} cleared stale FULL — wagons freed, window is now ` +
|
||||
`${customerWindowOpen ? "OPEN" : "CLOSED"} and the batch can fill it again`,
|
||||
);
|
||||
}
|
||||
|
||||
// ---- timer plumbing -------------------------------------------------------
|
||||
|
||||
/** Configured customer pay window in ms (global rules, with defaults). */
|
||||
|
||||
@@ -328,6 +328,18 @@ export class BookingWindowService implements OnModuleInit {
|
||||
return;
|
||||
}
|
||||
|
||||
// Not full, so any FULL flag left over from a batch whose bookings later
|
||||
// expired is stale. Clear it here too: the PRE_WINDOW→OPEN transition below
|
||||
// refuses to reopen a FULL schedule, which is how a train with an empty
|
||||
// consist used to cycle forever without ever being fillable again. Re-read
|
||||
// the flag onto the in-memory row — advanceSchedule keeps looping on this
|
||||
// same object, and PRE_WINDOW→OPEN reads it.
|
||||
if (schedule.bookingWindowStatus === 'FULL') {
|
||||
await this.bookingBatchService.refreshWindowStatus(schedule.id);
|
||||
const fresh = await this.trainSchedulesRepository.findById(schedule.id);
|
||||
if (fresh) schedule.bookingWindowStatus = fresh.bookingWindowStatus;
|
||||
}
|
||||
|
||||
// Doc review + payment have already run, so the desk is ready to reopen NOW —
|
||||
// office hours decide whether that is this afternoon or tomorrow morning. Past
|
||||
// the last cycle before departure, nextCycleOpensAt returns null and we finish.
|
||||
|
||||
@@ -1,64 +1,183 @@
|
||||
import {
|
||||
bookingGrossWeightTons,
|
||||
bookingTrainLengthMeters,
|
||||
consistUsage,
|
||||
consistViolations,
|
||||
deriveTrainCapacityFromLocomotive,
|
||||
grossWagonWeightTons,
|
||||
minLocomotiveLimits,
|
||||
} from './train-capacity.util';
|
||||
|
||||
describe('train-capacity.util', () => {
|
||||
const nw5 = { lengthMeters: 14, capacityTons: 70 };
|
||||
// Real EDR wagon specs.
|
||||
const nw5 = { lengthMeters: 13.966, capacityTons: 70, tareWeightTons: 22.4 };
|
||||
const pw2 = { lengthMeters: 17.066, capacityTons: 70, tareWeightTons: 25.2 };
|
||||
const gw2 = { lengthMeters: 12.228, capacityTons: 70, tareWeightTons: 23 };
|
||||
|
||||
it('derives wagon slots from locomotive length and weight, not a fixed 53', () => {
|
||||
const shortLoco = deriveTrainCapacityFromLocomotive(
|
||||
{ maxPullWeightTons: 2000, maxTrainLengthMeters: 280 },
|
||||
[nw5],
|
||||
);
|
||||
expect(shortLoco.maxWagonSlots).toBe(20); // 280 / 14
|
||||
expect(shortLoco.maxWagonSlots).not.toBe(53);
|
||||
|
||||
const heavyLoco = deriveTrainCapacityFromLocomotive(
|
||||
{ maxPullWeightTons: 2100, maxTrainLengthMeters: 760 },
|
||||
[nw5],
|
||||
);
|
||||
expect(heavyLoco.maxWagonSlots).toBe(30); // min(54, 30) from weight 2100/70
|
||||
const caps = (over = {}) => ({
|
||||
maxWeightTons: 3500,
|
||||
maxLengthMeters: 760,
|
||||
maxWagonSlots: 54,
|
||||
...over,
|
||||
});
|
||||
|
||||
it('uses shortest wagon type when mixed types are present', () => {
|
||||
const longBulk = { lengthMeters: 18, capacityTons: 80 };
|
||||
const mixed = deriveTrainCapacityFromLocomotive(
|
||||
{ maxPullWeightTons: 3500, maxTrainLengthMeters: 760 },
|
||||
[nw5, longBulk],
|
||||
);
|
||||
expect(mixed.maxWagonSlots).toBe(
|
||||
Math.min(Math.floor(760 / 14), Math.floor(3500 / 70)),
|
||||
);
|
||||
const slots = (n: number, type: typeof nw5, cargoTons: number) =>
|
||||
Array.from({ length: n }, () => ({
|
||||
lengthMeters: type.lengthMeters,
|
||||
tareWeightTons: type.tareWeightTons,
|
||||
cargoTons,
|
||||
}));
|
||||
|
||||
describe('deriveTrainCapacityFromLocomotive', () => {
|
||||
it('derives wagon slots from train length, not a fixed 53', () => {
|
||||
const shortLoco = deriveTrainCapacityFromLocomotive(
|
||||
{ maxPullWeightTons: 2000, maxTrainLengthMeters: 280 },
|
||||
[nw5],
|
||||
);
|
||||
expect(shortLoco.maxWagonSlots).toBe(20); // floor(280 / 13.966)
|
||||
expect(shortLoco.maxWagonSlots).not.toBe(53);
|
||||
});
|
||||
|
||||
it('does not shrink slots by assuming every wagon rides at full payload', () => {
|
||||
// A 2100T loco could only pull 30 fully-laden 70T wagons, but slots are a
|
||||
// LENGTH figure — the cargo that decides weight does not exist yet.
|
||||
const derived = deriveTrainCapacityFromLocomotive(
|
||||
{ maxPullWeightTons: 2100, maxTrainLengthMeters: 760 },
|
||||
[nw5],
|
||||
);
|
||||
expect(derived.maxWagonSlots).toBe(54); // floor(760 / 13.966), not 30
|
||||
expect(derived.maxWeightTons).toBe(2100);
|
||||
});
|
||||
|
||||
it('admits the railway 53-wagon NW5 marshalling figure', () => {
|
||||
const derived = deriveTrainCapacityFromLocomotive(
|
||||
{ maxPullWeightTons: 3500, maxTrainLengthMeters: 760 },
|
||||
[nw5],
|
||||
);
|
||||
expect(derived.maxWagonSlots).toBeGreaterThanOrEqual(53);
|
||||
});
|
||||
|
||||
it('uses the shortest wagon type when mixed types are present', () => {
|
||||
const mixed = deriveTrainCapacityFromLocomotive(
|
||||
{ maxPullWeightTons: 3500, maxTrainLengthMeters: 760 },
|
||||
[nw5, pw2, gw2],
|
||||
);
|
||||
expect(mixed.maxWagonSlots).toBe(Math.floor(760 / gw2.lengthMeters)); // 62
|
||||
});
|
||||
|
||||
it('extends weight/length caps by the locomotive overage tolerance', () => {
|
||||
const derived = deriveTrainCapacityFromLocomotive(
|
||||
{ maxPullWeightTons: 3500, maxTrainLengthMeters: 760, overageToleranceTons: 90 },
|
||||
[pw2],
|
||||
);
|
||||
expect(derived.maxWeightTons).toBe(3590);
|
||||
});
|
||||
|
||||
it('ignores overage tolerance when unset (strict cap)', () => {
|
||||
const derived = deriveTrainCapacityFromLocomotive(
|
||||
{ maxPullWeightTons: 3500, maxTrainLengthMeters: 760 },
|
||||
[nw5],
|
||||
);
|
||||
expect(derived.maxWeightTons).toBe(3500);
|
||||
expect(derived.maxLengthMeters).toBe(760);
|
||||
});
|
||||
|
||||
it('floors the locomotive by the global rule caps', () => {
|
||||
const derived = deriveTrainCapacityFromLocomotive(
|
||||
{ maxPullWeightTons: 5000, maxTrainLengthMeters: 900 },
|
||||
[nw5],
|
||||
{ maxTrainWeightTons: 3500, maxTrainLengthMeters: 760 },
|
||||
);
|
||||
expect(derived.maxWeightTons).toBe(3500);
|
||||
expect(derived.maxLengthMeters).toBe(760);
|
||||
});
|
||||
});
|
||||
|
||||
describe('gross weight', () => {
|
||||
it('counts the wagon as well as its cargo', () => {
|
||||
expect(grossWagonWeightTons({ tareWeightTons: 25.2, cargoTons: 70 })).toBe(95.2);
|
||||
});
|
||||
|
||||
it('charges a booking one tare per wagon it occupies', () => {
|
||||
// 3 flat wagons carrying 100T of cargo still drag 3 × 22.4T of steel.
|
||||
expect(bookingGrossWeightTons(100, 3, 22.4)).toBe(167.2);
|
||||
});
|
||||
|
||||
it('is cargo alone when the wagon type has no tare on record', () => {
|
||||
expect(bookingGrossWeightTons(100, 3, 0)).toBe(100);
|
||||
});
|
||||
});
|
||||
|
||||
describe('consistUsage', () => {
|
||||
it('sums each wagon own length and tare rather than averaging a type', () => {
|
||||
const mixed = [...slots(2, nw5, 10), ...slots(1, pw2, 20)];
|
||||
const usage = consistUsage(mixed, caps());
|
||||
|
||||
expect(usage.wagonCount).toBe(3);
|
||||
expect(usage.usedLengthMeters).toBe(44.998); // 2×13.966 + 17.066
|
||||
expect(usage.usedTareWeightTons).toBe(70); // 2×22.4 + 25.2
|
||||
expect(usage.usedCargoWeightTons).toBe(40);
|
||||
expect(usage.usedGrossWeightTons).toBe(110);
|
||||
expect(usage.remainingGrossWeightTons).toBe(3390);
|
||||
expect(usage.remainingWagons).toBe(51);
|
||||
});
|
||||
|
||||
it('reports an empty consist as fully available', () => {
|
||||
const usage = consistUsage([], caps());
|
||||
expect(usage.usedGrossWeightTons).toBe(0);
|
||||
expect(usage.remainingLengthMeters).toBe(760);
|
||||
expect(usage.remainingWagons).toBe(54);
|
||||
});
|
||||
});
|
||||
|
||||
describe('consistViolations', () => {
|
||||
it('accepts 37 fully-laden PW2 box wagons only via the overage tolerance', () => {
|
||||
// 37 × (25.2 + 70) = 3522.4T — over 3500T, inside 3590T.
|
||||
const consist = slots(37, pw2, 70);
|
||||
|
||||
expect(consistViolations(consist, caps({ maxWagonSlots: 44 }))).toEqual([
|
||||
expect.stringContaining('3522.4T'),
|
||||
]);
|
||||
expect(
|
||||
consistViolations(consist, caps({ maxWeightTons: 3590, maxWagonSlots: 44 })),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it('blocks a train the old cargo-only math would have waved through', () => {
|
||||
// Cargo alone is 2590T — comfortably "under" 3500T. Gross is 3522.4T.
|
||||
const consist = slots(37, pw2, 70);
|
||||
const cargoOnly = consist.reduce((sum, s) => sum + s.cargoTons, 0);
|
||||
|
||||
expect(cargoOnly).toBeLessThan(3500);
|
||||
expect(consistViolations(consist, caps({ maxWagonSlots: 44 }))).not.toEqual([]);
|
||||
});
|
||||
|
||||
it('lets 53 NW5 flat wagons pass when the cargo is what the railway really loads', () => {
|
||||
// 53 × 13.966 = 740.2m < 760m; 53 × (22.4 + 40) = 3307.2T < 3500T.
|
||||
expect(consistViolations(slots(53, nw5, 40), caps({ maxWagonSlots: 54 }))).toEqual([]);
|
||||
});
|
||||
|
||||
it('flags an over-length consist', () => {
|
||||
const violations = consistViolations(slots(50, pw2, 5), caps({ maxWagonSlots: 60 }));
|
||||
expect(violations).toEqual([expect.stringContaining('exceeds max train length')]);
|
||||
});
|
||||
|
||||
it('flags an over-count consist', () => {
|
||||
const violations = consistViolations(slots(10, nw5, 1), caps({ maxWagonSlots: 9 }));
|
||||
expect(violations).toEqual([expect.stringContaining('exceeds max wagons per train')]);
|
||||
});
|
||||
|
||||
it('reports every broken axis at once', () => {
|
||||
expect(consistViolations(slots(60, pw2, 70), caps())).toHaveLength(3);
|
||||
});
|
||||
});
|
||||
|
||||
it('computes booking length by freight type', () => {
|
||||
expect(
|
||||
bookingTrainLengthMeters('CONTAINER', 2, { container: 14, bulk: 14 }),
|
||||
).toBe(28);
|
||||
expect(bookingTrainLengthMeters('CONTAINER', 2, { container: 14, bulk: 14 })).toBe(28);
|
||||
expect(bookingTrainLengthMeters('BULK', 3, { container: 14, bulk: 18 })).toBe(54);
|
||||
});
|
||||
|
||||
it('extends weight/length caps by the locomotive overage tolerance (fertilizer +90T example)', () => {
|
||||
const pw2 = { lengthMeters: 17.066, capacityTons: 70 };
|
||||
const derived = deriveTrainCapacityFromLocomotive(
|
||||
{ maxPullWeightTons: 3500, maxTrainLengthMeters: 760, overageToleranceTons: 90 },
|
||||
[pw2],
|
||||
);
|
||||
expect(derived.maxWeightTons).toBe(3590);
|
||||
});
|
||||
|
||||
it('ignores overage tolerance when unset (strict cap, no behavior change)', () => {
|
||||
const derived = deriveTrainCapacityFromLocomotive(
|
||||
{ maxPullWeightTons: 3500, maxTrainLengthMeters: 760 },
|
||||
[{ lengthMeters: 14, capacityTons: 70 }],
|
||||
);
|
||||
expect(derived.maxWeightTons).toBe(3500);
|
||||
expect(derived.maxLengthMeters).toBe(760);
|
||||
});
|
||||
|
||||
it('takes the weakest locomotive tolerance across a multi-locomotive set', () => {
|
||||
it('takes the weakest locomotive across a multi-locomotive set', () => {
|
||||
const limits = minLocomotiveLimits([
|
||||
{ maxPullWeightTons: 3500, maxTrainLengthMeters: 760, overageToleranceTons: 90 },
|
||||
{ maxPullWeightTons: 4000, maxTrainLengthMeters: 760, overageToleranceTons: 20 },
|
||||
|
||||
@@ -1,7 +1,40 @@
|
||||
/**
|
||||
* Train capacity is a THREE-AXIS constraint, and the axes are not interchangeable:
|
||||
*
|
||||
* count — how many wagons fit end to end on the longest allowed train
|
||||
* length — Σ wagonType.lengthMeters over the real consist
|
||||
* weight — Σ (wagonType.tareWeightTons + cargoTons) over the real consist
|
||||
*
|
||||
* The weight axis is GROSS: a locomotive pulls the wagon as well as what is in it.
|
||||
* The old code compared the locomotive's pull limit against cargo payload alone
|
||||
* and so overbooked every train by roughly the tare fraction (~27% on PW2).
|
||||
*
|
||||
* The weight axis is also driven by ACTUAL booked cargo, never by an assumed
|
||||
* full payload. That is what makes the real EDR numbers fall out:
|
||||
*
|
||||
* NW5 13.966m tare 22.4T → 760 / 13.966 = 54 slots by length; the 53-wagon
|
||||
* marshalling figure is length-bound, and those trains never carry 53×70T.
|
||||
* PW2 17.066m tare 25.2T → 44 slots by length, but 37 × (25.2 + 70) = 3522.4T,
|
||||
* which clears 3500T only via the locomotive's overage tolerance. Weight
|
||||
* binds first, hence "37 wagons per train".
|
||||
*
|
||||
* So: `maxWagonSlots` is a LENGTH-derived planning number, shown before any cargo
|
||||
* exists. Weight is enforced against the consist as bookings are allocated.
|
||||
*/
|
||||
|
||||
/** Physical dimensions used when deriving how many wagons a locomotive can pull. */
|
||||
export type WagonTypeDimensions = {
|
||||
lengthMeters: number;
|
||||
capacityTons: number;
|
||||
tareWeightTons: number;
|
||||
};
|
||||
|
||||
/** One occupied wagon slot in a real consist. */
|
||||
export type ConsistSlot = {
|
||||
lengthMeters: number;
|
||||
tareWeightTons: number;
|
||||
/** Actual cargo/container weight riding on this wagon, not its rated capacity. */
|
||||
cargoTons: number;
|
||||
};
|
||||
|
||||
export type LocomotiveLimits = {
|
||||
@@ -14,69 +47,166 @@ export type LocomotiveLimits = {
|
||||
};
|
||||
|
||||
export type DerivedTrainCapacity = {
|
||||
/** Gross (tare + cargo) tons the train may weigh, tolerance included. */
|
||||
maxWeightTons: number;
|
||||
maxLengthMeters: number;
|
||||
/** Length-derived slot count. Weight is enforced separately against real cargo. */
|
||||
maxWagonSlots: number;
|
||||
};
|
||||
|
||||
/** What a consist currently uses, and what is left on each axis. */
|
||||
export type ConsistUsage = {
|
||||
wagonCount: number;
|
||||
usedLengthMeters: number;
|
||||
/** Σ (tare + cargo). */
|
||||
usedGrossWeightTons: number;
|
||||
usedTareWeightTons: number;
|
||||
usedCargoWeightTons: number;
|
||||
remainingLengthMeters: number;
|
||||
remainingGrossWeightTons: number;
|
||||
remainingWagons: number;
|
||||
};
|
||||
|
||||
export const MAX_FALLBACK_WEIGHT = 3500;
|
||||
export const MAX_FALLBACK_LENGTH = 760;
|
||||
|
||||
const DEFAULT_WAGON_LENGTH_M = 14;
|
||||
const DEFAULT_WAGON_CAPACITY_T = 70;
|
||||
/** NW5's tare — the commonest wagon — used only when a type predates the NOT NULL backfill. */
|
||||
const DEFAULT_WAGON_TARE_T = 22.4;
|
||||
|
||||
function num(value: unknown, fallback = 0): number {
|
||||
const n = Number(value);
|
||||
return Number.isFinite(n) ? n : fallback;
|
||||
}
|
||||
|
||||
/** Gross weight of one loaded wagon: it hauls itself plus its cargo. */
|
||||
export function grossWagonWeightTons(slot: Pick<ConsistSlot, 'tareWeightTons' | 'cargoTons'>): number {
|
||||
return num(slot.tareWeightTons) + num(slot.cargoTons);
|
||||
}
|
||||
|
||||
/**
|
||||
* Derive train capacity from locomotive pull weight and train length.
|
||||
* Wagon count is NOT a fixed 53 — it is the minimum of:
|
||||
* - floor(maxLength / shortest wagon type length)
|
||||
* - floor(maxWeight / lightest wagon type capacity)
|
||||
* Hard caps for a train: the locomotive's own limits, floored by the global rule
|
||||
* caps, then widened by the locomotive's overage tolerance.
|
||||
*/
|
||||
export function trainHardCaps(
|
||||
locomotive: LocomotiveLimits,
|
||||
ruleCaps?: { maxTrainWeightTons?: number; maxTrainLengthMeters?: number },
|
||||
): { maxWeightTons: number; maxLengthMeters: number } {
|
||||
const overageTons = num(locomotive.overageToleranceTons);
|
||||
const overageMeters = num(locomotive.overageToleranceMeters);
|
||||
|
||||
const weight =
|
||||
Math.min(
|
||||
num(locomotive.maxPullWeightTons, Infinity) || Infinity,
|
||||
ruleCaps?.maxTrainWeightTons ?? Infinity,
|
||||
) + overageTons;
|
||||
const length =
|
||||
Math.min(
|
||||
num(locomotive.maxTrainLengthMeters, Infinity) || Infinity,
|
||||
ruleCaps?.maxTrainLengthMeters ?? Infinity,
|
||||
) + overageMeters;
|
||||
|
||||
return {
|
||||
maxWeightTons: Number.isFinite(weight) ? weight : MAX_FALLBACK_WEIGHT,
|
||||
maxLengthMeters: Number.isFinite(length) ? length : MAX_FALLBACK_LENGTH,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Derive the planning capacity of a train from its locomotive.
|
||||
*
|
||||
* `maxWagonSlots` counts how many of the SHORTEST allowed wagon type fit within
|
||||
* the train-length cap — the optimistic slot count, since a mixed consist of
|
||||
* longer wagons will hit the length cap sooner. It is deliberately NOT reduced by
|
||||
* weight: with no bookings yet there is no cargo, and assuming every wagon rides
|
||||
* at full rated payload would report 37 NW5 slots where the railway marshals 53.
|
||||
* Weight is enforced by {@link consistUsage} / {@link consistViolations} against
|
||||
* the cargo actually allocated.
|
||||
*/
|
||||
export function deriveTrainCapacityFromLocomotive(
|
||||
locomotive: LocomotiveLimits,
|
||||
wagonTypes: WagonTypeDimensions[],
|
||||
ruleCaps?: { maxTrainWeightTons?: number; maxTrainLengthMeters?: number },
|
||||
): DerivedTrainCapacity {
|
||||
const overageTons = Number(locomotive.overageToleranceTons) || 0;
|
||||
const overageMeters = Number(locomotive.overageToleranceMeters) || 0;
|
||||
const { maxWeightTons, maxLengthMeters } = trainHardCaps(locomotive, ruleCaps);
|
||||
|
||||
const maxWeightTons =
|
||||
Math.min(
|
||||
Number(locomotive.maxPullWeightTons) || Infinity,
|
||||
ruleCaps?.maxTrainWeightTons ?? Infinity,
|
||||
) + overageTons;
|
||||
const maxLengthMeters =
|
||||
Math.min(
|
||||
Number(locomotive.maxTrainLengthMeters) || Infinity,
|
||||
ruleCaps?.maxTrainLengthMeters ?? Infinity,
|
||||
) + overageMeters;
|
||||
const lengths = wagonTypes
|
||||
.map((w) => num(w.lengthMeters))
|
||||
.filter((l) => l > 0);
|
||||
const minLength = lengths.length ? Math.min(...lengths) : DEFAULT_WAGON_LENGTH_M;
|
||||
|
||||
const types =
|
||||
wagonTypes.length > 0
|
||||
? wagonTypes
|
||||
: [{ lengthMeters: DEFAULT_WAGON_LENGTH_M, capacityTons: DEFAULT_WAGON_CAPACITY_T }];
|
||||
const maxWagonSlots =
|
||||
minLength > 0 ? Math.max(0, Math.floor(maxLengthMeters / minLength)) : 0;
|
||||
|
||||
const minLength = Math.min(...types.map((w) => Number(w.lengthMeters) || DEFAULT_WAGON_LENGTH_M));
|
||||
const minCapacity = Math.min(
|
||||
...types.map((w) => Number(w.capacityTons) || DEFAULT_WAGON_CAPACITY_T),
|
||||
);
|
||||
return { maxWeightTons, maxLengthMeters, maxWagonSlots };
|
||||
}
|
||||
|
||||
const byLength =
|
||||
minLength > 0 && Number.isFinite(maxLengthMeters)
|
||||
? Math.floor(maxLengthMeters / minLength)
|
||||
: 0;
|
||||
const byWeight =
|
||||
minCapacity > 0 && Number.isFinite(maxWeightTons)
|
||||
? Math.floor(maxWeightTons / minCapacity)
|
||||
: byLength;
|
||||
/**
|
||||
* What a real, mixed-type consist uses on all three axes, and what is left.
|
||||
* Every wagon contributes its own length and its own tare — no averaging over a
|
||||
* representative wagon type.
|
||||
*/
|
||||
export function consistUsage(
|
||||
slots: ConsistSlot[],
|
||||
caps: { maxWeightTons: number; maxLengthMeters: number; maxWagonSlots: number },
|
||||
): ConsistUsage {
|
||||
let usedLengthMeters = 0;
|
||||
let usedTareWeightTons = 0;
|
||||
let usedCargoWeightTons = 0;
|
||||
|
||||
const maxWagonSlots = Math.max(0, Math.min(byLength, byWeight));
|
||||
for (const slot of slots) {
|
||||
usedLengthMeters += num(slot.lengthMeters);
|
||||
usedTareWeightTons += num(slot.tareWeightTons);
|
||||
usedCargoWeightTons += num(slot.cargoTons);
|
||||
}
|
||||
|
||||
const usedGrossWeightTons = usedTareWeightTons + usedCargoWeightTons;
|
||||
|
||||
return {
|
||||
maxWeightTons: Number.isFinite(maxWeightTons) ? maxWeightTons : MAX_FALLBACK_WEIGHT,
|
||||
maxLengthMeters: Number.isFinite(maxLengthMeters) ? maxLengthMeters : MAX_FALLBACK_LENGTH,
|
||||
maxWagonSlots,
|
||||
wagonCount: slots.length,
|
||||
usedLengthMeters: round3(usedLengthMeters),
|
||||
usedGrossWeightTons: round3(usedGrossWeightTons),
|
||||
usedTareWeightTons: round3(usedTareWeightTons),
|
||||
usedCargoWeightTons: round3(usedCargoWeightTons),
|
||||
remainingLengthMeters: round3(caps.maxLengthMeters - usedLengthMeters),
|
||||
remainingGrossWeightTons: round3(caps.maxWeightTons - usedGrossWeightTons),
|
||||
remainingWagons: caps.maxWagonSlots - slots.length,
|
||||
};
|
||||
}
|
||||
|
||||
export const MAX_FALLBACK_WEIGHT = 3500;
|
||||
export const MAX_FALLBACK_LENGTH = 760;
|
||||
/** Human-readable reasons a consist breaks its train's limits. Empty = it fits. */
|
||||
export function consistViolations(
|
||||
slots: ConsistSlot[],
|
||||
caps: { maxWeightTons: number; maxLengthMeters: number; maxWagonSlots: number },
|
||||
): string[] {
|
||||
const usage = consistUsage(slots, caps);
|
||||
const violations: string[] = [];
|
||||
|
||||
if (usage.usedGrossWeightTons > caps.maxWeightTons) {
|
||||
violations.push(
|
||||
`Total train gross weight ${usage.usedGrossWeightTons}T ` +
|
||||
`(${usage.usedTareWeightTons}T tare + ${usage.usedCargoWeightTons}T cargo) ` +
|
||||
`exceeds max pull weight ${round3(caps.maxWeightTons)}T`,
|
||||
);
|
||||
}
|
||||
if (usage.usedLengthMeters > caps.maxLengthMeters) {
|
||||
violations.push(
|
||||
`Total wagon length ${usage.usedLengthMeters}m exceeds max train length ${round3(caps.maxLengthMeters)}m`,
|
||||
);
|
||||
}
|
||||
if (usage.wagonCount > caps.maxWagonSlots) {
|
||||
violations.push(
|
||||
`Wagon count ${usage.wagonCount} exceeds max wagons per train (${caps.maxWagonSlots})`,
|
||||
);
|
||||
}
|
||||
|
||||
return violations;
|
||||
}
|
||||
|
||||
function round3(value: number): number {
|
||||
return Number.isFinite(value) ? Number(value.toFixed(3)) : value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Effective pull limits for a train set with multiple locomotives: the weakest
|
||||
@@ -92,18 +222,14 @@ export function minLocomotiveLimits(
|
||||
if (!locomotives.length) return null;
|
||||
return {
|
||||
maxPullWeightTons: Math.min(
|
||||
...locomotives.map((l) => Number(l.maxPullWeightTons) || Infinity),
|
||||
...locomotives.map((l) => num(l.maxPullWeightTons, Infinity) || Infinity),
|
||||
),
|
||||
maxTrainLengthMeters: Math.min(
|
||||
...locomotives.map((l) => Number(l.maxTrainLengthMeters) || Infinity),
|
||||
...locomotives.map((l) => num(l.maxTrainLengthMeters, Infinity) || Infinity),
|
||||
),
|
||||
// Weakest locomotive's tolerance governs the set, same as its caps.
|
||||
overageToleranceTons: Math.min(
|
||||
...locomotives.map((l) => Number(l.overageToleranceTons) || 0),
|
||||
),
|
||||
overageToleranceMeters: Math.min(
|
||||
...locomotives.map((l) => Number(l.overageToleranceMeters) || 0),
|
||||
),
|
||||
overageToleranceTons: Math.min(...locomotives.map((l) => num(l.overageToleranceTons))),
|
||||
overageToleranceMeters: Math.min(...locomotives.map((l) => num(l.overageToleranceMeters))),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -117,12 +243,27 @@ export function bookingTrainLengthMeters(
|
||||
return wagonCount * perWagon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gross weight a booking adds to its train: its cargo plus the tare of every
|
||||
* wagon it occupies. A booking is never weightless just because it is light —
|
||||
* the empty wagons still have to be pulled.
|
||||
*/
|
||||
export function bookingGrossWeightTons(
|
||||
cargoTons: number,
|
||||
wagonCount: number,
|
||||
tarePerWagonTons: number,
|
||||
): number {
|
||||
return round3(num(cargoTons) + wagonCount * num(tarePerWagonTons));
|
||||
}
|
||||
|
||||
export function wagonTypeDimensionsFromEntity(wt: {
|
||||
lengthMeters?: number | string | null;
|
||||
capacityTons?: number | string | null;
|
||||
tareWeightTons?: number | string | null;
|
||||
}): WagonTypeDimensions {
|
||||
return {
|
||||
lengthMeters: Number(wt.lengthMeters) || DEFAULT_WAGON_LENGTH_M,
|
||||
capacityTons: Number(wt.capacityTons) || DEFAULT_WAGON_CAPACITY_T,
|
||||
lengthMeters: num(wt.lengthMeters) || DEFAULT_WAGON_LENGTH_M,
|
||||
capacityTons: num(wt.capacityTons) || DEFAULT_WAGON_CAPACITY_T,
|
||||
tareWeightTons: num(wt.tareWeightTons) || DEFAULT_WAGON_TARE_T,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -102,10 +102,13 @@ import {
|
||||
deriveTrainCapacityFromLocomotive,
|
||||
minLocomotiveLimits,
|
||||
wagonTypeDimensionsFromEntity,
|
||||
WagonTypeDimensions,
|
||||
} from './train-capacity.util';
|
||||
import {
|
||||
DEFAULT_BULK_WAGON_LENGTH_METERS,
|
||||
DEFAULT_BULK_WAGON_TARE_TONS,
|
||||
DEFAULT_CONTAINER_WAGON_LENGTH_METERS,
|
||||
DEFAULT_CONTAINER_WAGON_TARE_TONS,
|
||||
} from './booking-batch.constants';
|
||||
import {
|
||||
computeExportWindowTimes,
|
||||
@@ -3125,16 +3128,27 @@ export class TrainSchedulingService {
|
||||
};
|
||||
}
|
||||
|
||||
private async loadSchedulingWagonTypeDimensions(): Promise<
|
||||
Array<{ lengthMeters: number; capacityTons: number }>
|
||||
> {
|
||||
const types = await this.dataSource.getRepository(WagonType).find({
|
||||
where: [{ code: 'NW5' }, { code: 'CW3' }],
|
||||
});
|
||||
/**
|
||||
* Every active wagon type: the slot count derives from the shortest wagon the
|
||||
* fleet can marshal, so sampling only NW5/CW3 would miss a shorter type (GW2 at
|
||||
* 12.228m) and under-report how many wagons the train length allows.
|
||||
*/
|
||||
private async loadSchedulingWagonTypeDimensions(): Promise<WagonTypeDimensions[]> {
|
||||
const types = await this.dataSource
|
||||
.getRepository(WagonType)
|
||||
.find({ where: { isActive: true } });
|
||||
if (types.length) return types.map(wagonTypeDimensionsFromEntity);
|
||||
return [
|
||||
{ lengthMeters: DEFAULT_CONTAINER_WAGON_LENGTH_METERS, capacityTons: 70 },
|
||||
{ lengthMeters: DEFAULT_BULK_WAGON_LENGTH_METERS, capacityTons: 60 },
|
||||
{
|
||||
lengthMeters: DEFAULT_CONTAINER_WAGON_LENGTH_METERS,
|
||||
capacityTons: 70,
|
||||
tareWeightTons: DEFAULT_CONTAINER_WAGON_TARE_TONS,
|
||||
},
|
||||
{
|
||||
lengthMeters: DEFAULT_BULK_WAGON_LENGTH_METERS,
|
||||
capacityTons: 60,
|
||||
tareWeightTons: DEFAULT_BULK_WAGON_TARE_TONS,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { AllocationLoadType } from '@edr/types';
|
||||
|
||||
import { Booking } from '../bookings/entities/booking.entity';
|
||||
import { WagonType } from '../wagon-types/entities/wagon-type.entity';
|
||||
import { consistViolations } from './train-capacity.util';
|
||||
|
||||
export const MAX_TRAIN_WEIGHT_TONS = 3500;
|
||||
export const MAX_TRAIN_LENGTH_METERS = 760;
|
||||
@@ -35,6 +36,9 @@ export type WagonPlanSlot = {
|
||||
wagonTypeCode: string;
|
||||
capacityTons: number;
|
||||
lengthMeters: number;
|
||||
/** Empty weight of this wagon — the locomotive pulls it whether or not it is loaded. */
|
||||
tareWeightTons: number;
|
||||
/** Cargo tons on this wagon. Gross weight = tareWeightTons + assignedWeightTons. */
|
||||
assignedWeightTons: number;
|
||||
allocations: WagonAllocationRecord[];
|
||||
slotLoadType?: SlotLoadType;
|
||||
@@ -78,6 +82,14 @@ export function roundTons(value: number | string | null | undefined): number {
|
||||
return Number(numericValue.toFixed(3));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tare of a wagon type. Nullable only on rows predating the NOT NULL backfill;
|
||||
* a missing tare must read as 0 rather than silently inventing dead weight.
|
||||
*/
|
||||
export function tareTonsOf(wagonType: Pick<WagonType, 'tareWeightTons'>): number {
|
||||
return roundTons(wagonType.tareWeightTons ?? 0);
|
||||
}
|
||||
|
||||
/** TEU slots on a wagon: 40ft = 2, 20ft = 1 (max 2 TEU / wagon). */
|
||||
export function teuSlotsForSizeFt(sizeFt: number): number {
|
||||
return sizeFt >= 40 ? 2 : 1;
|
||||
@@ -146,6 +158,7 @@ export function buildContainerWagonPlan(
|
||||
wagonTypeCode: wagonType.code,
|
||||
capacityTons: Number(wagonType.capacityTons),
|
||||
lengthMeters: Number(wagonType.lengthMeters),
|
||||
tareWeightTons: tareTonsOf(wagonType),
|
||||
assignedWeightTons: 0,
|
||||
allocations: [],
|
||||
}));
|
||||
@@ -175,6 +188,7 @@ export function buildBulkWagonPlan(
|
||||
wagonTypeCode: wagonType.code,
|
||||
capacityTons: capacity,
|
||||
lengthMeters: Number(wagonType.lengthMeters),
|
||||
tareWeightTons: tareTonsOf(wagonType),
|
||||
assignedWeightTons: 0,
|
||||
allocations: [],
|
||||
}));
|
||||
@@ -214,6 +228,7 @@ export function buildMixedWagonPlan(
|
||||
wagonTypeCode: containerWagonType.code,
|
||||
capacityTons: Number(containerWagonType.capacityTons),
|
||||
lengthMeters: Number(containerWagonType.lengthMeters),
|
||||
tareWeightTons: tareTonsOf(containerWagonType),
|
||||
assignedWeightTons: 0,
|
||||
allocations: [],
|
||||
slotLoadType: 'CONTAINER',
|
||||
@@ -443,47 +458,46 @@ export function validateBulkWagonSlotWeights(wagonPlan: WagonPlanSlot[]): string
|
||||
return violations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check a consist against its train's three limits. Weight is GROSS — every slot
|
||||
* contributes its own tare plus the cargo assigned to it — because the locomotive
|
||||
* pull limit governs what it drags, not what was sold. Length and tare are summed
|
||||
* per slot, so a mixed consist is measured as it actually stands rather than
|
||||
* through one representative wagon type.
|
||||
*
|
||||
* `wagonType` only supplies the fallback wagon count when `limits.maxWagonsPerTrain`
|
||||
* is absent; slot dimensions always win over it.
|
||||
*/
|
||||
export function validateTrainLimits(
|
||||
wagonPlan: WagonPlanSlot[],
|
||||
wagonType: Pick<WagonType, 'lengthMeters'>,
|
||||
limits?: TrainLimitConfig,
|
||||
): string[] {
|
||||
const violations: string[] = [];
|
||||
const maxWeightTons = limits?.maxWeightTons ?? MAX_TRAIN_WEIGHT_TONS;
|
||||
const maxLengthMeters = limits?.maxLengthMeters ?? MAX_TRAIN_LENGTH_METERS;
|
||||
const wagonLength = Number(wagonType.lengthMeters) || 14;
|
||||
const maxWagonsPerTrain =
|
||||
limits?.maxWagonsPerTrain ??
|
||||
Math.floor(maxLengthMeters / wagonLength);
|
||||
const maxWagonSlots =
|
||||
limits?.maxWagonsPerTrain ?? Math.floor(maxLengthMeters / wagonLength);
|
||||
|
||||
const totalWeightTons = roundTons(
|
||||
wagonPlan.reduce((sum, w) => sum + w.assignedWeightTons, 0),
|
||||
const violations = consistViolations(
|
||||
wagonPlan.map((slot) => ({
|
||||
lengthMeters: Number(slot.lengthMeters),
|
||||
tareWeightTons: Number(slot.tareWeightTons ?? 0),
|
||||
cargoTons: Number(slot.assignedWeightTons),
|
||||
})),
|
||||
{ maxWeightTons, maxLengthMeters, maxWagonSlots },
|
||||
);
|
||||
const totalLengthMeters = roundTons(
|
||||
wagonPlan.reduce((sum, w) => sum + w.lengthMeters, 0),
|
||||
);
|
||||
|
||||
if (totalWeightTons > maxWeightTons) {
|
||||
violations.push(
|
||||
`Total booking weight ${totalWeightTons}T exceeds max train weight ${maxWeightTons}T`,
|
||||
);
|
||||
}
|
||||
if (totalLengthMeters > maxLengthMeters) {
|
||||
violations.push(
|
||||
`Total wagon length ${totalLengthMeters}m exceeds max train length ${maxLengthMeters}m`,
|
||||
);
|
||||
}
|
||||
if (wagonPlan.length > maxWagonsPerTrain) {
|
||||
violations.push(
|
||||
`Wagon count ${wagonPlan.length} exceeds max wagons per train (${maxWagonsPerTrain})`,
|
||||
);
|
||||
}
|
||||
|
||||
violations.push(...validateBulkWagonSlotWeights(wagonPlan));
|
||||
|
||||
return violations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixed consist: the wagon-count fallback uses the shortest type present, since
|
||||
* that is the most wagons that could ever fit. Weight and length still come from
|
||||
* the slots themselves.
|
||||
*/
|
||||
export function validateMixedTrainLimits(
|
||||
wagonPlan: WagonPlanSlot[],
|
||||
wagonTypes: WagonType[],
|
||||
|
||||
Reference in New Issue
Block a user