SubscriptionGroupHandler.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* eslint-disable
  2. camelcase,
  3. node/handle-callback-err,
  4. max-len,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS103: Rewrite code to no longer use __guard__
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const async = require('async')
  17. const _ = require('underscore')
  18. const { promisify } = require('util')
  19. const SubscriptionUpdater = require('./SubscriptionUpdater')
  20. const SubscriptionLocator = require('./SubscriptionLocator')
  21. const UserGetter = require('../User/UserGetter')
  22. const { Subscription } = require('../../models/Subscription')
  23. const LimitationsManager = require('./LimitationsManager')
  24. const logger = require('@overleaf/logger')
  25. const OneTimeTokenHandler = require('../Security/OneTimeTokenHandler')
  26. const EmailHandler = require('../Email/EmailHandler')
  27. const settings = require('@overleaf/settings')
  28. const NotificationsBuilder = require('../Notifications/NotificationsBuilder')
  29. const UserMembershipViewModel = require('../UserMembership/UserMembershipViewModel')
  30. const SubscriptionGroupHandler = {
  31. removeUserFromGroup(subscriptionId, userToRemove_id, callback) {
  32. return SubscriptionUpdater.removeUserFromGroup(
  33. subscriptionId,
  34. userToRemove_id,
  35. callback
  36. )
  37. },
  38. replaceUserReferencesInGroups(oldId, newId, callback) {
  39. return Subscription.updateOne(
  40. { admin_id: oldId },
  41. { admin_id: newId },
  42. function (error) {
  43. if (error != null) {
  44. return callback(error)
  45. }
  46. return replaceInArray(
  47. Subscription,
  48. 'manager_ids',
  49. oldId,
  50. newId,
  51. function (error) {
  52. if (error != null) {
  53. return callback(error)
  54. }
  55. return replaceInArray(
  56. Subscription,
  57. 'member_ids',
  58. oldId,
  59. newId,
  60. callback
  61. )
  62. }
  63. )
  64. }
  65. )
  66. },
  67. isUserPartOfGroup(user_id, subscription_id, callback) {
  68. if (callback == null) {
  69. callback = function () {}
  70. }
  71. return SubscriptionLocator.getSubscriptionByMemberIdAndId(
  72. user_id,
  73. subscription_id,
  74. function (err, subscription) {
  75. let partOfGroup
  76. if (subscription != null) {
  77. partOfGroup = true
  78. } else {
  79. partOfGroup = false
  80. }
  81. return callback(err, partOfGroup)
  82. }
  83. )
  84. },
  85. getTotalConfirmedUsersInGroup(subscription_id, callback) {
  86. if (callback == null) {
  87. callback = function () {}
  88. }
  89. return SubscriptionLocator.getSubscription(
  90. subscription_id,
  91. (err, subscription) =>
  92. callback(
  93. err,
  94. __guard__(
  95. subscription != null ? subscription.member_ids : undefined,
  96. x => x.length
  97. )
  98. )
  99. )
  100. },
  101. }
  102. function replaceInArray(model, property, oldValue, newValue, callback) {
  103. // Mongo won't let us pull and addToSet in the same query, so do it in
  104. // two. Note we need to add first, since the query is based on the old user.
  105. const query = {}
  106. query[property] = oldValue
  107. const setNewValue = {}
  108. setNewValue[property] = newValue
  109. const setOldValue = {}
  110. setOldValue[property] = oldValue
  111. model.updateMany(query, { $addToSet: setNewValue }, function (error) {
  112. if (error) {
  113. return callback(error)
  114. }
  115. model.updateMany(query, { $pull: setOldValue }, callback)
  116. })
  117. }
  118. function __guard__(value, transform) {
  119. return typeof value !== 'undefined' && value !== null
  120. ? transform(value)
  121. : undefined
  122. }
  123. SubscriptionGroupHandler.promises = {
  124. getTotalConfirmedUsersInGroup: promisify(
  125. SubscriptionGroupHandler.getTotalConfirmedUsersInGroup
  126. ),
  127. isUserPartOfGroup: promisify(SubscriptionGroupHandler.isUserPartOfGroup),
  128. }
  129. module.exports = SubscriptionGroupHandler