UserMembershipController.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* eslint-disable
  2. max-len,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SessionManager = require('../Authentication/SessionManager')
  14. const UserMembershipHandler = require('./UserMembershipHandler')
  15. const Errors = require('../Errors/Errors')
  16. const EmailHelper = require('../Helpers/EmailHelper')
  17. const CSVParser = require('json2csv').Parser
  18. module.exports = {
  19. index(req, res, next) {
  20. const { entity, entityConfig } = req
  21. return entity.fetchV1Data(function (error, entity) {
  22. if (error != null) {
  23. return next(error)
  24. }
  25. return UserMembershipHandler.getUsers(
  26. entity,
  27. entityConfig,
  28. function (error, users) {
  29. let entityName
  30. if (error != null) {
  31. return next(error)
  32. }
  33. const entityPrimaryKey = entity[
  34. entityConfig.fields.primaryKey
  35. ].toString()
  36. if (entityConfig.fields.name) {
  37. entityName = entity[entityConfig.fields.name]
  38. }
  39. return res.render('user_membership/index', {
  40. name: entityName,
  41. users,
  42. groupSize: entityConfig.hasMembersLimit
  43. ? entity.membersLimit
  44. : undefined,
  45. translations: entityConfig.translations,
  46. paths: entityConfig.pathsFor(entityPrimaryKey),
  47. })
  48. }
  49. )
  50. })
  51. },
  52. add(req, res, next) {
  53. const { entity, entityConfig } = req
  54. const email = EmailHelper.parseEmail(req.body.email)
  55. if (email == null) {
  56. return res.status(400).json({
  57. error: {
  58. code: 'invalid_email',
  59. message: req.i18n.translate('invalid_email'),
  60. },
  61. })
  62. }
  63. if (entityConfig.readOnly) {
  64. return next(new Errors.NotFoundError('Cannot add users to entity'))
  65. }
  66. return UserMembershipHandler.addUser(
  67. entity,
  68. entityConfig,
  69. email,
  70. function (error, user) {
  71. if (error != null ? error.alreadyAdded : undefined) {
  72. return res.status(400).json({
  73. error: {
  74. code: 'user_already_added',
  75. message: req.i18n.translate('user_already_added'),
  76. },
  77. })
  78. }
  79. if (error != null ? error.userNotFound : undefined) {
  80. return res.status(404).json({
  81. error: {
  82. code: 'user_not_found',
  83. message: req.i18n.translate('user_not_found'),
  84. },
  85. })
  86. }
  87. if (error != null) {
  88. return next(error)
  89. }
  90. return res.json({ user })
  91. }
  92. )
  93. },
  94. remove(req, res, next) {
  95. const { entity, entityConfig } = req
  96. const { userId } = req.params
  97. if (entityConfig.readOnly) {
  98. return next(new Errors.NotFoundError('Cannot remove users from entity'))
  99. }
  100. const loggedInUserId = SessionManager.getLoggedInUserId(req.session)
  101. if (loggedInUserId === userId) {
  102. return res.status(400).json({
  103. error: {
  104. code: 'managers_cannot_remove_self',
  105. message: req.i18n.translate('managers_cannot_remove_self'),
  106. },
  107. })
  108. }
  109. return UserMembershipHandler.removeUser(
  110. entity,
  111. entityConfig,
  112. userId,
  113. function (error, user) {
  114. if (error != null ? error.isAdmin : undefined) {
  115. return res.status(400).json({
  116. error: {
  117. code: 'managers_cannot_remove_admin',
  118. message: req.i18n.translate('managers_cannot_remove_admin'),
  119. },
  120. })
  121. }
  122. if (error != null) {
  123. return next(error)
  124. }
  125. return res.sendStatus(200)
  126. }
  127. )
  128. },
  129. exportCsv(req, res, next) {
  130. const { entity, entityConfig } = req
  131. const fields = ['email', 'last_logged_in_at']
  132. return UserMembershipHandler.getUsers(
  133. entity,
  134. entityConfig,
  135. function (error, users) {
  136. if (error != null) {
  137. return next(error)
  138. }
  139. const csvParser = new CSVParser({ fields })
  140. res.header('Content-Disposition', 'attachment; filename=Group.csv')
  141. res.contentType('text/csv')
  142. return res.send(csvParser.parse(users))
  143. }
  144. )
  145. },
  146. new(req, res, next) {
  147. return res.render('user_membership/new', {
  148. entityName: req.params.name,
  149. entityId: req.params.id,
  150. })
  151. },
  152. create(req, res, next) {
  153. const entityId = req.params.id
  154. const entityConfig = req.entityConfig
  155. return UserMembershipHandler.createEntity(
  156. entityId,
  157. entityConfig,
  158. function (error, entity) {
  159. if (error != null) {
  160. return next(error)
  161. }
  162. return res.redirect(entityConfig.pathsFor(entityId).index)
  163. }
  164. )
  165. },
  166. }