WMS_MAIN/test/app/service/systems/waybill.warehouse.mainWaybillCode.in.test.js
liufumingren 0717640eff feat(waybill): waybill.warehouse.list/export 支持 mainWaybillCode 数组(0.3.243)
- systems/waybill.js 新增 resolveMainWaybillOrderIds:主单号单值/数组统一解析为 order_id 集合
  - 数组按 Op.in 精确匹配多个主单号,返回这些订单下的全部运单(主+子)
  - 单值仍写标量 order_id(向后兼容);主单号均不存在时直接返回空结果;空数组/全空值数组不加该筛选条件
- 新增回归测试 test/app/service/systems/waybill.warehouse.mainWaybillCode.in.test.js(13 用例,含 list/export 与仓库隔离)
- 接口文档:where.mainWaybillCode 类型改为 string|string[],并修正原「与 waybillCode 互斥、mainWaybillCode 优先」的错误描述(实际为 AND 叠加);根版本 0.3.242 → 0.3.243
- 设计文档 database/designs/waybill-module-design.md 同步(请求示例、参数表、查询逻辑、筛选条件清单)

注:methods/system.json 与 methods/changes/system.json 中同时包含上一批盘点(stock.check)的未提交文档改动(JSON 无法按 hunk 拆分)。
2026-10-04 18:50:03 +08:00

