Initial LinkedSign platform
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
const auditModel = {
|
||||
id: 'number',
|
||||
type: 'string',
|
||||
userId: 'number|null',
|
||||
email: 'string|null',
|
||||
ip: 'string',
|
||||
userAgent: 'string',
|
||||
timestamp: 'Date',
|
||||
};
|
||||
|
||||
module.exports = auditModel;
|
||||
@@ -0,0 +1,44 @@
|
||||
const db = require('../../config/db');
|
||||
const fs = require('fs/promises');
|
||||
const path = require('path');
|
||||
|
||||
const saveEvent = async (event) => {
|
||||
try {
|
||||
await db.execute(
|
||||
'INSERT INTO audit_logs (type, user_id, email, ip, user_agent, metadata) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
[
|
||||
event.type,
|
||||
event.userId || null,
|
||||
event.email || null,
|
||||
event.ip || null,
|
||||
event.userAgent || null,
|
||||
event.metadata ? JSON.stringify(event.metadata) : null,
|
||||
]
|
||||
);
|
||||
} catch (err) {
|
||||
// Fallback to file log if DB fails
|
||||
try {
|
||||
const logsDir = path.resolve(process.cwd(), 'logs');
|
||||
await fs.mkdir(logsDir, { recursive: true });
|
||||
await fs.appendFile(
|
||||
path.resolve(logsDir, 'audit.log'),
|
||||
`${JSON.stringify({ ...event, _fallback: true })}\n`
|
||||
);
|
||||
} catch {
|
||||
// Silent
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const getEvents = async ({ type, userId, limit = 100, offset = 0 } = {}) => {
|
||||
let sql = 'SELECT * FROM audit_logs WHERE 1=1';
|
||||
const params = [];
|
||||
if (type) { sql += ' AND type = ?'; params.push(type); }
|
||||
if (userId) { sql += ' AND user_id = ?'; params.push(userId); }
|
||||
sql += ' ORDER BY created_at DESC LIMIT ? OFFSET ?';
|
||||
params.push(limit, offset);
|
||||
const [rows] = await db.execute(sql, params);
|
||||
return rows;
|
||||
};
|
||||
|
||||
module.exports = { saveEvent, getEvents };
|
||||
@@ -0,0 +1,11 @@
|
||||
const auditRepository = require('./audit.repository');
|
||||
|
||||
const logEvent = async ({ type, userId = null, email = null, ip = null, userAgent = null, metadata = null } = {}) => {
|
||||
await auditRepository.saveEvent({ type, userId, email, ip, userAgent, metadata });
|
||||
};
|
||||
|
||||
const getEvents = async (filters) => {
|
||||
return auditRepository.getEvents(filters);
|
||||
};
|
||||
|
||||
module.exports = { logEvent, getEvents };
|
||||
Reference in New Issue
Block a user