118 lines
2.7 KiB
JavaScript
118 lines
2.7 KiB
JavaScript
const { DataTypes } = require('sequelize');
|
|
|
|
module.exports = (sequelize) => {
|
|
const AuditLog = sequelize.define('AuditLog', {
|
|
id: {
|
|
type: DataTypes.INTEGER,
|
|
primaryKey: true,
|
|
autoIncrement: true
|
|
},
|
|
timestamp: {
|
|
type: DataTypes.DATE,
|
|
allowNull: false,
|
|
defaultValue: DataTypes.NOW
|
|
},
|
|
level: {
|
|
type: DataTypes.ENUM('INFO', 'WARNING', 'ERROR', 'CRITICAL'),
|
|
allowNull: false
|
|
},
|
|
action: {
|
|
type: DataTypes.STRING(100),
|
|
allowNull: false,
|
|
comment: 'The action performed (e.g., logo_upload, logo_removal)'
|
|
},
|
|
message: {
|
|
type: DataTypes.TEXT,
|
|
allowNull: false,
|
|
comment: 'Human-readable description of the event'
|
|
},
|
|
user_id: {
|
|
type: DataTypes.INTEGER,
|
|
allowNull: true,
|
|
comment: 'ID of the user who performed the action'
|
|
},
|
|
username: {
|
|
type: DataTypes.STRING(255),
|
|
allowNull: true,
|
|
comment: 'Username of the user who performed the action'
|
|
},
|
|
tenant_id: {
|
|
type: DataTypes.INTEGER,
|
|
allowNull: true,
|
|
comment: 'ID of the tenant affected by the action'
|
|
},
|
|
tenant_slug: {
|
|
type: DataTypes.STRING(255),
|
|
allowNull: true,
|
|
comment: 'Slug of the tenant affected by the action'
|
|
},
|
|
ip_address: {
|
|
type: DataTypes.STRING(45),
|
|
allowNull: true,
|
|
comment: 'IP address of the user (supports IPv6)'
|
|
},
|
|
user_agent: {
|
|
type: DataTypes.TEXT,
|
|
allowNull: true,
|
|
comment: 'User agent string from the request'
|
|
},
|
|
path: {
|
|
type: DataTypes.STRING(500),
|
|
allowNull: true,
|
|
comment: 'Request path that triggered the action'
|
|
},
|
|
metadata: {
|
|
type: DataTypes.JSON,
|
|
allowNull: true,
|
|
comment: 'Additional metadata about the event'
|
|
},
|
|
success: {
|
|
type: DataTypes.BOOLEAN,
|
|
allowNull: true,
|
|
comment: 'Whether the action was successful'
|
|
}
|
|
}, {
|
|
tableName: 'audit_logs',
|
|
timestamps: false, // We use our own timestamp field
|
|
indexes: [
|
|
{
|
|
fields: ['timestamp']
|
|
},
|
|
{
|
|
fields: ['action']
|
|
},
|
|
{
|
|
fields: ['user_id']
|
|
},
|
|
{
|
|
fields: ['tenant_id']
|
|
},
|
|
{
|
|
fields: ['level']
|
|
},
|
|
{
|
|
fields: ['timestamp', 'action']
|
|
},
|
|
{
|
|
fields: ['tenant_id', 'timestamp']
|
|
}
|
|
]
|
|
});
|
|
|
|
// Define associations
|
|
AuditLog.associate = function(models) {
|
|
// Association with User
|
|
AuditLog.belongsTo(models.User, {
|
|
foreignKey: 'user_id',
|
|
as: 'user'
|
|
});
|
|
|
|
// Association with Tenant
|
|
AuditLog.belongsTo(models.Tenant, {
|
|
foreignKey: 'tenant_id',
|
|
as: 'tenant'
|
|
});
|
|
};
|
|
|
|
return AuditLog;
|
|
}; |