2014-02-12 05:23:40 -05:00
|
|
|
async = require("async")
|
|
|
|
_ = require("underscore")
|
|
|
|
Subscription = require('../../models/Subscription').Subscription
|
|
|
|
SubscriptionLocator = require("./SubscriptionLocator")
|
2018-06-28 08:25:04 -04:00
|
|
|
UserGetter = require("../User/UserGetter")
|
2014-02-12 05:23:40 -05:00
|
|
|
PlansLocator = require("./PlansLocator")
|
|
|
|
Settings = require("settings-sharelatex")
|
|
|
|
logger = require("logger-sharelatex")
|
2018-05-21 10:06:01 -04:00
|
|
|
ObjectId = require('mongoose').Types.ObjectId
|
|
|
|
FeaturesUpdater = require('./FeaturesUpdater')
|
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)->
|
2018-05-21 10:06:01 -04:00
|
|
|
return callback(err) if err?
|
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)->
|
2018-05-21 10:06:01 -04:00
|
|
|
return callback(err) if err?
|
2016-03-31 11:58:16 -04:00
|
|
|
SubscriptionUpdater._updateSubscriptionFromRecurly recurlySubscription, subscription, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-07-11 04:31:57 -04:00
|
|
|
addUserToGroup: (subscriptionId, userId, callback)->
|
|
|
|
@addUsersToGroup(subscriptionId, [userId], callback)
|
2018-06-28 08:25:04 -04:00
|
|
|
|
2018-07-11 04:31:57 -04:00
|
|
|
addUsersToGroup: (subscriptionId, memberIds, callback)->
|
|
|
|
logger.log subscriptionId: subscriptionId, memberIds: memberIds, "adding members into mongo subscription"
|
2018-06-27 07:09:58 -04:00
|
|
|
searchOps =
|
2018-07-11 04:31:57 -04:00
|
|
|
_id: subscriptionId
|
2018-06-27 07:09:58 -04:00
|
|
|
insertOperation =
|
|
|
|
{ $push: { member_ids: { $each: memberIds } } }
|
|
|
|
|
2018-06-28 08:25:04 -04:00
|
|
|
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
|
2018-06-27 07:09:58 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-07-11 04:31:57 -04:00
|
|
|
removeUserFromGroup: (subscriptionId, user_id, callback)->
|
2018-06-01 06:23:25 -04:00
|
|
|
searchOps =
|
2018-07-11 04:31:57 -04:00
|
|
|
_id: subscriptionId
|
2018-06-01 06:23:25 -04:00
|
|
|
removeOperation =
|
2014-02-12 05:23:40 -05:00
|
|
|
"$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)
|
2018-05-21 10:06:01 -04:00
|
|
|
FeaturesUpdater.refreshFeatures user_id, callback
|
2016-03-31 11:58:16 -04:00
|
|
|
|
2018-06-27 07:09:58 -04:00
|
|
|
deleteWithV1Id: (v1TeamId, callback)->
|
|
|
|
Subscription.deleteOne { "overleaf.id": v1TeamId }, callback
|
|
|
|
|
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?
|
2018-05-21 10:06:01 -04:00
|
|
|
async.mapSeries affected_user_ids, FeaturesUpdater.refreshFeatures, callback
|
2017-08-02 03:59:45 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
_createNewSubscription: (adminUser_id, callback)->
|
|
|
|
logger.log adminUser_id:adminUser_id, "creating new subscription"
|
2018-06-29 06:22:58 -04:00
|
|
|
subscription = new Subscription(admin_id:adminUser_id, manager_ids: [adminUser_id])
|
2014-02-12 05:23:40 -05:00
|
|
|
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"
|
|
|
|
if recurlySubscription.state == "expired"
|
2018-07-10 05:31:25 -04:00
|
|
|
return SubscriptionUpdater.deleteSubscription subscription._id, callback
|
|
|
|
subscription.recurlySubscription_id = recurlySubscription.uuid
|
|
|
|
subscription.freeTrial.expiresAt = undefined
|
|
|
|
subscription.freeTrial.planCode = undefined
|
|
|
|
subscription.freeTrial.allowed = true
|
|
|
|
subscription.planCode = recurlySubscription.plan.plan_code
|
|
|
|
plan = PlansLocator.findLocalPlanInSettings(subscription.planCode)
|
|
|
|
if !plan?
|
|
|
|
return callback(new Error("plan code not found: #{subscription.planCode}"))
|
2014-02-12 05:23:40 -05:00
|
|
|
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)->
|
2018-05-21 10:06:01 -04:00
|
|
|
FeaturesUpdater.refreshFeatures user_id, cb
|
2015-10-05 11:33:13 -04:00
|
|
|
async.series jobs, callback
|