2022-05-08 01:36:22 +00:00
|
|
|
import { Router, json as jsonMiddleware } from "express";
|
2022-10-08 17:47:46 +00:00
|
|
|
import {
|
|
|
|
getTests,
|
|
|
|
getPipelineMappings,
|
|
|
|
upsertTest,
|
2022-10-15 11:47:47 +00:00
|
|
|
truncateTests,
|
|
|
|
removeDroppedTests,
|
2022-10-08 17:47:46 +00:00
|
|
|
} from "../database/queries/catalog.js";
|
2022-05-08 01:36:22 +00:00
|
|
|
const router = Router();
|
|
|
|
|
|
|
|
const maxSize = 1024 * 1024 * 100; // 100MB
|
|
|
|
|
|
|
|
// Apply Middlewares
|
2022-05-17 12:32:04 +00:00
|
|
|
router.use(jsonMiddleware({ limit: maxSize }));
|
2022-05-08 01:36:22 +00:00
|
|
|
|
|
|
|
// Get Routes
|
2022-08-06 21:21:41 +00:00
|
|
|
router.get("/tests", (req, res) => {
|
2022-08-09 04:29:10 +00:00
|
|
|
getTests().then((t) => res.json(t));
|
2022-05-08 01:36:22 +00:00
|
|
|
});
|
|
|
|
|
2022-08-09 04:29:10 +00:00
|
|
|
router.get("/pipeline-mappings", (req, res) => {
|
|
|
|
getPipelineMappings().then((m) => res.json(m));
|
|
|
|
});
|
2022-08-06 21:21:41 +00:00
|
|
|
|
2022-05-08 01:36:22 +00:00
|
|
|
// Post Routes
|
2022-05-17 12:32:04 +00:00
|
|
|
router.post("/update", (req, res) => {
|
2022-10-08 17:47:46 +00:00
|
|
|
if (!req.body) return res.status(400).send("Body required!");
|
2022-10-15 11:47:47 +00:00
|
|
|
if (!Array.isArray(req.body))
|
|
|
|
return res.status(400).send("Body must be an array!");
|
|
|
|
const wrongImage = req.body.find(({ image }) => image !== req.body[0].image);
|
|
|
|
if (wrongImage)
|
|
|
|
return res.status(400).send("Tests cannot have unique images!");
|
|
|
|
const testNames = req.body.map(({ name }) => name);
|
|
|
|
|
|
|
|
// Upsert new tests
|
|
|
|
const upserts = Promise.all(
|
|
|
|
req.body.map((catalogItem) => upsertTest(catalogItem))
|
|
|
|
);
|
|
|
|
const dropRm = upserts.then(() => removeDroppedTests(testNames));
|
|
|
|
|
|
|
|
dropRm.then(() => res.sendStatus(200)).catch((e) => res.status(500).send(e));
|
2022-05-08 01:36:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default router;
|