user.controller.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @file Provides the model and operation for users.
  3. * @author Inigo Valentin
  4. * @since 4.0.0
  5. */
  6. const logger = require('pino')();
  7. const LocaleService = require('../services/locale.service.js');
  8. const localeService = new LocaleService();
  9. //const AuthService = require("../services/auth.service.js");
  10. //const authService = new AuthService(db);
  11. /**
  12. * Retrieve the active user info.
  13. *
  14. * @param req The received request by the server.
  15. * @param res The request to be sent by the server.
  16. */
  17. exports.findActive = (req, res) => {
  18. User.findAll({
  19. where: {active: true},
  20. include: [
  21. {model: UserUrl, as: "urls", attributes: {exclude: ['id', 'userId', 'priority'], order: [['priority', 'ASC']]}},
  22. {model: UserText, as: "texts", attributes: {exclude: ['id', 'userId', 'section', 'global']}}
  23. ],
  24. attributes: {exclude: ['id', 'password', 'email', 'salt', 'active', 'admin', 'createdAt', 'updatedAt']},
  25. })
  26. .then(async data => {
  27. data = await localeService.localizeUser(data[0], req);
  28. res.send(data);
  29. })
  30. .catch(err => {
  31. logger.error("Error retrieving active user: " + err);
  32. res.status(500).send("Error retrieving active user.");
  33. });
  34. };
  35. /**
  36. * Retrieve all users from the database.
  37. *
  38. * @param req The received request by the server.
  39. * @param res The request to be sent by the server.
  40. */
  41. exports.findAll = (req, res) => {
  42. // Check authentication
  43. const validation = authService.validateToken(req, res);
  44. if (validation.success === false){
  45. res.status(validation.code).send(validation.message);
  46. return;
  47. }
  48. User.findAll()
  49. .then(async data => {
  50. data = await localeService.localizeUsers(data, req);
  51. res.send(data);
  52. })
  53. .catch(err => {
  54. logger.error("Error retrieving users: " + err);
  55. res.status(500).send("Error retrieving users.");
  56. });
  57. };
  58. /**
  59. * Find a single user by it's ID.
  60. *
  61. * @param req The received request by the server.
  62. * @param res The request to be sent by the server.
  63. */
  64. exports.findOne = (req, res) => {
  65. // Check authentication
  66. const validation = authService.validateToken(req, res);
  67. if (validation.success === false){
  68. res.status(validation.code).send(validation.message);
  69. return;
  70. }
  71. const id = req.params.id;
  72. User.findByPk(id)
  73. .then(async data => {
  74. if (data) {
  75. data = await localeService.localizeUser(data, req);
  76. res.send(data);
  77. }
  78. else res.status(404).send(`No user with id ${id}.`)
  79. })
  80. .catch(err => {
  81. logger.error("Error retrieving user with id " + id + ": " + err);
  82. res.status(500).send("Error retrieving user with id " + id + ".");
  83. });
  84. };
  85. /**
  86. * Update a single user by it's ID.
  87. *
  88. * @param req The received request by the server.
  89. * @param res The request to be sent by the server.
  90. */
  91. exports.update = (req, res) => {
  92. // Check authentication
  93. const validation = authService.validateToken(req, res);
  94. if (validation.success === false){
  95. res.status(validation.code).send(validation.message);
  96. return;
  97. }
  98. const id = req.params.id;
  99. User.update(req.body, {where: {id: id}})
  100. .then(num => {
  101. if (num == 1) res.send({message: "License was updated successfully."});
  102. else res.send(`Cannot update user with id=${id}.`);
  103. })
  104. .catch(err => {
  105. logger.error("Error updateing user: " + err);
  106. res.status(500).send("Error updating user.");
  107. });
  108. };
  109. /**
  110. * Delete a single user by it's ID. Currently disabled.
  111. *
  112. * @param req The received request by the server.
  113. * @param res The request to be sent by the server.
  114. */
  115. exports.delete = (req, res) => {return res.status(405).send("User deletion is disabled.");};
  116. /**
  117. * Delete all users. Currently disabled.
  118. *
  119. * @param req The received request by the server.
  120. * @param res The request to be sent by the server.
  121. */
  122. exports.deleteAll = (req, res) => {return res.status(405).send("User deletion is disabled.");};
  123. /**
  124. * Create and save a new user. Currently disabled.
  125. *
  126. * @param req The received request by the server.
  127. * @param res The request to be sent by the server.
  128. */
  129. exports.create = async (req, res) => {return res.status(405).send("User creation is disabled.");};