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")
|
2018-06-04 12:55:02 -04:00
|
|
|
V1SubscriptionManager = require("./V1SubscriptionManager")
|
2018-11-20 05:48:21 -05:00
|
|
|
InstitutionsGetter = require("../Institutions/InstitutionsGetter")
|
2018-12-18 08:22:44 -05:00
|
|
|
PublishersGetter = require("../Publishers/PublishersGetter")
|
2016-08-31 05:40:30 -04:00
|
|
|
logger = require('logger-sharelatex')
|
2014-02-12 05:23:40 -05:00
|
|
|
_ = require("underscore")
|
2018-11-19 10:44:51 -05:00
|
|
|
async = require('async')
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
|
2018-01-05 11:25:01 -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 =
|
2018-11-19 10:44:51 -05:00
|
|
|
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)
|
|
|
|
]
|
2018-11-20 05:48:21 -05:00
|
|
|
memberGroupSubscriptions: (cb) ->
|
2018-11-19 10:44:51 -05:00
|
|
|
SubscriptionLocator.getMemberSubscriptions user, cb
|
2018-11-20 05:48:21 -05:00
|
|
|
managedGroupSubscriptions: (cb) ->
|
|
|
|
SubscriptionLocator.getManagedGroupSubscriptions user, cb
|
|
|
|
confirmedMemberInstitutions: (cb) ->
|
|
|
|
InstitutionsGetter.getConfirmedInstitutions user._id, cb
|
|
|
|
managedInstitutions: (cb) ->
|
|
|
|
InstitutionsGetter.getManagedInstitutions user._id, cb
|
2018-12-18 08:22:44 -05:00
|
|
|
managedPublishers: (cb) ->
|
|
|
|
PublishersGetter.getManagedPublishers user._id, cb
|
2018-11-30 08:03:35 -05:00
|
|
|
v1SubscriptionStatus: (cb) ->
|
|
|
|
V1SubscriptionManager.getSubscriptionStatusFromV1 user._id, (error, status, v1Id) ->
|
|
|
|
return cb(error) if error?
|
|
|
|
cb(null, status)
|
2018-11-19 10:44:51 -05:00
|
|
|
}, (err, results) ->
|
2014-08-07 10:29:06 -04:00
|
|
|
return callback(err) if err?
|
2018-11-20 05:48:21 -05:00
|
|
|
{
|
|
|
|
personalSubscription,
|
|
|
|
memberGroupSubscriptions,
|
|
|
|
managedGroupSubscriptions,
|
|
|
|
confirmedMemberInstitutions,
|
|
|
|
managedInstitutions,
|
2018-12-18 08:22:44 -05:00
|
|
|
managedPublishers,
|
2018-11-30 08:03:35 -05:00
|
|
|
v1SubscriptionStatus,
|
2018-11-20 05:48:21 -05:00
|
|
|
recurlySubscription,
|
|
|
|
plan
|
|
|
|
} = results
|
|
|
|
memberGroupSubscriptions ?= []
|
|
|
|
managedGroupSubscriptions ?= []
|
|
|
|
confirmedMemberInstitutions ?= []
|
|
|
|
managedInstitutions ?= []
|
2018-11-30 08:03:35 -05:00
|
|
|
v1SubscriptionStatus ?= {}
|
2018-11-19 10:44:51 -05:00
|
|
|
|
2018-11-20 05:48:21 -05:00
|
|
|
|
2018-11-19 10:44:51 -05:00
|
|
|
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, {
|
2018-11-20 05:48:21 -05:00
|
|
|
personalSubscription,
|
|
|
|
managedGroupSubscriptions,
|
|
|
|
memberGroupSubscriptions,
|
|
|
|
confirmedMemberInstitutions,
|
|
|
|
managedInstitutions,
|
2018-12-18 08:22:44 -05:00
|
|
|
managedPublishers,
|
2018-11-30 08:03:35 -05:00
|
|
|
v1SubscriptionStatus
|
2018-11-19 10:44:51 -05:00
|
|
|
}
|
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
|
2016-10-13 08:47:05 -04:00
|
|
|
|
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
|
2016-10-13 08:47:05 -04:00
|
|
|
|
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
|