WMS_MAIN/test/app/service/systemMethods/order/pendingOrder.manualWaybill.log.test.js
liufumingren 1e1c13f38c feat(order): 新增运单号批量导入,并增加运单号仓库内唯一校验
- 新增 order.pendingOrder.importWaybill:按 delivery_order_code + store_remark 定位当前仓库待处理订单,逐条写入运单记录并返回逐条结果
- manualWaybill 与 importWaybill 共用 assertWaybillEntryAllowed / assertWaybillCodeUnused / createManualWaybillRecord,保证口径一致
- manualWaybill 增加运单号同仓库唯一校验(delete_at 为空时已存在即拒绝)
- 同步更新 API 文档(methods/system.json、methods/changes/system.json)与 AI_METHOD_ROUTING.md
- 补充 importWaybill 单元测试,修复 manualWaybill 测试缺失的 OrderExpress.findOne 桩

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-23 15:39:38 +08:00

185 lines
5.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const assert = require('node:assert/strict');
const PendingOrderService = require('../../../../../app/service/systemMethods/order/pendingOrder');
/**
* 创建 manualWaybill 操作日志回归测试上下文。
*
* 仅覆盖 operationLog.createOperationLog 的 businessId 传入值。
* @param options
*/
function createManualWaybillLogHarness(options = {}) {
const calls = {
operationLogPayload: null,
orderExpressCreatePayload: null,
orderDeliveryUpdatePayload: null,
syncOrderIdsPayload: null,
};
const warehouseId = options.warehouseId || 100000;
const testOrderId = options.orderId || 999001;
const orderRow = {
id: testOrderId,
step: options.orderStep || 1,
outbound_status: options.outboundStatus || 'none',
warehouse_id: warehouseId,
store_id: options.storeId || 1000000,
};
const orderExpressMax = options.orderExpressMax !== undefined ? options.orderExpressMax : null;
const ctx = {
app: {
Sequelize: {
Op: {
in: 'in',
},
},
},
user: {
store_id: warehouseId,
employees_id: '9001',
employees_name: '测试员',
},
model: {
OrderExpress: {
async findOne() {
return options.existingCompleted || null;
},
async max(field, whereOptions) {
return orderExpressMax;
},
async create(values) {
calls.orderExpressCreatePayload = values;
return {
id: 10001,
...values,
};
},
},
OrderDeliveryFactory: {
getCurrentModel: () => {
throw new Error('应使用 getOrderModel,不是 OrderDeliveryFactory');
},
},
},
service: {
systems: {
operationLog: {
async createOperationLog(payload) {
calls.operationLogPayload = payload;
return { id: 1 };
},
},
storeWarehouse: {
async getContractConfig() {
return null;
},
},
},
},
logger: {
info() {},
warn() {},
error() {},
},
};
const service = new PendingOrderService(ctx);
// 注入 mock getOrderModel / buildOrderWhere / success / getWarehouseId
service.getWarehouseId = () => warehouseId;
service.getOrderLogOperator = () => ({
id: ctx.user && ctx.user.employees_id,
name: ctx.user && ctx.user.employees_name,
});
service.getOrderLogWarehouseId = () => warehouseId;
service.getOrderModel = async () => ({
async findOne(query) {
return orderRow;
},
async update(values, whereOpts) {
calls.orderDeliveryUpdatePayload = { values, where: whereOpts.where };
return [ 1 ];
},
});
service.buildOrderWhere = orderIds => ({
id: { [ctx.app.Sequelize.Op.in]: orderIds },
warehouse_id: warehouseId,
});
service.success = (msg, data) => ({ code: 0, msg, data });
service.syncOrderIds = payload => {
calls.syncOrderIdsPayload = payload;
return Promise.resolve();
};
return { service, calls, ctx };
}
describe('test/app/service/systems/express.manualWaybill.guard.test.js — manualWaybill 操作日志', () => {
it('createOperationLog 的 businessId 应为 String(orderId),而非仓库ID', async () => {
const testOrderId = 888001;
const { service, calls } = createManualWaybillLogHarness({
orderId: testOrderId,
warehouseId: 100000,
});
await service.manualWaybill({
order_id: testOrderId,
waybill_code: 'SF1234567890',
});
assert.ok(calls.operationLogPayload, '应调用 createOperationLog');
assert.equal(calls.operationLogPayload.businessType, 'order');
assert.equal(calls.operationLogPayload.businessId, String(testOrderId),
`businessId 应为订单ID "${testOrderId}",而非仓库ID`);
assert.equal(calls.operationLogPayload.operationCode, 'ORDER_MANUAL_WAYBILL');
assert.equal(calls.operationLogPayload.extras.warehouseId, 100000);
assert.equal(calls.operationLogPayload.extras.orderId, testOrderId);
assert.equal(calls.operationLogPayload.extras.waybillCode, 'SF1234567890');
});
it('businessId 不应为 warehouseId(回归验证)', async () => {
const testOrderId = 888002;
const warehouseId = 100000;
const { service, calls } = createManualWaybillLogHarness({
orderId: testOrderId,
warehouseId,
});
await service.manualWaybill({
order_id: testOrderId,
waybill_code: 'YT9876543210',
});
assert.ok(calls.operationLogPayload, '应调用 createOperationLog');
assert.notEqual(calls.operationLogPayload.businessId, String(warehouseId),
`businessId 不应为仓库ID "${warehouseId}"`);
assert.equal(calls.operationLogPayload.businessId, String(testOrderId),
`businessId 应为订单ID "${testOrderId}"`);
});
it('business_key 从已有最大值递增', async () => {
const testOrderId = 888003;
const { service, calls } = createManualWaybillLogHarness({
orderId: testOrderId,
orderExpressMax: 3,
});
await service.manualWaybill({
order_id: testOrderId,
waybill_code: 'EMS0000000001',
});
assert.ok(calls.orderExpressCreatePayload);
assert.equal(calls.orderExpressCreatePayload.business_key, 4, '应从 max(3)+1=4 递增');
assert.equal(calls.orderExpressCreatePayload.status, 1, '手工录入 status 应为 1');
});
});