WMS_MAIN/test/app/service/systems/express.createWaybillCode.balance.test.js
liufumingren dc4894cb6d 1
2026-05-25 17:08:56 +08:00

934 lines
31 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const assert = require('node:assert/strict');
const ExpressService = require('../../../../app/service/systems/express');
/**
* 创建快递余额测试桩。
*
* @param {Object} [options={}] 测试配置。
* @return {Object} 测试上下文、调用记录与服务实例。
*/
function createBalanceHarness(options = {}) {
const calls = {
transaction: 0,
orderDeliveryFindAll: 0,
orderDeliveryUpdate: 0,
orderDeliveryUpdateArgsList: [],
storeWarehouseFindAll: 0,
storeWarehouseUpdate: 0,
storeWarehouseUpdateArgsList: [],
storeWarehouseIncrement: 0,
storeWarehouseIncrementArgsList: [],
orderExpressFindAll: 0,
orderExpressUpdate: 0,
orderExpressBulkCreate: 0,
warnMessages: [],
};
const orderDeliveryRows = Array.isArray(options.orderDeliveryRows)
? options.orderDeliveryRows
: [
{
id: 101,
store_id: 2001,
warehouse_id: 100000,
delivery_order_code: 'DO-101',
source_platform_code: 'source-a',
source_platform_name: 'Source A',
sys_platform_code: 'sys-a',
sys_platform_name: 'System A',
order_type: 'normal',
status: 'paid',
step: 2,
order_flag: 'flag-a',
from_key: 'web',
create_at: '2026-04-18T01:02:03.000Z',
place_order_time: '2026-04-18T01:03:04.000Z',
pay_time: '2026-04-18T01:04:05.000Z',
operate_time: '2026-04-18T01:05:06.000Z',
delivery_time: '2026-04-18T01:06:07.000Z',
name: 'Alice',
tel: '010-88888888',
mobile: '13800000001',
email: 'alice@example.com',
country_code: 'CN',
province: 'Beijing',
city: 'Beijing',
area: 'Chaoyang',
town: 'Wangjing',
detail_address: 'Road 1',
zip_code: '100000',
total_amount: 120.5,
item_amount: 100.5,
discount_amount: 10,
freight: 20,
ar_amount: 90.5,
got_amount: 80.5,
items: 'sku-a | sku-b',
items_count: 2,
items_total: 3,
items_weight: 1.5,
logistics_name: 'Logi A',
sys_logistics_code: 'SYS-LOGI-A',
sys_logistics_name: 'Sys Logi A',
express_code: 'EXP-A',
logistics_code: 'logi-a',
logistics_area_code: 'AREA-A',
oaid: 'oaid-101',
},
{
id: 102,
store_id: 2001,
warehouse_id: 100000,
delivery_order_code: 'DO-102',
source_platform_code: 'source-a',
source_platform_name: 'Source A',
sys_platform_code: 'sys-a',
sys_platform_name: 'System A',
order_type: 'normal',
status: 'paid',
step: 2,
order_flag: 'flag-b',
from_key: 'web',
create_at: '2026-04-18T02:02:03.000Z',
place_order_time: '2026-04-18T02:03:04.000Z',
pay_time: '2026-04-18T02:04:05.000Z',
operate_time: '2026-04-18T02:05:06.000Z',
delivery_time: '2026-04-18T02:06:07.000Z',
name: 'Bob',
tel: '010-77777777',
mobile: '13800000002',
email: 'bob@example.com',
country_code: 'CN',
province: 'Shanghai',
city: 'Shanghai',
area: 'Pudong',
town: 'Lujiazui',
detail_address: 'Road 2',
zip_code: '200000',
total_amount: 220,
item_amount: 200,
discount_amount: 20,
freight: 18,
ar_amount: 180,
got_amount: 160,
items: 'sku-c',
items_count: 1,
items_total: 1,
items_weight: 2.5,
logistics_name: 'Logi A',
sys_logistics_code: 'SYS-LOGI-A',
sys_logistics_name: 'Sys Logi A',
express_code: 'EXP-A',
logistics_code: 'logi-a',
logistics_area_code: 'AREA-A',
oaid: 'oaid-102',
},
{
id: 103,
store_id: 2001,
warehouse_id: 100000,
delivery_order_code: 'DO-103',
source_platform_code: 'source-a',
source_platform_name: 'Source A',
sys_platform_code: 'sys-a',
sys_platform_name: 'System A',
order_type: 'normal',
status: 'paid',
step: 2,
order_flag: 'flag-c',
from_key: 'web',
create_at: '2026-04-18T03:02:03.000Z',
place_order_time: '2026-04-18T03:03:04.000Z',
pay_time: '2026-04-18T03:04:05.000Z',
operate_time: '2026-04-18T03:05:06.000Z',
delivery_time: '2026-04-18T03:06:07.000Z',
name: 'Carol',
tel: '010-66666666',
mobile: '13800000003',
email: 'carol@example.com',
country_code: 'CN',
province: 'Zhejiang',
city: 'Hangzhou',
area: 'Xihu',
town: 'Gudang',
detail_address: 'Road 3',
zip_code: '310000',
total_amount: 320,
item_amount: 300,
discount_amount: 30,
freight: 15,
ar_amount: 290,
got_amount: 260,
items: 'sku-d | sku-e',
items_count: 2,
items_total: 4,
items_weight: 3.5,
logistics_name: 'Logi A',
sys_logistics_code: 'SYS-LOGI-A',
sys_logistics_name: 'Sys Logi A',
express_code: 'EXP-A',
logistics_code: 'logi-a',
logistics_area_code: 'AREA-A',
oaid: 'oaid-103',
},
{
id: 104,
store_id: 2001,
warehouse_id: 100000,
delivery_order_code: 'DO-104',
source_platform_code: 'source-a',
source_platform_name: 'Source A',
sys_platform_code: 'sys-a',
sys_platform_name: 'System A',
order_type: 'normal',
status: 'paid',
step: 2,
order_flag: 'flag-d',
from_key: 'web',
create_at: '2026-04-18T04:02:03.000Z',
place_order_time: '2026-04-18T04:03:04.000Z',
pay_time: '2026-04-18T04:04:05.000Z',
operate_time: '2026-04-18T04:05:06.000Z',
delivery_time: '2026-04-18T04:06:07.000Z',
name: 'Dave',
tel: '010-55555555',
mobile: '13800000004',
email: 'dave@example.com',
country_code: 'CN',
province: 'Guangdong',
city: 'Shenzhen',
area: 'Nanshan',
town: '科技园',
detail_address: 'Road 4',
zip_code: '518000',
total_amount: 420,
item_amount: 400,
discount_amount: 40,
freight: 16,
ar_amount: 360,
got_amount: 330,
items: 'sku-f',
items_count: 1,
items_total: 2,
items_weight: 4.5,
logistics_name: 'Logi A',
sys_logistics_code: 'SYS-LOGI-A',
sys_logistics_name: 'Sys Logi A',
express_code: 'EXP-A',
logistics_code: 'logi-a',
logistics_area_code: 'AREA-A',
oaid: 'oaid-104',
},
];
const relationRows = Array.isArray(options.relationRows)
? options.relationRows
: [
{
id: 77,
store_id: 2001,
warehouse_id: 100000,
print_quantity: 5,
print_quantity_locked: 0,
policy_config: {},
},
];
const existingOrderExpressRows = Array.isArray(options.existingOrderExpressRows)
? options.existingOrderExpressRows
: [];
const transaction = {
LOCK: {
UPDATE: 'UPDATE',
},
};
const app = {
Sequelize: {
Op: {
in: 'in',
or: 'or',
},
},
model: {
async transaction(callback) {
calls.transaction += 1;
return await callback(transaction);
},
},
};
const orderDeliveryModel = {
async findAll(query) {
calls.orderDeliveryFindAll += 1;
calls.orderDeliveryFindAllQuery = query;
return orderDeliveryRows;
},
async update(values, query) {
calls.orderDeliveryUpdate += 1;
calls.orderDeliveryUpdateArgs = { values, query };
calls.orderDeliveryUpdateArgsList.push({ values, query });
return [ options.orderDeliveryUpdateCount || 1 ];
},
};
const storeWarehouseModel = {
async findAll(query) {
calls.storeWarehouseFindAll += 1;
calls.storeWarehouseFindAllQuery = query;
return relationRows;
},
async increment(field, options = {}) {
calls.storeWarehouseIncrement += 1;
calls.storeWarehouseIncrementArgs = { field, options };
calls.storeWarehouseIncrementArgsList.push({ field, options });
const targetWhere = options.where || {};
const targetId = Number(targetWhere.id);
const targetRow = relationRows.find(item => Number(item.id) === targetId);
const delta = Number(options.by || 0);
if (targetRow && field && typeof field === 'string') {
targetRow[field] = Number(targetRow[field] || 0) + delta;
}
return [ options.storeWarehouseIncrementCount || 1 ];
},
async update(values, query) {
calls.storeWarehouseUpdate += 1;
calls.storeWarehouseUpdateArgs = { values, query };
calls.storeWarehouseUpdateArgsList.push({ values, query });
const targetId = query && query.where && query.where.id;
const targetRow = relationRows.find(item => item.id === targetId);
if (targetRow && values && typeof values === 'object') {
Object.assign(targetRow, values);
}
return [ options.storeWarehouseUpdateCount || 1 ];
},
};
const orderExpressModel = {
async findAll(query) {
calls.orderExpressFindAll += 1;
calls.orderExpressFindAllQuery = query;
return existingOrderExpressRows;
},
async update(values, query) {
calls.orderExpressUpdate += 1;
calls.orderExpressUpdateArgs = { values, query };
return [ options.orderExpressUpdateCount || 1 ];
},
async bulkCreate(rows, query) {
calls.orderExpressBulkCreate += 1;
calls.orderExpressBulkCreateArgs = { rows, query };
return rows;
},
};
const ctx = {
app,
user: {
store_id: options.currentWarehouseId || 100000,
},
logger: {
info() {},
warn(...args) {
calls.warnMessages.push(args.map(arg => String(arg)).join(' '));
},
error() {},
},
model: {
OrderExpress: orderExpressModel,
StoreWarehouse: storeWarehouseModel,
OrderDeliveryFactory: {
async getCurrentModel() {
calls.getCurrentOrderDeliveryModel = (calls.getCurrentOrderDeliveryModel || 0) + 1;
return orderDeliveryModel;
},
},
},
};
return {
app,
ctx,
calls,
service: Object.assign(new ExpressService(ctx), {
async loadExpressList() {
return [];
},
}),
};
}
/**
* 生成批量运单创建入参。
*
* @param {Array<number>} orderIds 订单ID 列表。
* @param {Object} [extraFields={}] 额外字段。
* @return {Array<Object>} 运单创建参数列表。
*/
function buildWaybillOrders(orderIds = [], extraFields = {}) {
return orderIds.map(orderId => ({
order_id: orderId,
business_key: 9000 + orderId,
...extraFields,
}));
}
describe('test/app/service/systems/express.createWaybillCode.balance.test.js', () => {
it('should allocate waybill codes and lock balance once for a batch when balance is sufficient', async () => {
const { calls, service } = createBalanceHarness({
relationRows: [
{
id: 77,
store_id: 2001,
warehouse_id: 100000,
print_quantity: 6,
print_quantity_locked: 1,
policy_config: {},
},
],
});
service.scheduleWaybillAsyncPostProcess = async () => {};
const result = await service.createWaybillCode(buildWaybillOrders([ 101, 102, 103 ]));
assert.equal(calls.transaction, 1);
assert.equal(calls.getCurrentOrderDeliveryModel, 1);
assert.equal(calls.orderDeliveryFindAll, 1);
assert.deepEqual(calls.orderDeliveryFindAllQuery.attributes, [
'id',
'store_id',
'warehouse_id',
'delivery_order_code',
'source_platform_code',
'source_platform_name',
'sys_platform_code',
'sys_platform_name',
'order_type',
'status',
'step',
'order_flag',
'from_key',
'create_at',
'place_order_time',
'pay_time',
'operate_time',
'delivery_time',
'name',
'tel',
'mobile',
'email',
'country_code',
'province',
'city',
'area',
'town',
'detail_address',
'zip_code',
'total_amount',
'item_amount',
'discount_amount',
'freight',
'ar_amount',
'got_amount',
'items',
'items_count',
'items_total',
'items_weight',
'logistics_name',
'sys_logistics_code',
'sys_logistics_name',
'express_code',
'logistics_area_code',
'oaid',
'source_order_codes',
'logistics_code',
]);
assert.equal(calls.storeWarehouseFindAll, 1);
assert.equal(calls.storeWarehouseUpdate, 1);
assert.equal(calls.orderDeliveryUpdate, 1);
assert.equal(calls.orderDeliveryUpdateArgsList[0].values.step, 2);
assert.deepEqual(calls.orderDeliveryUpdateArgsList[0].query.where.id.in, [ 101, 102, 103 ]);
assert.equal(calls.orderExpressFindAll, 1);
assert.equal(calls.orderExpressBulkCreate, 1);
assert.equal(calls.orderExpressBulkCreateArgs.rows.length, 3);
assert.equal(calls.storeWarehouseUpdateArgs.values.print_quantity_locked, 4);
assert.deepEqual(calls.storeWarehouseFindAllQuery.where.or, [
{ store_id: 2001, warehouse_id: 100000 },
]);
assert.equal(result.orders.length, 3);
assert.deepEqual(result.orders.map(item => item.order_id), [ 101, 102, 103 ]);
});
it('should not lock balance again for an existing status zero express row', async () => {
const { calls, service } = createBalanceHarness({
relationRows: [
{
id: 77,
store_id: 2001,
warehouse_id: 100000,
print_quantity: 5,
print_quantity_locked: 1,
policy_config: {},
},
],
existingOrderExpressRows: [
{
id: 901,
order_id: 101,
business_key: 9101,
status: 0,
retry_count: 2,
},
],
});
service.scheduleWaybillAsyncPostProcess = async () => {};
const result = await service.createWaybillCode(buildWaybillOrders([ 101, 102 ]));
assert.equal(calls.transaction, 1);
assert.equal(calls.orderExpressFindAll, 1);
assert.equal(calls.orderExpressUpdate, 1);
assert.equal(calls.orderExpressBulkCreate, 1);
assert.equal(calls.orderExpressBulkCreateArgs.rows.length, 1);
assert.deepEqual(calls.orderExpressBulkCreateArgs.rows.map(item => item.order_id), [ 102 ]);
assert.equal(calls.storeWarehouseUpdate, 1);
assert.equal(calls.storeWarehouseUpdateArgs.values.print_quantity_locked, 2);
assert.deepEqual(result.orders.map(item => item.order_id), [ 101, 102 ]);
});
it('should drop existing non-zero status express rows from success orders and async dispatch', async () => {
const { calls, service } = createBalanceHarness({
existingOrderExpressRows: [
{
id: 901,
order_id: 101,
business_key: 9101,
status: 1,
retry_count: 0,
},
],
});
const dispatchCalls = [];
service.scheduleWaybillAsyncPostProcess = async () => {};
service.dispatchWaybillAsyncPlatform = async (platformCode, bizcontent) => {
dispatchCalls.push({ platformCode, bizcontent });
return {
flag: 'success',
message: 'ok',
};
};
const result = await service.createWaybillCode(buildWaybillOrders([ 101, 102 ]), {
default_express_interface: 'POLYAPI',
});
assert.equal(calls.orderExpressFindAll, 1);
assert.equal(calls.orderExpressUpdate, 0);
assert.equal(calls.orderExpressBulkCreate, 1);
assert.equal(calls.orderExpressBulkCreateArgs.rows.length, 1);
assert.deepEqual(calls.orderExpressBulkCreateArgs.rows.map(item => item.order_id), [ 102 ]);
assert.equal(calls.storeWarehouseUpdate, 1);
assert.deepEqual(calls.storeWarehouseUpdateArgs.values, { print_quantity_locked: 1 });
assert.deepEqual(calls.orderDeliveryUpdateArgsList[0].query.where.id.in, [ 102 ]);
assert.equal(dispatchCalls.length, 1);
assert.deepEqual(dispatchCalls[0].bizcontent.orders.map(item => `${item.order_id}:${item.business_key}`), [ '102:9102' ]);
assert.deepEqual(result.orders.map(item => `${item.order_id}:${item.business_key}`), [ '102:9102' ]);
});
it('should normalize duplicate inputs before transaction processing and keep missing order exceptions separate', async () => {
const { calls, service } = createBalanceHarness();
service.scheduleWaybillAsyncPostProcess = async () => {};
const result = await service.createWaybillCode([
{ order_id: 101, business_key: 9101 },
{ order_id: 101, business_key: 9101 },
{ order_id: 999, business_key: 9999 },
]);
assert.equal(calls.transaction, 1);
assert.deepEqual(calls.orderDeliveryFindAllQuery.where.id.in, [ 101, 999 ]);
assert.equal(calls.orderDeliveryUpdate, 2);
assert.equal(calls.orderDeliveryUpdateArgsList[0].values.step, 3);
assert.deepEqual(calls.orderDeliveryUpdateArgsList[0].query.where.id.in, [ 999 ]);
assert.equal(calls.orderDeliveryUpdateArgsList[1].values.step, 2);
assert.deepEqual(calls.orderDeliveryUpdateArgsList[1].query.where.id.in, [ 101 ]);
assert.equal(calls.orderExpressBulkCreate, 1);
assert.equal(calls.orderExpressBulkCreateArgs.rows.length, 1);
assert.deepEqual(result.orders.map(item => item.order_id), [ 101 ]);
});
it('should drop overflow orders and write them back as exceptions when overallocate is disabled', async () => {
const { calls, service } = createBalanceHarness({
relationRows: [
{
id: 77,
store_id: 2001,
warehouse_id: 100000,
print_quantity: 2,
print_quantity_locked: 0,
policy_config: {},
},
],
});
service.scheduleWaybillAsyncPostProcess = async () => {};
const result = await service.createWaybillCode(buildWaybillOrders([ 101, 102, 103, 104 ]));
assert.equal(calls.transaction, 1);
assert.equal(calls.storeWarehouseFindAll, 1);
assert.equal(calls.storeWarehouseUpdate, 1);
assert.equal(calls.storeWarehouseUpdateArgs.values.print_quantity_locked, 2);
assert.equal(calls.orderDeliveryUpdate, 2);
assert.equal(calls.orderDeliveryUpdateArgsList[0].values.inspection_status, 'error');
assert.equal(calls.orderDeliveryUpdateArgsList[0].values.step, 3);
assert.equal(calls.orderDeliveryUpdateArgsList[0].values.order_flag, '货主快递余额不足:可用 2,需要 4,不足 2');
assert.deepEqual(calls.orderDeliveryUpdateArgsList[0].query.where.id.in, [ 103, 104 ]);
assert.equal(calls.orderDeliveryUpdateArgsList[1].values.step, 2);
assert.deepEqual(calls.orderDeliveryUpdateArgsList[1].query.where.id.in, [ 101, 102 ]);
assert.equal(calls.orderExpressBulkCreateArgs.rows.length, 2);
assert.deepEqual(result.orders.map(item => item.order_id), [ 101, 102 ]);
});
it('should reject overflow orders regardless of removed overallocate config', async () => {
const { calls, service } = createBalanceHarness({
relationRows: [
{
id: 77,
store_id: 2001,
warehouse_id: 100000,
print_quantity: 2,
print_quantity_locked: 0,
policy_config: {},
},
],
});
service.scheduleWaybillAsyncPostProcess = async () => {};
const result = await service.createWaybillCode(buildWaybillOrders([ 101, 102, 103, 104 ]));
assert.equal(calls.transaction, 1);
assert.equal(calls.storeWarehouseFindAll, 1);
assert.equal(calls.storeWarehouseUpdate, 1);
assert.equal(calls.storeWarehouseUpdateArgs.values.print_quantity_locked, 2);
assert.equal(calls.orderDeliveryUpdate, 2);
assert.equal(calls.orderDeliveryUpdateArgsList[0].values.inspection_status, 'error');
assert.equal(calls.orderDeliveryUpdateArgsList[0].values.step, 3);
assert.equal(calls.orderDeliveryUpdateArgsList[0].values.order_flag, '货主快递余额不足:可用 2,需要 4,不足 2');
assert.deepEqual(calls.orderDeliveryUpdateArgsList[0].query.where.id.in, [ 103, 104 ]);
assert.equal(calls.orderDeliveryUpdateArgsList[1].values.step, 2);
assert.deepEqual(calls.orderDeliveryUpdateArgsList[1].query.where.id.in, [ 101, 102 ]);
assert.equal(calls.orderExpressBulkCreateArgs.rows.length, 2);
assert.equal(calls.warnMessages.length, 0);
assert.deepEqual(result.orders.map(item => item.order_id), [ 101, 102 ]);
});
it('should allow extra waybill allocation within express quota and fail the overflow beyond quota plus balance', async () => {
const { calls, service } = createBalanceHarness({
relationRows: [
{
id: 77,
store_id: 2001,
warehouse_id: 100000,
print_quantity: 1,
print_quantity_locked: 0,
policy_config: {
express_quota: 2,
},
},
],
});
service.scheduleWaybillAsyncPostProcess = async () => {};
const result = await service.createWaybillCode(buildWaybillOrders([ 101, 102, 103, 104 ]));
assert.equal(calls.transaction, 1);
assert.equal(calls.storeWarehouseFindAll, 1);
assert.equal(calls.storeWarehouseUpdate, 1);
assert.equal(calls.storeWarehouseUpdateArgs.values.print_quantity_locked, 3);
assert.equal(calls.orderDeliveryUpdate, 2);
assert.deepEqual(calls.orderDeliveryUpdateArgsList[0].query.where.id.in, [ 104 ]);
assert.equal(calls.orderDeliveryUpdateArgsList[0].values.step, 3);
assert.equal(calls.orderDeliveryUpdateArgsList[0].values.inspection_status, 'error');
assert.equal(calls.orderDeliveryUpdateArgsList[0].values.order_flag, '货主快递余额不足:可用 3,需要 4,不足 1');
assert.deepEqual(calls.orderDeliveryUpdateArgsList[1].query.where.id.in, [ 101, 102, 103 ]);
assert.equal(calls.orderDeliveryUpdateArgsList[1].values.step, 2);
assert.equal(calls.orderExpressBulkCreateArgs.rows.length, 3);
assert.deepEqual(result.orders.map(item => item.order_id), [ 101, 102, 103 ]);
});
it('should restore locked balance after async post process fails', async () => {
const { calls, service } = createBalanceHarness({
relationRows: [
{
id: 77,
store_id: 2001,
warehouse_id: 100000,
print_quantity: 6,
print_quantity_locked: 1,
policy_config: {},
},
],
});
service.runWaybillAsyncPostProcess = async () => {
throw new Error('post process failed');
};
const resultPromise = service.createWaybillCode(buildWaybillOrders([ 101, 102, 103 ]), {
default_express_interface: 'POLYAPI',
});
const rejectionPromise = assert.rejects(resultPromise, /post process failed/);
for (let index = 0; index < 10 && calls.storeWarehouseIncrement < 1; index += 1) {
await new Promise(resolve => setTimeout(resolve, 0));
}
await rejectionPromise;
assert.equal(calls.transaction, 1);
assert.equal(calls.storeWarehouseUpdate, 1);
assert.equal(calls.storeWarehouseUpdateArgsList[0].values.print_quantity_locked, 4);
assert.deepEqual(calls.storeWarehouseUpdateArgsList[0].query.where, { id: 77 });
assert.equal(calls.storeWarehouseIncrement, 1);
assert.equal(calls.storeWarehouseIncrementArgsList[0].field, 'print_quantity_locked');
assert.equal(calls.storeWarehouseIncrementArgsList[0].options.by, -3);
assert.deepEqual(calls.storeWarehouseIncrementArgsList[0].options.where, { id: 77 });
});
it('should resolve the balance relation only once for a batched request', async () => {
const { calls, service } = createBalanceHarness({
relationRows: [
{
id: 77,
store_id: 2001,
warehouse_id: 100000,
print_quantity: 10,
print_quantity_locked: 0,
policy_config: {},
},
],
});
await service.createWaybillCode(buildWaybillOrders([ 101, 102, 103 ]));
assert.equal(calls.getCurrentOrderDeliveryModel, 1);
assert.equal(calls.orderDeliveryFindAll, 1);
assert.equal(calls.storeWarehouseFindAll, 1);
assert.equal(calls.storeWarehouseUpdate, 1);
assert.equal(calls.orderExpressFindAll, 1);
assert.equal(calls.orderExpressBulkCreate, 1);
});
it('should expand array business_key inputs before creating rows and dispatching async post process', async () => {
const { calls, service } = createBalanceHarness();
const dispatchCalls = [];
let releaseDispatch;
const dispatchGate = new Promise(resolve => {
releaseDispatch = resolve;
});
service.dispatchWaybillAsyncPlatform = async (platformCode, bizcontent) => {
dispatchCalls.push({ platformCode, bizcontent });
await dispatchGate;
return {
flag: 'success',
message: 'ok',
};
};
const resultPromise = service.createWaybillCode([
{
order_id: 101,
business_key: [ 9101, '9102' ],
wms_express_platform: {
platform_code: 'POLYAPI',
},
},
{
order_id: 102,
business_key: 9103,
wms_express_platform: {
platform_code: 'POLYAPI',
},
},
], {
default_express_interface: 'POLYAPI',
});
let settled = false;
resultPromise.then(() => {
settled = true;
});
await new Promise(resolve => setImmediate(resolve));
assert.equal(calls.orderDeliveryFindAll, 1);
assert.deepEqual(Array.from(new Set(calls.orderDeliveryFindAllQuery.where.id.in)), [ 101, 102 ]);
assert.equal(calls.orderExpressFindAll, 1);
assert.equal(calls.orderExpressBulkCreate, 1);
assert.equal(calls.orderExpressBulkCreateArgs.rows.length, 3);
assert.equal(settled, false);
assert.equal(dispatchCalls.length, 1);
assert.deepEqual(dispatchCalls[0].bizcontent.bizcontent.order_list.map(item => `${item.order_id}:${item.business_key}`), [
'101:9101',
'101:9102',
'102:9103',
]);
releaseDispatch();
await new Promise(resolve => setImmediate(resolve));
const result = await resultPromise;
assert.equal(settled, true);
assert.deepEqual(result.orders.map(item => `${item.order_id}:${item.business_key}`), [
'101:9101',
'101:9102',
'102:9103',
]);
});
it('should trigger async post process after transaction without waiting and pass platform bizcontent skeleton', async () => {
const { calls, service } = createBalanceHarness({
orderDeliveryRows: [
{
id: 101,
store_id: 2001,
warehouse_id: 100000,
delivery_order_code: 'DO-101',
source_platform_code: 'source-a',
source_platform_name: 'Source A',
sys_platform_code: 'sys-a',
sys_platform_name: 'System A',
order_type: 'normal',
status: 'paid',
step: 2,
order_flag: 'flag-a',
from_key: 'web',
create_at: '2026-04-18T01:02:03.000Z',
place_order_time: '2026-04-18T01:03:04.000Z',
pay_time: '2026-04-18T01:04:05.000Z',
operate_time: '2026-04-18T01:05:06.000Z',
delivery_time: '2026-04-18T01:06:07.000Z',
name: 'Alice',
tel: '010-88888888',
mobile: '13800000001',
email: 'alice@example.com',
country_code: 'CN',
province: 'Beijing',
city: 'Beijing',
area: 'Chaoyang',
town: 'Wangjing',
detail_address: 'Road 1',
zip_code: '100000',
total_amount: 120.5,
item_amount: 100.5,
discount_amount: 10,
freight: 20,
ar_amount: 90.5,
got_amount: 80.5,
items: 'sku-a | sku-b',
items_count: 2,
items_total: 3,
items_weight: 1.5,
logistics_name: 'Logi A',
sys_logistics_code: 'SYS-LOGI-A',
sys_logistics_name: 'Sys Logi A',
express_code: 'EXP-A',
logistics_code: 'logi-a',
logistics_area_code: 'AREA-A',
oaid: 'oaid-101',
},
],
});
const dispatchCalls = [];
let releaseDispatch;
const dispatchGate = new Promise(resolve => {
releaseDispatch = resolve;
});
service.dispatchWaybillAsyncPlatform = async (platformCode, bizcontent) => {
dispatchCalls.push({ platformCode, bizcontent });
await dispatchGate;
return {
platform_code: platformCode,
bizcontent,
handled: false,
};
};
const resultPromise = service.createWaybillCode(buildWaybillOrders([ 101 ], {
wms_express_platform: {
platform_code: 'POLYAPI',
},
}), {
default_express_interface: 'POLYAPI',
});
let settled = false;
resultPromise.then(() => {
settled = true;
});
await new Promise(resolve => setImmediate(resolve));
assert.equal(calls.transaction, 1);
assert.equal(settled, false);
assert.equal(dispatchCalls.length, 1);
assert.equal(String(dispatchCalls[0].platformCode || '').toLowerCase(), 'polyapi');
assert.equal(dispatchCalls[0].bizcontent.bizcontent.platform_code, '');
assert.deepEqual(dispatchCalls[0].bizcontent.bizcontent.platform_payload, {});
assert.equal(dispatchCalls[0].bizcontent.bizcontent.order_list.length, 1);
assert.equal(dispatchCalls[0].bizcontent.bizcontent.order_list[0].order_id, 101);
assert.equal(dispatchCalls[0].bizcontent.bizcontent.order_list[0].business_key, 9101);
assert.equal(dispatchCalls[0].bizcontent.bizcontent.order_list[0].sys_platform_code, 'sys-a');
assert.equal(dispatchCalls[0].bizcontent.bizcontent.order_list[0].logistics_code, 'logi-a');
assert.equal(dispatchCalls[0].bizcontent.bizcontent.order_list[0].express_platform_code, 'polyapi');
assert.ok(dispatchCalls[0].bizcontent.bizcontent.order_list[0].route_info);
assert.ok(dispatchCalls[0].bizcontent.bizcontent.order_list[0].order_info);
assert.ok(dispatchCalls[0].bizcontent.bizcontent.order_list[0].receiver_info);
assert.ok(dispatchCalls[0].bizcontent.bizcontent.order_list[0].settlement_info);
assert.ok(dispatchCalls[0].bizcontent.bizcontent.order_list[0].items_summary);
assert.equal(dispatchCalls[0].bizcontent.bizcontent.order_list[0].route_info.sys_platform_code, 'sys-a');
assert.equal(dispatchCalls[0].bizcontent.bizcontent.order_list[0].route_info.logistics_code, 'logi-a');
assert.equal(dispatchCalls[0].bizcontent.bizcontent.order_list[0].receiver_info.name, 'Alice');
assert.equal(dispatchCalls[0].bizcontent.bizcontent.order_list[0].settlement_info.ar_amount, 90.5);
assert.deepEqual(dispatchCalls[0].bizcontent.bizcontent.order_list[0].items_summary.items, [ 'sku-a', 'sku-b' ]);
releaseDispatch();
await new Promise(resolve => setImmediate(resolve));
const result = await resultPromise;
assert.equal(settled, true);
assert.deepEqual(result.orders.map(item => item.order_id), [ 101 ]);
});
it('should use route platform code for async dispatch instead of default_express_interface', async () => {
const { service } = createBalanceHarness();
const dispatchCalls = [];
service.dispatchWaybillAsyncPlatform = async (platformCode, bizcontent) => {
dispatchCalls.push({ platformCode, bizcontent });
return {
flag: 'success',
message: 'ok',
};
};
await service.createWaybillCode(buildWaybillOrders([ 101 ]), {
code: 'PDD',
platform_code: 'LEGACY_CODE',
route_platform_code: 'PDD_ROUTE',
default_express_interface: 'POLYAPI',
});
assert.equal(dispatchCalls.length, 1);
assert.equal(dispatchCalls[0].platformCode, 'PDD_ROUTE');
assert.equal(dispatchCalls[0].bizcontent.matchedPlatform.default_express_interface, 'POLYAPI');
});
});