2014-02-12 05:23:40 -05:00
|
|
|
async = require("async")
|
|
|
|
_ = require("underscore")
|
|
|
|
Subscription = require('../../models/Subscription').Subscription
|
|
|
|
SubscriptionLocator = require("./SubscriptionLocator")
|
|
|
|
UserFeaturesUpdater = require("./UserFeaturesUpdater")
|
|
|
|
PlansLocator = require("./PlansLocator")
|
|
|
|
Settings = require("settings-sharelatex")
|
|
|
|
logger = require("logger-sharelatex")
|
2017-05-10 10:31:17 -04:00
|
|
|
ObjectId = require('mongoose').Types.ObjectId
|
2015-10-05 11:33:13 -04:00
|
|
|
ReferalAllocator = require("../Referal/ReferalAllocator")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
oneMonthInSeconds = 60 * 60 * 24 * 30
|
|
|
|
|
2016-03-31 11:58:16 -04:00
|
|
|
module.exports = SubscriptionUpdater =
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
syncSubscription: (recurlySubscription, adminUser_id, callback) ->
|
|
|
|
logger.log adminUser_id:adminUser_id, recurlySubscription:recurlySubscription, "syncSubscription, creating new if subscription does not exist"
|
2016-03-31 11:58:16 -04:00
|
|
|
SubscriptionLocator.getUsersSubscription adminUser_id, (err, subscription)->
|
2014-02-12 05:23:40 -05:00
|
|
|
if subscription?
|
|
|
|
logger.log adminUser_id:adminUser_id, recurlySubscription:recurlySubscription, "subscription does exist"
|
2016-03-31 11:58:16 -04:00
|
|
|
SubscriptionUpdater._updateSubscriptionFromRecurly recurlySubscription, subscription, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
|
|
|
logger.log adminUser_id:adminUser_id, recurlySubscription:recurlySubscription, "subscription does not exist, creating a new one"
|
2016-03-31 11:58:16 -04:00
|
|
|
SubscriptionUpdater._createNewSubscription adminUser_id, (err, subscription)->
|
|
|
|
SubscriptionUpdater._updateSubscriptionFromRecurly recurlySubscription, subscription, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
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)->
|
2016-02-22 11:01:17 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, searchOps:searchOps, insertOperation:insertOperation, "error findy and modify add user to group"
|
|
|
|
return callback(err)
|
2014-02-12 05:23:40 -05:00
|
|
|
UserFeaturesUpdater.updateFeatures user_id, subscription.planCode, callback
|
2017-06-08 07:12:08 -04:00
|
|
|
|
|
|
|
addEmailInviteToGroup: (adminUser_id, email, callback) ->
|
|
|
|
logger.log {adminUser_id, email}, "adding email into mongo subscription"
|
|
|
|
searchOps =
|
|
|
|
admin_id: adminUser_id
|
|
|
|
insertOperation =
|
|
|
|
"$addToSet": {invited_emails: email}
|
|
|
|
Subscription.findAndModify searchOps, insertOperation, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
removeUserFromGroup: (adminUser_id, user_id, callback)->
|
|
|
|
searchOps =
|
|
|
|
admin_id: adminUser_id
|
|
|
|
removeOperation =
|
|
|
|
"$pull": {member_ids:user_id}
|
2016-02-22 11:01:17 -05:00
|
|
|
Subscription.update searchOps, removeOperation, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, searchOps:searchOps, removeOperation:removeOperation, "error removing user from group"
|
|
|
|
return callback(err)
|
2016-03-31 11:58:16 -04:00
|
|
|
SubscriptionUpdater._setUsersMinimumFeatures user_id, callback
|
|
|
|
|
2017-06-08 07:12:08 -04:00
|
|
|
removeEmailInviteFromGroup: (adminUser_id, email, callback)->
|
|
|
|
Subscription.update {
|
|
|
|
admin_id: adminUser_id
|
|
|
|
}, "$pull": {
|
|
|
|
invited_emails: email
|
|
|
|
}, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2017-08-02 03:59:45 -04:00
|
|
|
deleteSubscription: (subscription_id, callback = (error) ->) ->
|
|
|
|
SubscriptionLocator.getSubscription subscription_id, (err, subscription) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
affected_user_ids = [subscription.admin_id].concat(subscription.member_ids or [])
|
|
|
|
logger.log {subscription_id, affected_user_ids}, "deleting subscription and downgrading users"
|
|
|
|
Subscription.remove {_id: ObjectId(subscription_id)}, (err) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
async.mapSeries affected_user_ids, SubscriptionUpdater._setUsersMinimumFeatures, callback
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
_createNewSubscription: (adminUser_id, callback)->
|
|
|
|
logger.log adminUser_id:adminUser_id, "creating new subscription"
|
|
|
|
subscription = new Subscription(admin_id:adminUser_id)
|
|
|
|
subscription.freeTrial.allowed = false
|
|
|
|
subscription.save (err)->
|
|
|
|
callback err, subscription
|
|
|
|
|
2016-03-31 11:58:16 -04:00
|
|
|
_updateSubscriptionFromRecurly: (recurlySubscription, subscription, callback)->
|
2014-02-12 05:23:40 -05:00
|
|
|
logger.log recurlySubscription:recurlySubscription, subscription:subscription, "updaing subscription"
|
|
|
|
plan = PlansLocator.findLocalPlanInSettings(recurlySubscription.plan.plan_code)
|
|
|
|
if recurlySubscription.state == "expired"
|
|
|
|
subscription.recurlySubscription_id = undefined
|
|
|
|
subscription.planCode = Settings.defaultPlanCode
|
|
|
|
else
|
|
|
|
subscription.recurlySubscription_id = recurlySubscription.uuid
|
|
|
|
subscription.freeTrial.expiresAt = undefined
|
|
|
|
subscription.freeTrial.planCode = undefined
|
|
|
|
subscription.freeTrial.allowed = true
|
|
|
|
subscription.planCode = recurlySubscription.plan.plan_code
|
|
|
|
if plan.groupPlan
|
|
|
|
subscription.groupPlan = true
|
|
|
|
subscription.membersLimit = plan.membersLimit
|
|
|
|
subscription.save ->
|
2016-03-31 12:36:06 -04:00
|
|
|
allIds = _.union subscription.member_ids, [subscription.admin_id]
|
2014-02-12 05:23:40 -05:00
|
|
|
jobs = allIds.map (user_id)->
|
|
|
|
return (cb)->
|
2016-03-31 11:58:16 -04:00
|
|
|
SubscriptionUpdater._setUsersMinimumFeatures user_id, cb
|
2015-10-05 11:33:13 -04:00
|
|
|
async.series jobs, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-03-31 11:58:16 -04:00
|
|
|
_setUsersMinimumFeatures: (user_id, callback)->
|
|
|
|
jobs =
|
|
|
|
subscription: (cb)->
|
|
|
|
SubscriptionLocator.getUsersSubscription user_id, cb
|
|
|
|
groupSubscription: (cb)->
|
|
|
|
SubscriptionLocator.getGroupSubscriptionMemberOf user_id, cb
|
|
|
|
async.series jobs, (err, results)->
|
2016-03-31 12:28:03 -04:00
|
|
|
if err?
|
|
|
|
logger.err err:err, user_id:user, "error getting subscription or group for _setUsersMinimumFeatures"
|
|
|
|
return callback(err)
|
2016-03-31 11:58:16 -04:00
|
|
|
{subscription, groupSubscription} = results
|
2016-03-31 13:04:20 -04:00
|
|
|
if subscription? and subscription.planCode? and subscription.planCode != Settings.defaultPlanCode
|
2016-03-31 12:28:03 -04:00
|
|
|
logger.log user_id:user_id, "using users subscription plan code for features"
|
2016-03-31 11:58:16 -04:00
|
|
|
UserFeaturesUpdater.updateFeatures user_id, subscription.planCode, callback
|
|
|
|
else if groupSubscription? and groupSubscription.planCode?
|
2016-03-31 12:28:03 -04:00
|
|
|
logger.log user_id:user_id, "using group which user is memor of for features"
|
2016-03-31 11:58:16 -04:00
|
|
|
UserFeaturesUpdater.updateFeatures user_id, groupSubscription.planCode, callback
|
|
|
|
else
|
2016-03-31 12:28:03 -04:00
|
|
|
logger.log user_id:user_id, "using default features for user with no subscription or group"
|
2016-03-31 11:58:16 -04:00
|
|
|
UserFeaturesUpdater.updateFeatures user_id, Settings.defaultPlanCode, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, user_id:user_id, "Error setting minimum user feature"
|
|
|
|
return callback(err)
|
|
|
|
ReferalAllocator.assignBonus user_id, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|