Use the admin_ids array to query for subscriptions

This commit is contained in:
Alberto Fernández Capel 2018-06-29 17:01:34 +01:00
parent abcfa0b0d2
commit 46a1cdc510
6 changed files with 62 additions and 35 deletions

View file

@ -9,9 +9,6 @@ module.exports = SubscriptionDomainHandler =
licence = SubscriptionDomainHandler._findDomainLicence(user.email)
return licence
rejectInvitationToGroup: (user, subscription, callback)->
removeUserFromGroup(subscription.admin_id, user._id, callback)
getDomainLicencePage: (user)->
licence = SubscriptionDomainHandler._findDomainLicence(user.email)
if licence?.verifyEmail

View file

@ -46,15 +46,10 @@ module.exports = SubscriptionGroupHandler =
Subscription.update {admin_id: oldId}, {admin_id: newId}, (error) ->
callback(error) if error?
# Mongo won't let us pull and addToSet in the same query, so do it in
# two. Note we need to add first, since the query is based on the old user.
query = { member_ids: oldId }
addNewUserUpdate = $addToSet: { member_ids: newId }
removeOldUserUpdate = $pull: { member_ids: oldId }
replaceInArray Subscription, "manager_ids", oldId, newId, (error) ->
callback(error) if error?
Subscription.update query, addNewUserUpdate, { multi: true }, (error) ->
return callback(error) if error?
Subscription.update query, removeOldUserUpdate, { multi: true }, callback
replaceInArray Subscription, "member_ids", oldId, newId, callback
getPopulatedListOfMembers: (subscriptionId, callback)->
SubscriptionLocator.getSubscription subscriptionId, (err, subscription)->
@ -87,6 +82,24 @@ module.exports = SubscriptionGroupHandler =
logger.log user_id:user_id, subscription_id:subscription_id, partOfGroup:partOfGroup, "checking if user is part of a group"
callback(err, partOfGroup)
replaceInArray = (model, property, oldValue, newValue, callback) ->
logger.log "Replacing #{oldValue} with #{newValue} in #{property} of #{model}"
# Mongo won't let us pull and addToSet in the same query, so do it in
# two. Note we need to add first, since the query is based on the old user.
query = {}
query[property] = oldValue
setNewValue = {}
setNewValue[property] = newValue
setOldValue = {}
setOldValue[property] = oldValue
model.update query, { $addToSet: setNewValue }, { multi: true }, (error) ->
return callback(error) if error?
model.update query, { $pull: setOldValue }, { multi: true }, callback
buildUserViewModel = (user)->
u =
email: user.email

View file

@ -2,7 +2,7 @@ Subscription = require('../../models/Subscription').Subscription
logger = require("logger-sharelatex")
ObjectId = require('mongoose').Types.ObjectId
module.exports =
module.exports = SubscriptionLocator =
getUsersSubscription: (user_or_id, callback)->
if user_or_id? and user_or_id._id?
@ -15,8 +15,7 @@ module.exports =
callback(err, subscription)
getManagedSubscription: (managerId, callback)->
logger.log managerId: managerId, "getting managed subscription"
Subscription.findOne admin_id: managerId, (err, subscription)->
SubscriptionLocator.findManagedSubscription managerId, (err, subscription)->
if subscription?
logger.log managerId: managerId, "got managed subscription"
else
@ -24,6 +23,10 @@ module.exports =
callback(err, subscription)
findManagedSubscription: (managerId, callback)->
logger.log managerId: managerId, "finding managed subscription"
Subscription.findOne manager_ids: managerId, callback
getMemberSubscriptions: (user_or_id, callback) ->
if user_or_id? and user_or_id._id?
user_id = user_or_id._id

View file

@ -39,6 +39,7 @@ describe "FeatureUpdater.refreshFeatures", ->
beforeEach ->
Subscription.create {
admin_id: @user._id
manager_ids: [@user._id]
planCode: 'collaborator'
customAccount: true
} # returns a promise
@ -163,6 +164,7 @@ describe "FeatureUpdater.refreshFeatures", ->
beforeEach (done) ->
Subscription.create {
admin_id: @user._id
manager_ids: [@user._id]
planCode: 'professional'
customAccount: true
}, (error) =>

View file

@ -16,7 +16,8 @@ describe "SubscriptionGroupHandler", ->
@subscription_id = "31DSd1123D"
@subscription =
admin_id:@adminUser_id
admin_id: @adminUser_id
manager_ids: [@adminUser_id]
_id:@subscription_id
@SubscriptionLocator =
@ -124,23 +125,28 @@ describe "SubscriptionGroupHandler", ->
done()
describe "replaceUserReferencesInGroups", ->
beforeEach ->
beforeEach (done)->
@oldId = "ba5eba11"
@newId = "5ca1ab1e"
@Handler.replaceUserReferencesInGroups @oldId, @newId, ->
done()
it "replaces the admin_id", (done) ->
@Handler.replaceUserReferencesInGroups @oldId, @newId, (err) =>
it "replaces the admin_id", ->
@Subscription.update.calledWith(
{ admin_id: @oldId },
{ admin_id: @newId }
).should.equal true
done()
it "replaces the manager_ids", ->
@Subscription.update.calledWith(
{manager_ids:"ba5eba11"},{$addToSet:{manager_ids:"5ca1ab1e"}},{multi:true}
).should.equal true
it "replaces the member ids", (done) ->
@Handler.replaceUserReferencesInGroups @oldId, @newId, (err) =>
@Subscription.update.calledWith(
{manager_ids:"ba5eba11"},{$pull:{manager_ids:"ba5eba11"}},{multi:true}
).should.equal true
it "replaces the member ids", ->
@Subscription.update.calledWith(
{ member_ids: @oldId },
{ $addToSet: { member_ids: @newId } }
@ -151,8 +157,6 @@ describe "SubscriptionGroupHandler", ->
{ $pull: { member_ids: @oldId } }
).should.equal true
done()
describe "getPopulatedListOfMembers", ->
beforeEach ->
@subscription = {}

View file

@ -41,3 +41,11 @@ describe "Subscription Locator Tests", ->
subscription.should.equal @subscription
done()
describe "finding managed subscription", ->
it "should query the database", (done)->
@Subscription.findOne.callsArgWith(1, null, @subscription)
@SubscriptionLocator.getManagedSubscription @user._id, (err, subscription)=>
@Subscription.findOne.calledWith({"manager_ids":@user._id}).should.equal true
subscription.should.equal @subscription
done()