mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-27 00:52:50 +00:00
34 lines
1.3 KiB
TypeScript
34 lines
1.3 KiB
TypeScript
import { YardCountry, type ScheduleTradeDirection } from '@edr/types';
|
|
|
|
type YardLike = { country?: string | null };
|
|
|
|
/**
|
|
* Derive trade direction from origin and destination yard countries.
|
|
* Ethiopia → Djibouti = EXPORT, Djibouti → Ethiopia = IMPORT, same country =
|
|
* DOMESTIC (shown as "Intercity"; scheduling/contracts reject it for now).
|
|
* Comparison is strict against the YardCountry enum values the yards table is
|
|
* constrained to; the trim/case fold only shields legacy rows.
|
|
*/
|
|
export function deriveTradeDirection(
|
|
originYard: YardLike,
|
|
destinationYard: YardLike,
|
|
): ScheduleTradeDirection {
|
|
const origin = normalizeCountry(originYard.country);
|
|
const destination = normalizeCountry(destinationYard.country);
|
|
|
|
if (origin === YardCountry.DJIBOUTI && destination === YardCountry.ETHIOPIA) {
|
|
return 'IMPORT';
|
|
}
|
|
if (origin === YardCountry.ETHIOPIA && destination === YardCountry.DJIBOUTI) {
|
|
return 'EXPORT';
|
|
}
|
|
return 'DOMESTIC';
|
|
}
|
|
|
|
function normalizeCountry(country: string | null | undefined): YardCountry | null {
|
|
const folded = country?.trim().toLowerCase();
|
|
if (folded === YardCountry.ETHIOPIA.toLowerCase()) return YardCountry.ETHIOPIA;
|
|
if (folded === YardCountry.DJIBOUTI.toLowerCase()) return YardCountry.DJIBOUTI;
|
|
return null;
|
|
}
|