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

142 lines
5 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")
InstitutionsGetter = require("../Institutions/InstitutionsGetter")
PublishersGetter = require("../Publishers/PublishersGetter")
sanitizeHtml = require 'sanitize-html'
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)
]
memberGroupSubscriptions: (cb) ->
SubscriptionLocator.getMemberSubscriptions user, cb
managedGroupSubscriptions: (cb) ->
SubscriptionLocator.getManagedGroupSubscriptions user, cb
confirmedMemberInstitutions: (cb) ->
InstitutionsGetter.getConfirmedInstitutions user._id, cb
managedInstitutions: (cb) ->
InstitutionsGetter.getManagedInstitutions user._id, cb
managedPublishers: (cb) ->
PublishersGetter.getManagedPublishers user._id, cb
v1SubscriptionStatus: (cb) ->
V1SubscriptionManager.getSubscriptionStatusFromV1 user._id, (error, status, v1Id) ->
return cb(error) if error?
cb(null, status)
}, (err, results) ->
return callback(err) if err?
{
personalSubscription,
memberGroupSubscriptions,
managedGroupSubscriptions,
confirmedMemberInstitutions,
managedInstitutions,
managedPublishers,
v1SubscriptionStatus,
recurlySubscription,
plan
} = results
memberGroupSubscriptions ?= []
managedGroupSubscriptions ?= []
confirmedMemberInstitutions ?= []
managedInstitutions ?= []
v1SubscriptionStatus ?= {}
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
}
for memberGroupSubscription in memberGroupSubscriptions
if memberGroupSubscription.teamNotice
memberGroupSubscription.teamNotice = sanitizeHtml(memberGroupSubscription.teamNotice)
callback null, {
personalSubscription,
managedGroupSubscriptions,
memberGroupSubscriptions,
confirmedMemberInstitutions,
managedInstitutions,
managedPublishers,
v1SubscriptionStatus
}
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