WMS_MAIN/test/app/service/systems/express.manualWaybill.guard.test.js
liufumingren 19f0449992 fix: 手工运单号防误删三层防护 + 操作日志businessId修正
1. hardDeleteWaybillAsyncOrderExpressRows 加 status:0 过滤,保护手工录入 status=1 记录
2. createWaybillCodePipeline 入口跳过 business_key=1 且已分配运单的订单
3. manualWaybill 操作日志 businessId 从仓库ID 修正为订单ID String(orderId)
4. 新增 14 个单元测试,全部通过
2026-07-07 17:06:43 +08:00

408 lines
12 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 ExpressService = require('../../../../app/service/systems/express');
// ============================================================================
// Harness: hardDeleteWaybillAsyncOrderExpressRows
// ============================================================================
function createHardDeleteHarness(options = {}) {
const calls = {
destroyCount: 0,
destroyWhere: null,
loggerErrors: [],
};
const orderExpressModel = {
async destroy(whereOptions) {
calls.destroyCount += 1;
calls.destroyWhere = whereOptions;
if (typeof options.destroy === 'function') {
return await options.destroy(whereOptions);
}
return 1;
},
};
const ctx = {
app: {
Sequelize: {
Op: {
or: 'or',
},
},
},
model: {
OrderExpress: options.orderExpressModel !== undefined ? options.orderExpressModel : orderExpressModel,
},
logger: {
info() {},
warn() {},
error(...args) {
calls.loggerErrors.push(args);
},
},
};
const service = new ExpressService(ctx);
return { service, calls, ctx };
}
// ============================================================================
// Harness: createWaybillCodePipeline — skip business_key=1 + status=1
// ============================================================================
function createPipelineSkipHarness(options = {}) {
const calls = {
asyncTaskPayloads: [],
printPlatformFindAllQueries: [],
listByWarehousePlatformCodePayloads: [],
orderExpressFindAllQueries: [],
loggerInfoMessages: [],
};
const orderDeliveryRows = Array.isArray(options.orderDeliveryRows)
? options.orderDeliveryRows
: [
{
id: 1,
sys_platform_code: 'PDD',
logistics_code: 'SF',
sys_platform_name: '拼多多',
logistics_name: '顺丰',
store_id: 100000,
},
{
id: 2,
sys_platform_code: 'PDD',
logistics_code: 'SF',
sys_platform_name: '拼多多',
logistics_name: '顺丰',
store_id: 100000,
},
];
const orderExpressRows = Array.isArray(options.orderExpressRows)
? options.orderExpressRows
: [];
const platformList = Array.isArray(options.platformList) ? options.platformList : [{
code: 'PDD',
print_platform_id: 901,
default_express_interface: 'POLYAPI',
}];
const printPlatformRows = Array.isArray(options.printPlatformRows) ? options.printPlatformRows : [
{
id: 901,
platform_code: 'PDD_PRINT',
request_platform_code: 'OPEN_API',
},
];
const listData = Array.isArray(options.listData) ? options.listData : [];
const ctx = {
app: {
Sequelize: {
Op: {
in: 'in',
},
},
},
user: {
store_id: 100000,
},
model: {
OrderExpress: {
async findAll(query) {
calls.orderExpressFindAllQueries.push(query);
return orderExpressRows;
},
},
OrderDeliveryFactory: {
async getCurrentModel() {
return {
async findAll() {
return orderDeliveryRows;
},
async update() {
return [ 1 ];
},
};
},
},
PrintPlatform: {
async findAll(query) {
calls.printPlatformFindAllQueries.push(query);
return printPlatformRows;
},
},
},
service: {
systems: {
platform: {
async getPlatformList() {
return platformList;
},
},
setting: {
expressInterface: {
async listByWarehousePlatformCode(payload) {
calls.listByWarehousePlatformCodePayloads.push(payload);
return { data: listData };
},
},
},
},
},
logger: {
info(...args) {
calls.loggerInfoMessages.push(args.map(a => String(a)).join(' '));
},
warn() {},
error() {},
},
};
const service = new ExpressService(ctx);
service.asyncTask = (batch, opts, matchedPlatform, expressInterfaceConfig) => {
calls.asyncTaskPayloads.push({ batch, opts, matchedPlatform, expressInterfaceConfig });
};
return { service, calls, ctx };
}
// ============================================================================
// Tests: hardDeleteWaybillAsyncOrderExpressRows
// ============================================================================
describe('test/app/service/systems/express.manualWaybill.guard.test.js', () => {
describe('hardDeleteWaybillAsyncOrderExpressRows — status:0 过滤', () => {
it('destroy 应包含 status:0 条件,保护 status=1 的手工记录', async () => {
const { service, calls } = createHardDeleteHarness();
await service.hardDeleteWaybillAsyncOrderExpressRows(
[
{ order_id: 100, business_key: 1 },
{ order_id: 200, business_key: 2 },
],
{ platform_code: 'PDD', error_message: '测试失败' }
);
assert.equal(calls.destroyCount, 1);
assert.ok(calls.destroyWhere, 'destroy 应被调用');
assert.ok(calls.destroyWhere.where, 'where 条件应存在');
assert.equal(calls.destroyWhere.where.status, 0, 'status 必须为 0');
assert.ok(Array.isArray(calls.destroyWhere.where.or), 'or 条件应存在');
assert.equal(calls.destroyWhere.where.or.length, 2);
});
it('缺少 business_key 时应返回 false,不调用 destroy', async () => {
const { service, calls } = createHardDeleteHarness();
const result = await service.hardDeleteWaybillAsyncOrderExpressRows(
[{ order_id: 100 }],
{ platform_code: 'JD' }
);
assert.equal(result, false);
assert.equal(calls.destroyCount, 0, '缺少 business_key 不应调用 destroy');
});
it('缺少 order_id 时应返回 false,不调用 destroy', async () => {
const { service, calls } = createHardDeleteHarness();
const result = await service.hardDeleteWaybillAsyncOrderExpressRows(
[{ business_key: 1 }],
{ platform_code: 'JD' }
);
assert.equal(result, false);
assert.equal(calls.destroyCount, 0);
});
it('OrderExpress 模型不可用时应返回 false', async () => {
const { service, calls } = createHardDeleteHarness({
orderExpressModel: null,
});
const result = await service.hardDeleteWaybillAsyncOrderExpressRows(
[{ order_id: 100, business_key: 1 }],
{}
);
assert.equal(result, false);
assert.equal(calls.destroyCount, 0);
});
it('空数组应返回 false', async () => {
const { service, calls } = createHardDeleteHarness();
const result = await service.hardDeleteWaybillAsyncOrderExpressRows([], {});
assert.equal(result, false);
assert.equal(calls.destroyCount, 0);
});
it('destroy 抛异常时应返回 false 并记录错误日志', async () => {
const { service, calls } = createHardDeleteHarness({
destroy: async () => {
throw new Error('DB 连接失败');
},
});
const result = await service.hardDeleteWaybillAsyncOrderExpressRows(
[{ order_id: 100, business_key: 1 }],
{ platform_code: 'PDD', error_message: 'err' }
);
assert.equal(result, false);
assert.equal(calls.destroyCount, 1);
assert.ok(calls.loggerErrors.length > 0, '应记录错误日志');
});
});
// ==========================================================================
// Tests: createWaybillCodePipeline — 跳过已分配运单的订单
// ==========================================================================
describe('createWaybillCodePipeline — 跳过 business_key=1 已分配订单', () => {
it('订单已有 status=1 且传入 business_key=1 时,应跳过该订单', async () => {
const { service, calls } = createPipelineSkipHarness({
orderDeliveryRows: [
{
id: 101,
sys_platform_code: 'PDD',
logistics_code: 'SF',
sys_platform_name: '拼多多',
logistics_name: '顺丰',
store_id: 100000,
},
{
id: 102,
sys_platform_code: 'PDD',
logistics_code: 'SF',
sys_platform_name: '拼多多',
logistics_name: '顺丰',
store_id: 100000,
},
],
orderExpressRows: [
{ order_id: 101, business_key: 1, status: 1 },
],
});
await service.createWaybillCodePipeline([
{ order_id: 101, business_key: 1 },
{ order_id: 102, business_key: 1 },
]);
// 订单 101 应被跳过,只有 102 进入 asyncTask
assert.equal(calls.asyncTaskPayloads.length, 1, '只有未分配的订单应进入 asyncTask');
const batch = calls.asyncTaskPayloads[0].batch;
const batchOrderIds = batch.map(o => o.id);
assert.ok(!batchOrderIds.includes(101), '订单 101 不应在 batch 中');
assert.ok(batchOrderIds.includes(102), '订单 102 应在 batch 中');
});
it('订单有 status=1 但传入 business_key=2 时,不应跳过(不同 business_key)', async () => {
const { service, calls } = createPipelineSkipHarness({
orderDeliveryRows: [
{
id: 201,
sys_platform_code: 'PDD',
logistics_code: 'SF',
sys_platform_name: '拼多多',
logistics_name: '顺丰',
store_id: 100000,
},
],
orderExpressRows: [
{ order_id: 201, business_key: 1, status: 1 },
],
});
await service.createWaybillCodePipeline([
{ order_id: 201, business_key: 2 },
]);
// business_key=2 不同于已完成的 business_key=1,不应跳过
assert.equal(calls.asyncTaskPayloads.length, 1, 'business_key 不同时不应跳过');
});
it('订单有 status=1 但 business_key 不是 1 时,传入 business_key=1 应跳过', async () => {
const { service, calls } = createPipelineSkipHarness({
orderDeliveryRows: [
{
id: 301,
sys_platform_code: 'PDD',
logistics_code: 'SF',
sys_platform_name: '拼多多',
logistics_name: '顺丰',
store_id: 100000,
},
],
orderExpressRows: [
{ order_id: 301, business_key: 2, status: 1 },
],
});
await service.createWaybillCodePipeline([
{ order_id: 301, business_key: 1 },
]);
// skipSet 中有 "301:2",但过滤条件是 businessKey===1 && skipSet.has("301:1")
// 这里 skipSet 中没有 "301:1",所以不应跳过
assert.equal(calls.asyncTaskPayloads.length, 1, 'skipSet 中无匹配的 business_key=1 时不应跳过');
});
it('全部订单均已完成时,应直接返回无需处理', async () => {
const { service, calls } = createPipelineSkipHarness({
orderDeliveryRows: [
{
id: 401,
sys_platform_code: 'PDD',
logistics_code: 'SF',
sys_platform_name: '拼多多',
logistics_name: '顺丰',
store_id: 100000,
},
],
orderExpressRows: [
{ order_id: 401, business_key: 1, status: 1 },
],
});
const result = await service.createWaybillCodePipeline([
{ order_id: 401, business_key: 1 },
]);
assert.equal(calls.asyncTaskPayloads.length, 0, '全部跳过时不应调用 asyncTask');
assert.equal(result.message, '所有订单均已分配运单号,无需处理');
});
it('无 status=1 记录时,所有订单正常进入 pipeline', async () => {
const { service, calls } = createPipelineSkipHarness({
orderDeliveryRows: [
{
id: 501,
sys_platform_code: 'PDD',
logistics_code: 'SF',
sys_platform_name: '拼多多',
logistics_name: '顺丰',
store_id: 100000,
},
],
orderExpressRows: [],
});
await service.createWaybillCodePipeline([
{ order_id: 501, business_key: 1 },
]);
assert.equal(calls.asyncTaskPayloads.length, 1, '无已完成记录时所有订单应正常处理');
});
});
});