commit
32a36cceff
444 files changed
+67186
No files matched your search
@@ -0,0 +1,127 @@
|
||||
const { query } = require('../database/db');
|
||||
|
||||
class AuditLog {
|
||||
/**
|
||||
* Create a new audit log entry
|
||||
* @param {string} tableName - Name of the table that was modified
|
||||
* @param {string} recordId - ID of the record that was modified
|
||||
* @param {string} action - Type of action: CREATE, UPDATE, DELETE
|
||||
* @param {Object} oldValues - Previous values (null for CREATE)
|
||||
* @param {Object} newValues - New values (null for DELETE)
|
||||
* @param {string[]} changedFields - Array of field names that changed
|
||||
* @returns {Promise<Object>} Created audit log entry
|
||||
*/
|
||||
static async create(tableName, recordId, action, oldValues, newValues, changedFields) {
|
||||
const result = await query(
|
||||
`INSERT INTO audit_logs (table_name, record_id, action, old_values, new_values, changed_fields)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING *`,
|
||||
[
|
||||
tableName,
|
||||
recordId.toString(),
|
||||
action,
|
||||
oldValues ? JSON.stringify(oldValues) : null,
|
||||
newValues ? JSON.stringify(newValues) : null,
|
||||
changedFields || []
|
||||
]
|
||||
);
|
||||
return result.rows[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get audit logs for a specific table
|
||||
* @param {string} tableName - Name of the table
|
||||
* @param {number} limit - Maximum number of logs to return (default: 50)
|
||||
* @param {number} offset - Number of logs to skip (for pagination)
|
||||
* @returns {Promise<Array>} Array of audit log entries
|
||||
*/
|
||||
static async getByTable(tableName, limit = 50, offset = 0) {
|
||||
const result = await query(
|
||||
`SELECT * FROM audit_logs
|
||||
WHERE table_name = $1
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $2 OFFSET $3`,
|
||||
[tableName, limit, offset]
|
||||
);
|
||||
return result.rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get audit logs for a specific record
|
||||
* @param {string} tableName - Name of the table
|
||||
* @param {string} recordId - ID of the record
|
||||
* @returns {Promise<Array>} Array of audit log entries for this record
|
||||
*/
|
||||
static async getByRecord(tableName, recordId) {
|
||||
const result = await query(
|
||||
`SELECT * FROM audit_logs
|
||||
WHERE table_name = $1 AND record_id = $2
|
||||
ORDER BY created_at DESC`,
|
||||
[tableName, recordId.toString()]
|
||||
);
|
||||
return result.rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get recent audit logs across all tables
|
||||
* @param {number} limit - Maximum number of logs to return
|
||||
* @returns {Promise<Array>} Array of recent audit log entries
|
||||
*/
|
||||
static async getRecent(limit = 50) {
|
||||
const result = await query(
|
||||
`SELECT * FROM audit_logs
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $1`,
|
||||
[limit]
|
||||
);
|
||||
return result.rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get audit logs by date range
|
||||
* @param {string} tableName - Name of the table (optional)
|
||||
* @param {string} startDate - Start date (YYYY-MM-DD)
|
||||
* @param {string} endDate - End date (YYYY-MM-DD)
|
||||
* @param {number} limit - Maximum number of logs to return
|
||||
* @returns {Promise<Array>} Array of audit log entries
|
||||
*/
|
||||
static async getByDateRange(tableName, startDate, endDate, limit = 50) {
|
||||
let queryStr;
|
||||
let params;
|
||||
|
||||
if (tableName) {
|
||||
queryStr = `SELECT * FROM audit_logs
|
||||
WHERE table_name = $1
|
||||
AND created_at >= $2
|
||||
AND created_at <= $3
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $4`;
|
||||
params = [tableName, startDate, endDate, limit];
|
||||
} else {
|
||||
queryStr = `SELECT * FROM audit_logs
|
||||
WHERE created_at >= $1
|
||||
AND created_at <= $2
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $3`;
|
||||
params = [startDate, endDate, limit];
|
||||
}
|
||||
|
||||
const result = await query(queryStr, params);
|
||||
return result.rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get count of audit logs for a table
|
||||
* @param {string} tableName - Name of the table
|
||||
* @returns {Promise<number>} Count of audit logs
|
||||
*/
|
||||
static async getCount(tableName) {
|
||||
const result = await query(
|
||||
`SELECT COUNT(*) as count FROM audit_logs WHERE table_name = $1`,
|
||||
[tableName]
|
||||
);
|
||||
return parseInt(result.rows[0].count, 10);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AuditLog;
|
||||
Reference in new issue
Block a user