qualiteer/lib/server/routes/alerting-route.js

42 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-05-08 01:36:22 +00:00
import { Router, json as jsonMiddleware } from "express";
2022-10-08 17:47:46 +00:00
import {
getSilencedTests,
upsertAlertSilence,
deleteAlertSilence,
} from "../database/queries/alerting.js";
2022-05-08 01:36:22 +00:00
const router = Router();
// Apply Middlewares
router.use(jsonMiddleware());
// Get Routes
router.get("/silenced", (req, res) => {
2022-08-09 04:29:10 +00:00
getSilencedTests().then((t) => res.send(t));
2022-05-08 01:36:22 +00:00
});
// Post Routes
2022-05-17 12:32:04 +00:00
router.post("/silence", (req, res) => {
2022-10-08 17:47:46 +00:00
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));
2022-05-08 01:36:22 +00:00
});
export default router;