订单备注命中仓库「自动分配快递排除备注」时跳过自动分配快递,并补写 ORDER_AUTO_EXPRESS_SKIP 操作日志说明原因与命中关键字 储位分配默认排除存储储位(库位类型过滤下沉到 JS 层按货主存拣分离策略逐 SKU 生效),仅当货主关闭存拣分离或前端传入 allow_storage_location=true 时放行;自动分配不再强制 allow_storage_location=true 按当前实现基准修正 order_storage_allocation 过期单测,补充排除备注与存拣分离放行用例(26/26 通过,ESLint 0 错误)
365 lines
11 KiB
JavaScript
365 lines
11 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',
|
|
]);
|
|
});
|
|
});
|