config.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const asyncWrapper = require('../middleware/asyncWrapper');
  2. const ErrorResponse = require('../utils/ErrorResponse');
  3. const Config = require('../models/Config');
  4. const { Op } = require('sequelize');
  5. // @desc Insert new key:value pair
  6. // @route POST /api/config
  7. // @access Public
  8. exports.createPair = asyncWrapper(async (req, res, next) => {
  9. const pair = await Config.create(req.body);
  10. res.status(201).json({
  11. success: true,
  12. data: pair
  13. })
  14. })
  15. // @desc Get all key:value pairs
  16. // @route GET /api/config
  17. // @route GET /api/config?keys=foo,bar,baz
  18. // @access Public
  19. exports.getAllPairs = asyncWrapper(async (req, res, next) => {
  20. let pairs;
  21. if (req.query.keys) {
  22. // Check for specific keys to get in a single query
  23. const keys = req.query.keys
  24. .split(',')
  25. .map((key) => { return { key } });
  26. pairs = await Config.findAll({
  27. where: {
  28. [Op.or]: keys
  29. }
  30. });
  31. } else {
  32. // Else get all
  33. pairs = await Config.findAll();
  34. }
  35. res.status(200).json({
  36. success: true,
  37. data: pairs
  38. })
  39. })
  40. // @desc Get single key:value pair
  41. // @route GET /api/config/:key
  42. // @access Public
  43. exports.getSinglePair = asyncWrapper(async (req, res, next) => {
  44. const pair = await Config.findOne({
  45. where: { key: req.params.key }
  46. });
  47. if (!pair) {
  48. return next(new ErrorResponse(`Key ${req.params.key} was not found`, 404));
  49. }
  50. res.status(200).json({
  51. success: true,
  52. data: pair
  53. })
  54. })
  55. // @desc Update value
  56. // @route PUT /api/config/:key
  57. // @access Public
  58. exports.updateValue = asyncWrapper(async (req, res, next) => {
  59. let pair = await Config.findOne({
  60. where: { key: req.params.key }
  61. });
  62. if (!pair) {
  63. return next(new ErrorResponse(`Key ${req.params.key} was not found`, 404));
  64. }
  65. if (pair.isLocked) {
  66. return next(new ErrorResponse(`Value of key ${req.params.key} is locked and can not be changed`, 400));
  67. }
  68. pair = await pair.update({ ...req.body });
  69. res.status(200).json({
  70. success: true,
  71. data: pair
  72. })
  73. })
  74. // @desc Update multiple values
  75. // @route PUT /api/config/
  76. // @access Public
  77. exports.updateValues = asyncWrapper(async (req, res, next) => {
  78. Object.entries(req.body).forEach(async ([key, value]) => {
  79. await Config.update({ value }, {
  80. where: { key }
  81. })
  82. })
  83. const config = await Config.findAll();
  84. res.status(200).send({
  85. success: true,
  86. data: config
  87. })
  88. })
  89. // @desc Delete key:value pair
  90. // @route DELETE /api/config/:key
  91. // @access Public
  92. exports.deletePair = asyncWrapper(async (req, res, next) => {
  93. const pair = await Config.findOne({
  94. where: { key: req.params.key }
  95. });
  96. if (!pair) {
  97. return next(new ErrorResponse(`Key ${req.params.key} was not found`, 404));
  98. }
  99. if (pair.isLocked) {
  100. return next(new ErrorResponse(`Value of key ${req.params.key} is locked and can not be deleted`, 400));
  101. }
  102. await pair.destroy();
  103. res.status(200).json({
  104. success: true,
  105. data: {}
  106. })
  107. })