TeamInvitesController.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* eslint-disable
  2. max-len,
  3. no-undef,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const settings = require('settings-sharelatex')
  15. const logger = require('logger-sharelatex')
  16. const TeamInvitesHandler = require('./TeamInvitesHandler')
  17. const AuthenticationController = require('../Authentication/AuthenticationController')
  18. const SubscriptionLocator = require('./SubscriptionLocator')
  19. const ErrorController = require('../Errors/ErrorController')
  20. const EmailHelper = require('../Helpers/EmailHelper')
  21. module.exports = {
  22. createInvite(req, res, next) {
  23. const teamManagerId = AuthenticationController.getLoggedInUserId(req)
  24. const subscription = req.entity
  25. const email = EmailHelper.parseEmail(req.body.email)
  26. if (email == null) {
  27. return res.status(422).json({
  28. error: {
  29. code: 'invalid_email',
  30. message: req.i18n.translate('invalid_email')
  31. }
  32. })
  33. }
  34. return TeamInvitesHandler.createInvite(
  35. teamManagerId,
  36. subscription,
  37. email,
  38. function (err, inviteUserData) {
  39. if (err != null) {
  40. if (err.alreadyInTeam) {
  41. return res.status(400).json({
  42. error: {
  43. code: 'user_already_added',
  44. message: req.i18n.translate('user_already_added')
  45. }
  46. })
  47. }
  48. if (err.limitReached) {
  49. return res.status(400).json({
  50. error: {
  51. code: 'group_full',
  52. message: req.i18n.translate('group_full')
  53. }
  54. })
  55. }
  56. return next(err)
  57. }
  58. return res.json({ user: inviteUserData })
  59. }
  60. )
  61. },
  62. viewInvite(req, res, next) {
  63. const { token } = req.params
  64. const userId = AuthenticationController.getLoggedInUserId(req)
  65. return TeamInvitesHandler.getInvite(
  66. token,
  67. function (err, invite, teamSubscription) {
  68. if (err != null) {
  69. return next(err)
  70. }
  71. if (!invite) {
  72. return ErrorController.notFound(req, res, next)
  73. }
  74. return SubscriptionLocator.getUsersSubscription(
  75. userId,
  76. function (err, personalSubscription) {
  77. if (err != null) {
  78. return next(err)
  79. }
  80. const hasIndividualRecurlySubscription =
  81. personalSubscription != null &&
  82. personalSubscription.planCode.match(/(free|trial)/) == null &&
  83. personalSubscription.groupPlan === false &&
  84. personalSubscription.recurlySubscription_id != null &&
  85. personalSubscription.recurlySubscription_id !== ''
  86. return res.render('subscriptions/team/invite', {
  87. inviterName: invite.inviterName,
  88. inviteToken: invite.token,
  89. hasIndividualRecurlySubscription,
  90. appName: settings.appName,
  91. expired: req.query.expired
  92. })
  93. }
  94. )
  95. }
  96. )
  97. },
  98. acceptInvite(req, res, next) {
  99. const { token } = req.params
  100. const userId = AuthenticationController.getLoggedInUserId(req)
  101. return TeamInvitesHandler.acceptInvite(
  102. token,
  103. userId,
  104. function (err, results) {
  105. if (err != null) {
  106. return next(err)
  107. }
  108. return res.sendStatus(204)
  109. }
  110. )
  111. },
  112. revokeInvite(req, res) {
  113. const subscription = req.entity
  114. const email = EmailHelper.parseEmail(req.params.email)
  115. const teamManagerId = AuthenticationController.getLoggedInUserId(req)
  116. if (email == null) {
  117. return res.sendStatus(400)
  118. }
  119. return TeamInvitesHandler.revokeInvite(
  120. teamManagerId,
  121. subscription,
  122. email,
  123. function (err, results) {
  124. if (err != null) {
  125. return next(err)
  126. }
  127. return res.sendStatus(204)
  128. }
  129. )
  130. }
  131. }