import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import type { ContainerOutboundActionRequest, ContainerOutboundAggregate, ContainerOutboundDispatchDetail, ContainerOutboundGateResult, ContainerOutboundInboundBinding, ContainerOutboundInboundGate, OutboundDispatchDetail, OutboundDispatchQuery, OutboundDispatchResource, OutboundGateResult, } from '../types' const basePath = '/platform/v1/first-leg/outbound-dispatches'; const keys = ['orderId', 'inboundId', 'containerBookingId', 'customerId'] as const function item(raw: OutboundDispatchResource): OutboundDispatchResource { const value = { ...raw } as OutboundDispatchResource & Record; for (const key of keys) if (value[key] != null) value[key] = String(value[key]); value.version = Number(value.version ?? 0); return value } function idem() { return `outbound-dispatch-${Date.now()}-${Math.random().toString(36).slice(2)}` } export async function listOutboundDispatches(query: OutboundDispatchQuery) { const response = await http.get, ApiEnvelope>(basePath, { params: query }); return (unwrapApiResponse(response) || []).map(item) } export async function getOutboundDispatchDetail(query: OutboundDispatchQuery) { const response = await http.get, ApiEnvelope>(`${basePath}/detail`, { params: query }); const data = unwrapApiResponse(response); return { ...data, aggregate: item(data.aggregate) } } export async function checkOutboundGate(query: OutboundDispatchQuery) { const response = await http.get, ApiEnvelope>(`${basePath}/check-gate`, { params: query }); return unwrapApiResponse(response) } export async function changeOutboundDispatch(action: 'confirm-dispatch' | 'reverse-dispatch', request: { inboundId: string; containerBookingId?: string | null; expectedVersion: number; remark?: string }) { const response = await http.post, ApiEnvelope>(`${basePath}/${action}`, request, { headers: { 'Idempotency-Key': idem() } }); const data = unwrapApiResponse(response); return { ...data, aggregate: item(data.aggregate) } } function optionalId(value: unknown): string | null { return value == null ? null : String(value) } function inboundBinding(raw: ContainerOutboundInboundBinding): ContainerOutboundInboundBinding { return { ...raw, orderId: String(raw.orderId), inboundId: String(raw.inboundId), tenantId: String(raw.tenantId), customerId: optionalId(raw.customerId), pickTaskId: optionalId(raw.pickTaskId), } } function inboundGate(raw: ContainerOutboundInboundGate): ContainerOutboundInboundGate { return { ...raw, inboundId: String(raw.inboundId), gate: { ...raw.gate, items: raw.gate?.items || [] } } } function containerGate(raw: ContainerOutboundGateResult): ContainerOutboundGateResult { return { ...raw, inbounds: (raw.inbounds || []).map(inboundGate) } } function containerAggregate(raw: ContainerOutboundAggregate): ContainerOutboundAggregate { return { ...raw, containerBookingId: String(raw.containerBookingId), loadingPlanId: String(raw.loadingPlanId), oceanBookingId: optionalId(raw.oceanBookingId), truckAppointmentId: optionalId(raw.truckAppointmentId), version: Number(raw.version ?? 0), inbounds: (raw.inbounds || []).map(inboundBinding), gate: containerGate(raw.gate), } } function containerDetail(raw: ContainerOutboundDispatchDetail): ContainerOutboundDispatchDetail { return { ...raw, aggregate: containerAggregate(raw.aggregate), timeline: (raw.timeline || []).map((entry) => ({ ...entry, operatorId: optionalId(entry.operatorId) })), } } export async function getContainerOutboundDispatch(containerBookingId: string) { const response = await http.get, ApiEnvelope>( `${basePath}/${encodeURIComponent(containerBookingId)}`, ) return containerDetail(unwrapApiResponse(response)) } export async function checkContainerOutboundGate(containerBookingId: string) { const response = await http.get, ApiEnvelope>( `${basePath}/${encodeURIComponent(containerBookingId)}/gate`, ) return containerGate(unwrapApiResponse(response)) } export async function confirmContainerOutboundDispatch(containerBookingId: string, request: ContainerOutboundActionRequest) { const response = await http.post, ApiEnvelope>( `${basePath}/${encodeURIComponent(containerBookingId)}/confirm`, request, { headers: { 'Idempotency-Key': idem() } }, ) return containerDetail(unwrapApiResponse(response)) }