2019-05-29 05:21:06 -04:00
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const RecurlyWrapper = require('./RecurlyWrapper')
|
|
|
|
const PlansLocator = require('./PlansLocator')
|
|
|
|
const SubscriptionFormatters = require('./SubscriptionFormatters')
|
|
|
|
const SubscriptionLocator = require('./SubscriptionLocator')
|
|
|
|
const V1SubscriptionManager = require('./V1SubscriptionManager')
|
|
|
|
const InstitutionsGetter = require('../Institutions/InstitutionsGetter')
|
|
|
|
const PublishersGetter = require('../Publishers/PublishersGetter')
|
|
|
|
const sanitizeHtml = require('sanitize-html')
|
|
|
|
const _ = require('underscore')
|
|
|
|
const async = require('async')
|
|
|
|
|
2021-02-22 11:36:18 -05:00
|
|
|
function buildHostedLink(recurlySubscription, type) {
|
2019-07-15 09:09:04 -04:00
|
|
|
const recurlySubdomain = Settings.apis.recurly.subdomain
|
2019-08-20 08:44:59 -04:00
|
|
|
const hostedLoginToken = recurlySubscription.account.hosted_login_token
|
|
|
|
let path = ''
|
|
|
|
if (type === 'billingDetails') {
|
|
|
|
path = 'billing_info/edit?ht='
|
|
|
|
}
|
|
|
|
if (hostedLoginToken && recurlySubdomain) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return [
|
|
|
|
'https://',
|
|
|
|
recurlySubdomain,
|
2019-08-20 08:44:59 -04:00
|
|
|
'.recurly.com/account/',
|
|
|
|
path,
|
2021-04-27 03:52:58 -04:00
|
|
|
hostedLoginToken,
|
2019-05-29 05:21:06 -04:00
|
|
|
].join('')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
buildUsersSubscriptionViewModel(user, callback) {
|
2021-02-22 11:36:18 -05:00
|
|
|
async.auto(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
personalSubscription(cb) {
|
2021-02-22 11:36:18 -05:00
|
|
|
SubscriptionLocator.getUsersSubscription(user, cb)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
recurlySubscription: [
|
|
|
|
'personalSubscription',
|
2021-02-22 11:36:18 -05:00
|
|
|
(cb, { personalSubscription }) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (
|
2021-02-22 11:36:18 -05:00
|
|
|
personalSubscription == null ||
|
|
|
|
personalSubscription.recurlySubscription_id == null ||
|
|
|
|
personalSubscription.recurlySubscription_id === ''
|
2019-05-29 05:21:06 -04:00
|
|
|
) {
|
|
|
|
return cb(null, null)
|
|
|
|
}
|
2021-02-22 11:36:18 -05:00
|
|
|
RecurlyWrapper.getSubscription(
|
2019-05-29 05:21:06 -04:00
|
|
|
personalSubscription.recurlySubscription_id,
|
|
|
|
{ includeAccount: true },
|
|
|
|
cb
|
|
|
|
)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
],
|
|
|
|
recurlyCoupons: [
|
|
|
|
'recurlySubscription',
|
2021-02-22 11:36:18 -05:00
|
|
|
(cb, { recurlySubscription }) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (!recurlySubscription) {
|
|
|
|
return cb(null, null)
|
|
|
|
}
|
|
|
|
const accountId = recurlySubscription.account.account_code
|
2021-02-22 11:36:18 -05:00
|
|
|
RecurlyWrapper.getAccountActiveCoupons(accountId, cb)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
],
|
|
|
|
plan: [
|
|
|
|
'personalSubscription',
|
2021-02-22 11:36:18 -05:00
|
|
|
(cb, { personalSubscription }) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (personalSubscription == null) {
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
const plan = PlansLocator.findLocalPlanInSettings(
|
|
|
|
personalSubscription.planCode
|
|
|
|
)
|
|
|
|
if (plan == null) {
|
|
|
|
return cb(
|
|
|
|
new Error(
|
2020-12-15 05:23:54 -05:00
|
|
|
`No plan found for planCode '${personalSubscription.planCode}'`
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2021-02-22 11:36:18 -05:00
|
|
|
cb(null, plan)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
],
|
|
|
|
memberGroupSubscriptions(cb) {
|
2021-02-22 11:36:18 -05:00
|
|
|
SubscriptionLocator.getMemberSubscriptions(user, cb)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
managedGroupSubscriptions(cb) {
|
2021-02-22 11:36:18 -05:00
|
|
|
SubscriptionLocator.getManagedGroupSubscriptions(user, cb)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2020-02-20 11:07:54 -05:00
|
|
|
confirmedMemberAffiliations(cb) {
|
2021-02-22 11:36:18 -05:00
|
|
|
InstitutionsGetter.getConfirmedAffiliations(user._id, cb)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
managedInstitutions(cb) {
|
2021-02-22 11:36:18 -05:00
|
|
|
InstitutionsGetter.getManagedInstitutions(user._id, cb)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
managedPublishers(cb) {
|
2021-02-22 11:36:18 -05:00
|
|
|
PublishersGetter.getManagedPublishers(user._id, cb)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
v1SubscriptionStatus(cb) {
|
2021-02-22 11:36:18 -05:00
|
|
|
V1SubscriptionManager.getSubscriptionStatusFromV1(
|
2019-05-29 05:21:06 -04:00
|
|
|
user._id,
|
2021-02-22 11:36:18 -05:00
|
|
|
(error, status, v1Id) => {
|
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return cb(error)
|
|
|
|
}
|
2021-02-22 11:36:18 -05:00
|
|
|
cb(null, status)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2021-02-22 11:36:18 -05:00
|
|
|
(err, results) => {
|
|
|
|
if (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
let {
|
|
|
|
personalSubscription,
|
|
|
|
memberGroupSubscriptions,
|
|
|
|
managedGroupSubscriptions,
|
2020-02-20 11:07:54 -05:00
|
|
|
confirmedMemberAffiliations,
|
2019-05-29 05:21:06 -04:00
|
|
|
managedInstitutions,
|
|
|
|
managedPublishers,
|
|
|
|
v1SubscriptionStatus,
|
|
|
|
recurlySubscription,
|
|
|
|
recurlyCoupons,
|
2021-04-27 03:52:58 -04:00
|
|
|
plan,
|
2019-05-29 05:21:06 -04:00
|
|
|
} = results
|
|
|
|
if (memberGroupSubscriptions == null) {
|
|
|
|
memberGroupSubscriptions = []
|
|
|
|
}
|
|
|
|
if (managedGroupSubscriptions == null) {
|
|
|
|
managedGroupSubscriptions = []
|
|
|
|
}
|
2020-02-20 11:07:54 -05:00
|
|
|
if (confirmedMemberAffiliations == null) {
|
|
|
|
confirmedMemberAffiliations = []
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
if (managedInstitutions == null) {
|
|
|
|
managedInstitutions = []
|
|
|
|
}
|
|
|
|
if (v1SubscriptionStatus == null) {
|
|
|
|
v1SubscriptionStatus = {}
|
|
|
|
}
|
|
|
|
if (recurlyCoupons == null) {
|
|
|
|
recurlyCoupons = []
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2021-02-22 11:36:18 -05:00
|
|
|
personalSubscription &&
|
|
|
|
typeof personalSubscription.toObject === 'function'
|
2019-05-29 05:21:06 -04:00
|
|
|
) {
|
|
|
|
// Downgrade from Mongoose object, so we can add a recurly and plan attribute
|
|
|
|
personalSubscription = personalSubscription.toObject()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (plan != null) {
|
|
|
|
personalSubscription.plan = plan
|
|
|
|
}
|
|
|
|
|
2021-02-22 11:36:18 -05:00
|
|
|
if (personalSubscription && recurlySubscription) {
|
|
|
|
const tax = recurlySubscription.tax_in_cents || 0
|
2021-02-18 06:47:17 -05:00
|
|
|
// Some plans allow adding more seats than the base plan provides.
|
|
|
|
// This is recorded as a subscription add on.
|
|
|
|
// Note: tax_in_cents already includes the tax for any addon.
|
|
|
|
let addOnPrice = 0
|
2021-02-22 11:36:18 -05:00
|
|
|
let additionalLicenses = 0
|
2021-02-18 06:47:17 -05:00
|
|
|
if (
|
|
|
|
plan.membersLimitAddOn &&
|
|
|
|
Array.isArray(recurlySubscription.subscription_add_ons)
|
|
|
|
) {
|
|
|
|
recurlySubscription.subscription_add_ons.forEach(addOn => {
|
|
|
|
if (addOn.add_on_code === plan.membersLimitAddOn) {
|
|
|
|
addOnPrice += addOn.quantity * addOn.unit_amount_in_cents
|
2021-02-22 11:36:18 -05:00
|
|
|
additionalLicenses += addOn.quantity
|
2021-02-18 06:47:17 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-02-22 11:36:18 -05:00
|
|
|
const totalLicenses = (plan.membersLimit || 0) + additionalLicenses
|
2019-05-29 05:21:06 -04:00
|
|
|
personalSubscription.recurly = {
|
|
|
|
tax,
|
|
|
|
taxRate: parseFloat(
|
2021-02-22 11:36:18 -05:00
|
|
|
recurlySubscription.tax_rate && recurlySubscription.tax_rate._
|
2019-05-29 05:21:06 -04:00
|
|
|
),
|
2019-08-20 08:44:59 -04:00
|
|
|
billingDetailsLink: buildHostedLink(
|
|
|
|
recurlySubscription,
|
|
|
|
'billingDetails'
|
|
|
|
),
|
|
|
|
accountManagementLink: buildHostedLink(recurlySubscription),
|
2019-05-29 05:21:06 -04:00
|
|
|
price: SubscriptionFormatters.formatPrice(
|
2021-02-22 11:36:18 -05:00
|
|
|
recurlySubscription.unit_amount_in_cents + addOnPrice + tax,
|
|
|
|
recurlySubscription.currency
|
2019-05-29 05:21:06 -04:00
|
|
|
),
|
2021-02-22 11:36:18 -05:00
|
|
|
additionalLicenses,
|
|
|
|
totalLicenses,
|
2019-05-29 05:21:06 -04:00
|
|
|
nextPaymentDueAt: SubscriptionFormatters.formatDate(
|
2021-02-22 11:36:18 -05:00
|
|
|
recurlySubscription.current_period_ends_at
|
2019-05-29 05:21:06 -04:00
|
|
|
),
|
|
|
|
currency: recurlySubscription.currency,
|
|
|
|
state: recurlySubscription.state,
|
|
|
|
trialEndsAtFormatted: SubscriptionFormatters.formatDate(
|
2021-02-22 11:36:18 -05:00
|
|
|
recurlySubscription.trial_ends_at
|
2019-05-29 05:21:06 -04:00
|
|
|
),
|
|
|
|
trial_ends_at: recurlySubscription.trial_ends_at,
|
2020-02-20 11:08:30 -05:00
|
|
|
activeCoupons: recurlyCoupons,
|
2021-04-27 03:52:58 -04:00
|
|
|
account: recurlySubscription.account,
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 11:36:18 -05:00
|
|
|
for (const memberGroupSubscription of memberGroupSubscriptions) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (memberGroupSubscription.teamNotice) {
|
|
|
|
memberGroupSubscription.teamNotice = sanitizeHtml(
|
|
|
|
memberGroupSubscription.teamNotice
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 11:36:18 -05:00
|
|
|
callback(null, {
|
2019-05-29 05:21:06 -04:00
|
|
|
personalSubscription,
|
|
|
|
managedGroupSubscriptions,
|
|
|
|
memberGroupSubscriptions,
|
2020-02-20 11:07:54 -05:00
|
|
|
confirmedMemberAffiliations,
|
2019-05-29 05:21:06 -04:00
|
|
|
managedInstitutions,
|
|
|
|
managedPublishers,
|
2021-04-27 03:52:58 -04:00
|
|
|
v1SubscriptionStatus,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2020-02-05 11:34:29 -05:00
|
|
|
buildPlansList() {
|
2019-05-29 05:21:06 -04:00
|
|
|
const { plans } = Settings
|
|
|
|
|
|
|
|
const allPlans = {}
|
|
|
|
plans.forEach(plan => (allPlans[plan.planCode] = plan))
|
|
|
|
|
|
|
|
const result = { allPlans }
|
|
|
|
|
|
|
|
result.studentAccounts = _.filter(
|
|
|
|
plans,
|
|
|
|
plan => plan.planCode.indexOf('student') !== -1
|
|
|
|
)
|
|
|
|
|
|
|
|
result.groupMonthlyPlans = _.filter(
|
|
|
|
plans,
|
|
|
|
plan => plan.groupPlan && !plan.annual
|
|
|
|
)
|
|
|
|
|
|
|
|
result.groupAnnualPlans = _.filter(
|
|
|
|
plans,
|
|
|
|
plan => plan.groupPlan && plan.annual
|
|
|
|
)
|
|
|
|
|
|
|
|
result.individualMonthlyPlans = _.filter(
|
|
|
|
plans,
|
|
|
|
plan =>
|
|
|
|
!plan.groupPlan &&
|
|
|
|
!plan.annual &&
|
2020-01-27 08:54:07 -05:00
|
|
|
plan.planCode !== 'personal' && // Prevent the personal plan from appearing on the change-plans page
|
2019-05-29 05:21:06 -04:00
|
|
|
plan.planCode.indexOf('student') === -1
|
|
|
|
)
|
|
|
|
|
|
|
|
result.individualAnnualPlans = _.filter(
|
|
|
|
plans,
|
|
|
|
plan =>
|
|
|
|
!plan.groupPlan &&
|
|
|
|
plan.annual &&
|
|
|
|
plan.planCode.indexOf('student') === -1
|
|
|
|
)
|
|
|
|
|
|
|
|
return result
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|