diff --git a/apps/edr-freight-web/portal/src/pages/bookings/new-booking-form/schema.ts b/apps/edr-freight-web/portal/src/pages/bookings/new-booking-form/schema.ts index ea4b1214a..0d7ebee79 100644 --- a/apps/edr-freight-web/portal/src/pages/bookings/new-booking-form/schema.ts +++ b/apps/edr-freight-web/portal/src/pages/bookings/new-booking-form/schema.ts @@ -288,22 +288,33 @@ export interface WagonConfig { type: "20ft" | "40ft"; } +/** + * Derive the trade direction from the origin/destination yard countries. + * + * Mirrors the backend's `deriveTradeDirection` exactly so the value the portal + * sends always matches what the API re-derives (the API rejects mismatches): + * - origin in Djibouti → IMPORT + * - destination in Djibouti (origin not) → EXPORT + * - everything else (e.g. Ethiopia↔Ethiopia)→ DOMESTIC + * + * Returns null only while a yard is still unselected, so the UI can wait. + */ export function getRouteDirection( origin: Freight.BookingReferenceYard | null | undefined, dest: Freight.BookingReferenceYard | null | undefined, ): Freight.ScheduleTradeDirection | null { if (!origin || !dest) return null; - if (origin.country === "Ethiopia" && dest.country === "Ethiopia") { - return "DOMESTIC"; - } - if (origin.country === "Ethiopia" && dest.country === "Djibouti") { - return "EXPORT"; - } - if (origin.country === "Djibouti" && dest.country === "Ethiopia") { + + const originCountry = origin.country?.trim(); + const destCountry = dest.country?.trim(); + + if (originCountry === "Djibouti") { return "IMPORT"; } - - return null; + if (destCountry === "Djibouti" && originCountry !== "Djibouti") { + return "EXPORT"; + } + return "DOMESTIC"; } export function calcWagons(containers: ContainerConfig[]) {