WMS_MAIN/test/app/controller/system.order.pendingOrder.reverseReviewOrder.test.js
2026-04-16 20:00:01 +08:00

614 lines
18 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const Sequelize = require('sequelize');
const PendingOrderService = require('../../../app/service/systemMethods/order/pendingOrder');
const ReviewOrderService = require('../../../app/service/systemMethods/order/reviewOrder');
const ExpectionOrderService = require('../../../app/service/systemMethods/order/expectionOrder');
const { buildRows, createOrderHarness, matchesWhere } = require('../../support/order_service_test_helper');
const OrderIndexService = require('../../../app/service/systemMethods/order/index');
/**
* 创建支持更新结果回写的订单模型。
* @param {Array<Object>} rows 初始订单数据。
* @param {Object} capture 调用信息。
* @return {Object} 模拟模型。
*/
function createMutableOrderModel(rows, capture = {}) {
const storedRows = Array.isArray(rows) ? rows.map(row => ({ ...row })) : [];
capture.rows = storedRows;
return {
async count(options = {}) {
capture.countWhere = options.where;
return storedRows.filter(row => matchesWhere(row, options.where)).length;
},
async update(values = {}, options = {}) {
capture.updateValues = { ...values };
capture.updateWhere = options.where;
const matchedRows = storedRows.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 storedRows
.filter(row => matchesWhere(row, options.where))
.map(row => ({ ...row }));
},
};
}
/**
* 创建待处理订单反审服务测试实例。
* @param {Array<Object>} rows 初始订单数据。
* @return {{ service: PendingOrderService, capture: Object }} 服务与调用记录。
*/
function createPendingOrderService(rows) {
const { service } = createOrderHarness(PendingOrderService, {
defaultRows: rows,
});
const capture = {};
const model = createMutableOrderModel(rows, capture);
service.getOrderModel = async moduleName => {
capture.moduleName = moduleName;
return model;
};
return {
service,
capture,
};
}
/**
* 创建待处理订单取消与列表语义验证的共享服务实例。
* @param {Array<Object>} rows 初始订单数据。
* @return {{ pendingService: PendingOrderService, orderService: OrderIndexService, reviewService: ReviewOrderService, expectionService: ExpectionOrderService, capture: Object }} 共享服务与调用记录。
*/
function createPendingOrderWorkflowService(rows) {
const { ctx, service: pendingService } = createOrderHarness(PendingOrderService, {
defaultRows: rows,
});
const orderService = new OrderIndexService(ctx);
const reviewService = new ReviewOrderService(ctx);
const expectionService = new ExpectionOrderService(ctx);
const capture = {};
const model = createMutableOrderModel(rows, capture);
pendingService.getOrderModel = async moduleName => {
capture.moduleName = moduleName;
return model;
};
orderService.getOrderModel = async moduleName => {
capture.moduleName = moduleName;
return model;
};
reviewService.getOrderModel = async moduleName => {
capture.moduleName = moduleName;
return model;
};
expectionService.getOrderModel = async moduleName => {
capture.moduleName = moduleName;
return model;
};
return {
pendingService,
orderService,
reviewService,
expectionService,
capture,
};
}
describe('test/app/controller/system.order.pendingOrder.reverseReviewOrder.test.js', () => {
it('should reverse batch orders by ids array', async () => {
const { service, capture } = createPendingOrderService(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 result = await service.reverseReviewOrder({
ids: [ 1, 3 ],
});
assert.equal(capture.updateValues.inspection_status, 'none');
assert.equal(capture.updateValues.step, 0);
assert.equal(capture.updateValues.order_flag, undefined);
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(result.id, 1);
assert.equal(result.inspection_status, 'none');
assert.equal(result.step, 0);
assert.equal(result.updated_count, 2);
assert.deepEqual(result.ids, [ 1, 3 ]);
assert.equal(capture.rows.find(row => row.id === 1).order_flag, '待清理异常标记');
assert.equal(capture.rows.find(row => row.id === 3).order_flag, '待清理异常标记');
});
it('should mark batch orders as pending outbound by ids array', async () => {
const { service, capture } = createPendingOrderService(buildRows(3, index => ({
id: index + 1,
warehouse_id: 100000,
step: 1,
outbound_status: 'none',
updated_at: '2026-04-12 00:00:00',
})));
const result = await service.markPendingOutboundOrder({
ids: [ 1, 3 ],
});
assert.equal(capture.updateValues.outbound_status, 'padding');
assert.ok(capture.updateValues.updated_at instanceof Date);
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(result.id, 1);
assert.equal(result.outbound_status, 'padding');
assert.equal(result.updated_count, 2);
assert.deepEqual(result.ids, [ 1, 3 ]);
assert.equal(capture.rows.find(row => row.id === 1).outbound_status, 'padding');
assert.equal(capture.rows.find(row => row.id === 3).outbound_status, 'padding');
});
it('should mark batch orders by ids array and reject cross-warehouse orders', async () => {
const scenarios = [
{
payload: {
ids: [ 1 ],
},
expectedId: 1,
},
{
payload: {
ids: [ 2 ],
},
expectedId: 2,
},
];
for (const scenario of scenarios) {
const { service, capture } = createPendingOrderService(buildRows(2, index => ({
id: index + 1,
warehouse_id: 100000,
step: 1,
outbound_status: 'none',
updated_at: '2026-04-12 00:00:00',
})));
const result = await service.markPendingOutboundOrder(scenario.payload);
assert.equal(capture.updateValues.outbound_status, 'padding');
assert.ok(capture.updateValues.updated_at instanceof Date);
assert.equal(capture.updateWhere.id, scenario.expectedId);
assert.equal(capture.updateWhere.warehouse_id, 100000);
assert.equal(result.id, scenario.expectedId);
assert.equal(result.outbound_status, 'padding');
assert.equal(result.updated_count, 1);
assert.deepEqual(result.ids, [ scenario.expectedId ]);
assert.equal(capture.rows.find(row => row.id === scenario.expectedId).outbound_status, 'padding');
}
const invalidPayloads = [
{
payload: {
id: 1,
},
message: /仅支持 ids 数组入参,不再支持 id、order_id、逗号字符串或其他兼容字段/,
},
{
payload: {
order_id: 2,
},
message: /仅支持 ids 数组入参,不再支持 id、order_id、逗号字符串或其他兼容字段/,
},
{
payload: {
ids: '1,2',
},
message: /仅支持 ids 数组入参/,
},
{
payload: {
ids: [],
},
message: /订单ID不能为空/,
},
{
payload: {
ids: [ 1, 0 ],
},
message: /ids 仅支持正整数数组/,
},
{
payload: {
ids: [ 1, -2 ],
},
message: /ids 仅支持正整数数组/,
},
];
for (const scenario of invalidPayloads) {
const { service } = createPendingOrderService(buildRows(2, index => ({
id: index + 1,
warehouse_id: 100000,
step: 1,
outbound_status: 'none',
updated_at: '2026-04-12 00:00:00',
})));
await assert.rejects(
async () => await service.markPendingOutboundOrder(scenario.payload),
scenario.message
);
}
const { service: crossWarehouseService } = createPendingOrderService([
{
id: 1,
warehouse_id: 100000,
step: 1,
outbound_status: 'none',
updated_at: '2026-04-12 00:00:00',
},
{
id: 2,
warehouse_id: 100001,
step: 1,
outbound_status: 'none',
updated_at: '2026-04-12 00:00:00',
},
]);
await assert.rejects(
async () => await crossWarehouseService.markPendingOutboundOrder({
ids: [ 1, 2 ],
}),
/订单不存在或不属于当前仓库/
);
});
it('should reverse single order with legacy id/order_id fields', async () => {
const scenarios = [
{
payload: {
id: 1,
},
expectedId: 1,
},
{
payload: {
order_id: 2,
},
expectedId: 2,
},
];
for (const scenario of scenarios) {
const { service, capture } = createPendingOrderService(buildRows(2, index => ({
id: index + 1,
warehouse_id: 100000,
step: 1,
inspection_status: 'fin',
order_flag: '待清理异常标记',
updated_at: '2026-04-12 00:00:00',
})));
const result = await service.reverseReviewOrder(scenario.payload);
assert.equal(capture.updateValues.inspection_status, 'none');
assert.equal(capture.updateValues.step, 0);
assert.equal(capture.updateValues.order_flag, undefined);
assert.equal(capture.updateWhere.id, scenario.expectedId);
assert.equal(capture.updateWhere.warehouse_id, 100000);
assert.equal(result.id, scenario.expectedId);
assert.equal(result.inspection_status, 'none');
assert.equal(result.step, 0);
assert.equal(result.updated_count, 1);
assert.deepEqual(result.ids, [ scenario.expectedId ]);
assert.equal(capture.rows.find(row => row.id === scenario.expectedId).order_flag, '待清理异常标记');
}
});
it('should reject when pending outbound order ids are missing', async () => {
const { service } = createPendingOrderService(buildRows(1, index => ({
id: index + 1,
warehouse_id: 100000,
step: 1,
outbound_status: 'none',
updated_at: '2026-04-12 00:00:00',
})));
await assert.rejects(
async () => await service.markPendingOutboundOrder({
ids: [],
}),
/订单ID不能为空/
);
});
it('should reject when pending outbound ids is not an array', async () => {
const { service } = createPendingOrderService(buildRows(1, index => ({
id: index + 1,
warehouse_id: 100000,
step: 1,
outbound_status: 'none',
updated_at: '2026-04-12 00:00:00',
})));
await assert.rejects(
async () => await service.markPendingOutboundOrder({
ids: '1,2',
}),
/仅支持 ids 数组入参/
);
});
it('should reject when ids, id and order_id are missing', async () => {
const { service } = createPendingOrderService(buildRows(1, index => ({
id: index + 1,
warehouse_id: 100000,
step: 1,
inspection_status: 'fin',
updated_at: '2026-04-12 00:00:00',
})));
await assert.rejects(
async () => await service.reverseReviewOrder({}),
/订单ID不能为空/
);
});
it('should write back to pending review status', async () => {
const { service, capture } = createPendingOrderService(buildRows(1, index => ({
id: index + 1,
warehouse_id: 100000,
step: 1,
inspection_status: 'fin',
order_flag: '待清理异常标记',
updated_at: '2026-04-12 00:00:00',
})));
const result = await service.reverseReviewOrder({
id: 1,
});
assert.equal(capture.updateValues.inspection_status, 'none');
assert.equal(capture.updateValues.step, 0);
assert.equal(capture.updateValues.order_flag, undefined);
assert.equal(result.inspection_status, 'none');
assert.equal(result.step, 0);
assert.equal(result.updated_count, 1);
assert.deepEqual(result.ids, [ 1 ]);
assert.equal(capture.rows[0].order_flag, '待清理异常标记');
});
it('should cancel batch orders by ids array and remove them from pending review exception lists', async () => {
const { pendingService, orderService, reviewService, expectionService, capture } = createPendingOrderWorkflowService(buildRows(3, index => ({
id: index + 1,
warehouse_id: 100000,
step: index === 0 ? 0 : (index === 1 ? 1 : 3),
status: 'created',
outbound_status: 'none',
inspection_status: index === 0 ? 'none' : 'fin',
updated_at: '2026-04-12 00:00:00',
})));
const result = await pendingService.cancelOrder({
ids: [ 1, 2, 3 ],
});
assert.equal(capture.updateValues.status, 'cancel');
assert.equal(capture.updateValues.step, 9);
assert.ok(capture.updateValues.updated_at instanceof Date);
assert.equal(capture.updateWhere.warehouse_id, 100000);
assert.equal(capture.updateWhere.id[Sequelize.Op.in].length, 3);
assert.equal(result.id, 1);
assert.equal(result.status, 'cancel');
assert.equal(result.step, 9);
assert.equal(result.updated_count, 3);
assert.deepEqual(result.ids, [ 1, 2, 3 ]);
for (const orderId of [ 1, 2, 3 ]) {
const row = capture.rows.find(item => item.id === orderId);
assert.equal(row.status, 'cancel');
assert.equal(row.step, 9);
}
const pendingList = await pendingService.list({
pagination: {
page: 1,
pageSize: 20,
},
where: {},
});
const reviewList = await reviewService.list({
pagination: {
page: 1,
pageSize: 20,
},
where: {},
});
const exceptionList = await expectionService.list({
pagination: {
page: 1,
pageSize: 20,
},
where: {},
});
assert.equal(pendingList.pagination.total, 0);
assert.equal(reviewList.pagination.total, 0);
assert.equal(exceptionList.pagination.total, 0);
assert.deepEqual(pendingList.list, []);
assert.deepEqual(reviewList.list, []);
assert.deepEqual(exceptionList.list, []);
});
it('should reject cancel order when ids are empty invalid or forbidden', async () => {
const invalidPayloads = [
{
payload: {},
message: /仅支持 ids 数组入参/,
},
{
payload: {
id: 1,
},
message: /仅支持 ids 数组入参,不再支持 id、order_id、逗号字符串或其他兼容字段/,
},
{
payload: {
ids: [],
},
message: /订单ID不能为空/,
},
{
payload: {
ids: '1,2',
},
message: /仅支持 ids 数组入参/,
},
{
payload: {
ids: [ 1, 0 ],
},
message: /ids 仅支持正整数数组/,
},
{
payload: {
ids: [ 1, -2 ],
},
message: /ids 仅支持正整数数组/,
},
];
for (const scenario of invalidPayloads) {
const { service: pendingService } = createPendingOrderService(buildRows(2, index => ({
id: index + 1,
warehouse_id: 100000,
step: 1,
status: 'created',
outbound_status: 'none',
updated_at: '2026-04-12 00:00:00',
})));
await assert.rejects(
async () => await pendingService.cancelOrder(scenario.payload),
scenario.message
);
}
const { service: crossWarehouseService } = createPendingOrderService([
{
id: 1,
warehouse_id: 100000,
step: 1,
status: 'created',
outbound_status: 'none',
updated_at: '2026-04-12 00:00:00',
},
{
id: 2,
warehouse_id: 100001,
step: 1,
status: 'created',
outbound_status: 'none',
updated_at: '2026-04-12 00:00:00',
},
]);
await assert.rejects(
async () => await crossWarehouseService.cancelOrder({
ids: [ 1, 2 ],
}),
/订单不存在或不属于当前仓库/
);
const forbiddenScenarios = [
{
rows: [
{
id: 1,
warehouse_id: 100000,
step: 5,
status: 'created',
outbound_status: 'none',
updated_at: '2026-04-12 00:00:00',
},
],
message: /订单当前状态不允许取消/,
},
{
rows: [
{
id: 1,
warehouse_id: 100000,
step: 1,
status: 'created',
outbound_status: 'padding',
updated_at: '2026-04-12 00:00:00',
},
],
message: /订单当前状态不允许取消/,
},
{
rows: [
{
id: 1,
warehouse_id: 100000,
step: 9,
status: 'cancel',
outbound_status: 'none',
updated_at: '2026-04-12 00:00:00',
},
],
message: /订单当前状态不允许取消/,
},
];
for (const scenario of forbiddenScenarios) {
const { service: pendingService } = createPendingOrderService(scenario.rows);
await assert.rejects(
async () => await pendingService.cancelOrder({
ids: [ 1 ],
}),
scenario.message
);
}
});
it('should return success when pending table has no data', async () => {
const { service } = createPendingOrderService([]);
const result = await service.cancelOrder({
ids: [ 1 ],
});
assert.equal(result.flag, 'success');
assert.equal(result.message, '取消成功');
assert.equal(result.updated_count, 0);
assert.deepEqual(result.ids, []);
});
});