import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import type { InboundMeasurementDetail, InboundMeasurementDraftSaved, InboundMeasurementSubmitted, CartonMeasurementResult, ConfirmedPackageCountRequest, SaveInboundMeasurementDraftRequest, SubmitInboundMeasurementRequest, UpdateMeasurementCalculationStrategyRequest, MeasurementCalculationStrategyUpdated, UpdateMeasurementOuterDimensionsRequest, MeasurementOuterDimensionsUpdated, ManualCartonScanRequest, } from './types' function measurementPath(inboundId: string) { return `/platform/v1/first-leg/inbounds/${encodeURIComponent(inboundId)}/measurement` } export async function updateConfirmedPackageCount( customerId: string, inboundId: string, itemId: string, payload: ConfirmedPackageCountRequest, ) { const response = await http.put, ApiEnvelope>( `${measurementPath(inboundId)}/items/${segment(itemId)}/confirmed-package-count`, payload, { params: { tenantId: customerId } }, ) return unwrapApiResponse(response) } function segment(value: string) { return encodeURIComponent(value) } export async function getInboundMeasurement(customerId: string, inboundId: string) { // Device-scanned fields expected from the backend: originalPackageCount and // items[].boxCode, machineNo, deviceMeasuredAt. Keep the response aligned with the backend OpenAPI. const response = await http.get, ApiEnvelope>( measurementPath(inboundId), // Calculation-mode saves use a sibling PUT endpoint. A cache may otherwise keep // returning the pre-save measurement and make a successful update look failed. { params: { tenantId: customerId, requestAt: Date.now() } }, ) return unwrapApiResponse(response) } export async function saveInboundMeasurementDraft( customerId: string, inboundId: string, payload: SaveInboundMeasurementDraftRequest, ) { const response = await http.put, ApiEnvelope>( `${measurementPath(inboundId)}/draft`, payload, { params: { tenantId: customerId } }, ) return unwrapApiResponse(response) } export async function updateMeasurementCalculationStrategy( customerId: string, inboundId: string, payload: UpdateMeasurementCalculationStrategyRequest, ) { const response = await http.put, ApiEnvelope>( `${measurementPath(inboundId)}/calculation-strategy`, payload, { params: { tenantId: customerId } }, ) return unwrapApiResponse(response) } export async function updateMeasurementOuterDimensions( customerId: string, inboundId: string, itemId: string, payload: UpdateMeasurementOuterDimensionsRequest, ) { const response = await http.put, ApiEnvelope>( `${measurementPath(inboundId)}/items/${segment(itemId)}/outer-dimensions`, payload, { params: { tenantId: customerId } }, ) return unwrapApiResponse(response) } export async function submitInboundMeasurement( customerId: string, inboundId: string, payload: SubmitInboundMeasurementRequest, ) { const response = await http.post, ApiEnvelope>( `${measurementPath(inboundId)}/submit`, payload, { params: { tenantId: customerId } }, ) return unwrapApiResponse(response) } export async function createManualCartonScan( customerId: string, inboundId: string, payload: ManualCartonScanRequest, image: File, ) { const data = new FormData() data.append('data', new Blob([JSON.stringify(payload)], { type: 'application/json' })) data.append('image', image) const response = await http.post, ApiEnvelope>( `${measurementPath(inboundId)}/manual-cartons`, data, { params: { tenantId: customerId }, headers: { 'Content-Type': 'multipart/form-data' } }, ) return unwrapApiResponse(response) } export function downloadInboundMeasurementImage(customerId: string, inboundId: string, fileId: string) { return http.get( `/platform/v1/files/measurements/${segment(customerId)}/${segment(inboundId)}/${segment(fileId)}`, { responseType: 'blob' }, ) }