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")
|
|
|
|
_ = require("underscore")
|
|
|
|
|
|
|
|
module.exports =
|
|
|
|
|
2014-08-07 10:29:06 -04:00
|
|
|
buildUsersSubscriptionViewModel: (user, callback = (error, subscription, groups) ->) ->
|
|
|
|
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)
|
|
|
|
RecurlyWrapper.getSubscription subscription.recurlySubscription_id, (err, recurlySubscription)->
|
|
|
|
callback null, {
|
|
|
|
name: plan.name
|
|
|
|
nextPaymentDueAt: SubscriptionFormatters.formatDate(recurlySubscription.current_period_ends_at)
|
|
|
|
state: recurlySubscription.state
|
2014-09-08 05:59:12 -04:00
|
|
|
price: SubscriptionFormatters.formatPrice recurlySubscription.unit_amount_in_cents, recurlySubscription.currency
|
2014-08-07 10:29:06 -04:00
|
|
|
planCode: subscription.planCode
|
2014-10-13 11:14:13 -04:00
|
|
|
currency:recurlySubscription.currency
|
2014-08-07 10:29:06 -04:00
|
|
|
groupPlan: subscription.groupPlan
|
|
|
|
}, 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
|
|
|
|
|