import { ApiError, apiRequest } from '@/api/http' import type { ApiContainerMode, ApiDestinationType, ApiFirstLegStatus, ApiPickupMethod, MerchantFirstLegOrderDetail, PageResult } from '@/modules/first-leg/api/firstLeg' export type ApiUnifiedOrderMainStatus = | 'PENDING_REVIEW' | 'WAIT_MEASURE' | 'WAIT_PALLETIZE' | 'WAIT_SPLIT' | 'LOADING' | 'LOADED' | 'DELIVERED' | 'COMPLETED' export interface MerchantUnifiedMeasurementSummary { reviewStatus: string | null actualGrossWeightKg: number | null actualVolumeCbm: number | null chargeableWeightKg: number | null completedAt: string | null } export interface MerchantUnifiedPreloadSummary { status: string version: number itemCount: number updatedAt: string | null } export interface MerchantUnifiedLoadingSummary { status: string loadingVersion: number pickingVersion: number expectedCount: number | null pickedCount: number | null evidenceCount: number | null sealNo: string | null updatedAt: string | null } export interface MerchantUnifiedDeliverySummary { status: string completedAt: string | null trackingNo: string | null } export interface MerchantUnifiedTransportSummary { containerNo: string | null cabinetNo: string | null vesselName: string | null voyageNo: string | null sailingAt: string | null portArrivalAt: string | null } export interface MerchantUnifiedOrderRow { orderId: number | string tenantId: number | string orderNo: string | null fulfillmentType?: 'STANDARD' | 'ONE_CLICK_DROPSHIP' | null lxCode?: string | null mainStatus: ApiUnifiedOrderMainStatus nextAction: string | null customerFeeConfirmationRequired: boolean internalOrderStatus: ApiFirstLegStatus | null customerId: number | string | null customerName: string channelName: string | null receivingWarehouseId: number | string | null receivingWarehouseName: string | null destinationWarehouseId: number | string | null destinationWarehouseName: string | null inboundId: number | string | null inboundVersion: number | null inboundCode: string | null expectedShipDate: string | null totalPackageCount: number | null actualGrossWeightKg: number | null actualVolumeCbm: number | null pickupMethod: ApiPickupMethod | null requestedPickupAt: string | null containerMode: ApiContainerMode | null destinationType: ApiDestinationType | null destinationCode: string | null skuNos: string[] | null expressCompany: string | null expressTrackingNo: string | null totalNetWeightKg: number | null totalQuantity: number | null chargeableVolumeCbm: number | null shipmentId: string | null referenceId: string | null inboundCompletedAt: string | null measurement: MerchantUnifiedMeasurementSummary | null preload: MerchantUnifiedPreloadSummary | null loading: MerchantUnifiedLoadingSummary | null delivery: MerchantUnifiedDeliverySummary | null transport: MerchantUnifiedTransportSummary | null customerRemark: string | null createdAt: string updatedAt: string } export interface MerchantUnifiedTimelineEvent { source: string eventType: string mainStatus: ApiUnifiedOrderMainStatus | null action: string | null remark: string | null operatorId: number | string | null operatorName: string | null occurredAt: string | null } export interface MerchantUnifiedCustomNode { id: number | string name: string nodeType: string sortOrder: number customerVisible: boolean plannedAt: string | null completedAt: string | null operatorName: string | null remark: string | null attachmentIds: Array } export interface MerchantUnifiedOrderDetail { summary: MerchantUnifiedOrderRow order: MerchantFirstLegOrderDetail timeline: MerchantUnifiedTimelineEvent[] // C02 does not return custom nodes yet. Optionality keeps the client forward-compatible without inventing data. customNodes?: MerchantUnifiedCustomNode[] } export interface MerchantUnifiedOrderQuery { keyword?: string fbaCode?: string orderNo?: string inboundCode?: string mainStatus?: ApiUnifiedOrderMainStatus receivingWarehouseId?: number | string destinationWarehouseId?: number | string createdFrom?: string createdTo?: string page?: number size?: number } const unifiedOrderBaseUrl = '/merchant/v1/orders' export function getMerchantUnifiedOrders(params: MerchantUnifiedOrderQuery = {}) { const { page, size, ...filters } = params return apiRequest>({ method: 'GET', url: unifiedOrderBaseUrl, params: { page: page ?? 1, size: size ?? 200, ...filters }, }) } export function getMerchantUnifiedOrder(orderId: number | string) { return apiRequest({ method: 'GET', url: `${unifiedOrderBaseUrl}/${orderId}`, }) } export function isUnifiedOrderQueryUnavailable(error: unknown) { return error instanceof ApiError && (error.status === 404 || error.code === 'A0404') }