41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
import { Router, json as jsonMiddleware } from "express";
|
|
import {
|
|
getSilencedTests,
|
|
upsertAlertSilence,
|
|
deleteAlertSilence,
|
|
} from "../database/queries/alerting.js";
|
|
const router = Router();
|
|
|
|
// Apply Middlewares
|
|
router.use(jsonMiddleware());
|
|
|
|
// Get Routes
|
|
router.get("/silenced", (req, res) => {
|
|
getSilencedTests().then((t) => res.send(t));
|
|
});
|
|
|
|
// Post Routes
|
|
router.post("/silence", (req, res) => {
|
|
const { name, class: className, method, expires, keepExpires } = req.body;
|
|
if (!name || !className || !method)
|
|
return res
|
|
.status(400)
|
|
.send("'name', 'class', and 'method' are all required Fields!");
|
|
if (expires === null)
|
|
return deleteAlertSilence(req.body)
|
|
.then(() => res.sendStatus(200))
|
|
.catch((e) => res.status(500).send(e));
|
|
const { h, m } = keepExpires ? {} : expires;
|
|
if (!keepExpires && (h == null || m == null))
|
|
return res.status(400).send("Both 'h' and 'm' are required fields!");
|
|
if (!keepExpires && (h < 0 || m < 0))
|
|
return res
|
|
.status(400)
|
|
.send("'h' and 'm' must be greater than or equal to 0!");
|
|
// TODO set max times as well
|
|
if (!keepExpires && (h > 72 || m > 59))
|
|
res.status(400).send("'h' and 'm' must not exceed the set maxes!");
|
|
upsertAlertSilence(req.body).then(() => res.sendStatus(200));
|
|
});
|
|
|
|
export default router;
|