SubscriptionGroupController.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  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 SubscriptionGroupHandler = require('./SubscriptionGroupHandler')
  15. const logger = require('logger-sharelatex')
  16. const SubscriptionLocator = require('./SubscriptionLocator')
  17. const AuthenticationController = require('../Authentication/AuthenticationController')
  18. const _ = require('underscore')
  19. const async = require('async')
  20. module.exports = {
  21. removeUserFromGroup(req, res, next) {
  22. const subscription = req.entity
  23. const userToRemove_id = req.params.user_id
  24. logger.log(
  25. { subscriptionId: subscription._id, userToRemove_id },
  26. 'removing user from group subscription'
  27. )
  28. return SubscriptionGroupHandler.removeUserFromGroup(
  29. subscription._id,
  30. userToRemove_id,
  31. function(err) {
  32. if (err != null) {
  33. logger.warn(
  34. { err, subscriptionId: subscription._id, userToRemove_id },
  35. 'error removing user from group'
  36. )
  37. return next(err)
  38. }
  39. return res.send()
  40. }
  41. )
  42. },
  43. removeSelfFromGroup(req, res, next) {
  44. const adminUserId = req.query.admin_user_id
  45. const userToRemove_id = AuthenticationController.getLoggedInUserId(req)
  46. return getManagedSubscription(adminUserId, function(error, subscription) {
  47. if (error != null) {
  48. return next(error)
  49. }
  50. return SubscriptionGroupHandler.removeUserFromGroup(
  51. subscription._id,
  52. userToRemove_id,
  53. function(err) {
  54. if (err != null) {
  55. logger.err(
  56. { err, userToRemove_id, adminUserId },
  57. 'error removing self from group'
  58. )
  59. return res.sendStatus(500)
  60. }
  61. return res.send()
  62. }
  63. )
  64. })
  65. },
  66. // legacy route
  67. redirectToSubscriptionGroupAdminPage(req, res, next) {
  68. const user_id = AuthenticationController.getLoggedInUserId(req)
  69. return getManagedSubscription(user_id, function(error, subscription) {
  70. if (error != null) {
  71. return next(error)
  72. }
  73. if (!(subscription != null ? subscription.groupPlan : undefined)) {
  74. return res.redirect('/user/subscription')
  75. }
  76. return res.redirect(`/manage/groups/${subscription._id}/members`)
  77. })
  78. }
  79. }
  80. var getManagedSubscription = (managerId, callback) =>
  81. SubscriptionLocator.findManagedSubscription(managerId, function(
  82. err,
  83. subscription
  84. ) {
  85. if (err) {
  86. return callback(err)
  87. } else if (!subscription) {
  88. return callback(
  89. new Error(`No subscription found managed by user ${managerId}`)
  90. )
  91. }
  92. return callback(null, subscription)
  93. })