'use strict'; module.exports = app => { const { BIGINT, STRING, TINYINT, DATE, JSON } = app.Sequelize; const NotificationWebhook = app.model.define('notification_webhook', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, store_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '主体ID,对应 wms_store.id', references: { model: 'store', key: 'id', }, }, hook_name: { type: STRING(100), allowNull: true, comment: 'Webhook 名称', }, hook_type: { type: STRING(50), allowNull: false, validate: { notEmpty: { msg: 'hook_type 不能为空', }, len: { args: [ 1, 50 ], msg: 'hook_type 长度应在1-50个字符之间', }, }, comment: 'Webhook 类型,用于按类型分派通知结构处理逻辑', }, hook_url: { type: STRING(500), allowNull: false, validate: { notEmpty: { msg: 'hook_url 不能为空', }, len: { args: [ 1, 500 ], msg: 'hook_url 长度应在1-500个字符之间', }, }, comment: 'Webhook 链接地址', }, events: { type: JSON, allowNull: true, comment: '订阅事件列表', }, extra_config: { type: JSON, allowNull: true, comment: '扩展配置,如签名、请求头、模板映射等', }, status: { type: TINYINT(4), allowNull: false, defaultValue: 1, validate: { isIn: { args: [[ 0, 1 ]], msg: '状态值不合法,0-禁用,1-启用', }, }, comment: '状态:0-禁用,1-启用', }, remark: { type: STRING(255), allowNull: true, comment: '备注', }, created_by: { type: BIGINT.UNSIGNED, allowNull: true, comment: '创建人', }, created_at: { type: DATE, allowNull: false, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: DATE, allowNull: false, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), comment: '更新时间', }, deleted_at: { type: DATE, allowNull: true, comment: '删除时间', }, }, { tableName: 'wms_notification_webhook', freezeTableName: true, underscored: true, timestamps: true, createdAt: 'created_at', updatedAt: 'updated_at', paranoid: true, deletedAt: 'deleted_at', comment: '外部通知 WebHook 配置表', indexes: [ { name: 'idx_notification_webhook_store_id', fields: [ 'store_id' ], }, { name: 'idx_notification_webhook_hook_type', fields: [ 'hook_type' ], }, { name: 'idx_notification_webhook_status', fields: [ 'status' ], }, { name: 'idx_notification_webhook_store_type', fields: [ 'store_id', 'hook_type' ], }, { name: 'uk_notification_webhook_store_type_url', unique: true, fields: [ 'store_id', 'hook_type', 'hook_url' ], }, ], }); NotificationWebhook.associate = function() { const { Store, Account } = app.model; NotificationWebhook.belongsTo(Store, { foreignKey: 'store_id', targetKey: 'id', as: 'store', }); NotificationWebhook.belongsTo(Account, { foreignKey: 'created_by', targetKey: 'id', as: 'creator', constraints: false, }); }; NotificationWebhook.prototype.isEnabled = function() { return Number(this.status) === 1; }; return NotificationWebhook; };