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 133 134 135 136 137 138 139 140 141 | /** * API v1 — Webhooks (read + test) * * GET /api/v1/webhooks — Liste des webhooks configures * POST /api/v1/webhooks/:id/test — Envoyer un payload de test * * @module routes/api/v1/webhooks */ const express = require('express'); const { pool: db } = require('../../../db'); const { requireScope } = require('../../../middleware/apiAuth'); const { checkQuota } = require('../../../middleware/planLimits'); const { validatePublicUrl } = require('../../../utils/networkValidator'); const { apiSuccess, apiError, makeApiDeleteHandler } = require('../../../helpers/apiResponse'); const { VALID_PLATFORMS, VALID_EVENTS } = require('../../../helpers/webhookConstants'); const router = express.Router(); // ─── GET /webhooks ────────────────────────────────────────────────────────── router.get('/', requireScope('read'), async (req, res, next) => { try { const { rows } = await db.query( `SELECT id, label, url, platform, events, enabled, created_at FROM webhooks WHERE user_id = $1 ORDER BY created_at`, [req.user.id], ); apiSuccess(res, rows); } catch (err) { next(err); } }); // ─── POST /webhooks ───────────────────────────────────────────────────────── router.post('/', requireScope('write'), checkQuota('webhooks', 'webhooks'), async (req, res, next) => { try { const { label, url, platform = 'discord', events } = req.body; if (!label || !url) { return apiError(res, 400, 'VALIDATION_ERROR', 'label et url sont requis.'); } if (!VALID_PLATFORMS.includes(platform)) { return apiError(res, 400, 'VALIDATION_ERROR', `Platform invalide. Valeurs : ${VALID_PLATFORMS.join(', ')}`); } const urlCheck = await validatePublicUrl(url); if (!urlCheck.valid) { return apiError(res, 400, 'VALIDATION_ERROR', urlCheck.reason || 'URL non autorisee.'); } const filteredEvents = (events || VALID_EVENTS).filter(e => VALID_EVENTS.includes(e)); const { rows } = await db.query( `INSERT INTO webhooks (user_id, label, url, platform, events) VALUES ($1, $2, $3, $4, $5) RETURNING id, label, url, platform, events, enabled, created_at`, [req.user.id, label, url, platform, filteredEvents], ); apiSuccess(res, rows[0], 201); } catch (err) { next(err); } }); // ─── PATCH /webhooks/:id ──────────────────────────────────────────────────── router.patch('/:id', requireScope('write'), async (req, res, next) => { try { const { rows: existing } = await db.query( 'SELECT * FROM webhooks WHERE id = $1 AND user_id = $2', [req.params.id, req.user.id], ); if (existing.length === 0) { return apiError(res, 404, 'NOT_FOUND', 'Webhook introuvable.'); } const w = existing[0]; const { rows } = await db.query( `UPDATE webhooks SET label=$1, url=$2, platform=$3, events=$4, enabled=$5 WHERE id=$6 AND user_id=$7 RETURNING id, label, url, platform, events, enabled, created_at`, [ req.body.label ?? w.label, req.body.url ?? w.url, req.body.platform ?? w.platform, req.body.events ?? w.events, req.body.enabled == null ? w.enabled : Boolean(req.body.enabled), req.params.id, req.user.id, ], ); apiSuccess(res, rows[0]); } catch (err) { next(err); } }); // ─── DELETE /webhooks/:id ─────────────────────────────────────────────────── router.delete('/:id', requireScope('write'), makeApiDeleteHandler('webhooks', 'Webhook introuvable.')); // ─── POST /webhooks/:id/test ──────────────────────────────────────────────── router.post('/:id/test', requireScope('write'), async (req, res, next) => { try { const { rows } = await db.query( 'SELECT id, url, platform FROM webhooks WHERE id = $1 AND user_id = $2', [req.params.id, req.user.id], ); if (rows.length === 0) { return apiError(res, 404, 'NOT_FOUND', 'Webhook introuvable.'); } // Envoyer un payload de test const webhook = rows[0]; const testPayload = { event: 'test', message: 'Ceci est un test depuis l\'API IliaCloud.', timestamp: new Date().toISOString(), }; try { const response = await fetch(webhook.url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(testPayload), signal: AbortSignal.timeout(10000), }); apiSuccess(res, { sent: true, status: response.status }); } catch (error_) { apiSuccess(res, { sent: false, error: error_.message }); } } catch (err) { next(err); } }); module.exports = router; |