This commit is contained in:
Hagernesh
2026-07-04 09:42:09 +00:00
parent 00cd1fccf6
commit d8da2e49cb

View File

@@ -1207,8 +1207,10 @@ export class TrainSchedulingService {
const schedule = await this.getImportDjiboutiSchedule(scheduleId); const schedule = await this.getImportDjiboutiSchedule(scheduleId);
const operation = await this.getOrCreateImportDjiboutiOperation(scheduleId); const operation = await this.getOrCreateImportDjiboutiOperation(scheduleId);
this.assertImportDjiboutiGatepassGranted(operation); this.assertImportDjiboutiGatepassGranted(operation);
if (!operation.loadedOnTrainAt) { if (!operation.loadedOnTrainAt && !(await this.hasLoadedBooking(schedule.id))) {
throw new BadRequestException('Import train cannot depart Djibouti before loading is confirmed'); throw new BadRequestException(
'Import train cannot depart Djibouti before at least one booking is loaded',
);
} }
if (schedule.status === TrainScheduleStatusEnum.Scheduled) { if (schedule.status === TrainScheduleStatusEnum.Scheduled) {
@@ -1586,11 +1588,28 @@ export class TrainSchedulingService {
where: { trainScheduleId: schedule.id }, where: { trainScheduleId: schedule.id },
}); });
this.assertImportDjiboutiGatepassGranted(operation); this.assertImportDjiboutiGatepassGranted(operation);
if (!operation?.loadedOnTrainAt) { // A train dispatches once loading is confirmed at the train level OR at least
throw new BadRequestException('Import train cannot depart Djibouti before loading is confirmed'); // one booking on it is marked LOADED — it never waits for every booking.
if (!operation?.loadedOnTrainAt && !(await this.hasLoadedBooking(schedule.id))) {
throw new BadRequestException(
'Import train cannot depart Djibouti before at least one booking is loaded',
);
} }
} }
/** True when at least one booking on the schedule is confirmed LOADED. */
private async hasLoadedBooking(scheduleId: string): Promise<boolean> {
const [row]: Array<{ loaded: string }> = await this.dataSource.query(
`SELECT COUNT(*) AS loaded
FROM freight.train_schedule_bookings
WHERE train_schedule_id = $1
AND loading_status = $2
AND deleted_at IS NULL`,
[scheduleId, LoadingStatus.Loaded],
);
return Number(row?.loaded ?? 0) > 0;
}
private async getImportDjiboutiSchedule(scheduleId: string): Promise<TrainSchedule> { private async getImportDjiboutiSchedule(scheduleId: string): Promise<TrainSchedule> {
const schedule = await this.getDjiboutiGatepassSchedule(scheduleId); const schedule = await this.getDjiboutiGatepassSchedule(scheduleId);
if (!this.isImportDjiboutiSchedule(schedule)) { if (!this.isImportDjiboutiSchedule(schedule)) {