From ec48a8462fe9e4f253151053df9ca2de7f61d497 Mon Sep 17 00:00:00 2001 From: Hagernesh Date: Fri, 31 Jul 2026 13:33:06 +0000 Subject: [PATCH] fix(freight-api): auto-unload bookings on final-yard bulk arrival MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit autoArriveAtFinalYard() bulk-updated booking status to ARRIVED/COMPLETED via raw SQL but never emitted booking.unloadedAtYard, so WarehouseInventoryService's listener never created the warehouse_inventory row — bookings caught by this fallback (e.g. KALITY import arrivals) stayed on awaiting unload indefinitely. Emit the event per row, covering both intercity (DOMESTIC) and IMPORT as the listener already expects. --- .../train-scheduling/booking-journey.service.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/apps/edr-freight-api/src/modules/train-scheduling/booking-journey.service.ts b/apps/edr-freight-api/src/modules/train-scheduling/booking-journey.service.ts index 50835d5b0..87e756ba6 100644 --- a/apps/edr-freight-api/src/modules/train-scheduling/booking-journey.service.ts +++ b/apps/edr-freight-api/src/modules/train-scheduling/booking-journey.service.ts @@ -327,11 +327,19 @@ export class BookingJourneyService { RETURNING b.id, b.trade_direction`, [schedule.id, schedule.destinationStationId, now], ); - // Intercity rows just completed — let a ONE_TIME contract close on delivery. for (const row of rows) { + // Intercity rows just completed — let a ONE_TIME contract close on delivery. if (row.trade_direction === 'DOMESTIC') { this.events.emit('booking.completed', { bookingId: row.id }); } + // Same event the per-booking unloadBooking() path emits — WarehouseInventoryService + // listens for this to auto-create the warehouse_inventory row (import/intercity only, + // it filters EXPORT itself). The bulk SQL update above skipped this entirely, so + // bookings caught by this fallback never left "awaiting unload". + this.events.emit('booking.unloadedAtYard', { + bookingId: row.id, + tradeDirection: row.trade_direction, + }); } return rows.map((r) => r.id); }