342 lines
13 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 Sequelize = require('sequelize');
const { Op } = Sequelize;
const WaybillService = require('../../../../app/service/systems/waybill');
const { matchesWhere } = require('../../../support/where_matcher');
const TEST_WAREHOUSE_ID = 100000;
const TEST_STORE_ID = 1000000;
/**
* 运单 fixture(主单号数组查询场景):
* - order 101:主单 SF9001-MAIN + 子单 SF9001-1 / SF9001-2
* - order 102:主单 SF9002-MAIN + 子单 SF9002-1
* - order 103:主单 SF9003-MAIN(无子单)
* - id 7 为其他仓库同主单号运单(验证仓库隔离)
* @return {Array<Object>} 运单行。
*/
function buildExpressRows() {
const base = [
{ id: 1, order_id: 101, waybill_code: 'SF9001-MAIN', business_key: 1, warehouse_id: TEST_WAREHOUSE_ID },
{ id: 2, order_id: 101, waybill_code: 'SF9001-1', business_key: 2, warehouse_id: TEST_WAREHOUSE_ID },
{ id: 3, order_id: 101, waybill_code: 'SF9001-2', business_key: 3, warehouse_id: TEST_WAREHOUSE_ID },
{ id: 4, order_id: 102, waybill_code: 'SF9002-MAIN', business_key: 1, warehouse_id: TEST_WAREHOUSE_ID },
{ id: 5, order_id: 102, waybill_code: 'SF9002-1', business_key: 2, warehouse_id: TEST_WAREHOUSE_ID },
{ id: 6, order_id: 103, waybill_code: 'SF9003-MAIN', business_key: 1, warehouse_id: TEST_WAREHOUSE_ID },
{ id: 7, order_id: 104, waybill_code: 'SF9001-MAIN', business_key: 1, warehouse_id: 999999 },
];
return base.map((item, index) => ({
...item,
store_id: TEST_STORE_ID,
status: 1,
delivery_order_code: `DO-${item.order_id}`,
source_order_codes: `SO-${item.order_id}`,
logistics_code: 'SF',
logistics_name: '顺丰',
logistics_interface_id: 1,
logistics_interface_name: 'SF-IF',
platform_code: 'PDD',
is_back: 'N',
is_intercept: 'N',
is_galaxy_receive: 'N',
weighing_weight: 0,
package_material_id: 0,
package_material_name_snapshot: '',
create_at: `2026-08-0${(index % 9) + 1} 03:20:00`,
}));
}
/**
* 创建 waybill list/export 服务测试环境(mock 模型按真实 where 语义过滤 fixture 行)。
* @return {{service: WaybillService, calls: Object, captured: Object}} 测试上下文。
*/
function createWaybillHarness() {
const expressRows = buildExpressRows();
const calls = {
listFindAndCountAllWhere: null,
listFindAllWheres: [],
exportCountWhere: null,
exportFindAllWheres: [],
};
const ctx = {
app: {
Sequelize,
config: {},
formatDate(value) {
return value === null || value === undefined ? null : String(value);
},
},
user: { store_id: TEST_WAREHOUSE_ID },
logger: { info() {}, warn() {}, error() {} },
service: {},
model: {
OrderExpress: {
async findAndCountAll(query = {}) {
calls.listFindAndCountAllWhere = query.where || null;
const filtered = expressRows.filter(item => matchesWhere(item, query.where || {}));
const offset = Number(query.offset) || 0;
const limit = Number(query.limit) || filtered.length;
return { count: filtered.length, rows: filtered.slice(offset, offset + limit) };
},
async findAll(query = {}) {
const where = query.where || {};
if (where.waybill_code && where.waybill_code[Op.in]) {
calls.listFindAllWheres.push(where);
} else if (where.order_id && where.order_id[Op.in]) {
calls.exportFindAllWheres.push(where);
calls.listFindAllWheres.push(where);
} else {
calls.listFindAllWheres.push(where);
}
return expressRows.filter(item => matchesWhere(item, where));
},
async count(query = {}) {
calls.exportCountWhere = query.where || null;
return expressRows.filter(item => matchesWhere(item, query.where || {})).length;
},
},
ExpressPrintRecord: {
async findAll() {
return [];
},
},
StoreWarehouse: {
async findAll() {
return [];
},
},
},
};
const service = new WaybillService(ctx);
// export 的建表/上传副作用与筛选逻辑无关,打桩到文件生成边界,
// 通过 captured.filePayload 断言筛选后的行集,确保测的是真实 where 构建链路。
const captured = { filePayload: null };
service.buildWaybillExportFile = async payload => {
captured.filePayload = payload;
return {
current_count: payload.totalCount,
total_count: payload.totalCount,
url: 'mock-url',
};
};
return { service, calls, captured };
}
/**
* 提取 list 结果中的运单号集合(排序后)。
* @param {Object} result list 返回结果。
* @return {Array<string>} 排序后的运单号列表。
*/
function resultWaybillCodes(result) {
return (result && result.list ? result.list : []).map(item => String(item.waybill_code)).sort();
}
/**
* 取主单号查询发出的 where(含 waybill_code Op.in 的那次)。
* @param {Array<Object>} wheres 捕获的 where 列表。
* @return {Object|null} 主单号查询 where。
*/
function mainCodeLookupWhere(wheres) {
return wheres.find(where => where.waybill_code && where.waybill_code[Op.in]) || null;
}
/**
* 从 where 中提取 order_id 条件(标量或 Op.in 数组)。
* @param {Object|null} where Sequelize where 条件。
* @return {number|Array<number>|null} order_id 条件。
*/
function orderIdCondition(where) {
if (!where) return null;
const value = where.order_id;
if (value === undefined || value === null) return null;
if (typeof value === 'object' && value[Op.in]) return value[Op.in];
return value;
}
describe('test/app/service/systems/waybill.warehouse.mainWaybillCode.in.test.js', () => {
describe('waybill.warehouse.list where.mainWaybillCode 数组支持', () => {
it('数组两个主单号:返回两个订单下全部运单(主+子),order_id 使用 Op.in', async () => {
const { service, calls } = createWaybillHarness();
const result = await service.list({
where: { mainWaybillCode: [ 'SF9001-MAIN', 'SF9002-MAIN' ] },
pagination: { page: 1, pageSize: 20 },
});
assert.deepEqual(orderIdCondition(calls.listFindAndCountAllWhere), [ 101, 102 ]);
assert.equal(result.pagination.total, 5);
assert.deepEqual(resultWaybillCodes(result), [ 'SF9001-1', 'SF9001-2', 'SF9001-MAIN', 'SF9002-1', 'SF9002-MAIN' ]);
});
it('数组内元素自动 trim 并过滤空值', async () => {
const { service, calls } = createWaybillHarness();
const result = await service.list({
where: { mainWaybillCode: [ ' SF9001-MAIN ', '', null, undefined ] },
pagination: { page: 1, pageSize: 20 },
});
assert.equal(orderIdCondition(calls.listFindAndCountAllWhere), 101);
assert.deepEqual(resultWaybillCodes(result), [ 'SF9001-1', 'SF9001-2', 'SF9001-MAIN' ]);
});
it('存在+不存在混合:仅返回存在主单号对应运单,不抛错', async () => {
const { service, calls } = createWaybillHarness();
const result = await service.list({
where: { mainWaybillCode: [ 'SF9001-MAIN', 'SF-NOT-EXIST' ] },
pagination: { page: 1, pageSize: 20 },
});
assert.equal(orderIdCondition(calls.listFindAndCountAllWhere), 101);
assert.equal(result.pagination.total, 3);
assert.deepEqual(resultWaybillCodes(result), [ 'SF9001-1', 'SF9001-2', 'SF9001-MAIN' ]);
});
it('全部不存在:直接返回空结果,不发起主查询', async () => {
const { service, calls } = createWaybillHarness();
const result = await service.list({
where: { mainWaybillCode: [ 'SF-NOT-EXIST-A', 'SF-NOT-EXIST-B' ] },
pagination: { page: 1, pageSize: 20 },
});
assert.equal(calls.listFindAndCountAllWhere, null, '主单号均不存在时不应发起主查询');
assert.deepEqual(result.list, []);
assert.equal(result.pagination.total, 0);
});
it('重复主单号自动去重,不产生重复 order_id', async () => {
const { service, calls } = createWaybillHarness();
const result = await service.list({
where: { mainWaybillCode: [ 'SF9001-MAIN', 'SF9001-MAIN', 'SF9002-MAIN' ] },
pagination: { page: 1, pageSize: 20 },
});
assert.deepEqual(orderIdCondition(calls.listFindAndCountAllWhere), [ 101, 102 ]);
assert.equal(result.pagination.total, 5);
});
it('单元素数组:与单值等价,order_id 保持标量(向后兼容)', async () => {
const { service, calls } = createWaybillHarness();
await service.list({
where: { mainWaybillCode: [ 'SF9001-MAIN' ] },
pagination: { page: 1, pageSize: 20 },
});
assert.equal(orderIdCondition(calls.listFindAndCountAllWhere), 101);
});
it('字符串单值:行为不变(order_id 标量)', async () => {
const { service, calls } = createWaybillHarness();
const result = await service.list({
where: { mainWaybillCode: 'SF9002-MAIN' },
pagination: { page: 1, pageSize: 20 },
});
assert.equal(orderIdCondition(calls.listFindAndCountAllWhere), 102);
assert.deepEqual(resultWaybillCodes(result), [ 'SF9002-1', 'SF9002-MAIN' ]);
});
it('标准 where 格式 {operator:"in", value:[...]}:经 normalizeWhereParams 后同样支持', async () => {
const { service, calls } = createWaybillHarness();
const result = await service.list({
where: { mainWaybillCode: { operator: 'in', value: [ 'SF9001-MAIN', 'SF9003-MAIN' ] } },
pagination: { page: 1, pageSize: 20 },
});
assert.deepEqual(orderIdCondition(calls.listFindAndCountAllWhere), [ 101, 103 ]);
assert.equal(result.pagination.total, 4);
});
it('空数组 / 全空值数组:不加主单号筛选,返回本仓全部运单', async () => {
const { service, calls } = createWaybillHarness();
const emptyResult = await service.list({
where: { mainWaybillCode: [] },
pagination: { page: 1, pageSize: 20 },
});
assert.equal(orderIdCondition(calls.listFindAndCountAllWhere), null, '空数组不应添加 order_id 条件');
assert.equal(emptyResult.pagination.total, 6, '仓库内全部 6 条');
const blankResult = await service.list({
where: { mainWaybillCode: [ ' ', null ] },
pagination: { page: 1, pageSize: 20 },
});
assert.equal(blankResult.pagination.total, 6);
});
it('仓库隔离:其他仓库同主单号运单不会被带出', async () => {
const { service, calls } = createWaybillHarness();
const result = await service.list({
where: { mainWaybillCode: [ 'SF9001-MAIN' ] },
pagination: { page: 1, pageSize: 20 },
});
const lookup = mainCodeLookupWhere(calls.listFindAllWheres);
assert.equal(lookup.warehouse_id, TEST_WAREHOUSE_ID);
assert.equal(lookup.business_key, 1);
assert.equal(orderIdCondition(calls.listFindAndCountAllWhere), 101, '不含其他仓库 order 104');
assert.equal(result.pagination.total, 3);
});
it('与 waybillCode 数组共存:AND 叠加', async () => {
const { service, calls } = createWaybillHarness();
const result = await service.list({
where: {
mainWaybillCode: [ 'SF9001-MAIN', 'SF9002-MAIN' ],
waybillCode: [ 'SF9002-1' ],
},
pagination: { page: 1, pageSize: 20 },
});
assert.deepEqual(orderIdCondition(calls.listFindAndCountAllWhere), [ 101, 102 ]);
assert.deepEqual(calls.listFindAndCountAllWhere.waybill_code[Op.in], [ 'SF9002-1' ]);
assert.deepEqual(resultWaybillCodes(result), [ 'SF9002-1' ]);
});
});
describe('waybill.warehouse.export where.mainWaybillCode 数组支持(与 list 保持一致)', () => {
it('数组两个主单号:导出两个订单下全部运单', async () => {
const { service, calls, captured } = createWaybillHarness();
await service.export({
where: { mainWaybillCode: [ 'SF9001-MAIN', 'SF9002-MAIN' ] },
});
assert.deepEqual(orderIdCondition(calls.exportCountWhere), [ 101, 102 ]);
assert.equal(captured.filePayload.totalCount, 5);
assert.deepEqual(
captured.filePayload.list.map(item => item.waybill_code).sort(),
[ 'SF9001-1', 'SF9001-2', 'SF9001-MAIN', 'SF9002-1', 'SF9002-MAIN' ]
);
});
it('全部不存在:导出空结果,不发起统计查询', async () => {
const { service, calls, captured } = createWaybillHarness();
const result = await service.export({
where: { mainWaybillCode: [ 'SF-NOT-EXIST' ] },
});
assert.equal(calls.exportCountWhere, null, '主单号不存在时不应发起统计查询');
assert.equal(result.total_count, 0);
assert.equal(captured.filePayload, null);
});
});
});