const { AUDIT_ACTIONS } = require('../config/constants'); module.exports = (sequelize, DataTypes) => { const AuditLog = sequelize.define('AuditLog', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true }, userId: { type: DataTypes.UUID, allowNull: true, references: { model: 'users', key: 'id' } }, action: { type: DataTypes.ENUM(Object.values(AUDIT_ACTIONS)), allowNull: false }, entityType: { type: DataTypes.STRING, allowNull: false }, entityId: { type: DataTypes.UUID, allowNull: false }, oldData: { type: DataTypes.JSON, allowNull: true }, newData: { type: DataTypes.JSON, allowNull: true }, ipAddress: { type: DataTypes.STRING, allowNull: true }, userAgent: { type: DataTypes.STRING, allowNull: true } }, { tableName: 'audit_logs', timestamps: true, indexes: [ { fields: ['userId'] }, { fields: ['action'] }, { fields: ['entityType'] }, { fields: ['entityId'] } ] }); AuditLog.associate = (models) => { AuditLog.belongsTo(models.User, { foreignKey: 'userId', as: 'user' }); }; return AuditLog; };