WMS_MAIN/database/migrations/20260929100000-add-callback-functions-to-wms-api-app.js

56 lines
2.1 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';
/**
* wms_api_app 新增 callback_functions 字段(出站回调 method 路由)。
*
* 语义:应用承接的出站回调 method 列表(JSON 数组)。
* 为空/未勾选 = 不承接任何回调;出站回调按 warehouse_id + method 自动路由到
* 勾选了该 method 的应用,多个应用按 id 升序逐个尝试,全部失败不改配置。
*
* A 方案存量回填:仓库接口类型(OPEN_API_WAREHOUSE)下启用的应用
* 默认承接 notify.reply,升级后站内信推送不受影响。
*/
module.exports = {
async up(queryInterface, Sequelize) {
const { sequelize } = queryInterface;
// 幂等:列已存在则跳过
const [ columns ] = await sequelize.query(
"SHOW COLUMNS FROM `wms_api_app` LIKE 'callback_functions'"
);
if (columns.length === 0) {
await queryInterface.addColumn('wms_api_app', 'callback_functions', {
type: Sequelize.JSON,
allowNull: true,
comment: '承接的出站回调 method 列表(空/未勾选=不承接任何回调;按 method 自动路由)',
});
}
// 存量回填:按 interface_code 查仓库接口类型 ID(避免硬编码数字漂移)
const [ interfaceRows ] = await sequelize.query(
"SELECT id FROM wms_interface_type WHERE interface_code = 'OPEN_API_WAREHOUSE' LIMIT 1"
);
const interfaceTypeId = interfaceRows && interfaceRows.length
? Number(interfaceRows[0].id)
: 3;
await sequelize.query(
'UPDATE wms_api_app SET callback_functions = JSON_ARRAY(\'notify.reply\') ' +
'WHERE interface_type_id = :interfaceTypeId AND is_enabled = 1 ' +
'AND (callback_functions IS NULL OR JSON_LENGTH(callback_functions) = 0)',
{ replacements: { interfaceTypeId } }
);
},
async down(queryInterface) {
const { sequelize } = queryInterface;
const [ columns ] = await sequelize.query(
"SHOW COLUMNS FROM `wms_api_app` LIKE 'callback_functions'"
);
if (columns.length > 0) {
await queryInterface.removeColumn('wms_api_app', 'callback_functions');
}
},
};