diff --git a/services/web/app/coffee/Features/Referal/ReferalFeatures.coffee b/services/web/app/coffee/Features/Referal/ReferalFeatures.coffee index a25adfc965..34651ef1f5 100644 --- a/services/web/app/coffee/Features/Referal/ReferalFeatures.coffee +++ b/services/web/app/coffee/Features/Referal/ReferalFeatures.coffee @@ -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" diff --git a/services/web/app/coffee/Features/Subscription/SubscriptionUpdater.coffee b/services/web/app/coffee/Features/Subscription/SubscriptionUpdater.coffee index 3b97702df7..7867db29b9 100644 --- a/services/web/app/coffee/Features/Subscription/SubscriptionUpdater.coffee +++ b/services/web/app/coffee/Features/Subscription/SubscriptionUpdater.coffee @@ -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 = diff --git a/services/web/test/unit/coffee/Subscription/SubscriptionUpdaterTests.coffee b/services/web/test/unit/coffee/Subscription/SubscriptionUpdaterTests.coffee index 40045ed09b..31ea6ebb0f 100644 --- a/services/web/test/unit/coffee/Subscription/SubscriptionUpdaterTests.coffee +++ b/services/web/test/unit/coffee/Subscription/SubscriptionUpdaterTests.coffee @@ -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