update train scheduling logic to clear trainScheduleId when unassigning bookings

This commit is contained in:
Marshal
2026-07-13 11:47:27 +00:00
parent 9e748d6291
commit 3fe4ea72ac
2 changed files with 18 additions and 3 deletions

View File

@@ -1269,7 +1269,12 @@ export class BookingsRepository extends BaseRepository<Booking> {
fields: Partial<
Pick<
Booking,
'schedulingStatus' | 'wagonsRequired' | 'scheduledAt' | 'holdStartedAt' | 'holdExpiresAt'
| 'schedulingStatus'
| 'wagonsRequired'
| 'scheduledAt'
| 'holdStartedAt'
| 'holdExpiresAt'
| 'trainScheduleId'
>
>,
manager?: EntityManager,

View File

@@ -1387,9 +1387,13 @@ export class TrainSchedulingService {
const booking = await this.bookingsRepository.findById(bookingId);
const schedulingStatus = this.resolvePostUnassignStatus(booking);
// Clear the schedule pointer too: unassign fully detaches the booking from
// this train. Leaving trainScheduleId set glued the booking to a schedule
// that may then be dispatched/cancelled/deleted, orphaning it — the
// assign-bookings parity guard would reject it from every OTHER schedule.
await this.bookingsRepository.updateSchedulingFields(
bookingId,
{ schedulingStatus, wagonsRequired: null },
{ schedulingStatus, wagonsRequired: null, trainScheduleId: null },
manager,
);
@@ -3043,9 +3047,15 @@ export class TrainSchedulingService {
}
for (const sb of schedule.scheduleBookings ?? []) {
const booking = await this.bookingsRepository.findById(sb.bookingId);
// Detach from the cancelled schedule — clear the pointer so the freed
// booking can be assigned to another train. Leaving it set orphans the
// booking against a schedule that is about to be gone.
await this.bookingsRepository.updateSchedulingFields(
sb.bookingId,
{ schedulingStatus: this.resolvePostUnassignStatus(booking) },
{
schedulingStatus: this.resolvePostUnassignStatus(booking),
trainScheduleId: null,
},
manager,
);
}