- storeWarehouse.store.saveApiConfig:新增 resolveContractApiInterfaceType, interface_type_id=3(OPEN_API_WAREHOUSE 仓库开放接口)自动映射为 2(OPEN_API 货主开放接口)落库, 不再要求当前主体为仓库(货主可维护自身签约关系下的接口配置) - callback_url 不再取入参(含 callbackUrl 别名),统一写入 config.openapi.defaultCallbackUrl(新增配置项,默认空) - storeWarehouse.store.listApiConfig:interface_type_id 过滤同步映射(传 3 按 2 查询) - 文档:methods/system.json 升级 0.3.350、methods/changes/system.json 变更记录、docs/openapi-callback-events.md 回调地址来源说明 - 测试:saveApiConfig 新增 4 条用例(3→2 落库、忽略入参回调地址、默认值未配置、货主主体可保存、缺失类型 404),listApiConfig 新增 1 条过滤映射用例 同时提交工作区其余在制品改动: - waybill:移除 wms_order_express_trace 模型并新增 drop 迁移,轨迹查询/取号/打印链路调整 - order:订单 SN 编码(order_item_sn / order_item_sn_log / sn_code_rule、has_sn_item 回填迁移),pendingOrder/recheckOrder/wave/base 调整 - express_platform、order_async、order_export、order_storage_allocation、storage.report、stock_store 配套修复 - lib:新增 jd_client,调整 qimen_client、callback_functions 及配套测试 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
161 lines
4.7 KiB
JavaScript
161 lines
4.7 KiB
JavaScript
'use strict';
|
|
|
|
const Sequelize = require('sequelize');
|
|
|
|
function createServiceContext(storeId = 100000) {
|
|
return {
|
|
app: {
|
|
Sequelize,
|
|
config: {},
|
|
},
|
|
user: {
|
|
store_id: storeId,
|
|
},
|
|
};
|
|
}
|
|
|
|
function buildRows(count, rowFactory = () => ({})) {
|
|
return Array.from({ length: count }, (_, index) => ({
|
|
id: index + 1,
|
|
warehouse_id: 100000,
|
|
step: index % 2 === 0 ? 1 : 2,
|
|
dispatch_lane: 'm4',
|
|
inspection_status: 'fin',
|
|
updated_at: '2026-04-12 00:00:00',
|
|
...rowFactory(index),
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* 判断模拟行数据是否匹配 where 条件。
|
|
* @param {Object|Array|number|string|null} row 待匹配数据。
|
|
* @param {Object} where Sequelize where 条件。
|
|
* @return {boolean} 是否匹配。
|
|
*/
|
|
function matchesWhere(row, where) {
|
|
if (!where || typeof where !== 'object') {
|
|
return true;
|
|
}
|
|
|
|
const andConditions = where[Sequelize.Op.and];
|
|
if (Array.isArray(andConditions)) {
|
|
return andConditions.every(condition => matchesWhere(row, condition));
|
|
}
|
|
|
|
const orConditions = where[Sequelize.Op.or];
|
|
if (Array.isArray(orConditions)) {
|
|
return orConditions.some(condition => matchesWhere(row, condition));
|
|
}
|
|
|
|
return Object.entries(where).every(([ fieldName, condition ]) => {
|
|
if (condition && typeof condition === 'object' && !Array.isArray(condition)) {
|
|
if (Object.prototype.hasOwnProperty.call(condition, Sequelize.Op.eq)) {
|
|
return row[fieldName] === condition[Sequelize.Op.eq];
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(condition, Sequelize.Op.ne)) {
|
|
return row[fieldName] !== condition[Sequelize.Op.ne];
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(condition, Sequelize.Op.is)) {
|
|
const expected = condition[Sequelize.Op.is];
|
|
const actual = row[fieldName];
|
|
if (expected === null) {
|
|
return actual === null || actual === undefined;
|
|
}
|
|
return actual === expected;
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(condition, Sequelize.Op.in)) {
|
|
const values = condition[Sequelize.Op.in];
|
|
return Array.isArray(values) && values.includes(row[fieldName]);
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(condition, Sequelize.Op.notIn)) {
|
|
const values = condition[Sequelize.Op.notIn];
|
|
return Array.isArray(values) && !values.includes(row[fieldName]);
|
|
}
|
|
|
|
if (Object.keys(condition).length > 0 || Object.getOwnPropertySymbols(condition).length > 0) {
|
|
return matchesWhere(row[fieldName], condition);
|
|
}
|
|
}
|
|
|
|
return row[fieldName] === condition;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 创建分页查询用的模拟模型。
|
|
* @param {Array<Object>} rows 初始行数据。
|
|
* @param {Object} capture 调用记录。
|
|
* @return {Object} 模拟模型。
|
|
*/
|
|
function createPagedMockModel(rows, capture = {}) {
|
|
const normalizedRows = Array.isArray(rows) ? rows.map(row => ({ ...row })) : [];
|
|
|
|
return {
|
|
async count({ where } = {}) {
|
|
capture.countWhere = where;
|
|
return normalizedRows.filter(row => matchesWhere(row, where)).length;
|
|
},
|
|
async findAll(options = {}) {
|
|
capture.findOptions = options;
|
|
|
|
const filteredRows = normalizedRows.filter(row => matchesWhere(row, options.where));
|
|
const offset = Number.parseInt(options.offset, 10) || 0;
|
|
const limitValue = options.limit === undefined ? filteredRows.length : Number.parseInt(options.limit, 10);
|
|
const limit = Number.isFinite(limitValue) ? Math.max(limitValue, 0) : filteredRows.length;
|
|
|
|
return filteredRows.slice(offset, offset + limit).map(row => ({ ...row }));
|
|
},
|
|
async update() {
|
|
return [ 1 ];
|
|
},
|
|
async findOne() {
|
|
const firstRow = normalizedRows[0];
|
|
return firstRow ? { ...firstRow } : null;
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 创建订单服务测试环境。
|
|
* @param {Function} ServiceClass 服务类构造器。
|
|
* @param {Object} [options] 测试配置。
|
|
* @return {{ ctx: Object, service: Object, captures: Array<Object> }} 测试上下文。
|
|
*/
|
|
function createOrderHarness(ServiceClass, options = {}) {
|
|
const ctx = createServiceContext(options.storeId || 100000);
|
|
const service = new ServiceClass(ctx);
|
|
const captures = [];
|
|
const rowsByModule = options.rowsByModule || {};
|
|
const defaultRows = Array.isArray(options.defaultRows) ? options.defaultRows : buildRows(1);
|
|
|
|
service.getOrderModel = async moduleName => {
|
|
const capture = {
|
|
moduleName,
|
|
};
|
|
captures.push(capture);
|
|
|
|
const rows = Object.prototype.hasOwnProperty.call(rowsByModule, moduleName)
|
|
? rowsByModule[moduleName]
|
|
: defaultRows;
|
|
|
|
return createPagedMockModel(rows, capture);
|
|
};
|
|
|
|
return {
|
|
ctx,
|
|
service,
|
|
captures,
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
buildRows,
|
|
createOrderHarness,
|
|
createServiceContext,
|
|
matchesWhere,
|
|
};
|