settings.js 1.8 KB

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