fix intercity unloading at facility

This commit is contained in:
Hagernesh
2026-08-01 09:10:04 +00:00
parent c3ba0192a9
commit 035369af43
2 changed files with 41 additions and 4 deletions

View File

@@ -27,12 +27,20 @@ async function main() {
const dataSource = app.get(DataSource);
const inventory = app.get(WarehouseInventoryService);
// Skip bookings destined for a pure facility yard (has a facility but no
// warehouse, e.g. Dire Dawa) — those are fully represented by their
// facility_handling_events UNLOAD record, not a warehouse_inventory row.
// Same gate as BookingJourneyService.autoArriveAtFinalYard.
const bookings: { id: string; tradeDirection: string }[] = await dataSource.query(
`SELECT b.id, b.trade_direction AS "tradeDirection"
FROM freight.bookings b
JOIN freight.yards dy ON dy.id = b.destination_yard_id
LEFT JOIN freight.yard_facilities yf
ON yf.yard_id = dy.id AND yf.deleted_at IS NULL AND yf.is_active = true
WHERE b.deleted_at IS NULL
AND b.trade_direction IN ('IMPORT', 'DOMESTIC')
AND b.status IN ('ARRIVED', 'COMPLETED')
AND NOT (dy.has_facility = true AND COALESCE(yf.has_warehouse, false) = false)
AND NOT EXISTS (
SELECT 1 FROM freight.warehouse_inventory wi
WHERE wi.booking_id = b.id AND wi.deleted_at IS NULL