feat(freight): added routes and locomotives

This commit is contained in:
Michael Abebe
2026-06-06 16:23:48 +03:00
parent e067da29df
commit a9c2f2eb97
32 changed files with 1443 additions and 64 deletions

View File

@@ -191,6 +191,7 @@ export class TrainSchedulingService {
const locomotive = await this.selectOrValidateLocomotive(
dto.locomotiveId,
validation.summary.totalWeightTons,
validation.summary.totalLengthMeters,
);
const createdSchedule = await this.dataSource.transaction(
@@ -220,6 +221,15 @@ export class TrainSchedulingService {
);
}
if (
Number(lockedLocomotive.maxTrainLengthMeters) <
validation.summary.totalLengthMeters
) {
throw new BadRequestException(
`Locomotive ${lockedLocomotive.code} cannot support ${validation.summary.totalLengthMeters}m`,
);
}
const existingScheduleCount = await manager
.getRepository(TrainScheduleBooking)
.count({
@@ -461,10 +471,14 @@ export class TrainSchedulingService {
where: { status: "AVAILABLE" },
});
const canPull = capableLocomotives.some(
(locomotive) => Number(locomotive.maxPullWeightTons) >= totalWeightTons,
(locomotive) =>
Number(locomotive.maxPullWeightTons) >= totalWeightTons &&
Number(locomotive.maxTrainLengthMeters) >= totalLengthMeters,
);
if (!canPull) {
violations.push("No available locomotive can pull the total weight");
violations.push(
'No available locomotive can support the total train weight and length',
);
}
}
@@ -513,6 +527,7 @@ export class TrainSchedulingService {
async selectOrValidateLocomotive(
locomotiveId: string,
totalWeightTons: number,
totalLengthMeters: number,
) {
const locomotive = await this.locomotivesRepository.findById(locomotiveId);
@@ -532,6 +547,12 @@ export class TrainSchedulingService {
);
}
if (Number(locomotive.maxTrainLengthMeters) < totalLengthMeters) {
throw new BadRequestException(
`Locomotive ${locomotive.code} cannot support ${totalLengthMeters}m`,
);
}
return locomotive;
}
@@ -711,6 +732,9 @@ export class TrainSchedulingService {
maxPullWeightTons: this.roundTons(
Number(schedule.trainSet.locomotive.maxPullWeightTons),
),
maxTrainLengthMeters: this.roundTons(
Number(schedule.trainSet.locomotive.maxTrainLengthMeters),
),
}
: null,
wagons: [...(schedule.trainSet.wagons ?? [])]