2014-02-12 05:23:40 -05:00
|
|
|
async = require("async")
|
|
|
|
_ = require("underscore")
|
|
|
|
SubscriptionUpdater = require("./SubscriptionUpdater")
|
|
|
|
SubscriptionLocator = require("./SubscriptionLocator")
|
2018-05-23 10:12:23 -04:00
|
|
|
UserGetter = require("../User/UserGetter")
|
2018-05-29 10:35:46 -04:00
|
|
|
Subscription = require("../../models/Subscription").Subscription
|
2014-02-12 05:23:40 -05:00
|
|
|
LimitationsManager = require("./LimitationsManager")
|
2015-05-27 11:33:47 -04:00
|
|
|
logger = require("logger-sharelatex")
|
2015-05-27 15:50:16 -04:00
|
|
|
OneTimeTokenHandler = require("../Security/OneTimeTokenHandler")
|
2018-05-30 06:29:21 -04:00
|
|
|
TeamInvitesHandler = require("./TeamInvitesHandler")
|
2015-05-27 15:50:16 -04:00
|
|
|
EmailHandler = require("../Email/EmailHandler")
|
|
|
|
settings = require("settings-sharelatex")
|
2016-02-18 06:43:43 -05:00
|
|
|
NotificationsBuilder = require("../Notifications/NotificationsBuilder")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2015-05-27 15:50:16 -04:00
|
|
|
module.exports = SubscriptionGroupHandler =
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-02-22 11:50:41 -05:00
|
|
|
addUserToGroup: (adminUserId, newEmail, callback)->
|
2016-03-09 06:17:03 -05:00
|
|
|
logger.log adminUserId:adminUserId, newEmail:newEmail, "adding user to group"
|
2017-06-08 07:12:08 -04:00
|
|
|
LimitationsManager.hasGroupMembersLimitReached adminUserId, (err, limitReached, subscription)->
|
2016-02-22 11:50:41 -05:00
|
|
|
if err?
|
2017-06-08 07:12:08 -04:00
|
|
|
logger.err err:err, adminUserId:adminUserId, newEmail:newEmail, "error checking if limit reached for group plan"
|
2016-02-22 11:50:41 -05:00
|
|
|
return callback(err)
|
2017-06-08 07:12:08 -04:00
|
|
|
if limitReached
|
|
|
|
logger.err adminUserId:adminUserId, newEmail:newEmail, "group subscription limit reached not adding user to group"
|
|
|
|
return callback(limitReached:limitReached)
|
2018-05-28 10:09:22 -04:00
|
|
|
UserGetter.getUserByAnyEmail newEmail, (err, user)->
|
2017-06-08 07:12:08 -04:00
|
|
|
return callback(err) if err?
|
|
|
|
if user?
|
|
|
|
SubscriptionUpdater.addUserToGroup adminUserId, user._id, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, "error adding user to group"
|
|
|
|
return callback(err)
|
|
|
|
NotificationsBuilder.groupPlan(user, {subscription_id:subscription._id}).read()
|
|
|
|
userViewModel = buildUserViewModel(user)
|
|
|
|
callback(err, userViewModel)
|
|
|
|
else
|
2018-06-07 10:35:18 -04:00
|
|
|
TeamInvitesHandler.createInvite adminUserId, newEmail, (err) ->
|
2017-06-08 07:12:08 -04:00
|
|
|
return callback(err) if err?
|
|
|
|
userViewModel = buildEmailInviteViewModel(newEmail)
|
|
|
|
callback(err, userViewModel)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
removeUserFromGroup: (adminUser_id, userToRemove_id, callback)->
|
|
|
|
SubscriptionUpdater.removeUserFromGroup adminUser_id, userToRemove_id, callback
|
2018-05-25 10:45:33 -04:00
|
|
|
|
|
|
|
replaceUserReferencesInGroups: (oldId, newId, callback) ->
|
2018-05-29 10:35:46 -04:00
|
|
|
Subscription.update {admin_id: oldId}, {admin_id: newId}, (error) ->
|
2018-05-25 10:45:33 -04:00
|
|
|
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.
|
2018-05-29 10:35:46 -04:00
|
|
|
query = { member_ids: oldId }
|
|
|
|
addNewUserUpdate = $addToSet: { member_ids: newId }
|
|
|
|
removeOldUserUpdate = $pull: { member_ids: oldId }
|
2018-05-25 10:45:33 -04:00
|
|
|
|
|
|
|
Subscription.update query, addNewUserUpdate, { multi: true }, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
Subscription.update query, removeOldUserUpdate, { multi: true }, callback
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
getPopulatedListOfMembers: (adminUser_id, callback)->
|
2018-05-25 10:45:33 -04:00
|
|
|
SubscriptionLocator.getUsersSubscription adminUser_id, (err, subscription)->
|
2014-02-12 05:23:40 -05:00
|
|
|
users = []
|
2018-05-30 06:29:21 -04:00
|
|
|
|
2017-06-08 07:12:08 -04:00
|
|
|
for email in subscription.invited_emails or []
|
|
|
|
users.push buildEmailInviteViewModel(email)
|
2018-06-07 11:22:38 -04:00
|
|
|
|
2018-05-31 06:54:50 -04:00
|
|
|
for teamInvite in subscription.teamInvites or []
|
|
|
|
users.push buildEmailInviteViewModel(teamInvite.email)
|
2018-05-30 06:29:21 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
jobs = _.map subscription.member_ids, (user_id)->
|
|
|
|
return (cb)->
|
2018-05-24 09:55:12 -04:00
|
|
|
UserGetter.getUser user_id, (err, user)->
|
2014-03-25 12:19:30 -04:00
|
|
|
if err? or !user?
|
|
|
|
users.push _id:user_id
|
|
|
|
return cb()
|
2014-02-12 05:23:40 -05:00
|
|
|
userViewModel = buildUserViewModel(user)
|
|
|
|
users.push(userViewModel)
|
|
|
|
cb()
|
|
|
|
async.series jobs, (err)->
|
|
|
|
callback(err, users)
|
|
|
|
|
2015-05-27 11:33:47 -04:00
|
|
|
isUserPartOfGroup: (user_id, subscription_id, callback=(err, partOfGroup)->)->
|
|
|
|
SubscriptionLocator.getSubscriptionByMemberIdAndId user_id, subscription_id, (err, subscription)->
|
|
|
|
if subscription?
|
|
|
|
partOfGroup = true
|
|
|
|
else
|
|
|
|
partOfGroup = false
|
|
|
|
logger.log user_id:user_id, subscription_id:subscription_id, partOfGroup:partOfGroup, "checking if user is part of a group"
|
|
|
|
callback(err, partOfGroup)
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
buildUserViewModel = (user)->
|
2018-05-25 10:45:33 -04:00
|
|
|
u =
|
2014-02-12 05:23:40 -05:00
|
|
|
email: user.email
|
|
|
|
first_name: user.first_name
|
|
|
|
last_name: user.last_name
|
2018-06-07 09:10:01 -04:00
|
|
|
invite: user.holdingAccount
|
2014-02-12 05:23:40 -05:00
|
|
|
_id: user._id
|
|
|
|
return u
|
2017-06-08 07:12:08 -04:00
|
|
|
|
|
|
|
buildEmailInviteViewModel = (email) ->
|
|
|
|
return {
|
|
|
|
email: email
|
2018-06-07 09:10:01 -04:00
|
|
|
invite: true
|
2018-05-25 10:45:33 -04:00
|
|
|
}
|