273 lines
9.9 KiB
JavaScript
273 lines
9.9 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const Sequelize = require('sequelize');
|
|
const OrderPendingExceptionService = require('../../../../app/service/systems/order_pending_exception');
|
|
const PendingOrderService = require('../../../../app/service/systemMethods/order/pendingOrder');
|
|
const ExpectionOrderService = require('../../../../app/service/systemMethods/order/expectionOrder');
|
|
const { buildRows, createServiceContext, matchesWhere } = require('../../../support/order_service_test_helper');
|
|
|
|
/**
|
|
* 创建可回写的订单模型。
|
|
* @param {Array<Object>} rows 初始订单数据。
|
|
* @param {Object} capture 调用记录。
|
|
* @return {Object} 模拟模型。
|
|
*/
|
|
function createMutableOrderModel(rows, capture = {}) {
|
|
return {
|
|
async update(values = {}, options = {}) {
|
|
capture.updateValues = { ...values };
|
|
capture.updateWhere = options.where;
|
|
|
|
const matchedRows = rows.filter(row => matchesWhere(row, options.where));
|
|
for (const row of matchedRows) {
|
|
Object.assign(row, values);
|
|
}
|
|
|
|
return [ matchedRows.length ];
|
|
},
|
|
async findAll(options = {}) {
|
|
capture.findAllWhere = options.where;
|
|
return rows
|
|
.filter(row => matchesWhere(row, options.where))
|
|
.map(row => ({ ...row }));
|
|
},
|
|
async count({ where } = {}) {
|
|
capture.countWhere = where;
|
|
return rows.filter(row => matchesWhere(row, where)).length;
|
|
},
|
|
async findOne(options = {}) {
|
|
capture.findOneWhere = options.where;
|
|
const matchedRow = rows.find(row => matchesWhere(row, options.where));
|
|
return matchedRow ? { ...matchedRow } : null;
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 创建待处理异常标记测试所需的服务实例。
|
|
* @param {Array<Object>} rows 初始订单数据。
|
|
* @return {{ ctx: Object, capture: Object, exceptionService: OrderPendingExceptionService, pendingService: PendingOrderService, exceptionListService: ExpectionOrderService }} 服务与调用记录。
|
|
*/
|
|
function createServices(rows) {
|
|
const ctx = createServiceContext(100000);
|
|
ctx.logger = {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
debug() {},
|
|
};
|
|
ctx.service = {
|
|
systems: {},
|
|
};
|
|
|
|
const capture = {};
|
|
const sharedModel = createMutableOrderModel(rows, capture);
|
|
|
|
const exceptionService = new OrderPendingExceptionService(ctx);
|
|
const pendingService = new PendingOrderService(ctx);
|
|
const exceptionListService = new ExpectionOrderService(ctx);
|
|
|
|
exceptionService.getOrderModel = async () => sharedModel;
|
|
pendingService.getOrderModel = async () => sharedModel;
|
|
exceptionListService.getOrderModel = async () => sharedModel;
|
|
|
|
return {
|
|
ctx,
|
|
capture,
|
|
exceptionService,
|
|
pendingService,
|
|
exceptionListService,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/order_pending_exception.test.js', () => {
|
|
it('should mark a single order as pending exception and change list visibility', async () => {
|
|
const rows = buildRows(1, () => ({
|
|
id: 1,
|
|
warehouse_id: 100000,
|
|
step: 1,
|
|
inspection_status: 'fin',
|
|
order_flag: '',
|
|
updated_at: '2026-04-12 00:00:00',
|
|
}));
|
|
const { capture, exceptionService, pendingService, exceptionListService } = createServices(rows);
|
|
|
|
const pendingBefore = await pendingService.list({ where: {} });
|
|
const exceptionBefore = await exceptionListService.list({ where: {} });
|
|
|
|
assert.equal(pendingBefore.list.length, 1);
|
|
assert.equal(exceptionBefore.list.length, 0);
|
|
|
|
const result = await exceptionService.markPendingException({
|
|
order_id: 1,
|
|
reason: '缺少异常处理单据',
|
|
});
|
|
|
|
assert.equal(capture.updateWhere.id, 1);
|
|
assert.equal(capture.updateWhere.warehouse_id, 100000);
|
|
assert.equal(capture.updateValues.inspection_status, 'error');
|
|
assert.equal(capture.updateValues.step, 3);
|
|
assert.equal(capture.updateValues.order_flag, '缺少异常处理单据');
|
|
assert.equal(result.id, 1);
|
|
assert.equal(result.inspection_status, 'error');
|
|
assert.equal(result.step, 3);
|
|
assert.equal(result.exception_text, '缺少异常处理单据');
|
|
assert.equal(result.order_flag, '缺少异常处理单据');
|
|
|
|
const pendingAfter = await pendingService.list({ where: {} });
|
|
const exceptionAfter = await exceptionListService.list({ where: {} });
|
|
|
|
assert.equal(pendingAfter.list.length, 0);
|
|
assert.equal(exceptionAfter.list.length, 1);
|
|
assert.equal(exceptionAfter.list[0].step, 3);
|
|
assert.equal(exceptionAfter.list[0].inspection_status, 'error');
|
|
assert.equal(exceptionAfter.list[0].order_flag, '缺少异常处理单据');
|
|
});
|
|
|
|
it('should support batch ids and return affected ids', async () => {
|
|
const rows = buildRows(3, index => ({
|
|
id: index + 1,
|
|
warehouse_id: 100000,
|
|
step: 1,
|
|
inspection_status: 'fin',
|
|
order_flag: '',
|
|
updated_at: '2026-04-12 00:00:00',
|
|
}));
|
|
const { capture, exceptionService, pendingService, exceptionListService } = createServices(rows);
|
|
|
|
const result = await exceptionService.markPendingException({
|
|
ids: [ 1, 3 ],
|
|
exception_text: '批量异常标记',
|
|
});
|
|
|
|
assert.equal(capture.updateWhere.warehouse_id, 100000);
|
|
assert.equal(capture.updateWhere.id[Sequelize.Op.in].length, 2);
|
|
assert.ok(matchesWhere({ id: 1, warehouse_id: 100000 }, capture.updateWhere));
|
|
assert.ok(!matchesWhere({ id: 2, warehouse_id: 100000 }, capture.updateWhere));
|
|
assert.ok(matchesWhere({ id: 3, warehouse_id: 100000 }, capture.updateWhere));
|
|
assert.equal(capture.updateValues.inspection_status, 'error');
|
|
assert.equal(capture.updateValues.step, 3);
|
|
assert.equal(capture.updateValues.order_flag, '批量异常标记');
|
|
assert.equal(result.updated_count, 2);
|
|
assert.deepEqual(result.ids, [ 1, 3 ]);
|
|
|
|
const pendingAfter = await pendingService.list({ where: {} });
|
|
const exceptionAfter = await exceptionListService.list({ where: {} });
|
|
|
|
assert.equal(pendingAfter.list.length, 1);
|
|
assert.equal(exceptionAfter.list.length, 2);
|
|
assert.ok(exceptionAfter.list.every(item => item.step === 3));
|
|
});
|
|
|
|
it('should restore a single pending exception order to normal', async () => {
|
|
const rows = buildRows(1, () => ({
|
|
id: 1,
|
|
warehouse_id: 100000,
|
|
step: 3,
|
|
inspection_status: 'error',
|
|
order_flag: '待处理异常标记',
|
|
updated_at: '2026-04-12 00:00:00',
|
|
}));
|
|
const { capture, exceptionService, pendingService, exceptionListService } = createServices(rows);
|
|
|
|
const pendingBefore = await pendingService.list({ where: {} });
|
|
const exceptionBefore = await exceptionListService.list({ where: {} });
|
|
|
|
assert.equal(pendingBefore.list.length, 0);
|
|
assert.equal(exceptionBefore.list.length, 1);
|
|
|
|
const result = await exceptionService.reversePendingException({
|
|
order_id: 1,
|
|
});
|
|
|
|
assert.equal(capture.updateWhere.id, 1);
|
|
assert.equal(capture.updateWhere.warehouse_id, 100000);
|
|
assert.equal(capture.updateValues.inspection_status, 'padding');
|
|
assert.equal(capture.updateValues.step, 2);
|
|
assert.equal(capture.updateValues.order_flag, '');
|
|
assert.equal(result.id, 1);
|
|
assert.equal(result.inspection_status, 'padding');
|
|
assert.equal(result.step, 2);
|
|
assert.equal(result.order_flag, '');
|
|
|
|
const pendingAfter = await pendingService.list({ where: {} });
|
|
const exceptionAfter = await exceptionListService.list({ where: {} });
|
|
|
|
assert.equal(pendingAfter.list.length, 1);
|
|
assert.equal(pendingAfter.list[0].step, 2);
|
|
assert.equal(pendingAfter.list[0].inspection_status, 'padding');
|
|
assert.equal(pendingAfter.list[0].order_flag, '');
|
|
assert.equal(exceptionAfter.list.length, 0);
|
|
});
|
|
|
|
it('should restore batch pending exception orders to normal', async () => {
|
|
const rows = buildRows(2, index => ({
|
|
id: index + 1,
|
|
warehouse_id: 100000,
|
|
step: 3,
|
|
inspection_status: 'error',
|
|
order_flag: '批量异常标记',
|
|
updated_at: '2026-04-12 00:00:00',
|
|
}));
|
|
const { capture, exceptionService, pendingService, exceptionListService } = createServices(rows);
|
|
|
|
const result = await exceptionService.reversePendingException({
|
|
ids: [ 1, 2 ],
|
|
});
|
|
|
|
assert.equal(capture.updateWhere.warehouse_id, 100000);
|
|
assert.equal(capture.updateWhere.id[Sequelize.Op.in].length, 2);
|
|
assert.ok(matchesWhere({ id: 1, warehouse_id: 100000 }, capture.updateWhere));
|
|
assert.ok(matchesWhere({ id: 2, warehouse_id: 100000 }, capture.updateWhere));
|
|
assert.equal(capture.updateValues.inspection_status, 'padding');
|
|
assert.equal(capture.updateValues.step, 2);
|
|
assert.equal(capture.updateValues.order_flag, '');
|
|
assert.equal(result.updated_count, 2);
|
|
assert.deepEqual(result.ids, [ 1, 2 ]);
|
|
assert.equal(result.inspection_status, 'padding');
|
|
assert.equal(result.step, 2);
|
|
assert.equal(result.order_flag, '');
|
|
|
|
const pendingAfter = await pendingService.list({ where: {} });
|
|
const exceptionAfter = await exceptionListService.list({ where: {} });
|
|
|
|
assert.equal(pendingAfter.list.length, 2);
|
|
assert.ok(pendingAfter.list.every(item => item.step === 2));
|
|
assert.ok(pendingAfter.list.every(item => item.inspection_status === 'padding'));
|
|
assert.ok(pendingAfter.list.every(item => item.order_flag === ''));
|
|
assert.equal(exceptionAfter.list.length, 0);
|
|
});
|
|
|
|
it('should reject invalid parameters', async () => {
|
|
const rows = buildRows(1, () => ({
|
|
id: 1,
|
|
warehouse_id: 100000,
|
|
step: 1,
|
|
inspection_status: 'fin',
|
|
order_flag: '',
|
|
updated_at: '2026-04-12 00:00:00',
|
|
}));
|
|
const { exceptionService } = createServices(rows);
|
|
|
|
await assert.rejects(
|
|
async () => await exceptionService.markPendingException({
|
|
id: 1,
|
|
}),
|
|
/异常文本不能为空/
|
|
);
|
|
|
|
await assert.rejects(
|
|
async () => await exceptionService.markPendingException({
|
|
exception_text: '缺少订单ID',
|
|
}),
|
|
/订单ID不能为空/
|
|
);
|
|
|
|
await assert.rejects(
|
|
async () => await exceptionService.reversePendingException({}),
|
|
/订单ID不能为空/
|
|
);
|
|
});
|
|
});
|