audit-log.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import express from "express";
  2. import internalAuditLog from "../internal/audit-log.js";
  3. import jwtdecode from "../lib/express/jwt-decode.js";
  4. import validator from "../lib/validator/index.js";
  5. import { express as logger } from "../logger.js";
  6. const router = express.Router({
  7. caseSensitive: true,
  8. strict: true,
  9. mergeParams: true,
  10. });
  11. /**
  12. * /api/audit-log
  13. */
  14. router
  15. .route("/")
  16. .options((_, res) => {
  17. res.sendStatus(204);
  18. })
  19. .all(jwtdecode())
  20. /**
  21. * GET /api/audit-log
  22. *
  23. * Retrieve all logs
  24. */
  25. .get(async (req, res, next) => {
  26. try {
  27. const data = await validator(
  28. {
  29. additionalProperties: false,
  30. properties: {
  31. expand: {
  32. $ref: "common#/properties/expand",
  33. },
  34. query: {
  35. $ref: "common#/properties/query",
  36. },
  37. },
  38. },
  39. {
  40. expand: typeof req.query.expand === "string" ? req.query.expand.split(",") : null,
  41. query: typeof req.query.query === "string" ? req.query.query : null,
  42. },
  43. );
  44. const rows = await internalAuditLog.getAll(res.locals.access, data.expand, data.query);
  45. res.status(200).send(rows);
  46. } catch (err) {
  47. logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
  48. next(err);
  49. }
  50. });
  51. export default router;