WMS_MAIN/test/app/service/systems/delivery_order.asyncProcessOrderCreated.test.js
liufumingren da7df4139d feat(order): 新增 need_oms_waybill 回 OMS 取号标记与本地取号分流
- 模型/迁移:wms_order_delivery 新增 need_oms_waybill TINYINT(4) NOT NULL DEFAULT 0,
  迁移覆盖基表与全部分表(含 _history/_dispatch/_outsourced),幂等且 down 可回滚
- 写入链路:openapi 请求根节点 need_oms_waybill(兼容 camelCase needOmsWaybill)→
  openapiMethods/deliveryorder.js 透传;delivery_order.js 仅 API 来源可能为 1,
  QM/MANUAL/STOCKOUT 一律强制 0(见相邻提交)
- 取号分流:need_oms_waybill=1 的订单不参与本地自动分配快递
  1. asyncProcessOrderCreated 不入本地取号队列,写 ORDER_NEED_OMS_WAYBILL_WAIT 操作日志
  2. express.createWaybillCodePipeline 按订单级标记过滤(同订单全部 business_key 一并跳过),
     返回值新增 skipped_need_oms_waybill_order_ids
  3. schedule/consume_waybill_allocate_queue 将对应队列行置终态 waiting_oms
     (不再走 pending 重试 → failed)
- waybill_allocate_queue.status 注释补充 waiting_oms 语义(STRING(20),无需迁移)
- test: delivery_order.asyncProcessOrderCreated.test.js 新增分流/保持本地取号用例,
  express.createWaybillCodePipeline.skipCancelled.test.js 补齐返回字段断言
2026-09-13 15:30:55 +08:00

