settings.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import express from "express";
  2. import internalSetting from "../internal/setting.js";
  3. import jwtdecode from "../lib/express/jwt-decode.js";
  4. import apiValidator from "../lib/validator/api.js";
  5. import validator from "../lib/validator/index.js";
  6. import { express as logger } from "../logger.js";
  7. import { getValidationSchema } from "../schema/index.js";
  8. const router = express.Router({
  9. caseSensitive: true,
  10. strict: true,
  11. mergeParams: true,
  12. });
  13. /**
  14. * /api/settings
  15. */
  16. router
  17. .route("/")
  18. .options((_, res) => {
  19. res.sendStatus(204);
  20. })
  21. .all(jwtdecode())
  22. /**
  23. * GET /api/settings
  24. *
  25. * Retrieve all settings
  26. */
  27. .get(async (req, res, next) => {
  28. try {
  29. const rows = await internalSetting.getAll(res.locals.access);
  30. res.status(200).send(rows);
  31. } catch (err) {
  32. logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
  33. next(err);
  34. }
  35. });
  36. /**
  37. * Specific setting
  38. *
  39. * /api/settings/something
  40. */
  41. router
  42. .route("/:setting_id")
  43. .options((_, res) => {
  44. res.sendStatus(204);
  45. })
  46. .all(jwtdecode())
  47. /**
  48. * GET /settings/something
  49. *
  50. * Retrieve a specific setting
  51. */
  52. .get(async (req, res, next) => {
  53. try {
  54. const data = await validator(
  55. {
  56. required: ["setting_id"],
  57. additionalProperties: false,
  58. properties: {
  59. setting_id: {
  60. type: "string",
  61. minLength: 1,
  62. },
  63. },
  64. },
  65. {
  66. setting_id: req.params.setting_id,
  67. },
  68. );
  69. const row = await internalSetting.get(res.locals.access, {
  70. id: data.setting_id,
  71. });
  72. res.status(200).send(row);
  73. } catch (err) {
  74. logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
  75. next(err);
  76. }
  77. })
  78. /**
  79. * PUT /api/settings/something
  80. *
  81. * Update and existing setting
  82. */
  83. .put(async (req, res, next) => {
  84. try {
  85. const payload = await apiValidator(getValidationSchema("/settings/{settingID}", "put"), req.body);
  86. payload.id = req.params.setting_id;
  87. const result = await internalSetting.update(res.locals.access, payload);
  88. res.status(200).send(result);
  89. } catch (err) {
  90. logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
  91. next(err);
  92. }
  93. });
  94. export default router;