Subscription.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. const { db, ObjectId } = require('../../../../app/src/infrastructure/mongodb')
  2. const { expect } = require('chai')
  3. const { callbackifyClass } = require('@overleaf/promise-utils')
  4. const SubscriptionUpdater = require('../../../../app/src/Features/Subscription/SubscriptionUpdater')
  5. const PermissionsManager = require('../../../../app/src/Features/Authorization/PermissionsManager')
  6. const SSOConfigManager = require('../../../../modules/group-settings/app/src/sso/SSOConfigManager')
  7. const SubscriptionModel =
  8. require('../../../../app/src/models/Subscription').Subscription
  9. const DeletedSubscriptionModel =
  10. require('../../../../app/src/models/DeletedSubscription').DeletedSubscription
  11. const Modules = require('../../../../app/src/infrastructure/Modules')
  12. class PromisifiedSubscription {
  13. constructor(options = {}) {
  14. this.admin_id = options.adminId || new ObjectId()
  15. this.overleaf = options.overleaf || {}
  16. this.groupPlan = options.groupPlan
  17. this.manager_ids = options.managerIds || [this.admin_id]
  18. this.member_ids = options.memberIds || []
  19. this.membersLimit = options.membersLimit || 0
  20. this.invited_emails = options.invitedEmails || []
  21. this.teamName = options.teamName
  22. this.teamInvites = options.teamInvites || []
  23. this.planCode = options.planCode
  24. this.recurlySubscription_id = options.recurlySubscription_id
  25. this.features = options.features
  26. this.ssoConfig = options.ssoConfig
  27. this.groupPolicy = options.groupPolicy
  28. }
  29. async ensureExists() {
  30. if (this._id) {
  31. return null
  32. }
  33. const options = { upsert: true, new: true, setDefaultsOnInsert: true }
  34. const subscription = await SubscriptionModel.findOneAndUpdate(
  35. { admin_id: this.admin_id },
  36. this,
  37. options
  38. ).exec()
  39. this._id = subscription._id
  40. }
  41. async get() {
  42. return await db.subscriptions.findOne({ _id: new ObjectId(this._id) })
  43. }
  44. async getWithGroupPolicy() {
  45. // eslint-disable-next-line no-restricted-syntax
  46. return await SubscriptionModel.findById(this._id)
  47. .populate('groupPolicy')
  48. .exec()
  49. }
  50. async setManagerIds(managerIds) {
  51. return await SubscriptionModel.findOneAndUpdate(
  52. { _id: new ObjectId(this._id) },
  53. { manager_ids: managerIds }
  54. )
  55. }
  56. async setSSOConfig(ssoConfig) {
  57. const subscription = await this.get()
  58. return await SSOConfigManager.promises.updateSubscriptionSSOConfig(
  59. subscription,
  60. ssoConfig
  61. )
  62. }
  63. async refreshUsersFeatures() {
  64. return await SubscriptionUpdater.promises.refreshUsersFeatures(this)
  65. }
  66. async enableManagedUsers() {
  67. await Modules.promises.hooks.fire('enableManagedUsers', this._id)
  68. }
  69. async enableFeatureSSO() {
  70. await SubscriptionModel.findOneAndUpdate(
  71. { _id: new ObjectId(this._id) },
  72. { 'features.groupSSO': true }
  73. ).exec()
  74. }
  75. async setValidatedSSO() {
  76. const doc = await db.subscriptions.findOne({ _id: new ObjectId(this._id) })
  77. const ssoConfigId = doc.ssoConfig
  78. return await db.ssoConfigs.findOneAndUpdate(
  79. { _id: ssoConfigId },
  80. { $set: { validated: true } }
  81. )
  82. }
  83. async setValidatedAndEnabledSSO() {
  84. const doc = await db.subscriptions.findOne({ _id: new ObjectId(this._id) })
  85. const ssoConfigId = doc.ssoConfig
  86. return await db.ssoConfigs.findOneAndUpdate(
  87. { _id: ssoConfigId },
  88. { $set: { enabled: true, validated: true } }
  89. )
  90. }
  91. async getEnrollmentForUser(user) {
  92. const [enrollment] = await Modules.promises.hooks.fire(
  93. 'getManagedUsersEnrollmentForUser',
  94. user
  95. )
  96. return enrollment
  97. }
  98. getCapabilities(groupPolicy) {
  99. return PermissionsManager.getUserCapabilities(groupPolicy)
  100. }
  101. async getUserValidationStatus(params) {
  102. return await PermissionsManager.promises.getUserValidationStatus(params)
  103. }
  104. async enrollManagedUser(user) {
  105. const subscription = await SubscriptionModel.findById(this._id).exec()
  106. return await Modules.promises.hooks.fire(
  107. 'enrollInManagedSubscription',
  108. user._id,
  109. subscription
  110. )
  111. }
  112. async expectDeleted(deleterData) {
  113. const deletedSubscriptions = await DeletedSubscriptionModel.find({
  114. 'subscription._id': this._id,
  115. }).exec()
  116. expect(deletedSubscriptions.length).to.equal(1)
  117. const deletedSubscription = deletedSubscriptions[0]
  118. expect(deletedSubscription.subscription.teamInvites).to.be.empty
  119. expect(deletedSubscription.subscription.invited_emails).to.be.empty
  120. expect(deletedSubscription.deleterData.deleterIpAddress).to.equal(
  121. deleterData.ip
  122. )
  123. if (deleterData.id) {
  124. expect(deletedSubscription.deleterData.deleterId.toString()).to.equal(
  125. deleterData.id.toString()
  126. )
  127. } else {
  128. expect(deletedSubscription.deleterData.deleterId).to.be.undefined
  129. }
  130. const subscription = await SubscriptionModel.findById(this._id).exec()
  131. expect(subscription).to.be.null
  132. }
  133. async addMember(userId) {
  134. return await SubscriptionModel.findOneAndUpdate(
  135. { _id: new ObjectId(this._id) },
  136. { $push: { member_ids: userId } }
  137. ).exec()
  138. }
  139. async inviteUser(adminUser, email) {
  140. await adminUser.login()
  141. return await adminUser.doRequest('POST', {
  142. url: `/manage/groups/${this._id}/invites`,
  143. json: {
  144. email,
  145. },
  146. })
  147. }
  148. }
  149. const Subscription = callbackifyClass(PromisifiedSubscription, {
  150. without: ['getCapabilities'],
  151. })
  152. Subscription.promises = class extends PromisifiedSubscription {}
  153. module.exports = Subscription