implementar visualizacion de logs y seteo de usuario administrador
This commit is contained in:
@@ -30,15 +30,39 @@ const saveEvent = async (event) => {
|
||||
}
|
||||
};
|
||||
|
||||
const getEvents = async ({ type, userId, limit = 100, offset = 0 } = {}) => {
|
||||
const getEvents = async ({ type, userId, limit = 100, offset = 0, types = [] } = {}) => {
|
||||
let sql = 'SELECT * FROM audit_logs WHERE 1=1';
|
||||
let countSql = 'SELECT COUNT(*) AS total FROM audit_logs WHERE 1=1';
|
||||
const params = [];
|
||||
const countParams = [];
|
||||
if (type) { sql += ' AND type = ?'; params.push(type); }
|
||||
if (userId) { sql += ' AND user_id = ?'; params.push(userId); }
|
||||
if (type) { countSql += ' AND type = ?'; countParams.push(type); }
|
||||
if (types.length > 0) {
|
||||
const placeholders = types.map(() => '?').join(', ');
|
||||
sql += ` AND type IN (${placeholders})`;
|
||||
countSql += ` AND type IN (${placeholders})`;
|
||||
params.push(...types);
|
||||
countParams.push(...types);
|
||||
}
|
||||
if (userId) {
|
||||
sql += ' AND user_id = ?';
|
||||
countSql += ' AND user_id = ?';
|
||||
params.push(userId);
|
||||
countParams.push(userId);
|
||||
}
|
||||
sql += ' ORDER BY created_at DESC LIMIT ? OFFSET ?';
|
||||
params.push(limit, offset);
|
||||
const [rows] = await db.execute(sql, params);
|
||||
return rows;
|
||||
const [countRows] = await db.execute(countSql, countParams);
|
||||
return {
|
||||
rows,
|
||||
total: countRows[0]?.total || 0,
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = { saveEvent, getEvents };
|
||||
const deleteEventsByUserId = async (userId) => {
|
||||
const [result] = await db.execute('DELETE FROM audit_logs WHERE user_id = ?', [userId]);
|
||||
return result.affectedRows || 0;
|
||||
};
|
||||
|
||||
module.exports = { saveEvent, getEvents, deleteEventsByUserId };
|
||||
|
||||
Reference in New Issue
Block a user