fix(procurement): normalize integer wire fields before commands

This commit is contained in:
2026-10-10 08:48:15 +08:00
parent 84488adf12
commit fb062b5ec4
2 changed files with 52 additions and 3 deletions
@@ -10,6 +10,7 @@ import {
updateUsTruckOrder,
} from './platformOverseasBusiness'
import type { UpdateLocalTransportOrderRequest } from '../types'
import { readPendingProcurement } from '../procurementPending'
const envelope = <T>(data: T) => ({ code: '200', message: 'success', data })
const purchaseSummary = {
@@ -34,6 +35,29 @@ const looseboardSummary = {
describe('platform overseas business API adapter', () => {
beforeEach(() => vi.clearAllMocks())
it('normalizes wire integer strings before building replayable procurement commands', async () => {
overseas.get.mockResolvedValue(envelope({
customerView: { order: { ...purchaseSummary, version: '0', purchaseQuantity: '1' }, boxes: [{ quantity: '1' }], shipments: [{ quantity: '1' }], fees: [{ version: '2' }] },
purchaseFacts: { purchased_quantity: '1' }, physical: { packingVersion: '3', dispositions: [{ id: '901', quantity: '1' }], receipts: [{ id: '902', goodQuantity: '1', damagedQuantity: '0', extraQuantity: '0' }], cartons: [{ id: '903', quantity: '1', sources: [{ receiptId: '902', quantity: '1' }] }] },
fees: [{ id: '904', feeVersion: '2' }], controls: { notes: [], remaining: [{ quantity: '1' }], closures: [], closureItems: [{ quantity: '1' }] }, files: [{ id: '905', fileId: '9007199254740995', sizeBytes: '123' }],
transport: { overseasCargo: [{ cartonId: '903', quantity: '1', sources: [{ receiptId: '902', quantity: '1' }] }], domesticCargo: [], overseasLabels: { id: '906', manifestId: '907', version: '4', labels: [{ cartonId: '903', fileId: '9007199254740995', page: '1' }] }, domesticLabels: null, shipments: [] },
}))
const detail = await getOverseasPurchase('OP-1', '10001')
const payload = { version: detail.customerView.order.version, idempotencyKey: 'review-wire', decision: 'APPROVE', purchaseWarehouseId: '9007199254740995' }
expect(() => readPendingProcurement(JSON.stringify({ command: 'review', payload }))).not.toThrow()
expect(detail.customerView.order.purchaseQuantity).toBe(1)
expect(detail.physical.receipts[0].goodQuantity + detail.physical.receipts[0].damagedQuantity).toBe(1)
expect(detail.physical.packingVersion).toBe(3)
expect(detail.physical.cartons[0].sources[0].quantity).toBe(1)
expect(detail.transport.overseasCargo[0].quantity).toBe(1)
expect(detail.transport.overseasLabels?.version).toBe(4)
expect(detail.transport.overseasLabels?.labels[0].page).toBe(1)
expect(detail.files[0]).toMatchObject({ fileId: '9007199254740995', sizeBytes: 123 })
expect(detail.fees[0].feeVersion).toBe(2)
expect(detail.controls.remaining[0].quantity).toBe(1)
expect(detail.controls.closureItems[0].quantity).toBe(1)
})
it('loads procurement candidates and normalizes procurement pages', async () => {
overseas.get
.mockResolvedValueOnce(envelope([{ customerId: '10001', customerName: '客户一' }]))
@@ -49,7 +73,7 @@ describe('platform overseas business API adapter', () => {
it('loads and patches procurement detail with three-state null payloads', async () => {
const detail = { summary: purchaseSummary, productUrl: null, histories: [{ operationType: 'UPDATE', fromStatus: 'WAIT_PURCHASE', toStatus: 'WAIT_PURCHASE', operatorId: '7', changeDetail: '{}', remark: null, createdAt: '2026-08-02T00:00:00' }] }
overseas.get.mockResolvedValue(envelope({ customerView: { order: purchaseSummary }, reviewHistory: detail.histories, physical: { dispositions: [], receipts: [], cartons: [] }, fees: [], feeCorrections: [], controls: { notes: [], remaining: [], closures: [], closureItems: [] }, files: [], transport: { overseasCargo: [], domesticCargo: [], overseasLabels: null, domesticLabels: null, shipments: [] } }))
overseas.get.mockResolvedValue(envelope({ customerView: { order: purchaseSummary, boxes: [], shipments: [], fees: [] }, reviewHistory: detail.histories, physical: { dispositions: [], receipts: [], cartons: [] }, fees: [], feeCorrections: [], controls: { notes: [], remaining: [], closures: [], closureItems: [] }, files: [], transport: { overseasCargo: [], domesticCargo: [], overseasLabels: null, domesticLabels: null, shipments: [] } }))
expect((await getOverseasPurchase('OP/1', '10001')).reviewHistory[0]?.operationType).toBe('UPDATE')
overseas.patch.mockResolvedValue(envelope(detail))
const payload = { version: 4, remark: null }
@@ -32,8 +32,12 @@ interface LooseboardDetailPayload {
function normalizePage<TPayload, TResult>(payload: PagePayload<TPayload>, normalize: (value: TPayload) => TResult): OverseasBusinessPage<TResult> {
return { records: (payload.records || []).map(normalize), page: Number(payload.page), size: Number(payload.size), total: Number(payload.total), pages: Number(payload.pages) }
}
function procurementInteger(value: unknown): number {
if ((typeof value !== 'number' && typeof value !== 'string') || !/^\d+$/.test(String(value)) || !Number.isSafeInteger(Number(value))) throw new Error('采购数据中的数量或版本无效,请刷新后重试')
return Number(value)
}
function procurementSummary(value: ProcurementSummaryPayload): OverseasPurchaseSummary {
return { ...value, id: String(value.id), purchaseWarehouseId: value.purchaseWarehouseId == null ? null : String(value.purchaseWarehouseId), budgetUnitPrice: value.budgetUnitPrice == null ? null : String(value.budgetUnitPrice) }
return { ...value, version: procurementInteger(value.version), purchaseQuantity: procurementInteger(value.purchaseQuantity), id: String(value.id), purchaseWarehouseId: value.purchaseWarehouseId == null ? null : String(value.purchaseWarehouseId), budgetUnitPrice: value.budgetUnitPrice == null ? null : String(value.budgetUnitPrice) }
}
function truckSummary(value: TruckSummaryPayload): LocalTransportOrder {
return { ...value, id: String(value.id), totalWeightKg: String(value.totalWeightKg), totalVolumeCbm: String(value.totalVolumeCbm), budgetAmount: value.budgetAmount == null ? null : String(value.budgetAmount) }
@@ -63,6 +67,22 @@ export async function listOverseasPurchases(query: OverseasPurchaseQuery = {}) {
export async function getOverseasPurchase(orderNo: string, customerId: string) {
const response = await http.get<ApiEnvelope<PlatformProcurement>, ApiEnvelope<PlatformProcurement>>(`${procurementBasePath}/${encodeURIComponent(orderNo)}`, { params: { customerId } })
const detail = unwrapApiResponse(response)
detail.customerView.order.version = procurementInteger(detail.customerView.order.version)
detail.customerView.order.purchaseQuantity = procurementInteger(detail.customerView.order.purchaseQuantity)
for (const row of [...detail.customerView.boxes, ...detail.customerView.shipments]) row.quantity = procurementInteger(row.quantity)
for (const fee of detail.customerView.fees) if (fee.version != null) fee.version = procurementInteger(fee.version)
if (detail.purchaseFacts?.purchased_quantity != null) detail.purchaseFacts.purchased_quantity = procurementInteger(detail.purchaseFacts.purchased_quantity)
if (detail.physical.packingVersion != null) detail.physical.packingVersion = procurementInteger(detail.physical.packingVersion)
for (const row of detail.physical.receipts) {
row.goodQuantity = procurementInteger(row.goodQuantity); row.damagedQuantity = procurementInteger(row.damagedQuantity); row.extraQuantity = procurementInteger(row.extraQuantity)
}
for (const row of [...detail.physical.dispositions, ...detail.controls.remaining, ...detail.controls.closureItems]) row.quantity = procurementInteger(row.quantity)
for (const row of [...detail.physical.cartons, ...detail.transport.overseasCargo, ...detail.transport.domesticCargo]) {
row.quantity = procurementInteger(row.quantity)
for (const source of row.sources) source.quantity = procurementInteger(source.quantity)
}
for (const fee of detail.fees) fee.feeVersion = procurementInteger(fee.feeVersion)
for (const file of detail.files) file.sizeBytes = procurementInteger(file.sizeBytes)
if (detail.purchaseFacts?.actual_warehouse_id != null) detail.purchaseFacts.actual_warehouse_id = String(detail.purchaseFacts.actual_warehouse_id)
detail.physical.dispositions = detail.physical.dispositions.map(row => ({ ...row, id: String(row.id), source_carton_id: row.source_carton_id == null ? null : String(row.source_carton_id), receipt_id: row.receipt_id == null ? null : String(row.receipt_id), voucher_file_id: String(row.voucher_file_id) }))
detail.physical.receipts = detail.physical.receipts.map(row => ({ ...row, id: String(row.id), sourceCartonId: row.sourceCartonId == null ? null : String(row.sourceCartonId) }))
@@ -76,6 +96,8 @@ export async function getOverseasPurchase(orderNo: string, customerId: string) {
detail.files = detail.files.map(file => ({ ...file, id: String(file.id), fileId: String(file.fileId) }))
for (const leg of ['overseasCargo', 'domesticCargo'] as const) detail.transport[leg] = detail.transport[leg].map(row => ({ ...row, cartonId: String(row.cartonId), packagingRecordId: row.packagingRecordId == null ? null : String(row.packagingRecordId), sources: row.sources.map(source => ({ ...source, receiptId: String(source.receiptId) })) }))
for (const labels of [detail.transport.overseasLabels, detail.transport.domesticLabels]) if (labels) {
labels.version = procurementInteger(labels.version)
for (const label of labels.labels) label.page = procurementInteger(label.page)
labels.id = String(labels.id); labels.manifestId = String(labels.manifestId)
labels.labels = labels.labels.map(row => ({ ...row, cartonId: String(row.cartonId), fileId: String(row.fileId) }))
}
@@ -83,7 +105,10 @@ export async function getOverseasPurchase(orderNo: string, customerId: string) {
}
export async function getOverseasPurchaseEditor(orderNo: string, customerId: string) {
const response = await http.get<ApiEnvelope<OverseasPurchaseEditorDetail>, ApiEnvelope<OverseasPurchaseEditorDetail>>(`${procurementBasePath}/${encodeURIComponent(orderNo)}`, { params: { customerId } })
return unwrapApiResponse(response)
const detail = unwrapApiResponse(response)
detail.customerView.order.version = procurementInteger(detail.customerView.order.version)
detail.customerView.order.purchaseQuantity = procurementInteger(detail.customerView.order.purchaseQuantity)
return detail
}
export async function updateOverseasPurchase(orderNo: string, customerId: string, payload: UpdateOverseasPurchaseRequest) {
const response = await http.patch<ApiEnvelope<ProcurementDetailPayload>, ApiEnvelope<ProcurementDetailPayload>>(`${procurementBasePath}/${encodeURIComponent(orderNo)}`, { version: payload.version, remark: payload.remark }, { params: { customerId } })