291 lines
9.7 KiB
JavaScript
291 lines
9.7 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,
|
|
}));
|
|
},
|
|
|
|
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);
|
|
});
|
|
});
|