mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-28 05:30:55 +00:00
Refactor wagon specifications to rely on wagon type; remove tare weight and max payload from wagon entity and related components
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
/**
|
||||
* Wagon spec belongs to the wagon TYPE, not to each physical wagon.
|
||||
*
|
||||
* `wagons.tare_weight` and `wagons.max_payload_weight` duplicated
|
||||
* `wagon_types.tare_weight_tons` / `wagon_types.capacity_tons` on all 1100 rows,
|
||||
* with nothing keeping them in step. They had drifted completely: every wagon
|
||||
* disagreed with its type's tare (seeded ~20T against a real 22.4T NW5), and a
|
||||
* third disagreed on payload (NW5 wagons claiming 22T–70T against a flat 70T).
|
||||
* None of those numbers came from the railway.
|
||||
*
|
||||
* Nothing reads them for capacity — that math resolves tare and capacity through
|
||||
* `wagon_type_id` — so dropping them removes a source of fiction rather than a
|
||||
* source of truth. `wagon_type_id` is NOT NULL with no orphans, so the type is
|
||||
* always reachable.
|
||||
*
|
||||
* A wagon re-tared after repair would need a nullable override column on
|
||||
* `wagons` falling back to the type; deliberately not added, since no such
|
||||
* per-wagon value exists today.
|
||||
*/
|
||||
export class DropWagonSpecColumns2080000000000 implements MigrationInterface {
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE freight.wagons
|
||||
DROP COLUMN IF EXISTS tare_weight,
|
||||
DROP COLUMN IF EXISTS max_payload_weight;
|
||||
`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
// Re-add nullable, backfill from the owning type, then restore NOT NULL.
|
||||
// The pre-drop values were drifted seed data and are not recoverable — the
|
||||
// type's spec is what they should always have held.
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE freight.wagons
|
||||
ADD COLUMN IF NOT EXISTS tare_weight NUMERIC(10, 2),
|
||||
ADD COLUMN IF NOT EXISTS max_payload_weight NUMERIC(10, 2);
|
||||
`);
|
||||
await queryRunner.query(`
|
||||
UPDATE freight.wagons w
|
||||
SET tare_weight = t.tare_weight_tons,
|
||||
max_payload_weight = t.capacity_tons
|
||||
FROM freight.wagon_types t
|
||||
WHERE t.id = w.wagon_type_id;
|
||||
`);
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE freight.wagons
|
||||
ALTER COLUMN tare_weight SET NOT NULL,
|
||||
ALTER COLUMN max_payload_weight SET NOT NULL;
|
||||
`);
|
||||
}
|
||||
}
|
||||
@@ -1900,7 +1900,7 @@ export class TrainSchedulingService {
|
||||
<td>${esc(wagon.physicalWagon?.wagonNumber)}</td>
|
||||
<td>${esc(wagon.wagonType?.code ?? wagon.wagonType?.name)}</td>
|
||||
<td class="num">${esc(Number(wagon.lengthMeters || 0).toFixed(3))}</td>
|
||||
<td class="num">${esc(Number(wagon.physicalWagon?.tareWeight ?? 0).toFixed(2))}</td>
|
||||
<td class="num">${esc(Number(wagon.wagonType?.tareWeightTons ?? 0).toFixed(2))}</td>
|
||||
<td class="num">${esc(Number(wagon.capacityTons || 0).toFixed(3))}</td>
|
||||
<td>${esc(company?.name ?? company?.legalName ?? company?.tradeName ?? booking?.companyId)}</td>
|
||||
<td>${esc(booking?.companyId)}</td>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { WagonStatus } from '@edr/types';
|
||||
import { IsString, IsUUID, IsOptional, IsInt, Min, IsNumber, IsEnum } from 'class-validator';
|
||||
import { IsString, IsUUID, IsOptional, IsInt, Min, IsEnum } from 'class-validator';
|
||||
|
||||
export class CreateWagonDto {
|
||||
@IsString()
|
||||
@@ -17,13 +17,8 @@ export class CreateWagonDto {
|
||||
@Min(1)
|
||||
sequenceNumber?: number;
|
||||
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
tareWeight!: number;
|
||||
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
maxPayloadWeight!: number;
|
||||
// Tare weight and payload capacity are not accepted here: they belong to the
|
||||
// wagon type and are resolved through wagonTypeId.
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(WagonStatus)
|
||||
|
||||
@@ -7,6 +7,7 @@ import { TrainSchedule } from '../../train-schedules/entities/train-schedule.ent
|
||||
import { TrainSetWagon } from '../../train-sets/entities/train-set-wagon.entity';
|
||||
import { Container } from '../../container-management/entities/container.entity';
|
||||
import { Yard } from '../../rule-engine/entities/yard.entity';
|
||||
import { WagonType } from '../../wagon-types/entities/wagon-type.entity';
|
||||
|
||||
export const WAGON_STATUSES = [
|
||||
WagonStatus.Available,
|
||||
@@ -28,17 +29,19 @@ export class Wagon extends BaseEntity {
|
||||
@Column({ name: 'wagon_type_id', type: 'uuid' })
|
||||
wagonTypeId!: string;
|
||||
|
||||
/** Owns this wagon's spec: tare weight, payload capacity, length. */
|
||||
@ManyToOne(() => WagonType)
|
||||
@JoinColumn({ name: 'wagon_type_id' })
|
||||
wagonType?: WagonType;
|
||||
|
||||
@Column({ name: 'train_id', type: 'uuid', nullable: true })
|
||||
trainId!: string | null;
|
||||
|
||||
@Column({ name: 'sequence_number', type: 'int', nullable: true })
|
||||
sequenceNumber!: number | null;
|
||||
|
||||
@Column({ name: 'tare_weight', type: 'decimal', precision: 10, scale: 2 })
|
||||
tareWeight!: number;
|
||||
|
||||
@Column({ name: 'max_payload_weight', type: 'decimal', precision: 10, scale: 2 })
|
||||
maxPayloadWeight!: number;
|
||||
// Tare weight and payload capacity are properties of the wagon TYPE — read them
|
||||
// through `wagonType`, never off the individual wagon.
|
||||
|
||||
@Column({ type: 'varchar', length: 20, default: WagonStatus.Available })
|
||||
status!: WagonStatusType;
|
||||
|
||||
@@ -52,14 +52,23 @@ export class WagonsService {
|
||||
});
|
||||
}
|
||||
|
||||
const sortBy = ['wagonNumber', 'tareWeight', 'maxPayloadWeight', 'status', 'currentYardId', 'sequenceNumber'].includes(query.sortBy ?? '')
|
||||
// Spec columns (tare, payload) are no longer sortable here — they live on the
|
||||
// wagon type, so sorting by them is sorting by wagonTypeId.
|
||||
const sortable: Array<keyof Wagon> = [
|
||||
'wagonNumber',
|
||||
'status',
|
||||
'currentYardId',
|
||||
'sequenceNumber',
|
||||
'wagonTypeId',
|
||||
];
|
||||
const sortBy = sortable.includes((query.sortBy ?? '') as keyof Wagon)
|
||||
? (query.sortBy as keyof Wagon)
|
||||
: 'wagonNumber';
|
||||
const sortOrder = query.sortOrder?.toUpperCase() === 'DESC' ? 'DESC' : 'ASC';
|
||||
|
||||
return this.wagonRepo.find({
|
||||
where: search ? where : filters,
|
||||
relations: { currentYard: true },
|
||||
relations: { currentYard: true, wagonType: true },
|
||||
order: { [sortBy]: sortOrder } as FindOptionsOrder<Wagon>,
|
||||
skip: query.page && query.limit ? (Number(query.page) - 1) * Number(query.limit) : undefined,
|
||||
take: query.limit ? Number(query.limit) : undefined,
|
||||
@@ -69,7 +78,7 @@ export class WagonsService {
|
||||
async findById(id: string): Promise<Wagon> {
|
||||
const wagon = await this.wagonRepo.findOne({
|
||||
where: { id },
|
||||
relations: { currentYard: true },
|
||||
relations: { currentYard: true, wagonType: true },
|
||||
});
|
||||
if (!wagon) throw new NotFoundException(`Wagon ${id} not found`);
|
||||
return wagon;
|
||||
|
||||
@@ -419,8 +419,6 @@ async function ensureWagon(manager: any, scenario: ScenarioTrain, sequenceNo: nu
|
||||
wagonTypeId,
|
||||
trainId: null,
|
||||
sequenceNumber: sequenceNo,
|
||||
tareWeight: 20,
|
||||
maxPayloadWeight: 70,
|
||||
status: WagonStatus.Assigned,
|
||||
currentYardId: yardId,
|
||||
currentTrainScheduleId: scheduleId,
|
||||
|
||||
@@ -306,8 +306,6 @@ async function main() {
|
||||
wagonTypeId: wagonType.id,
|
||||
trainId: null,
|
||||
sequenceNumber: sequenceNo,
|
||||
tareWeight: 20,
|
||||
maxPayloadWeight: 70,
|
||||
status: WagonStatus.Assigned,
|
||||
currentYardId: indode.id,
|
||||
notes: 'Demo wagon for Negad to Indode marshalling',
|
||||
|
||||
@@ -504,8 +504,6 @@ export class DemoBookingsSeeder {
|
||||
wagonTypeId: nw5.id,
|
||||
trainId: null,
|
||||
sequenceNumber: null,
|
||||
tareWeight: 20,
|
||||
maxPayloadWeight: 70,
|
||||
status: WagonStatus.Available,
|
||||
currentYardId: index % 2 === 0 ? djibouti.id : addis.id,
|
||||
notes: "Demo wagon for train scheduling",
|
||||
|
||||
@@ -76,15 +76,11 @@ export class DemoFreightDataSeeder {
|
||||
}
|
||||
|
||||
const toCreate = MIN_WAGONS_PER_TYPE - existing;
|
||||
const tare = Number(type.tareWeightTons ?? 20);
|
||||
const maxPayload = Number(type.capacityTons ?? 60);
|
||||
const rows = Array.from({ length: toCreate }, (_, i) => {
|
||||
const seq = existing + i + 1;
|
||||
return wagonRepo.create({
|
||||
wagonNumber: `${type.code}-${String(seq).padStart(4, '0')}`,
|
||||
wagonTypeId: type.id,
|
||||
tareWeight: tare,
|
||||
maxPayloadWeight: maxPayload,
|
||||
status: WagonStatus.Available,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -203,7 +203,6 @@ export class MarshallingDemoTrainsSeeder {
|
||||
const totalWeight = bookingWeights.reduce((sum, weight) => sum + weight, 0);
|
||||
const wagonCapacity = Number(refs.wagonType.capacityTons) || 70;
|
||||
const wagonLength = Number(refs.wagonType.lengthMeters) || 14;
|
||||
const tareWeight = Number(refs.wagonType.tareWeightTons) || 14;
|
||||
|
||||
const trainSet = await trainSetRepo.save(
|
||||
trainSetRepo.create({
|
||||
@@ -275,8 +274,6 @@ export class MarshallingDemoTrainsSeeder {
|
||||
wagonTypeId: refs.wagonType.id,
|
||||
yardId: originYard.id,
|
||||
trainScheduleId: schedule.id,
|
||||
tareWeight,
|
||||
capacityTons: wagonCapacity,
|
||||
dispatched: hasDeparted,
|
||||
});
|
||||
|
||||
@@ -420,8 +417,6 @@ export class MarshallingDemoTrainsSeeder {
|
||||
wagonTypeId: string;
|
||||
yardId: string;
|
||||
trainScheduleId: string;
|
||||
tareWeight: number;
|
||||
capacityTons: number;
|
||||
dispatched: boolean;
|
||||
}): Promise<Wagon> {
|
||||
const repo = this.dataSource.getRepository(Wagon);
|
||||
@@ -433,8 +428,6 @@ export class MarshallingDemoTrainsSeeder {
|
||||
wagonTypeId: input.wagonTypeId,
|
||||
currentYardId: input.yardId,
|
||||
currentTrainScheduleId: input.trainScheduleId,
|
||||
tareWeight: input.tareWeight,
|
||||
maxPayloadWeight: input.capacityTons,
|
||||
status: WagonStatus.Assigned,
|
||||
notes: 'Marshalling demo seed wagon',
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user