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

89 lines
3.1 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")
2016-08-31 05:40:30 -04:00
logger = require('logger-sharelatex')
2014-02-12 05:23:40 -05:00
_ = require("underscore")
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, subscription, memberSubscriptions, billingDetailsLink) ->) ->
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)
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"
return callback(err)
RecurlyWrapper.getSubscription subscription.recurlySubscription_id, includeAccount: true, (err, recurlySubscription)->
tax = recurlySubscription?.tax_in_cents || 0
callback null, {
admin_id:subscription.admin_id
name: plan.name
nextPaymentDueAt: SubscriptionFormatters.formatDate(recurlySubscription?.current_period_ends_at)
state: recurlySubscription?.state
price: SubscriptionFormatters.formatPrice (recurlySubscription?.unit_amount_in_cents + tax), recurlySubscription?.currency
planCode: subscription.planCode
currency:recurlySubscription?.currency
2015-02-06 10:54:16 -05:00
taxRate:parseFloat(recurlySubscription?.tax_rate?._)
groupPlan: subscription.groupPlan
trial_ends_at:recurlySubscription?.trial_ends_at
}, memberSubscriptions, buildBillingDetails(recurlySubscription)
else
callback null, null, memberSubscriptions, null
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