add yard distances management to rule engine

- Introduced new yard distances resource with CRUD operations.
- Created migration for yard distances table with necessary constraints.
- Implemented service and repository for yard distances handling.
- Added controller for API endpoints to manage yard distances.
- Updated rule engine configuration to include yard distances.
- Enhanced rule engine resource page to support yard distance selection.
- Updated contracts and train builder pages to handle new yard distance logic.
- Added error handling utility for better error message extraction.
This commit is contained in:
Marshal
2026-07-21 08:50:50 +00:00
parent 1647681840
commit 603537a20b
45 changed files with 1275 additions and 227 deletions

View File

@@ -29,6 +29,9 @@ import {
const round = (value: unknown) => Math.round((Number(value) || 0) * 100) / 100;
/** Locomotive statuses that block a train from reactivating. */
const UNFIT_FOR_REACTIVATION = new Set(['MAINTENANCE', 'OUT_OF_SERVICE', 'UNAVAILABLE']);
/** The one active (DRAFT/SCHEDULED/DISPATCHED) schedule surfaced per built train. */
export interface ActiveScheduleRef {
id: string;
@@ -596,11 +599,30 @@ export class TrainBuilderService {
return this.getComposition(id);
}
/** Reactivate a DEACTIVATED train back to AVAILABLE so it can be scheduled again. */
/**
* Reactivate a DEACTIVATED train back to AVAILABLE so it can be scheduled
* again. Blocked if any coupled locomotive is unfit for service — a
* deactivated train can sit parked for a while and its locomotives may have
* since been sent to maintenance independently; reactivating must not wave
* a down locomotive back onto the schedule board.
*/
async activate(id: string) {
const train = await this.dataSource.getRepository(Train).findOne({ where: { id } });
if (!train) throw new NotFoundException(`Train ${id} not found`);
if (train.status === Freight.TrainStatus.Deactivated) {
const links = await this.dataSource
.getRepository(TrainLocomotive)
.find({ where: { trainId: id }, relations: { locomotive: true } });
const unfit = links
.map((link) => link.locomotive)
.filter((loco): loco is Locomotive => Boolean(loco))
.filter((loco) => UNFIT_FOR_REACTIVATION.has(loco.status));
if (unfit.length) {
const names = unfit.map((l) => `${l.code} (${l.status})`).join(', ');
throw new ConflictException(
`Train cannot be reactivated: ${names} ${unfit.length > 1 ? 'are' : 'is'} not fit for service. Detach and replace before reactivating.`,
);
}
await this.dataSource
.getRepository(Train)
.update(id, { status: Freight.TrainStatus.Available });