169 lines
4.6 KiB
JavaScript
169 lines
4.6 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const DeliveryOrderService = require('../../../../app/service/systems/delivery_order');
|
|
|
|
/**
|
|
* 构建 addOrderLines 测试桩。
|
|
*
|
|
* @param {Object} options 可配置项。
|
|
* @param {Object} options.stockStoreRecords StockStore 桩返回值:{ byPk, byCode }。
|
|
* @return {Object} 服务实例与调用记录。
|
|
*/
|
|
function createHarness({ stockStoreRecords = {} } = {}) {
|
|
const findByPkCalls = [];
|
|
const findOneCalls = [];
|
|
const bulkCreateCalls = [];
|
|
|
|
const appLogger = {
|
|
debug() {},
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
};
|
|
|
|
const app = {
|
|
logger: appLogger,
|
|
getCacheAsync: async () => null,
|
|
setCacheAsync: async () => {},
|
|
delCacheAsync: async () => {},
|
|
};
|
|
|
|
const ctx = {
|
|
app,
|
|
logger: appLogger,
|
|
model: {
|
|
OrderItemsFactory: {
|
|
async getCurrentModel() {
|
|
return {
|
|
async bulkCreate(lineData) {
|
|
bulkCreateCalls.push(lineData);
|
|
return lineData;
|
|
},
|
|
};
|
|
},
|
|
},
|
|
StockStore: {
|
|
async findByPk(itemId) {
|
|
findByPkCalls.push(String(itemId));
|
|
return stockStoreRecords.byPk || null;
|
|
},
|
|
async findOne({ where } = {}) {
|
|
findOneCalls.push(where);
|
|
return stockStoreRecords.byCode || null;
|
|
},
|
|
},
|
|
StockWarehouse: {
|
|
async findAll() {
|
|
return [];
|
|
},
|
|
},
|
|
},
|
|
service: {
|
|
systems: {
|
|
stockStore: {
|
|
async getStoreWarehousePolicyByStoreAndWarehouse() {
|
|
return {};
|
|
},
|
|
async lockItemStorages() {
|
|
return { success: true };
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
app.model = ctx.model;
|
|
const service = new DeliveryOrderService(ctx);
|
|
|
|
return { service, findByPkCalls, findOneCalls, bulkCreateCalls };
|
|
}
|
|
|
|
const WMS_STOCK = {
|
|
id: 1001076,
|
|
stock_id: 154103,
|
|
store_id: 1000186,
|
|
warehouse_id: 1000028,
|
|
item_code: '6934864204356',
|
|
item_name: '黑妹炭丝护龈牙刷',
|
|
goods_code: 'A.16.006.000001',
|
|
barcode: '6934864204356',
|
|
brand_name: null,
|
|
sku_property: null,
|
|
stock_unit: null,
|
|
category_id: 0,
|
|
category_name: null,
|
|
item_type: 'ZC',
|
|
retail_price: 0,
|
|
gross_weight: 0,
|
|
net_weight: 0,
|
|
length: 0,
|
|
width: 0,
|
|
height: 0,
|
|
volume: 0,
|
|
};
|
|
|
|
const INFO = { warehouseId: 1000028, appKey: '21226717', storeId: 1000186 };
|
|
const LINE = {
|
|
itemId: '6934864204356',
|
|
itemCode: '6934864204356',
|
|
itemName: '',
|
|
planQty: 12,
|
|
orderLineNo: '0',
|
|
};
|
|
|
|
describe('test/app/service/systems/delivery_order.addOrderLines.fallback.test.js', () => {
|
|
it('itemId 非空但非 WMS 主键时,降级按 item_code 查到商品并成功创建明细', async () => {
|
|
const { service, findByPkCalls, findOneCalls, bulkCreateCalls } = createHarness({
|
|
stockStoreRecords: { byCode: WMS_STOCK },
|
|
});
|
|
|
|
const result = await service.addOrderLines([ LINE ], 7788, INFO, {});
|
|
|
|
assert.equal(result.stock_count, 1);
|
|
assert.ok(result.items_str.includes('154103+12'), `items_str=${result.items_str}`);
|
|
assert.deepEqual(findByPkCalls, [ '6934864204356' ]);
|
|
assert.equal(findOneCalls.length, 1);
|
|
assert.equal(findOneCalls[0].item_code, '6934864204356');
|
|
assert.equal(findOneCalls[0].warehouse_id, 1000028);
|
|
assert.equal(findOneCalls[0].store_id, 1000186);
|
|
assert.equal(findOneCalls[0].app_key, '21226717');
|
|
assert.equal(bulkCreateCalls.length, 1);
|
|
assert.equal(bulkCreateCalls[0][0].item_id, 1001076);
|
|
});
|
|
|
|
it('itemId 对应主键存在且同货主时,直接按主键命中,不做 item_code 降级查询', async () => {
|
|
const { service, findOneCalls } = createHarness({
|
|
stockStoreRecords: { byPk: WMS_STOCK },
|
|
});
|
|
|
|
const result = await service.addOrderLines([ LINE ], 7788, INFO, {});
|
|
|
|
assert.equal(result.stock_count, 1);
|
|
assert.equal(findOneCalls.length, 0);
|
|
});
|
|
|
|
it('主键记录跨货主且 item_code 也查不到时,报未同步推送到仓库', async () => {
|
|
const { service } = createHarness({
|
|
stockStoreRecords: { byPk: { ...WMS_STOCK, store_id: 999999 } },
|
|
});
|
|
|
|
await assert.rejects(
|
|
() => service.addOrderLines([ LINE ], 7788, INFO, {}),
|
|
/未同步推送到仓库/
|
|
);
|
|
});
|
|
|
|
it('itemId 为空时,仅按 item_code 查询(既有行为回归)', async () => {
|
|
const { service, findByPkCalls, findOneCalls } = createHarness({
|
|
stockStoreRecords: { byCode: WMS_STOCK },
|
|
});
|
|
|
|
const result = await service.addOrderLines([{ ...LINE, itemId: '' }], 7788, INFO, {});
|
|
|
|
assert.equal(result.stock_count, 1);
|
|
assert.equal(findByPkCalls.length, 0);
|
|
assert.equal(findOneCalls.length, 1);
|
|
});
|
|
});
|