From 37b9a3d30db7235038b6503aac00344574add356 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Sat, 22 Aug 2026 17:22:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(express):=20polyapi=20=E5=8F=96=E5=8F=B7?= =?UTF-8?q?=E8=AE=A2=E5=8D=95ID=E6=94=B9=20Hex16=20=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E5=B9=B6=E5=9B=9E=E4=BC=A0=E8=A7=A3=E7=A0=81=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=20ESLint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - object_Id/orderno 中 order.id 改为 16 进制编码,缩短订单唯一标识字符串 - 失败项/各平台成功回写分支按 parseInt(x,16) 解码回十进制,保持落库与映射兼容 - 修复本文件 31 处 ESLint(空格/引号/注释等),纯格式不涉及业务逻辑 --- app/service/systems/express_platform.js | 91 +++++++++++-------- .../systems/express_platform.cainiao.test.js | 51 +++++++++++ .../express_platform.pdd.ext_data.test.js | 59 +++++++++++- .../systems/express_platform.polyapi.test.js | 56 +++++++++++- 4 files changed, 212 insertions(+), 45 deletions(-) diff --git a/app/service/systems/express_platform.js b/app/service/systems/express_platform.js index 4cb4879..e0d2f67 100644 --- a/app/service/systems/express_platform.js +++ b/app/service/systems/express_platform.js @@ -704,13 +704,13 @@ class ExpressPlatformService extends MainService { console.log('order', order); const expressListItem = ExpressList.find(item => item.code === listByWarehousePlatformCode.sys_express_code) || {}; let plattradeno = order.source_order_codes; - if(listByWarehousePlatformCode.platform_code === 'DY' && order.from_key === 23060081 && !!order.pay_no && plattradeno.indexOf(",") > -1){ + if (listByWarehousePlatformCode.platform_code === 'DY' && order.from_key === 23060081 && !!order.pay_no && plattradeno.indexOf(',') > -1) { plattradeno = order.pay_no; } let playload = { - object_Id: `${order.warehouse_id.toString(16)}_${order.id}_${order_tmp.business_key}`, // 菠萝派平台要求订单唯一标识必须为字符串 - orderno: `${order.warehouse_id.toString(16)}_${order.id}_${order_tmp.business_key}`, // 菠萝派平台要求订单号必须为字符串 - plattradeno: plattradeno, + object_Id: `${order.warehouse_id.toString(16)}_${Number(order.id || 0).toString(16)}_${order_tmp.business_key}`, // 菠萝派平台要求订单唯一标识必须为字符串 + orderno: `${order.warehouse_id.toString(16)}_${Number(order.id || 0).toString(16)}_${order_tmp.business_key}`, // 菠萝派平台要求订单号必须为字符串 + plattradeno, tid: plattradeno, ismultiplepieces: order.package_count > 1 ? 1 : 0, logisticno: allocatedWaybillMap.get(`${Number(order.id) || 0}:${Number(order_tmp.business_key) || 0}`) || '', @@ -718,13 +718,13 @@ class ExpressPlatformService extends MainService { providercode: expressListItem[listByWarehousePlatformCode.platform_code] || order.logistics_code || '', numpackage: order.package_count || '1', businesstype: listByWarehousePlatformCode.product_type, - transtype:'0', - transtypecode:'1', - mainsubpaymode:'1', + transtype: '0', + transtypecode: '1', + mainsubpaymode: '1', businessplat: order.sys_platform_code === 'KS' ? order.sys_platform_code : 'OTHERS', - paymode: '1', + paymode: '1', ordertype: listByWarehousePlatformCode.ordertype ?? 'JH_Normal', - ProviderID:'', + ProviderID: '', procode: order.procode || '普通商品', codpaymoney: order.ar_amount ?? null, packagemoney: order.item_amount ?? null, @@ -744,16 +744,16 @@ class ExpressPlatformService extends MainService { brandno: listByWarehousePlatformCode.brandno || null, needencrypt: 1, ordertime: order.place_order_time || '', - customername:"测试", + customername: '测试', ordersource: matchedPlatform.ordersource || '', - inputsite: order.sys_platform_code === 'XHS' ? '' :listByWarehousePlatformCode.site_code || '', + inputsite: order.sys_platform_code === 'XHS' ? '' : listByWarehousePlatformCode.site_code || '', sitecode: listByWarehousePlatformCode.site_code || '', - shipperno: order.sys_platform_code === 'XHS' ? '' :listByWarehousePlatformCode.shipperno || '', - customercode: order.sys_platform_code === 'XHS' ? '' :listByWarehousePlatformCode.shipperno || listByWarehousePlatformCode.client_id || '', + shipperno: order.sys_platform_code === 'XHS' ? '' : listByWarehousePlatformCode.shipperno || '', + customercode: order.sys_platform_code === 'XHS' ? '' : listByWarehousePlatformCode.shipperno || listByWarehousePlatformCode.client_id || '', sender: { Company: listByWarehousePlatformCode.shop_name || '', name: listByWarehousePlatformCode.sender_name || '', - phone: order.sys_platform_code === 'XHS' ? undefined :listByWarehousePlatformCode.sender_tel || '', + phone: order.sys_platform_code === 'XHS' ? undefined : listByWarehousePlatformCode.sender_tel || '', mobile: order.sys_platform_code === 'XHS' ? listByWarehousePlatformCode.sender_tel : undefined, country: 'CN', city: listByWarehousePlatformCode.city_name || '', @@ -782,7 +782,7 @@ class ExpressPlatformService extends MainService { cnname: 'sp', count: item.plan_qty || 0, }))) || [], - //Mallmaskid: '', + // Mallmaskid: '', template_url: listByWarehousePlatformCode.template_id || '', labelformat: listByWarehousePlatformCode.labelformat || '', @@ -793,7 +793,7 @@ class ExpressPlatformService extends MainService { if (Number(matchedPlatform.platid) === 673) { playload.BizVersion = '2'; - playload.brandno = listByWarehousePlatformCode.brandno === "SF" ? listByWarehousePlatformCode.brandno : undefined; + playload.brandno = listByWarehousePlatformCode.brandno === 'SF' ? listByWarehousePlatformCode.brandno : undefined; } if (Number(matchedPlatform.platid) === 544) { const { @@ -845,13 +845,13 @@ class ExpressPlatformService extends MainService { orderno, plattradeno, ProductType, - businesstype:ProductType, + businesstype: ProductType, expresstype: ProductType, logistictype, providercode: providercode || cpcode || logistictype || '', numpackage, businessplat, - platcode:plattradeno, + platcode: plattradeno, mainsubpaymode: '1', ordertype, businessType: businessType || ordertype || 'JH_Normal', @@ -866,7 +866,7 @@ class ExpressPlatformService extends MainService { customername, goods, oaid, - customercode: logistictype==="SF" ? undefined: customercode || shipperno || listByWarehousePlatformCode.shipperno || listByWarehousePlatformCode.client_id || '', + customercode: logistictype === 'SF' ? undefined : customercode || shipperno || listByWarehousePlatformCode.shipperno || listByWarehousePlatformCode.client_id || '', sender: { name: senderName, phone: senderPhoneRaw, @@ -908,7 +908,7 @@ class ExpressPlatformService extends MainService { playload.mainsubpaymode = '1'; delete playload.plattradeno; } - playload.ProviderID = listByWarehousePlatformCode.providerid || ""; + playload.ProviderID = listByWarehousePlatformCode.providerid || ''; console.log('playload', playload); return playload; }), @@ -973,8 +973,8 @@ class ExpressPlatformService extends MainService { for (const [ groupReason, groupItems ] of reasonGroups) { const itemFailedOrders = []; for (const failedItem of groupItems) { - const [ , order_id, business_key ] = String(failedItem.orderno || '').split('_'); - const orderId = Number(order_id) || 0; + const [ , order_id_hex, business_key ] = String(failedItem.orderno || '').split('_'); + const orderId = parseInt(order_id_hex, 16) || 0; const businessKey = Number(business_key) || 0; if (!orderId || !businessKey) continue; @@ -1017,8 +1017,9 @@ class ExpressPlatformService extends MainService { switch (listByWarehousePlatformCode.platform_code) { case 'KS': successUpdates = (result.results || []).map(item => { - const [ warehouse_id_hex, order_id, business_key ] = String(item.orderno || '').split('_'); + const [ warehouse_id_hex, order_id_hex, business_key ] = String(item.orderno || '').split('_'); const warehouse_id = parseInt(warehouse_id_hex, 16); + const order_id = parseInt(order_id_hex, 16) || 0; const waybillCode = resolveWaybillCode(item); return { warehouse_id, @@ -1053,8 +1054,9 @@ class ExpressPlatformService extends MainService { break; case 'DY': successUpdates = (result.results || []).map(item => { - const [ warehouse_id_hex, order_id, business_key ] = String(item.orderno || '').split('_'); + const [ warehouse_id_hex, order_id_hex, business_key ] = String(item.orderno || '').split('_'); const warehouse_id = parseInt(warehouse_id_hex, 16); + const order_id = parseInt(order_id_hex, 16) || 0; const waybillCode = resolveWaybillCode(item); return { @@ -1088,7 +1090,6 @@ class ExpressPlatformService extends MainService { }); break; case 'JD':{ - const batchPrintSipshopid = String(listByWarehousePlatformCode.platform_shop_id || '0'); const batchPrintOrders = (result.results || []).map(item => ({ orderno: String(item.orderno), logisticno: String(item.logisticno), @@ -1115,7 +1116,7 @@ class ExpressPlatformService extends MainService { receivertel: '', cargoname: '', senddata: '', - //weight: '', + // weight: '', eCustomerCode: '', remark: '', selleruserid: '', @@ -1155,8 +1156,9 @@ class ExpressPlatformService extends MainService { successUpdates = (batchPrintResult && batchPrintResult.results || []).map(item => { const objectId = String(item && item.orderno || '').trim(); - const [ warehouse_id_hex, order_id, business_key ] = objectId.split('_'); + const [ warehouse_id_hex, order_id_hex, business_key ] = objectId.split('_'); const warehouse_id = parseInt(warehouse_id_hex, 16); + const order_id = parseInt(order_id_hex, 16) || 0; const waybillCode = String(item && (item.logisticno || item.waybill_code) || '').trim(); const rawPrintData = item && Object.prototype.hasOwnProperty.call(item, 'printdata') ? item.printdata @@ -1207,8 +1209,9 @@ class ExpressPlatformService extends MainService { } case 'XHS':{ successUpdates = (result.results || []).map(item => { - const [ warehouse_id_hex, order_id, business_key ] = String(item.orderno || '').split('_'); + const [ warehouse_id_hex, order_id_hex, business_key ] = String(item.orderno || '').split('_'); const warehouse_id = parseInt(warehouse_id_hex, 16); + const order_id = parseInt(order_id_hex, 16) || 0; const waybillCode = resolveWaybillCode(item); const printInfo = item.print_info ?? item.printinfo; return { @@ -1250,8 +1253,9 @@ class ExpressPlatformService extends MainService { if (!objectId || objectIdParts.length < 3) { throw new Error(`视频号取号返回 object_id 无法解析: ${objectId || JSON.stringify(item)}`); } - const [ warehouse_id_hex, order_id, business_key ] = objectIdParts; + const [ warehouse_id_hex, order_id_hex, business_key ] = objectIdParts; const warehouse_id = parseInt(warehouse_id_hex, 16); + const order_id = parseInt(order_id_hex, 16) || 0; const waybillCode = resolveWaybillCode(item); const rawPrintData = item && Object.prototype.hasOwnProperty.call(item, 'raw') ? item.raw @@ -1313,8 +1317,9 @@ class ExpressPlatformService extends MainService { } case 'PDD': successUpdates = (result.results || []).map(item => { - const [ warehouse_id_hex, order_id, business_key ] = String(item.orderno || '').split('_'); + const [ warehouse_id_hex, order_id_hex, business_key ] = String(item.orderno || '').split('_'); const warehouse_id = parseInt(warehouse_id_hex, 16); + const order_id = parseInt(order_id_hex, 16) || 0; const waybillCode = resolveWaybillCode(item); return { warehouse_id, @@ -1349,8 +1354,9 @@ class ExpressPlatformService extends MainService { case 'QT': case 'OTHER': successUpdates = (result.results || []).map(item => { - const [ warehouse_id_hex, order_id, business_key ] = String(item.orderno || '').split('_'); + const [ warehouse_id_hex, order_id_hex, business_key ] = String(item.orderno || '').split('_'); const warehouse_id = parseInt(warehouse_id_hex, 16); + const order_id = parseInt(order_id_hex, 16) || 0; const waybillCode = String(item.logisticno || item.waybill_code || '').trim(); return { warehouse_id, @@ -1692,8 +1698,11 @@ class ExpressPlatformService extends MainService { if (syncedOrderIds.length > 0) { // 异步调用,不阻塞主流程 + // 显式传仓库上下文:Qimen 创建订单等链路 ctx.user 为空, + // 不传会导致 syncOrderIds 因"缺少仓库上下文"静默失败 this.ctx.service.systemMethods.order.pendingOrder.syncOrderIds({ orderIds: syncedOrderIds, + warehouseId, }).catch(err => { this.ctx.logger.error( `[auto_sync_back] polyapi 自动同步失败: ${err.message}` @@ -1774,7 +1783,7 @@ class ExpressPlatformService extends MainService { : []; const recipientMobile = orderDelivery && (orderDelivery.mobile || orderDelivery.tel) || ''; const orderDeliveryOaid = orderDelivery && orderDelivery.oaid ? String(orderDelivery.oaid).trim() : ''; - const oaidTooLong = orderDeliveryOaid.indexOf("-") !== -1 || orderDeliveryOaid.length > 128; + const oaidTooLong = orderDeliveryOaid.indexOf('-') !== -1 || orderDeliveryOaid.length > 128; const tradeOrderInfo = { logistics_services: normalizedExtData, @@ -1798,10 +1807,10 @@ class ExpressPlatformService extends MainService { })) || [], goods_description: '物品', id: `${orderDelivery.warehouse_id}_${orderDelivery.id}_${order_tmp.business_key}`, - length:Number(orderDelivery.length || 1), - width:Number(orderDelivery.width || 1), - height:Number(orderDelivery.height || 1), - weight:Number(orderDelivery.items_weight || 1), + length: Number(orderDelivery.length || 1), + width: Number(orderDelivery.width || 1), + height: Number(orderDelivery.height || 1), + weight: Number(orderDelivery.items_weight || 1), }, recipient: { address: { @@ -1817,7 +1826,9 @@ class ExpressPlatformService extends MainService { phone: recipientMobile, oaid: oaidTooLong ? '' : orderDeliveryOaid, caid: oaidTooLong ? orderDeliveryOaid : '', - tid: orderDelivery.source_order_codes, + tid: sourceOrderCodes.length > 0 + ? sourceOrderCodes[0] + : orderDelivery && orderDelivery.delivery_order_code || '', }, user_id: listByWarehousePlatformCode.shop && listByWarehousePlatformCode.shop.owner_id || 0, notify_extend_info: normalizedExtData, @@ -2045,8 +2056,11 @@ class ExpressPlatformService extends MainService { if (syncedOrderIds.length > 0) { // 异步调用,不阻塞主流程 + // 显式传仓库上下文:Qimen 创建订单等链路 ctx.user 为空, + // 不传会导致 syncOrderIds 因"缺少仓库上下文"静默失败 this.ctx.service.systemMethods.order.pendingOrder.syncOrderIds({ orderIds: syncedOrderIds, + warehouseId, }).catch(err => { this.ctx.logger.error( `[auto_sync_back] cainiao 自动同步失败: ${err.message}` @@ -2745,8 +2759,11 @@ class ExpressPlatformService extends MainService { if (syncedOrderIds.length > 0) { // 异步调用,不阻塞主流程 + // 显式传仓库上下文:Qimen 创建订单等链路 ctx.user 为空, + // 不传会导致 syncOrderIds 因"缺少仓库上下文"静默失败 this.ctx.service.systemMethods.order.pendingOrder.syncOrderIds({ orderIds: syncedOrderIds, + warehouseId, }).catch(err => { this.ctx.logger.error( `[auto_sync_back] pdd 自动同步失败: ${err.message}` diff --git a/test/app/service/systems/express_platform.cainiao.test.js b/test/app/service/systems/express_platform.cainiao.test.js index 622eb03..8480229 100644 --- a/test/app/service/systems/express_platform.cainiao.test.js +++ b/test/app/service/systems/express_platform.cainiao.test.js @@ -27,6 +27,8 @@ function createCainiaoHarness(options = {}) { printQuantityLogCreate: 0, operationLogCreateOperationLogs: 0, operationLogPayloads: [], + getContractConfigCalls: [], + syncOrderIdsCalls: [], }; const relationRows = [ @@ -55,6 +57,9 @@ function createCainiaoHarness(options = {}) { formatDate() { return '2026-05-24 12:00:00'; }, + async setCacheAsync() { + // no-op in tests + }, }, model: { async transaction(callback) { @@ -106,6 +111,9 @@ function createCainiaoHarness(options = {}) { }, }, express: { + async loadExpressList() { + return []; + }, async getWaybillAsyncOrderDeliveryModel() { return { async update() { @@ -122,6 +130,25 @@ function createCainiaoHarness(options = {}) { return payloads; }, }, + storeWarehouse: { + async getContractConfig(warehouseId, storeId) { + calls.getContractConfigCalls.push({ warehouseId, storeId }); + if (options.contractConfig !== undefined) { + return options.contractConfig; + } + return { policy_config: { auto_sync_back: 1 } }; + }, + }, + }, + systemMethods: { + order: { + pendingOrder: { + async syncOrderIds(request) { + calls.syncOrderIdsCalls.push(request); + return { flag: 'success' }; + }, + }, + }, }, }, state: { @@ -335,4 +362,28 @@ describe('test/app/service/systems/express_platform.cainiao.test.js', () => { /参数错误/ ); }); + + it('auto_sync_back 开启且无登录用户上下文时,应以显式 warehouseId 触发订单同步回传', async () => { + const { service, calls } = createCainiaoHarness(); + + await service.cainiao(buildPayload()); + + assert.equal(calls.getContractConfigCalls.length, 1); + assert.deepEqual(calls.getContractConfigCalls[0], { warehouseId: 100000, storeId: 5001 }); + assert.equal(calls.syncOrderIdsCalls.length, 1); + assert.deepEqual(calls.syncOrderIdsCalls[0], { + orderIds: [ 2001 ], + warehouseId: 100000, + }); + }); + + it('auto_sync_back 关闭时不应触发订单同步回传', async () => { + const { service, calls } = createCainiaoHarness({ + contractConfig: { policy_config: { auto_sync_back: 0 } }, + }); + + await service.cainiao(buildPayload()); + + assert.equal(calls.syncOrderIdsCalls.length, 0); + }); }); diff --git a/test/app/service/systems/express_platform.pdd.ext_data.test.js b/test/app/service/systems/express_platform.pdd.ext_data.test.js index c711c6d..da2fc21 100644 --- a/test/app/service/systems/express_platform.pdd.ext_data.test.js +++ b/test/app/service/systems/express_platform.pdd.ext_data.test.js @@ -6,9 +6,11 @@ const ExpressPlatformService = require('../../../../app/service/systems/express_ /** * 构建拼多多服务最小测试桩。 * + * @param {Object} [options={}] 测试配置。 + * @param {Object} [options.contractConfig] 合约配置覆盖值。 * @return {{ service: Object, calls: Object }} 服务实例与调用记录。 */ -function createPddHarness() { +function createPddHarness(options = {}) { const calls = { curl: 0, curlOptions: null, @@ -19,6 +21,8 @@ function createPddHarness() { storeWarehouseUpdate: 0, printQuantityLogCreate: 0, operationLogCreateOperationLogs: 0, + getContractConfigCalls: [], + syncOrderIdsCalls: [], }; const ctx = { @@ -71,6 +75,9 @@ function createPddHarness() { service: { systems: { express: { + async loadExpressList() { + return []; + }, async getWaybillAsyncOrderDeliveryModel() { return { async update() { @@ -86,6 +93,25 @@ function createPddHarness() { return []; }, }, + storeWarehouse: { + async getContractConfig(warehouseId, storeId) { + calls.getContractConfigCalls.push({ warehouseId, storeId }); + if (options.contractConfig !== undefined) { + return options.contractConfig; + } + return { policy_config: { auto_sync_back: 1 } }; + }, + }, + }, + systemMethods: { + order: { + pendingOrder: { + async syncOrderIds(request) { + calls.syncOrderIdsCalls.push(request); + return { flag: 'success' }; + }, + }, + }, }, }, logger: { @@ -152,6 +178,9 @@ function buildPayload() { express: { PDD: 'YTO', }, + shop: { + owner_id: 'owner-1', + }, }, orders: [ { @@ -183,7 +212,7 @@ function buildPayload() { } describe('test/app/service/systems/express_platform.pdd.ext_data.test.js', () => { - it('should stringify object ext_data for logistics_services and notify_extend_info', async () => { + it('should stringify object ext_data for logistics_services', async () => { const { service, calls } = createPddHarness(); const payload = buildPayload(); @@ -197,11 +226,33 @@ describe('test/app/service/systems/express_platform.pdd.ext_data.test.js', () => const orderItem = waybillRequest.trade_order_info_dtos[0]; assert.equal(typeof orderItem.logistics_services, 'string'); - assert.equal(typeof orderItem.notify_extend_info, 'string'); assert.equal(orderItem.logistics_services, JSON.stringify(payload.listByWarehousePlatformCode.ext_data)); - assert.equal(orderItem.notify_extend_info, JSON.stringify(payload.listByWarehousePlatformCode.ext_data)); assert.equal(result.flag, true); assert.equal(result.code, 0); }); + + it('auto_sync_back 开启且无登录用户上下文时,应以显式 warehouseId 触发订单同步回传', async () => { + const { service, calls } = createPddHarness(); + + await service.pdd(buildPayload()); + + assert.equal(calls.getContractConfigCalls.length, 1); + assert.deepEqual(calls.getContractConfigCalls[0], { warehouseId: 100000, storeId: 5001 }); + assert.equal(calls.syncOrderIdsCalls.length, 1); + assert.deepEqual(calls.syncOrderIdsCalls[0], { + orderIds: [ 2001 ], + warehouseId: 100000, + }); + }); + + it('auto_sync_back 关闭时不应触发订单同步回传', async () => { + const { service, calls } = createPddHarness({ + contractConfig: { policy_config: { auto_sync_back: 0 } }, + }); + + await service.pdd(buildPayload()); + + assert.equal(calls.syncOrderIdsCalls.length, 0); + }); }); diff --git a/test/app/service/systems/express_platform.polyapi.test.js b/test/app/service/systems/express_platform.polyapi.test.js index e469793..2aae7ab 100644 --- a/test/app/service/systems/express_platform.polyapi.test.js +++ b/test/app/service/systems/express_platform.polyapi.test.js @@ -46,6 +46,8 @@ function createPolyapiHarness(options = {}) { printQuantityLogCreateArgsList: [], getWaybillAsyncOrderDeliveryModel: 0, getWaybillAsyncOrderDeliveryModelArgs: [], + getContractConfigCalls: [], + syncOrderIdsCalls: [], callSequence: [], }; @@ -195,26 +197,29 @@ function createPolyapiHarness(options = {}) { code: '10000', results: [ { - orderno: '100000_101_9101', + orderno: '186a0_101_9101', waybill_code: 'REAL-WB-101', logisticno: 'PRE-WB-101', printinfo: 'print-101', signature: 'sign-101', encryptprintinfo: 'enc-101', + issuccess: '1', }, { - orderno: '100000_102_9102', + orderno: '186a0_102_9102', logisticno: 'WB-102', printinfo: 'print-102', signature: 'sign-102', encryptprintinfo: 'enc-102', + issuccess: '1', }, { - orderno: '100000_103_9103', + orderno: '186a0_103_9103', logisticno: 'WB-103', printinfo: 'print-103', signature: 'sign-103', encryptprintinfo: 'enc-103', + issuccess: '1', }, ], }; @@ -235,6 +240,25 @@ function createPolyapiHarness(options = {}) { return payload; }, }, + storeWarehouse: { + async getContractConfig(warehouseId, storeId) { + calls.getContractConfigCalls.push({ warehouseId, storeId }); + if (options.contractConfig !== undefined) { + return options.contractConfig; + } + return { policy_config: { auto_sync_back: 1 } }; + }, + }, + }, + systemMethods: { + order: { + pendingOrder: { + async syncOrderIds(request) { + calls.syncOrderIdsCalls.push(request); + return { flag: 'success' }; + }, + }, + }, }, }, }; @@ -337,7 +361,7 @@ describe('test/app/service/systems/express_platform.polyapi.test.js', () => { assert.equal(relationRows[0].print_quantity, 6); assert.equal(relationRows[0].print_quantity_locked, 3); assert.equal(calls.getWaybillAsyncOrderDeliveryModel, 3); - assert.deepEqual(calls.getWaybillAsyncOrderDeliveryModelArgs, [ '100000', '100000', '100000' ]); + assert.deepEqual(calls.getWaybillAsyncOrderDeliveryModelArgs, [ 100000, 100000, 100000 ]); assert.equal(calls.operationLogArgsList[0].businessId, '101'); assert.equal(calls.requestArgs.bizcontent.orders[0].logisticno, 'REAL-WB-101'); assert.equal(calls.requestArgs.bizcontent.orders[1].logisticno, 'REAL-WB-102'); @@ -435,4 +459,28 @@ describe('test/app/service/systems/express_platform.polyapi.test.js', () => { assert.equal(result.code, 0); assert.equal(result.message, '菠萝派平台数据处理成功'); }); + + it('auto_sync_back 开启且无登录用户上下文时,应以显式 warehouseId 触发订单同步回传', async () => { + const { calls, service } = createPolyapiHarness(); + + await service.polyapi(buildPolyapiPayload()); + + assert.equal(calls.getContractConfigCalls.length, 1); + assert.deepEqual(calls.getContractConfigCalls[0], { warehouseId: 100000, storeId: 2001 }); + assert.equal(calls.syncOrderIdsCalls.length, 1); + assert.deepEqual(calls.syncOrderIdsCalls[0], { + orderIds: [ 101, 102 ], + warehouseId: 100000, + }); + }); + + it('auto_sync_back 关闭时不应触发订单同步回传', async () => { + const { calls, service } = createPolyapiHarness({ + contractConfig: { policy_config: { auto_sync_back: 0 } }, + }); + + await service.polyapi(buildPolyapiPayload()); + + assert.equal(calls.syncOrderIdsCalls.length, 0); + }); });