overleaf/services/web/app/coffee/Features/Subscription/SubscriptionViewModelBuilder.coffee

108 lines
4 KiB
CoffeeScript
Raw Normal View History

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")
V1SubscriptionManager = require("./V1SubscriptionManager")
2016-08-31 05:40:30 -04:00
logger = require('logger-sharelatex')
2014-02-12 05:23:40 -05:00
_ = require("underscore")
async = require('async')
2014-02-12 05:23:40 -05:00
buildBillingDetails = (recurlySubscription) ->
hostedLoginToken = recurlySubscription?.account?.hosted_login_token
recurlySubdomain = Settings?.apis?.recurly?.subdomain
if hostedLoginToken? && recurlySubdomain?
return [
"https://",
recurlySubdomain,
".recurly.com/account/billing_info/edit?ht=",
hostedLoginToken
].join("")
module.exports =
buildUsersSubscriptionViewModel: (user, callback = (error, data) ->) ->
async.auto {
personalSubscription: (cb) ->
SubscriptionLocator.getUsersSubscription user, cb
recurlySubscription: ['personalSubscription', (cb, {personalSubscription}) ->
if !personalSubscription?.recurlySubscription_id? or personalSubscription?.recurlySubscription_id == ''
return cb(null, null)
RecurlyWrapper.getSubscription personalSubscription.recurlySubscription_id, includeAccount: true, cb
]
plan: ['personalSubscription', (cb, {personalSubscription}) ->
return cb() if !personalSubscription?
plan = PlansLocator.findLocalPlanInSettings(personalSubscription.planCode)
return cb(new Error("No plan found for planCode '#{personalSubscription.planCode}'")) if !plan?
cb(null, plan)
]
groupSubscriptions: (cb) ->
SubscriptionLocator.getMemberSubscriptions user, cb
v1Subscriptions: (cb) ->
V1SubscriptionManager.getSubscriptionsFromV1 user._id, (error, subscriptions, v1Id) ->
return cb(error) if error?
# Only return one argument to async.auto, otherwise it returns an array
cb(null, subscriptions)
}, (err, results) ->
return callback(err) if err?
{personalSubscription, groupSubscriptions, v1Subscriptions, recurlySubscription, plan} = results
groupSubscriptions ?= []
v1Subscriptions ?= {}
if personalSubscription?.toObject?
# Downgrade from Mongoose object, so we can add a recurly and plan attribute
personalSubscription = personalSubscription.toObject()
if plan?
personalSubscription.plan = plan
if personalSubscription? and recurlySubscription?
tax = recurlySubscription?.tax_in_cents || 0
personalSubscription.recurly = {
tax: tax
taxRate: parseFloat(recurlySubscription?.tax_rate?._)
billingDetailsLink: buildBillingDetails(recurlySubscription)
price: SubscriptionFormatters.formatPrice (recurlySubscription?.unit_amount_in_cents + tax), recurlySubscription?.currency
nextPaymentDueAt: SubscriptionFormatters.formatDate(recurlySubscription?.current_period_ends_at)
currency: recurlySubscription.currency
state: recurlySubscription.state
trialEndsAtFormatted: SubscriptionFormatters.formatDate(recurlySubscription?.trial_ends_at)
trial_ends_at: recurlySubscription.trial_ends_at
}
callback null, {
personalSubscription, groupSubscriptions, v1Subscriptions
}
2014-02-12 05:23:40 -05:00
buildViewModel : ->
plans = Settings.plans
allPlans = {}
plans.forEach (plan)->
allPlans[plan.planCode] = plan
2014-02-12 05:23:40 -05:00
result =
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
2014-02-12 05:23:40 -05:00
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