WMS_MAIN/test/app/service/systems/store_warehouse.saveApiConfig.test.js
liufumingren 9af7dcf8fb feat(storeWarehouse): 签约关系接口配置按货主开放接口落库且回调地址统一取配置
- 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>
2026-11-14 17:14:04 +08:00

368 lines
11 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const Sequelize = require('sequelize');
const StoreWarehouseService = require('../../../../app/service/systems/store_warehouse');
/**
* 构造 saveApiConfig 测试上下文。
*
* @param {Object} [options={}] 测试选项。
* @return {{service: StoreWarehouseService, capture: object, transaction: object}} 服务实例与埋点数据。
*/
function createSaveApiConfigHarness(options = {}) {
const interfaceType = {
id: Number(options.interfaceTypeId || 7),
interface_code: options.interfaceCode || 'QM_WMS',
interface_category: options.interfaceCategory || 'QM_WMS',
};
const effectiveInterfaceType = options.interfaceCode === 'OPEN_API_WAREHOUSE' && options.storeInterfaceType
? options.storeInterfaceType
: interfaceType;
const relation = {
id: Number(options.relationId || 88),
warehouse_id: Number(options.warehouseId || 100000),
};
const capture = {
qimenAppFindOneArgs: [],
apiAppFindOneArgs: [],
interfaceTypeFindOneArgs: [],
apiAppCreatePayload: null,
apiAppUpdatePayload: null,
operationLogPayload: null,
clearedConfigs: null,
};
const transaction = {
committed: false,
rolledBack: false,
async commit() {
this.committed = true;
},
async rollback() {
this.rolledBack = true;
},
};
let persistedConfig = options.persistedConfig || null;
let existingConfig = null;
if (options.existingConfig) {
persistedConfig = {
...options.existingConfig,
interface_type: {
id: effectiveInterfaceType.id,
interface_category: effectiveInterfaceType.interface_category,
},
};
existingConfig = {
...options.existingConfig,
async update(payload) {
capture.apiAppUpdatePayload = payload;
persistedConfig = {
...persistedConfig,
...payload,
interface_type: {
id: effectiveInterfaceType.id,
interface_category: effectiveInterfaceType.interface_category,
},
};
return this;
},
};
}
const ctx = {
app: {
config: options.config || {},
Sequelize,
},
user: {
id: 10001,
employees_id: 10001,
employees_name: '测试用户',
},
model: {
transaction: async () => transaction,
QimenApp: {
async findOne(args = {}) {
capture.qimenAppFindOneArgs.push(args);
return options.qimenAppRecord || null;
},
},
ApiApp: {
async findOne(args = {}) {
capture.apiAppFindOneArgs.push(args);
if (Object.prototype.hasOwnProperty.call(args.where || {}, 'contract_id')) {
return existingConfig;
}
return options.duplicateConfig || null;
},
async create(payload) {
capture.apiAppCreatePayload = payload;
persistedConfig = {
id: Number(options.createdConfigId || 321),
...payload,
interface_type: {
id: effectiveInterfaceType.id,
interface_category: effectiveInterfaceType.interface_category,
},
};
return {
id: persistedConfig.id,
};
},
async findByPk(id) {
return {
id,
...persistedConfig,
};
},
},
InterfaceType: {
async findOne(args = {}) {
capture.interfaceTypeFindOneArgs.push(args);
return options.storeInterfaceType || null;
},
},
StoreWarehouse: {
async findByPk(id) {
return {
id,
warehouse_id: relation.warehouse_id,
};
},
},
},
service: {
systems: {
operationLog: {
async createOperationLog(payload) {
capture.operationLogPayload = payload;
},
},
},
},
logger: {
info() {},
warn() {},
error() {},
},
};
ctx.app.model = ctx.model;
const service = new StoreWarehouseService(ctx);
service.getCurrentSubject = async () => options.currentSubject || {
id: relation.warehouse_id,
isWarehouse: true,
project: 'warehouse',
};
service.findScopedRelation = async () => relation;
service.getApiInterfaceType = async () => interfaceType;
service.getRelationIncludes = () => [];
service.serializeRelation = result => ({
id: Number(result.id || relation.id),
warehouse_id: Number(result.warehouse_id || relation.warehouse_id),
});
service.clearQimenContractCache = async configs => {
capture.clearedConfigs = configs;
};
return {
service,
capture,
transaction,
};
}
describe('test/app/service/systems/store_warehouse.saveApiConfig.test.js', () => {
it('should derive qimen_customer_id from QimenApp when request omits qimen_customer_id', async () => {
const { service, capture, transaction } = createSaveApiConfigHarness({
qimenAppRecord: {
id: 11,
app_key: 'qm-app-key-create',
customer_id: 'db_customer_create',
},
});
const result = await service.saveApiConfig({
id: 88,
interface_type_id: 7,
app_name: '奇门配置创建',
qimen_app_key: 'qm-app-key-create',
qimen_warehouse_code: 'QM_WH_CREATE',
contact_person: '测试联系人',
contact_phone: '13800138000',
email: 'create@example.com',
is_enabled: 1,
});
assert.equal(capture.qimenAppFindOneArgs.length, 1);
assert.deepEqual(capture.qimenAppFindOneArgs[0].where, {
app_key: 'qm-app-key-create',
});
assert.equal(String(capture.apiAppCreatePayload.qimen_customer_id || ''), 'db_customer_create');
assert.equal(String(result.data.api_config.qimen_customer_id || ''), 'db_customer_create');
assert.equal(transaction.committed, true);
assert.equal(transaction.rolledBack, false);
});
it('should use database customer_id instead of external qimen_customer_id on update', async () => {
const { service, capture, transaction } = createSaveApiConfigHarness({
qimenAppRecord: {
id: 12,
app_key: 'qm-app-key-update',
customer_id: 'db_customer_update',
},
existingConfig: {
id: 322,
app_name: '旧配置',
interface_type_id: 7,
contract_id: 88,
warehouse_id: 100000,
qimen_app_key: 'qm-app-key-update',
qimen_customer_id: 'legacy_customer',
qimen_warehouse_code: 'QM_WH_OLD',
api_version: '1.0',
sign_method: 'md5',
rate_limit: 1000,
timeout_seconds: 30,
is_enabled: 1,
},
});
const result = await service.saveApiConfig({
id: 88,
interface_type_id: 7,
app_name: '奇门配置更新',
qimen_app_key: 'qm-app-key-update',
qimen_customer_id: 'external_customer_should_be_ignored',
qimen_warehouse_code: 'QM_WH_NEW',
is_enabled: 0,
});
assert.equal(String(capture.apiAppUpdatePayload.qimen_customer_id || ''), 'db_customer_update');
assert.notEqual(String(capture.apiAppUpdatePayload.qimen_customer_id || ''), 'external_customer_should_be_ignored');
assert.equal(String(result.data.api_config.qimen_customer_id || ''), 'db_customer_update');
assert.equal(transaction.committed, true);
assert.equal(transaction.rolledBack, false);
});
it('should fail when qimen_app_key has no matching qimen app record', async () => {
const { service, capture, transaction } = createSaveApiConfigHarness({
qimenAppRecord: null,
});
await assert.rejects(async () => {
await service.saveApiConfig({
id: 88,
interface_type_id: 7,
app_name: '奇门配置失败',
qimen_app_key: 'qm-app-key-missing',
qimen_warehouse_code: 'QM_WH_MISSING',
is_enabled: 1,
});
}, error => {
assert.equal(Number(error.code), 400);
assert.match(String(error.message || ''), /qimen_app_key|奇门应用配置/);
return true;
});
assert.equal(capture.apiAppCreatePayload, null);
assert.equal(transaction.committed, false);
assert.equal(transaction.rolledBack, true);
});
it('should store warehouse open api as store open api and use default callback url', async () => {
const { service, capture, transaction } = createSaveApiConfigHarness({
interfaceTypeId: 3,
interfaceCode: 'OPEN_API_WAREHOUSE',
interfaceCategory: 'OPEN_API',
storeInterfaceType: {
id: 2,
interface_code: 'OPEN_API',
interface_name: '货主开放接口',
interface_category: 'OPEN_API',
},
config: {
openapi: {
defaultCallbackUrl: ' https://callback.example.com/openapi ',
},
},
});
const result = await service.saveApiConfig({
id: 88,
interface_type_id: 3,
app_name: '货主接口配置',
callback_url: 'https://ignored.example.com/hook',
});
assert.equal(capture.interfaceTypeFindOneArgs.length, 1);
assert.equal(capture.interfaceTypeFindOneArgs[0].where.interface_code, 'OPEN_API');
assert.equal(capture.apiAppCreatePayload.interface_type_id, 2);
assert.equal(capture.apiAppCreatePayload.callback_url, 'https://callback.example.com/openapi');
assert.equal(result.data.api_config.interface_type_id, 2);
assert.equal(transaction.committed, true);
});
it('should not write callback_url when default callback url is not configured', async () => {
const { service, capture } = createSaveApiConfigHarness({
interfaceTypeId: 2,
interfaceCode: 'OPEN_API',
interfaceCategory: 'OPEN_API',
});
await service.saveApiConfig({
id: 88,
interface_type_id: 2,
app_name: '无默认回调地址',
callback_url: 'https://ignored.example.com/hook',
});
assert.equal(capture.apiAppCreatePayload.callback_url, null);
});
it('should allow store subject to save contract api config', async () => {
const { service, capture, transaction } = createSaveApiConfigHarness({
interfaceTypeId: 2,
interfaceCode: 'OPEN_API',
interfaceCategory: 'OPEN_API',
currentSubject: { id: 1000000, isWarehouse: false, project: 'store' },
});
await service.saveApiConfig({
id: 88,
interface_type_id: 2,
app_name: '货主自建开放接口',
});
assert.equal(Number(capture.apiAppCreatePayload.interface_type_id), 2);
assert.equal(transaction.committed, true);
assert.equal(transaction.rolledBack, false);
});
it('should fail when store open api interface type is missing', async () => {
const { service, capture, transaction } = createSaveApiConfigHarness({
interfaceTypeId: 3,
interfaceCode: 'OPEN_API_WAREHOUSE',
interfaceCategory: 'OPEN_API',
});
await assert.rejects(async () => {
await service.saveApiConfig({
id: 88,
interface_type_id: 3,
app_name: '缺失货主开放接口类型',
});
}, error => {
assert.equal(Number(error.code), 404);
assert.match(String(error.message || ''), /货主开放接口类型/);
return true;
});
assert.equal(capture.apiAppCreatePayload, null);
assert.equal(transaction.committed, false);
assert.equal(transaction.rolledBack, true);
});
});