2014-02-12 05:23:40 -05:00
|
|
|
Settings = require('settings-sharelatex')
|
|
|
|
RecurlyWrapper = require("./RecurlyWrapper")
|
|
|
|
PlansLocator = require("./PlansLocator")
|
|
|
|
SubscriptionFormatters = require("./SubscriptionFormatters")
|
|
|
|
LimitationsManager = require("./LimitationsManager")
|
|
|
|
SubscriptionLocator = require("./SubscriptionLocator")
|
2016-08-31 05:40:30 -04:00
|
|
|
logger = require('logger-sharelatex')
|
2014-02-12 05:23:40 -05:00
|
|
|
_ = require("underscore")
|
|
|
|
|
|
|
|
module.exports =
|
|
|
|
|
2016-06-15 13:34:56 -04:00
|
|
|
buildUsersSubscriptionViewModel: (user, callback = (error, subscription, memberSubscriptions) ->) ->
|
2014-08-07 10:29:06 -04:00
|
|
|
SubscriptionLocator.getUsersSubscription user, (err, subscription) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
SubscriptionLocator.getMemberSubscriptions user, (err, memberSubscriptions = []) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
if subscription?
|
|
|
|
return callback(error) if error?
|
|
|
|
plan = PlansLocator.findLocalPlanInSettings(subscription.planCode)
|
2016-08-31 05:18:53 -04:00
|
|
|
if !plan?
|
2016-08-31 05:40:30 -04:00
|
|
|
err = new Error("No plan found for planCode '#{subscription.planCode}'")
|
|
|
|
logger.error {user_id: user._id, err}, "error getting subscription plan for user"
|
2016-08-31 05:18:53 -04:00
|
|
|
return callback(err)
|
2014-08-07 10:29:06 -04:00
|
|
|
RecurlyWrapper.getSubscription subscription.recurlySubscription_id, (err, recurlySubscription)->
|
2015-02-06 08:55:05 -05:00
|
|
|
tax = recurlySubscription?.tax_in_cents || 0
|
2014-08-07 10:29:06 -04:00
|
|
|
callback null, {
|
2016-06-15 13:34:56 -04:00
|
|
|
admin_id:subscription.admin_id
|
2014-08-07 10:29:06 -04:00
|
|
|
name: plan.name
|
2015-02-04 15:17:55 -05:00
|
|
|
nextPaymentDueAt: SubscriptionFormatters.formatDate(recurlySubscription?.current_period_ends_at)
|
|
|
|
state: recurlySubscription?.state
|
2015-02-06 08:55:05 -05:00
|
|
|
price: SubscriptionFormatters.formatPrice (recurlySubscription?.unit_amount_in_cents + tax), recurlySubscription?.currency
|
2014-08-07 10:29:06 -04:00
|
|
|
planCode: subscription.planCode
|
2015-02-04 15:17:55 -05:00
|
|
|
currency:recurlySubscription?.currency
|
2015-02-06 10:54:16 -05:00
|
|
|
taxRate:parseFloat(recurlySubscription?.tax_rate?._)
|
2014-08-07 10:29:06 -04:00
|
|
|
groupPlan: subscription.groupPlan
|
2015-12-08 13:23:28 -05:00
|
|
|
trial_ends_at:recurlySubscription?.trial_ends_at
|
2014-08-07 10:29:06 -04:00
|
|
|
}, memberSubscriptions
|
|
|
|
else
|
|
|
|
callback null, null, memberSubscriptions
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
buildViewModel : ->
|
|
|
|
plans = Settings.plans
|
|
|
|
|
2014-03-21 13:49:20 -04:00
|
|
|
allPlans = {}
|
|
|
|
plans.forEach (plan)->
|
|
|
|
allPlans[plan.planCode] = plan
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
result =
|
2014-03-21 13:49:20 -04:00
|
|
|
allPlans: allPlans
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
result.personalAccount = _.find plans, (plan)->
|
|
|
|
plan.planCode == "personal"
|
|
|
|
|
|
|
|
result.studentAccounts = _.filter plans, (plan)->
|
|
|
|
plan.planCode.indexOf("student") != -1
|
|
|
|
|
|
|
|
result.groupMonthlyPlans = _.filter plans, (plan)->
|
|
|
|
plan.groupPlan and !plan.annual
|
|
|
|
|
|
|
|
result.groupAnnualPlans = _.filter plans, (plan)->
|
|
|
|
plan.groupPlan and plan.annual
|
|
|
|
|
|
|
|
result.individualMonthlyPlans = _.filter plans, (plan)->
|
|
|
|
!plan.groupPlan and !plan.annual and plan.planCode != "personal" and plan.planCode.indexOf("student") == -1
|
|
|
|
|
|
|
|
result.individualAnnualPlans = _.filter plans, (plan)->
|
|
|
|
!plan.groupPlan and plan.annual and plan.planCode.indexOf("student") == -1
|
|
|
|
|
|
|
|
return result
|
|
|
|
|