All files / src/routes/api/v1 statusPages.js

0% Statements 0/25
0% Branches 0/12
0% Functions 0/2
0% Lines 0/25

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                                                                                                                                     
/**
 * API v1 — Status Pages (read + patch)
 *
 * GET   /api/v1/status-pages       — Liste des pages de statut
 * PATCH /api/v1/status-pages/:id   — Modifier une page
 *
 * @module routes/api/v1/statusPages
 */
 
const express = require('express');
const { pool: db } = require('../../../db');
const { requireScope } = require('../../../middleware/apiAuth');
const { apiSuccess, apiError } = require('../../../helpers/apiResponse');
 
const router = express.Router();
 
// ─── GET /status-pages ──────────────────────────────────────────────────────
 
router.get('/', requireScope('read'), async (req, res, next) => {
  try {
    const { rows } = await db.query(
      `SELECT id, slug, title, description, logo_url, accent_color, bg_color, text_color,
              show_uptime, show_latency, enabled, created_at
       FROM status_pages WHERE user_id = $1
       ORDER BY created_at`,
      [req.user.id],
    );
    apiSuccess(res, rows);
  } catch (err) {
    next(err);
  }
});
 
// ─── PATCH /status-pages/:id ────────────────────────────────────────────────
 
router.patch('/:id', requireScope('write'), async (req, res, next) => {
  try {
    const { rows: existing } = await db.query(
      'SELECT * FROM status_pages WHERE id = $1 AND user_id = $2',
      [req.params.id, req.user.id],
    );
    if (existing.length === 0) {
      return apiError(res, 404, 'NOT_FOUND', 'Page de statut introuvable.');
    }
 
    const page = existing[0];
    const title = req.body.title ?? page.title;
    const description = req.body.description ?? page.description;
    const enabled = req.body.enabled ?? page.enabled;
    const show_uptime = req.body.show_uptime ?? page.show_uptime;
    const show_latency = req.body.show_latency ?? page.show_latency;
 
    const { rows } = await db.query(
      `UPDATE status_pages SET title=$1, description=$2, enabled=$3, show_uptime=$4, show_latency=$5
       WHERE id=$6 AND user_id=$7
       RETURNING id, slug, title, description, enabled, show_uptime, show_latency, created_at`,
      [title, description, enabled, show_uptime, show_latency, req.params.id, req.user.id],
    );
 
    apiSuccess(res, rows[0]);
  } catch (err) {
    next(err);
  }
});
 
module.exports = router;