- products 新增 no_need_ship(默认 0),商品编辑页可开关,列表展示「免发货」标签 - 命中该商品的订单明细:审核不锁库存、发货不扣库存、推送 WMS 时不进入报文 - 整单明细都是这类商品时:审核报错提示、推送按永久失败转异常 - 补单测(推送明细过滤)与迁移 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
137 lines
3.9 KiB
JavaScript
137 lines
3.9 KiB
JavaScript
'use strict';
|
|
|
|
const { assert } = require('egg-mock/bootstrap');
|
|
const OrderWmsService = require('../../../app/service/orderWms');
|
|
|
|
function createService(rows, pushOne) {
|
|
const service = Object.create(OrderWmsService.prototype);
|
|
service.app = {
|
|
Sequelize: {
|
|
Op: {
|
|
in: Symbol('in'),
|
|
or: Symbol('or'),
|
|
lt: Symbol('lt'),
|
|
},
|
|
},
|
|
};
|
|
service.ctx = {
|
|
model: {
|
|
OrderStatusQueued: {
|
|
async findAll(options) {
|
|
assert(options.limit === 50);
|
|
return rows;
|
|
},
|
|
},
|
|
},
|
|
service: {
|
|
cache: {
|
|
async getEnabledPrincipalIds() {
|
|
return [ 1 ];
|
|
},
|
|
},
|
|
orderStatus: {
|
|
markPushFailed() {
|
|
return Promise.resolve();
|
|
},
|
|
},
|
|
},
|
|
logger: {
|
|
error() {},
|
|
},
|
|
};
|
|
service.pushOne = pushOne;
|
|
return service;
|
|
}
|
|
|
|
describe('test/app/service/orderWms.test.js', () => {
|
|
it('should push the pending batch concurrently up to its limit', async () => {
|
|
const rows = Array.from({ length: 50 }, (_, index) => ({ id: index + 1, principal_id: 1 }));
|
|
let active = 0;
|
|
let maxActive = 0;
|
|
const service = createService(rows, async () => {
|
|
active++;
|
|
maxActive = Math.max(maxActive, active);
|
|
await new Promise(resolve => setTimeout(resolve, 10));
|
|
active--;
|
|
return { ok: true };
|
|
});
|
|
|
|
const stat = await service.processPending();
|
|
assert(stat.scanned === 50);
|
|
assert(stat.pushed === 50);
|
|
assert(maxActive === 50);
|
|
});
|
|
|
|
it('should wait for all in-flight pushes before surfacing an error', async () => {
|
|
let secondFinished = false;
|
|
const rows = [
|
|
{ id: 1, principal_id: 1 },
|
|
{ id: 2, principal_id: 1 },
|
|
];
|
|
const service = createService(rows, async id => {
|
|
if (id === 1) throw new Error('push failed');
|
|
await new Promise(resolve => setTimeout(resolve, 10));
|
|
secondFinished = true;
|
|
return { ok: true };
|
|
});
|
|
|
|
let error;
|
|
try {
|
|
await service.processPending();
|
|
} catch (err) {
|
|
error = err;
|
|
}
|
|
assert(error instanceof AggregateError);
|
|
assert(secondFinished);
|
|
});
|
|
});
|
|
|
|
describe('test/app/service/orderWms 不用发货商品过滤', () => {
|
|
function createFilterService(products) {
|
|
const service = Object.create(OrderWmsService.prototype);
|
|
service.app = {
|
|
Sequelize: {
|
|
Op: { in: Symbol('in'), or: Symbol('or') },
|
|
},
|
|
};
|
|
service.ctx = {
|
|
model: {
|
|
Product: {
|
|
async findAll() {
|
|
return products;
|
|
},
|
|
},
|
|
},
|
|
};
|
|
return service;
|
|
}
|
|
|
|
it('按商品ID或商品编码命中「不用发货」时排除该明细', async () => {
|
|
const service = createFilterService([
|
|
{ id: 11, item_code: 'VIRTUAL-1', no_need_ship: 1 },
|
|
{ id: 12, item_code: 'REAL-1', no_need_ship: 0 },
|
|
]);
|
|
const result = await service.filterShippableItems({ principal_id: 5 }, [
|
|
{ id: 1, product_id: 11, item_code: 'VIRTUAL-1' },
|
|
{ id: 2, product_id: 12, item_code: 'REAL-1' },
|
|
{ id: 3, product_id: null, item_code: 'VIRTUAL-1' },
|
|
]);
|
|
assert.deepStrictEqual(result.map(item => item.id), [ 2 ]);
|
|
});
|
|
|
|
it('没有「不用发货」商品时原样返回', async () => {
|
|
const service = createFilterService([{ id: 12, item_code: 'REAL-1', no_need_ship: 0 }]);
|
|
const items = [{ id: 1, product_id: 12, item_code: 'REAL-1' }];
|
|
const result = await service.filterShippableItems({ principal_id: 5 }, items);
|
|
assert.strictEqual(result.length, 1);
|
|
assert.strictEqual(result[0], items[0]);
|
|
});
|
|
|
|
it('商品库查不到时不做过滤(交给后续校验报错)', async () => {
|
|
const service = createFilterService([]);
|
|
const items = [{ id: 1, product_id: 99, item_code: 'UNKNOWN' }];
|
|
const result = await service.filterShippableItems({ principal_id: 5 }, items);
|
|
assert.strictEqual(result.length, 1);
|
|
});
|
|
});
|