fix: improve route direction logic to align with backend derivation rules

This commit is contained in:
Marshal
2026-06-18 12:52:01 +00:00
parent 62038458f8
commit 578012dffd

View File

@@ -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[]) {