Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | /** * API v1 — Alertes (read-only) * * GET /api/v1/alerts/rules — Liste des regles d'alertes * GET /api/v1/alerts/history — Historique des alertes declenchees * * @module routes/api/v1/alerts */ const express = require('express'); const { pool: db } = require('../../../db'); const { requireScope } = require('../../../middleware/apiAuth'); const { checkQuota } = require('../../../middleware/planLimits'); const { apiSuccess, apiPaginated, apiError, parsePagination, makeApiDeleteHandler } = require('../../../helpers/apiResponse'); const { VALID_METRICS } = require('../../../helpers/alertConstants'); const { ownsServer } = require('../../../helpers/serverOwnership'); const router = express.Router(); // ─── GET /alerts/rules ────────────────────────────────────────────────────── router.get('/rules', requireScope('read'), async (req, res, next) => { try { const { rows } = await db.query( `SELECT ar.id, ar.server_id, ar.metric, ar.threshold, ar.enabled, ar.created_at, s.name AS server_name FROM alert_rules ar JOIN servers s ON s.id = ar.server_id WHERE ar.user_id = $1 ORDER BY s.name, ar.metric`, [req.user.id], ); apiSuccess(res, rows); } catch (err) { next(err); } }); // ─── GET /alerts/history ──────────────────────────────────────────────────── router.get('/history', requireScope('read'), async (req, res, next) => { try { const { page, perPage, offset } = parsePagination(req.query); const [{ rows }, { rows: countRows }] = await Promise.all([ db.query( `SELECT ah.id, ah.metric, ah.value, ah.threshold, ah.message, ah.created_at, s.name AS server_name FROM alert_history ah JOIN servers s ON s.id = ah.server_id WHERE ah.user_id = $1 ORDER BY ah.created_at DESC LIMIT $2 OFFSET $3`, [req.user.id, perPage, offset], ), db.query('SELECT COUNT(*) FROM alert_history WHERE user_id = $1', [req.user.id]), ]); apiPaginated(res, rows, { page, per_page: perPage, total: Number.parseInt(countRows[0].count) }); } catch (err) { next(err); } }); // ─── POST /alerts/rules ───────────────────────────────────────────────────── router.post('/rules', requireScope('write'), checkQuota('alert_rules', 'alert_rules'), async (req, res, next) => { try { const { server_id, metric, threshold = 90, enabled = true } = req.body; if (!server_id || !metric) { return apiError(res, 400, 'VALIDATION_ERROR', 'server_id et metric sont requis.'); } if (!VALID_METRICS.includes(metric)) { return apiError(res, 400, 'VALIDATION_ERROR', `Metric invalide. Valeurs : ${VALID_METRICS.join(', ')}`); } // Verifier ownership du serveur if (!(await ownsServer(server_id, req.user.id))) { return apiError(res, 404, 'NOT_FOUND', 'Serveur introuvable.'); } const { rows } = await db.query( `INSERT INTO alert_rules (server_id, user_id, metric, threshold, enabled) VALUES ($1, $2, $3, $4, $5) ON CONFLICT (server_id, user_id, metric) DO UPDATE SET threshold = EXCLUDED.threshold, enabled = EXCLUDED.enabled RETURNING id, server_id, metric, threshold, enabled, created_at`, [server_id, req.user.id, metric, threshold, enabled], ); apiSuccess(res, rows[0], 201); } catch (err) { next(err); } }); // ─── PATCH /alerts/rules/:id ──────────────────────────────────────────────── router.patch('/rules/:id', requireScope('write'), async (req, res, next) => { try { const { rows: existing } = await db.query( 'SELECT * FROM alert_rules WHERE id = $1 AND user_id = $2', [req.params.id, req.user.id], ); if (existing.length === 0) { return apiError(res, 404, 'NOT_FOUND', 'Regle introuvable.'); } const rule = existing[0]; const threshold = req.body.threshold ?? rule.threshold; const enabled = req.body.enabled ?? rule.enabled; const { rows } = await db.query( `UPDATE alert_rules SET threshold = $1, enabled = $2 WHERE id = $3 AND user_id = $4 RETURNING id, server_id, metric, threshold, enabled, created_at`, [threshold, enabled, req.params.id, req.user.id], ); apiSuccess(res, rows[0]); } catch (err) { next(err); } }); // ─── DELETE /alerts/rules/:id ─────────────────────────────────────────────── router.delete('/rules/:id', requireScope('write'), makeApiDeleteHandler('alert_rules', 'Regle introuvable.')); module.exports = router; |