'use strict'; /** * 用户协议同意记录(user_agreements) * * 一行 = 某个用户同意过某个协议的某个版本(同一版本重复登录只更新时间,不重复插入)。 * 版本号同时冗余一列 `version`,方便协议版本被改名/归档后仍能审计当时同意的是哪一版。 */ module.exports = (app) => { const { BIGINT, STRING, DATE } = app.Sequelize; const UserAgreement = app.model.define('user_agreement', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, userId: { type: BIGINT.UNSIGNED, allowNull: false, comment: 'users.id' }, agreementId: { type: BIGINT.UNSIGNED, allowNull: false, comment: 'agreements.id' }, versionId: { type: BIGINT.UNSIGNED, allowNull: false, comment: 'agreement_versions.id' }, version: { type: STRING(32), allowNull: false, comment: '同意时的版本号快照' }, source: { type: STRING(32), allowNull: true, comment: '来源:mini_program / web / h5' }, ip: { type: STRING(64), allowNull: true, comment: '同意时的客户端 IP' }, userAgent: { type: STRING(255), allowNull: true, comment: '同意时的 User-Agent' }, agreedAt: { type: DATE, allowNull: false, comment: '同意时间' }, }, { tableName: 'user_agreements', paranoid: true, comment: '用户协议同意记录表', }); UserAgreement.associate = function() { app.model.UserAgreement.belongsTo(app.model.User, { foreignKey: 'user_id', as: 'user' }); app.model.UserAgreement.belongsTo(app.model.Agreement, { foreignKey: 'agreement_id', as: 'agreement' }); app.model.UserAgreement.belongsTo(app.model.AgreementVersion, { foreignKey: 'version_id', as: 'versionInfo' }); }; return UserAgreement; };