81 lines
2.0 KiB
TypeScript
81 lines
2.0 KiB
TypeScript
import { Model, DataTypes, Sequelize } from 'sequelize';
|
|
import { AUDIT_ACTIONS } from '../../common/config/constants.js';
|
|
|
|
export interface AuditLogAttributes {
|
|
id: string;
|
|
userId: string | null;
|
|
action: typeof AUDIT_ACTIONS[keyof typeof AUDIT_ACTIONS];
|
|
entityType: string;
|
|
entityId: string;
|
|
oldData: any | null;
|
|
newData: any | null;
|
|
ipAddress: string | null;
|
|
userAgent: string | null;
|
|
}
|
|
|
|
export interface AuditLogInstance extends Model<AuditLogAttributes>, AuditLogAttributes { }
|
|
|
|
export default (sequelize: Sequelize) => {
|
|
const AuditLog = sequelize.define<AuditLogInstance>('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 as any).associate = (models: any) => {
|
|
AuditLog.belongsTo(models.User, {
|
|
foreignKey: 'userId',
|
|
as: 'user'
|
|
});
|
|
};
|
|
|
|
return AuditLog;
|
|
};
|