| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- const { db, ObjectId } = require('../../../../app/src/infrastructure/mongodb')
- const { expect } = require('chai')
- const SubscriptionUpdater = require('../../../../app/src/Features/Subscription/SubscriptionUpdater')
- const ManagedUsersHandler = require('../../../../app/src/Features/Subscription/ManagedUsersHandler')
- const SubscriptionModel =
- require('../../../../app/src/models/Subscription').Subscription
- const DeletedSubscriptionModel =
- require(`../../../../app/src/models/DeletedSubscription`).DeletedSubscription
- class Subscription {
- constructor(options = {}) {
- this.admin_id = options.adminId || ObjectId()
- this.overleaf = options.overleaf || {}
- this.groupPlan = options.groupPlan
- this.manager_ids = options.managerIds || [this.admin_id]
- this.member_ids = options.memberIds || []
- this.invited_emails = options.invitedEmails || []
- this.teamName = options.teamName
- this.teamInvites = options.teamInvites || []
- this.planCode = options.planCode
- this.recurlySubscription_id = options.recurlySubscription_id
- }
- ensureExists(callback) {
- if (this._id) {
- return callback(null)
- }
- const options = { upsert: true, new: true, setDefaultsOnInsert: true }
- SubscriptionModel.findOneAndUpdate(
- { admin_id: this.admin_id },
- this,
- options,
- (error, subscription) => {
- if (error) {
- return callback(error)
- }
- this._id = subscription._id
- callback()
- }
- )
- }
- get(callback) {
- db.subscriptions.findOne({ _id: ObjectId(this._id) }, callback)
- }
- getWithGroupPolicy(callback) {
- SubscriptionModel.findById(this._id).populate('groupPolicy').exec(callback)
- }
- setManagerIds(managerIds, callback) {
- return SubscriptionModel.findOneAndUpdate(
- { _id: ObjectId(this._id) },
- { manager_ids: managerIds },
- callback
- )
- }
- refreshUsersFeatures(callback) {
- SubscriptionUpdater.refreshUsersFeatures(this, callback)
- }
- enableManagedUsers(callback) {
- ManagedUsersHandler.enableManagedUsers(this._id, callback)
- }
- getGroupPolicyForUser(user, callback) {
- ManagedUsersHandler.getGroupPolicyForUser(user, callback)
- }
- enrollManagedUser(user, callback) {
- SubscriptionModel.findById(this._id).exec((error, subscription) => {
- if (error) {
- return callback(error)
- }
- ManagedUsersHandler.enrollInSubscription(user._id, subscription, callback)
- })
- }
- expectDeleted(deleterData, callback) {
- DeletedSubscriptionModel.find(
- { 'subscription._id': this._id },
- (error, deletedSubscriptions) => {
- if (error) {
- return callback(error)
- }
- expect(deletedSubscriptions.length).to.equal(1)
- const deletedSubscription = deletedSubscriptions[0]
- expect(deletedSubscription.subscription.teamInvites).to.be.empty
- expect(deletedSubscription.subscription.invited_emails).to.be.empty
- expect(deletedSubscription.deleterData.deleterIpAddress).to.equal(
- deleterData.ip
- )
- if (deleterData.id) {
- expect(deletedSubscription.deleterData.deleterId.toString()).to.equal(
- deleterData.id.toString()
- )
- } else {
- expect(deletedSubscription.deleterData.deleterId).to.be.undefined
- }
- SubscriptionModel.findById(this._id, (error, subscription) => {
- expect(subscription).to.be.null
- callback(error)
- })
- }
- )
- }
- }
- module.exports = Subscription
|