457 lines
14 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const DeliveryOrderService = require('../../../../app/service/systems/delivery_order');
/**
* 创建异步流程测试桩。
*
* @return {{ ctx: Object, service: DeliveryOrderService, calls: Object }} 测试上下文。
*/
function createHarness() {
const calls = {
sequence: [],
warns: [],
};
const ctx = {
app: {
config: {},
},
user: {
store_id: 100000,
warehouse_id: 100000,
employees_name: '测试员',
},
logger: {
info() {},
error() {},
warn(message) {
calls.warns.push(String(message || ''));
},
},
service: {
systemMethods: {
order: {
pendingOrder: {
async allocateExpressCode() {
calls.sequence.push('allocateExpress');
return {
code: 1,
message: 'ok',
};
},
},
wave: {
async getMinGroupOrderFetchQuantity() {
calls.sequence.push('wave.getMinGroupOrderFetchQuantity');
return 2;
},
},
},
},
systems: {
orderStorageAllocation: {
async allocateStorage() {
calls.sequence.push('allocateStorage');
return {
code: 200,
message: 'ok',
};
},
},
},
},
};
const service = new DeliveryOrderService(ctx);
service.withPatchedCtxUser = async (_patchUser, callback) => {
return await callback();
};
return {
ctx,
service,
calls,
};
}
describe('test/app/service/systems/delivery_order.asyncProcessOrderCreated.test.js', () => {
it('should execute async flow in order: express -> storage -> autoGrouping', async () => {
const { service, calls, ctx } = createHarness();
service.buildAsyncProcessContext = async () => ({
orderId: 101,
warehouseId: 100000,
storeId: 200000,
order: {
id: 101,
store_id: 200000,
warehouse_id: 100000,
logistics_code: 'STO',
logistics_name: '申通',
},
orderItemRows: [],
});
service.getStoreWarehousePolicyConfig = async () => ({
auto_allocate_express: 1,
auto_allocate_storage: 1,
need_order_approval: 0,
});
service.enqueueAutoWaybillAllocation = async () => {
calls.sequence.push('allocateExpress');
};
service.buildExpressAllocateRequest = () => ({ orders: [{ order_id: 101 }] });
service.buildStorageAllocateRequest = () => ({ order_ids: [ 101 ], warehouse_id: 100000 });
service.runAutoGrouping = async () => {
calls.sequence.push('autoGrouping');
};
ctx.service.systemMethods.order.wave.submitWaveStrategyQuickScanForWarehouse = async () => null;
await service.asyncProcessOrderCreated(101, {
warehouseId: 100000,
storeId: 200000,
}, 'API');
assert.deepEqual(calls.sequence, [ 'allocateExpress', 'allocateStorage', 'autoGrouping' ]);
});
it('should continue next steps when one step fails', async () => {
const { service, calls, ctx } = createHarness();
service.buildAsyncProcessContext = async () => ({
orderId: 102,
warehouseId: 100000,
storeId: 200000,
order: {
id: 102,
store_id: 200000,
warehouse_id: 100000,
},
orderItemRows: [],
});
service.getStoreWarehousePolicyConfig = async () => ({
auto_allocate_express: 1,
auto_allocate_storage: 1,
need_order_approval: 0,
});
service.enqueueAutoWaybillAllocation = async () => {
calls.sequence.push('allocateExpress');
throw new Error('分配快递异常');
};
ctx.service.systems.orderStorageAllocation.allocateStorage = async () => {
calls.sequence.push('allocateStorage');
return { code: 200 };
};
service.runAutoGrouping = async () => {
calls.sequence.push('autoGrouping');
};
ctx.service.systemMethods.order.wave.submitWaveStrategyQuickScanForWarehouse = async () => null;
await service.asyncProcessOrderCreated(102, {
warehouseId: 100000,
storeId: 200000,
}, 'API');
assert.deepEqual(calls.sequence, [ 'allocateExpress', 'allocateStorage', 'autoGrouping' ]);
assert.equal(calls.warns.length >= 1, true);
});
it('should skip auto express allocation when order remark matches exclude keywords', async () => {
const { service, calls, ctx } = createHarness();
const skipLogs = [];
service.buildAsyncProcessContext = async () => ({
orderId: 301,
warehouseId: 100000,
storeId: 200000,
order: {
id: 301,
store_id: 200000,
warehouse_id: 100000,
remark: '买家要求:请勿自动分配快递,联系客服',
},
orderItemRows: [],
});
service.getStoreWarehousePolicyConfig = async () => ({
auto_allocate_express: 1,
auto_allocate_storage: 1,
need_order_approval: 0,
});
service.getStoreSubjectConfig = async () => ({
auto_allocate_express_exclude_remarks: [ '请勿自动分配快递' ],
});
service.enqueueAutoWaybillAllocation = async () => {
calls.sequence.push('allocateExpress');
};
service.runAutoGrouping = async () => {
calls.sequence.push('autoGrouping');
};
ctx.service.systemMethods.order.wave.submitWaveStrategyQuickScanForWarehouse = async () => null;
ctx.service.systems.operationLog = {
createOperationLogs: async payloads => {
skipLogs.push(...payloads);
},
};
await service.asyncProcessOrderCreated(301, {
warehouseId: 100000,
storeId: 200000,
}, 'API');
assert.deepEqual(calls.sequence, [ 'allocateStorage', 'autoGrouping' ]);
assert.equal(skipLogs.length, 1);
assert.equal(skipLogs[0].businessType, 'order');
assert.equal(skipLogs[0].businessId, '301');
assert.equal(skipLogs[0].operationCode, 'ORDER_AUTO_EXPRESS_SKIP');
assert.equal(skipLogs[0].operator.name, 'system');
assert.match(skipLogs[0].extras.message, /请勿自动分配快递/);
});
it('should keep auto express allocation when order remark does not match exclude keywords', async () => {
const { service, calls, ctx } = createHarness();
const skipLogs = [];
service.buildAsyncProcessContext = async () => ({
orderId: 302,
warehouseId: 100000,
storeId: 200000,
order: {
id: 302,
store_id: 200000,
warehouse_id: 100000,
remark: '正常订单,尽快发货',
},
orderItemRows: [],
});
service.getStoreWarehousePolicyConfig = async () => ({
auto_allocate_express: 1,
auto_allocate_storage: 1,
need_order_approval: 0,
});
service.getStoreSubjectConfig = async () => ({
auto_allocate_express_exclude_remarks: [ '请勿自动分配快递' ],
});
service.enqueueAutoWaybillAllocation = async () => {
calls.sequence.push('allocateExpress');
};
service.runAutoGrouping = async () => {
calls.sequence.push('autoGrouping');
};
ctx.service.systemMethods.order.wave.submitWaveStrategyQuickScanForWarehouse = async () => null;
ctx.service.systems.operationLog = {
createOperationLogs: async payloads => {
skipLogs.push(...payloads);
},
};
await service.asyncProcessOrderCreated(302, {
warehouseId: 100000,
storeId: 200000,
}, 'API');
assert.deepEqual(calls.sequence, [ 'allocateExpress', 'allocateStorage', 'autoGrouping' ]);
assert.equal(skipLogs.length, 0);
});
it('should treat buyer_message and seller_message as remark source for exclusion', async () => {
const { service } = createHarness();
const config = {
auto_allocate_express_exclude_remarks: [ '勿发圆通', '', '中通' ],
};
assert.equal(service.findAutoExpressExcludedRemark({ remark: '正常备注' }, config), '');
assert.equal(service.findAutoExpressExcludedRemark({ remark: '勿发圆通' }, config), '勿发圆通');
assert.equal(service.findAutoExpressExcludedRemark({ buyer_message: '请用中通' }, config), '中通');
assert.equal(service.findAutoExpressExcludedRemark({ seller_message: '备注圆通' }, config), '');
assert.equal(service.findAutoExpressExcludedRemark({}, null), '');
assert.equal(service.findAutoExpressExcludedRemark({}, { auto_allocate_express_exclude_remarks: [] }), '');
});
it('should not pass allow_storage_location for auto storage allocation', () => {
const { service } = createHarness();
const request = service.buildStorageAllocateRequest({
orderId: 99,
warehouseId: 100000,
});
assert.deepEqual(request, {
order_ids: [ 99 ],
warehouse_id: 100000,
});
});
it('should skip auto grouping when switch is disabled', async () => {
const { service } = createHarness();
let triggered = false;
service.getStoreSubjectConfig = async () => ({
auto_grouping_enabled: false,
max_wave_quantity: 10,
});
service.buildAutoGroupingStructure = () => ({
itemPartType: 'items_hash',
itemPart: 'hash_1',
logisticsName: '申通',
itemSnapshotKey: 'snapshot',
itemDetailMachine: 'token',
});
service.findAutoGroupingWaveId = async () => {
triggered = true;
return 0;
};
await service.runAutoGrouping({
orderId: 103,
warehouseId: 100000,
storeId: 200000,
order: {},
orderItemRows: [],
});
assert.equal(triggered, false);
});
it('should trigger auto grouping chain when enabled and conditions matched', async () => {
const { service } = createHarness();
const called = [];
service.getStoreSubjectConfig = async () => ({
auto_grouping_enabled: true,
max_wave_quantity: 2,
});
service.buildAutoGroupingStructure = () => ({
itemPartType: 'items_hash',
itemPart: 'hash_2',
logisticsName: '申通',
itemSnapshotKey: 'snapshot_2',
itemDetailMachine: 'token_2',
});
service.findAutoGroupingWaveId = async () => 0;
service.countCandidateGroupOrders = async () => 2;
service.createGroupingWave = async () => {
called.push('createGroupingWave');
return {
wave_ids: [ 9001 ],
};
};
service.markWaveAutoGrouping = async (_context, waveIds) => {
called.push(`markWaveAutoGrouping:${waveIds.join(',')}`);
};
service.finishAutoGroupingIfNeeded = async (_context, waveId, maxWaveQuantity) => {
called.push(`finishAutoGroupingIfNeeded:${waveId}:${maxWaveQuantity}`);
};
await service.runAutoGrouping({
orderId: 104,
warehouseId: 100000,
storeId: 200000,
order: {
id: 104,
},
orderItemRows: [],
});
assert.deepEqual(called, [
'createGroupingWave',
'markWaveAutoGrouping:9001',
'finishAutoGroupingIfNeeded:9001:2',
]);
});
it('should divert need_oms_waybill orders away from local auto waybill allocation', async () => {
const { service, calls, ctx } = createHarness();
const waitLogs = [];
service.buildAsyncProcessContext = async () => ({
orderId: 401,
warehouseId: 100000,
storeId: 200000,
order: {
id: 401,
store_id: 200000,
warehouse_id: 100000,
need_oms_waybill: 1,
},
orderItemRows: [],
});
service.getStoreWarehousePolicyConfig = async () => ({
auto_allocate_express: 1,
auto_allocate_storage: 0,
need_order_approval: 0,
});
service.enqueueAutoWaybillAllocation = async () => {
calls.sequence.push('allocateExpress');
};
service.runAutoGrouping = async () => {
calls.sequence.push('autoGrouping');
};
ctx.service.systemMethods.order.wave.submitWaveStrategyQuickScanForWarehouse = async () => null;
ctx.service.systems.operationLog = {
createOperationLogs: async payloads => {
waitLogs.push(...payloads);
},
};
await service.asyncProcessOrderCreated(401, {
warehouseId: 100000,
storeId: 200000,
}, 'API');
// 不进入本地取号队列
assert.equal(calls.sequence.includes('allocateExpress'), false);
assert.equal(waitLogs.length, 1);
assert.equal(waitLogs[0].businessType, 'order');
assert.equal(waitLogs[0].businessId, '401');
assert.equal(waitLogs[0].operationCode, 'ORDER_NEED_OMS_WAYBILL_WAIT');
assert.equal(waitLogs[0].operator.name, 'system');
assert.match(waitLogs[0].extras.message, /等待外部 OMS 取号/);
});
it('should keep local auto waybill allocation when need_oms_waybill is 0', async () => {
const { service, calls, ctx } = createHarness();
const waitLogs = [];
service.buildAsyncProcessContext = async () => ({
orderId: 402,
warehouseId: 100000,
storeId: 200000,
order: {
id: 402,
store_id: 200000,
warehouse_id: 100000,
need_oms_waybill: 0,
},
orderItemRows: [],
});
service.getStoreWarehousePolicyConfig = async () => ({
auto_allocate_express: 1,
auto_allocate_storage: 0,
need_order_approval: 0,
});
service.enqueueAutoWaybillAllocation = async () => {
calls.sequence.push('allocateExpress');
};
service.runAutoGrouping = async () => {
calls.sequence.push('autoGrouping');
};
ctx.service.systemMethods.order.wave.submitWaveStrategyQuickScanForWarehouse = async () => null;
ctx.service.systems.operationLog = {
createOperationLogs: async payloads => {
waitLogs.push(...payloads);
},
};
await service.asyncProcessOrderCreated(402, {
warehouseId: 100000,
storeId: 200000,
}, 'API');
assert.equal(calls.sequence.includes('allocateExpress'), true);
assert.equal(waitLogs.length, 0);
});
});