Ensure features are updated for users but not for stubs

This commit is contained in:
Alberto Fernández Capel 2018-06-28 13:25:04 +01:00
parent 4366a0ea2c
commit 193579070c
3 changed files with 20 additions and 17 deletions

View file

@ -1,11 +1,12 @@
_ = require("underscore")
logger = require('logger-sharelatex')
UserGetter = require('../User/UserGetter')
User = require('../../models/User').User
Settings = require "settings-sharelatex"
module.exports = ReferalFeatures =
getBonusFeatures: (user_id, callback = (error) ->) ->
UserGetter.getUserOrUserStubById user_id, null, (error, user) ->
query = _id: user_id
User.findOne query, (error, user) ->
return callback(error) if error
return callback(new Error("user not found #{user_id} for assignBonus")) if !user?
logger.log user_id: user_id, refered_user_count: user.refered_user_count, "assigning bonus"

View file

@ -2,6 +2,7 @@ async = require("async")
_ = require("underscore")
Subscription = require('../../models/Subscription').Subscription
SubscriptionLocator = require("./SubscriptionLocator")
UserGetter = require("../User/UserGetter")
PlansLocator = require("./PlansLocator")
Settings = require("settings-sharelatex")
logger = require("logger-sharelatex")
@ -24,26 +25,26 @@ module.exports = SubscriptionUpdater =
return callback(err) if err?
SubscriptionUpdater._updateSubscriptionFromRecurly recurlySubscription, subscription, callback
addUsersToGroup: (subscriptionId, memberIds, callback)->
logger.log subscriptionId: subscriptionId, memberIds: memberIds, "adding members into mongo subscription"
addUserToGroup: (adminUserId, userId, callback)->
@addUsersToGroup(adminUserId, [userId], callback)
addUsersToGroup: (adminUserId, memberIds, callback)->
logger.log adminUserId: adminUserId, memberIds: memberIds, "adding members into mongo subscription"
searchOps =
_id: new ObjectId(subscriptionId.toString())
admin_id: adminUserId
insertOperation =
{ $push: { member_ids: { $each: memberIds } } }
Subscription.findAndModify searchOps, insertOperation, callback
Subscription.findAndModify searchOps, insertOperation, (err, subscription) ->
return callback(err) if err?
# Only apply features updates to users, not user stubs
UserGetter.getUsers memberIds, { _id: 1 }, (err, users) ->
return callback(err) if err?
userIds = users.map (u) -> u._id.toString()
async.map userIds, FeaturesUpdater.refreshFeatures, callback
addUserToGroup: (adminUser_id, user_id, callback)->
logger.log adminUser_id:adminUser_id, user_id:user_id, "adding user into mongo subscription"
searchOps =
admin_id: adminUser_id
insertOperation =
"$addToSet": {member_ids:user_id}
Subscription.findAndModify searchOps, insertOperation, (err, subscription)->
if err?
logger.err err:err, searchOps:searchOps, insertOperation:insertOperation, "error findy and modify add user to group"
return callback(err)
FeaturesUpdater.refreshFeatures user_id, callback
removeUserFromGroup: (adminUser_id, user_id, callback)->
searchOps =

View file

@ -64,6 +64,7 @@ describe "SubscriptionUpdater", ->
'../../models/Subscription': Subscription:@SubscriptionModel
'./UserFeaturesUpdater': @UserFeaturesUpdater
'./SubscriptionLocator': @SubscriptionLocator
'../User/UserGetter': @UserGetter
'./PlansLocator': @PlansLocator
"logger-sharelatex": log:->
'settings-sharelatex': @Settings