import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import type { 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) { 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) } }