33 lines
985 B
JavaScript
33 lines
985 B
JavaScript
const pool = require('../config/db');
|
|
|
|
exports.getLogs = async (req, res) => {
|
|
try {
|
|
const page = Math.max(1, parseInt(req.query.page) || 1);
|
|
const limit = Math.min(100, Math.max(10, parseInt(req.query.limit) || 50));
|
|
const offset = (page - 1) * limit;
|
|
const actionFilter = req.query.action || '';
|
|
|
|
let where = '';
|
|
const params = [];
|
|
if (actionFilter) {
|
|
where = 'WHERE al.action LIKE ?';
|
|
params.push(`%${actionFilter}%`);
|
|
}
|
|
|
|
const [countRows] = await pool.query(
|
|
`SELECT COUNT(*) AS total FROM activity_logs al ${where}`, params
|
|
);
|
|
const total = countRows[0].total;
|
|
|
|
const [rows] = await pool.query(
|
|
`SELECT al.* FROM activity_logs al ${where} ORDER BY al.created_at DESC LIMIT ? OFFSET ?`,
|
|
[...params, limit, offset]
|
|
);
|
|
|
|
res.json({ logs: rows, total, page, limit, pages: Math.ceil(total / limit) });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Erreur' });
|
|
}
|
|
};
|