- storeWarehouse.store.saveApiConfig:新增 resolveContractApiInterfaceType, interface_type_id=3(OPEN_API_WAREHOUSE 仓库开放接口)自动映射为 2(OPEN_API 货主开放接口)落库, 不再要求当前主体为仓库(货主可维护自身签约关系下的接口配置) - callback_url 不再取入参(含 callbackUrl 别名),统一写入 config.openapi.defaultCallbackUrl(新增配置项,默认空) - storeWarehouse.store.listApiConfig:interface_type_id 过滤同步映射(传 3 按 2 查询) - 文档:methods/system.json 升级 0.3.350、methods/changes/system.json 变更记录、docs/openapi-callback-events.md 回调地址来源说明 - 测试:saveApiConfig 新增 4 条用例(3→2 落库、忽略入参回调地址、默认值未配置、货主主体可保存、缺失类型 404),listApiConfig 新增 1 条过滤映射用例 同时提交工作区其余在制品改动: - waybill:移除 wms_order_express_trace 模型并新增 drop 迁移,轨迹查询/取号/打印链路调整 - order:订单 SN 编码(order_item_sn / order_item_sn_log / sn_code_rule、has_sn_item 回填迁移),pendingOrder/recheckOrder/wave/base 调整 - express_platform、order_async、order_export、order_storage_allocation、storage.report、stock_store 配套修复 - lib:新增 jd_client,调整 qimen_client、callback_functions 及配套测试 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
329 lines
11 KiB
JavaScript
329 lines
11 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const { Op } = require('sequelize');
|
|
|
|
const WaveService = require('../../../../../app/service/systemMethods/order/wave');
|
|
|
|
/**
|
|
* 创建 singleFetchByIds 轻量测试上下文。
|
|
*
|
|
* @param {Object} options 初始化参数。
|
|
* @param {Array<Object>} options.orders 订单数据。
|
|
* @param {number} [options.storeId=100000] 仓库ID。
|
|
* @return {{ service: WaveService, state: Object }} 测试上下文。
|
|
*/
|
|
function createHarness({ orders = [], storeId = 100000 } = {}) {
|
|
const orderStore = orders.map(item => ({ ...item }));
|
|
const state = {
|
|
committed: false,
|
|
rolledBack: false,
|
|
createWaveCalls: [],
|
|
orderLogItems: [],
|
|
};
|
|
|
|
const transaction = {
|
|
LOCK: {
|
|
UPDATE: 'UPDATE',
|
|
},
|
|
async commit() {
|
|
state.committed = true;
|
|
},
|
|
async rollback() {
|
|
state.rolledBack = true;
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {},
|
|
Sequelize: {
|
|
literal(value) {
|
|
return value;
|
|
},
|
|
},
|
|
},
|
|
user: {
|
|
store_id: storeId,
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
model: {
|
|
async transaction() {
|
|
return transaction;
|
|
},
|
|
async query() {
|
|
return [[], { affectedRows: 0 }];
|
|
},
|
|
},
|
|
};
|
|
|
|
const service = new WaveService(ctx);
|
|
service.getGroupWavePrefix = async () => 'B';
|
|
service.getSingleFetchByIdsWavePrefix = async () => 'A';
|
|
service.getMinGroupOrderFetchQuantity = async () => 3;
|
|
service.createWaveBatchReady = async params => {
|
|
state.createWaveCalls.push({ ...params });
|
|
const wavePrefix = String(params && params.wavePrefix || 'X');
|
|
return {
|
|
waveId: 9001,
|
|
waveNo: `${wavePrefix}202605060001`,
|
|
};
|
|
};
|
|
service.batchCreateOrderOperationLogs = async logItems => {
|
|
state.orderLogItems = Array.isArray(logItems) ? logItems : [];
|
|
return state.orderLogItems.length;
|
|
};
|
|
|
|
service.getOrderModel = async () => ({
|
|
async findAll(options = {}) {
|
|
const where = options.where || {};
|
|
const idIn = where.id && where.id[Op.in];
|
|
const filterIds = Array.isArray(idIn) ? idIn : [];
|
|
const waveEq = Number(where.wave_id || 0);
|
|
const whereOr = Array.isArray(where[Op.or]) ? where[Op.or] : [];
|
|
|
|
const matchGroupCandidates = row => {
|
|
if (waveEq !== 0 || whereOr.length === 0) {
|
|
return false;
|
|
}
|
|
|
|
return whereOr.some(clause => {
|
|
if (!clause || typeof clause !== 'object') {
|
|
return false;
|
|
}
|
|
|
|
if (clause.items_hash && clause.items_hash[Op.in]) {
|
|
return clause.items_hash[Op.in].includes(String(row.items_hash || ''));
|
|
}
|
|
|
|
if (clause.items && clause.items[Op.in]) {
|
|
return clause.items[Op.in].includes(String(row.items || ''));
|
|
}
|
|
|
|
return false;
|
|
});
|
|
};
|
|
|
|
if (waveEq > 0) {
|
|
return orderStore
|
|
.filter(row => filterIds.includes(Number(row.id || 0)))
|
|
.filter(row => Number(row.wave_id || 0) === waveEq)
|
|
.map(row => ({ id: row.id }));
|
|
}
|
|
|
|
if (whereOr.length > 0) {
|
|
return orderStore
|
|
.filter(row => Number(row.wave_id || 0) === 0)
|
|
.filter(row => [ 1, 2, 3 ].includes(Number(row.step || 0)))
|
|
.filter(row => String(row.status || '') !== 'cancel')
|
|
.filter(row => String(row.outbound_status || '') !== 'padding')
|
|
.filter(row => matchGroupCandidates(row))
|
|
.map(row => ({
|
|
id: row.id,
|
|
items_hash: row.items_hash,
|
|
items: row.items,
|
|
logistics_name: row.logistics_name,
|
|
}));
|
|
}
|
|
|
|
return orderStore
|
|
.filter(row => filterIds.includes(Number(row.id || 0)))
|
|
.map(row => ({
|
|
id: row.id,
|
|
wave_id: row.wave_id,
|
|
wave_no: row.wave_no,
|
|
step: row.step,
|
|
status: row.status,
|
|
outbound_status: row.outbound_status,
|
|
items_hash: row.items_hash,
|
|
items: row.items,
|
|
logistics_name: row.logistics_name,
|
|
intercept_status: row.intercept_status,
|
|
lock_status: row.lock_status,
|
|
is_b2b: row.is_b2b,
|
|
has_sn_item: row.has_sn_item,
|
|
}));
|
|
},
|
|
|
|
async update(values = {}, options = {}) {
|
|
const where = options.where || {};
|
|
const idIn = where.id && where.id[Op.in];
|
|
const ids = Array.isArray(idIn) ? idIn : [];
|
|
const waveIdEq = Number(where.wave_id || 0);
|
|
let affected = 0;
|
|
|
|
for (const row of orderStore) {
|
|
if (!ids.includes(Number(row.id || 0))) {
|
|
continue;
|
|
}
|
|
|
|
if (Number(row.wave_id || 0) !== waveIdEq) {
|
|
continue;
|
|
}
|
|
|
|
row.wave_id = Number(values.wave_id || 0);
|
|
row.wave_no = String(values.wave_no || '');
|
|
affected += 1;
|
|
}
|
|
|
|
return [ affected ];
|
|
},
|
|
});
|
|
|
|
return {
|
|
service,
|
|
state,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systemMethods/order/wave.singleFetchByIds.test.js', () => {
|
|
it('should exclude orders already in wave and fetch remaining orders into one single wave', async () => {
|
|
const { service, state } = createHarness({
|
|
orders: [
|
|
{ id: 1, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none' },
|
|
{ id: 2, wave_id: 88, wave_no: 'W88', step: 1, status: 'new', outbound_status: 'none' },
|
|
{ id: 3, wave_id: 0, wave_no: '', step: 2, status: 'cancel', outbound_status: 'none' },
|
|
{ id: 4, wave_id: 0, wave_no: '', step: 9, status: 'new', outbound_status: 'none' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ ids: [ 1, 2, 3, 4, 5 ] });
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.wave_id, 9001);
|
|
assert.equal(result.wave_no, 'A202605060001');
|
|
assert.deepEqual(result.fetched_order_ids, [ 1, 3, 4 ]);
|
|
assert.equal(result.fetched_order_count, 3);
|
|
assert.deepEqual(result.excluded_existing_wave_order_ids, [ 2 ]);
|
|
assert.deepEqual(result.not_found_order_ids, [ 5 ]);
|
|
|
|
assert.equal(state.createWaveCalls.length, 1);
|
|
assert.equal(state.createWaveCalls[0].wavePrefix, 'A');
|
|
assert.equal(state.createWaveCalls[0].waveType, 2);
|
|
assert.equal(state.createWaveCalls[0].distinguishExpress, false);
|
|
assert.equal(state.createWaveCalls[0].logisticsName, '');
|
|
assert.equal(state.orderLogItems.length, 3);
|
|
assert.equal(state.orderLogItems[0].operationCode, 'ORDER_WAVE_ASSIGN');
|
|
assert.equal(state.committed, true);
|
|
assert.equal(state.rolledBack, false);
|
|
});
|
|
|
|
it('should return empty fetch result when no order can be fetched', async () => {
|
|
const { service, state } = createHarness({
|
|
orders: [
|
|
{ id: 10, wave_id: 77, wave_no: 'W77', step: 1, status: 'new', outbound_status: 'none' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ ids: [ 10 ] });
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.wave_id, 0);
|
|
assert.equal(result.fetched_order_count, 0);
|
|
assert.deepEqual(result.excluded_existing_wave_order_ids, [ 10 ]);
|
|
assert.equal(state.createWaveCalls.length, 0);
|
|
assert.equal(state.orderLogItems.length, 0);
|
|
assert.equal(state.committed, true);
|
|
assert.equal(state.rolledBack, false);
|
|
});
|
|
|
|
it('should fetch same-structure orders when they are not in any wave', async () => {
|
|
const { service, state } = createHarness({
|
|
orders: [
|
|
{ id: 101, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', items_hash: 'H-A', items: 'I-A', logistics_name: 'ZTO' },
|
|
{ id: 102, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', items_hash: 'H-A', items: 'I-A', logistics_name: 'YTO' },
|
|
{ id: 103, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', items_hash: 'H-A', items: 'I-A', logistics_name: 'STO' },
|
|
{ id: 104, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', items_hash: 'H-B', items: 'I-B', logistics_name: 'ZTO' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ ids: [ 101, 104 ] });
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.wave_id, 9001);
|
|
assert.deepEqual(result.fetched_order_ids, [ 101, 104 ]);
|
|
assert.deepEqual(result.skipped_order_ids, []);
|
|
assert.equal(state.createWaveCalls.length, 1);
|
|
assert.equal(state.createWaveCalls[0].wavePrefix, 'A');
|
|
assert.equal(state.createWaveCalls[0].waveType, 2);
|
|
assert.equal(state.createWaveCalls[0].distinguishExpress, false);
|
|
assert.equal(state.committed, true);
|
|
assert.equal(state.rolledBack, false);
|
|
});
|
|
|
|
it('should support order_ids alias for delete-by-ids scene', async () => {
|
|
const { service, state } = createHarness({
|
|
orders: [
|
|
{ id: 201, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none' },
|
|
{ id: 202, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ order_ids: [ 201, 202 ] });
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.deepEqual(result.ids, [ 201, 202 ]);
|
|
assert.deepEqual(result.fetched_order_ids, [ 201, 202 ]);
|
|
assert.equal(result.fetched_order_count, 2);
|
|
assert.equal(state.createWaveCalls.length, 1);
|
|
assert.equal(state.committed, true);
|
|
assert.equal(state.rolledBack, false);
|
|
});
|
|
|
|
it('should support orderIds alias for delete-by-ids scene', async () => {
|
|
const { service, state } = createHarness({
|
|
orders: [
|
|
{ id: 301, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ orderIds: [ 301 ] });
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.deepEqual(result.ids, [ 301 ]);
|
|
assert.deepEqual(result.fetched_order_ids, [ 301 ]);
|
|
assert.equal(result.fetched_order_count, 1);
|
|
assert.equal(state.createWaveCalls.length, 1);
|
|
assert.equal(state.committed, true);
|
|
assert.equal(state.rolledBack, false);
|
|
});
|
|
|
|
it('should allow SN orders to be fetched by order ids', async () => {
|
|
const { service } = createHarness({
|
|
orders: [
|
|
{ id: 401, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', has_sn_item: 'Y' },
|
|
{ id: 402, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', has_sn_item: 'N' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ ids: [ 401, 402 ] });
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.deepEqual(result.fetched_order_ids, [ 401, 402 ]);
|
|
assert.equal(result.fetched_order_count, 2);
|
|
assert.deepEqual(result.excluded_sn_order_ids, []);
|
|
assert.deepEqual(result.skipped_order_ids, []);
|
|
});
|
|
|
|
it('should still exclude B2B and intercepted orders while allowing SN orders', async () => {
|
|
const { service } = createHarness({
|
|
orders: [
|
|
{ id: 501, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', has_sn_item: 'Y' },
|
|
{ id: 502, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', is_b2b: 'Y' },
|
|
{ id: 503, wave_id: 0, wave_no: '', step: 1, status: 'new', outbound_status: 'none', intercept_status: 'padding' },
|
|
],
|
|
});
|
|
|
|
const result = await service.singleFetchByIds({ ids: [ 501, 502, 503 ] });
|
|
|
|
assert.deepEqual(result.fetched_order_ids, [ 501 ]);
|
|
assert.deepEqual(result.excluded_b2b_order_ids, [ 502 ]);
|
|
assert.deepEqual(result.excluded_intercepted_order_ids, [ 503 ]);
|
|
assert.deepEqual(result.excluded_sn_order_ids, []);
|
|
});
|
|
});
|