// apps/edr-freight-api/src/modules/trains/entities/train.entity.ts import { BaseEntity } from '@edr/api-common'; import { Freight } from '@edr/types'; import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm'; import { Yard } from '../../rule-engine/entities/yard.entity'; import { Wagon } from '../../wagons/entities/wagon.entity'; import { TrainLocomotive } from './train-locomotive.entity'; /** * Fleet master data — a train built in the Train Builder: a coded consist * (e.g. 81001) of 2+ locomotives and ordered wagons, assembled in one yard. * Operational departures reference it through `train_sets.train_id`; the * schedule's own composition still lives on the train set. */ @Entity({ schema: 'freight', name: 'trains' }) export class Train extends BaseEntity { // --- existing fields (keep for backward compatibility) --- @Column({ name: 'code', type: 'varchar', length: 32, unique: true }) code!: string; @Column({ name: 'capacity_tons', type: 'numeric', precision: 10, scale: 2 }) capacityTons!: number; @Column({ name: 'status', type: 'enum', enum: Freight.TrainStatus, default: Freight.TrainStatus.Available, }) status!: Freight.TrainStatus; @Column({ name: 'notes', type: 'text', nullable: true }) notes?: string | null; // --- new required fields --- @Column({ name: 'train_number', type: 'varchar', length: 20, unique: true, nullable: true }) trainNumber?: string; @Column({ name: 'train_name', type: 'varchar', length: 100, nullable: true }) trainName?: string; @Column({ name: 'route_id', type: 'uuid', nullable: true }) routeId?: string; @Column({ name: 'origin_station_id', type: 'uuid', nullable: true }) originStationId?: string; @Column({ name: 'destination_station_id', type: 'uuid', nullable: true }) destinationStationId?: string; @Column({ name: 'departure_time', type: 'timestamp', nullable: true }) departureTime?: Date; @Column({ name: 'arrival_time', type: 'timestamp', nullable: true }) arrivalTime?: Date; @Column({ name: 'locomotive_number', type: 'varchar', length: 50, nullable: true }) locomotiveNumber?: string; @Column({ name: 'remarks', type: 'text', nullable: true }) remarks?: string; /** Yard where the train currently sits (set at build, moved on schedule arrival). */ @Column({ name: 'current_yard_id', type: 'uuid', nullable: true }) currentYardId!: string | null; @ManyToOne(() => Yard, { nullable: true, onDelete: 'SET NULL' }) @JoinColumn({ name: 'current_yard_id' }) currentYard?: Yard | null; // --- relationships --- @OneToMany(() => Wagon, (wagon) => wagon.train) wagons!: Wagon[]; /** Locomotives pulling this train (minimum 2), ordered by sequenceNo. */ @OneToMany(() => TrainLocomotive, (link) => link.train) locomotives?: TrainLocomotive[]; }