Check with v1 before allowing a new subscription

This commit is contained in:
James Allen 2018-06-05 11:57:25 +01:00
parent 9d84263463
commit ce60e6941c
3 changed files with 34 additions and 14 deletions

View file

@ -34,7 +34,7 @@ module.exports = LimitationsManager =
callback null, false
userHasSubscriptionOrIsGroupMember: (user, callback = (err, hasSubscriptionOrIsMember)->) ->
@userHasSubscription user, (err, hasSubscription, subscription)=>
@userHasV2Subscription user, (err, hasSubscription, subscription)=>
return callback(err) if err?
@userIsMemberOfGroupSubscription user, (err, isMember)=>
return callback(err) if err?
@ -43,7 +43,7 @@ module.exports = LimitationsManager =
logger.log {user_id:user._id, isMember, hasSubscription, hasV1Subscription}, "checking if user has subscription or is group member"
callback err, isMember or hasSubscription or hasV1Subscription, subscription
userHasSubscription: (user, callback = (err, hasSubscription, subscription)->) ->
userHasV2Subscription: (user, callback = (err, hasSubscription, subscription)->) ->
logger.log user_id:user._id, "checking if user has subscription"
SubscriptionLocator.getUsersSubscription user._id, (err, subscription)->
if err?
@ -52,12 +52,26 @@ module.exports = LimitationsManager =
logger.log user:user, hasValidSubscription:hasValidSubscription, subscription:subscription, "checking if user has subscription"
callback err, hasValidSubscription, subscription
userHasV1OrV2Subscription: (user, callback = (err, hasSubscription) ->) ->
@userHasV2Subscription user, (err, hasV2Subscription) =>
return callback(err) if err?
return callback null, true if hasV2Subscription
@userHasV1Subscription user, (err, hasV1Subscription) =>
return callback(err) if err?
return callback null, true if hasV1Subscription
return callback null, false
userIsMemberOfGroupSubscription: (user, callback = (error, isMember, subscriptions) ->) ->
logger.log user_id: user._id, "checking is user is member of subscription groups"
SubscriptionLocator.getMemberSubscriptions user._id, (err, subscriptions = []) ->
return callback(err) if err?
callback err, subscriptions.length > 0, subscriptions
userHasV1Subscription: (user, callback = (error, hasV1Subscription) ->) ->
V1SubscriptionManager.getSubscriptionsFromV1 user._id, (err, v1Subscription) ->
logger.log {user_id: user._id, v1Subscription}, '[userHasV1Subscription]'
callback err, !!v1Subscription?.has_subscription
userHasV1SubscriptionOrTeam: (user, callback = (error, hasV1Subscription) ->) ->
V1SubscriptionManager.getSubscriptionsFromV1 user._id, (err, v1Subscription = {}) ->
return callback(err) if err?

View file

@ -45,12 +45,12 @@ module.exports = SubscriptionController =
paymentPage: (req, res, next) ->
user = AuthenticationController.getSessionUser(req)
plan = PlansLocator.findLocalPlanInSettings(req.query.planCode)
LimitationsManager.userHasSubscription user, (err, hasSubscription)->
LimitationsManager.userHasV1OrV2Subscription user, (err, hasSubscription)->
return next(err) if err?
if hasSubscription or !plan?
res.redirect "/user/subscription"
else
# LimitationsManager.userHasSubscription only checks Mongo. Double check with
# LimitationsManager.userHasV2Subscription only checks Mongo. Double check with
# Recurly as well at this point (we don't do this most places for speed).
SubscriptionHandler.validateNoSubscriptionInRecurly user._id, (error, valid) ->
return next(error) if error?
@ -135,11 +135,17 @@ module.exports = SubscriptionController =
recurly_token_id = req.body.recurly_token_id
subscriptionDetails = req.body.subscriptionDetails
logger.log recurly_token_id: recurly_token_id, user_id:user._id, subscriptionDetails:subscriptionDetails, "creating subscription"
SubscriptionHandler.createSubscription user, subscriptionDetails, recurly_token_id, (err)->
if err?
logger.err err:err, user_id:user._id, "something went wrong creating subscription"
return res.sendStatus 500
res.sendStatus 201
LimitationsManager.userHasV1OrV2Subscription user, (err, hasSubscription)->
return next(err) if err?
if hasSubscription
logger.warn {user_id: user._id}, 'user already has subscription'
res.sendStatus 409 # conflict
SubscriptionHandler.createSubscription user, subscriptionDetails, recurly_token_id, (err)->
if err?
logger.err err:err, user_id:user._id, "something went wrong creating subscription"
return next(err)
res.sendStatus 201
successful_subscription: (req, res, next)->
user = AuthenticationController.getSessionUser(req)
@ -195,7 +201,7 @@ module.exports = SubscriptionController =
renderUpgradeToAnnualPlanPage: (req, res, next)->
user = AuthenticationController.getSessionUser(req)
LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
LimitationsManager.userHasV2Subscription user, (err, hasSubscription, subscription)->
return next(err) if err?
planCode = subscription?.planCode.toLowerCase()
if planCode?.indexOf("annual") != -1
@ -225,7 +231,7 @@ module.exports = SubscriptionController =
extendTrial: (req, res, next)->
user = AuthenticationController.getSessionUser(req)
LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
LimitationsManager.userHasV2Subscription user, (err, hasSubscription, subscription)->
return next(err) if err?
SubscriptionHandler.extendTrial subscription, 14, (err)->
if err?

View file

@ -36,7 +36,7 @@ module.exports =
updateSubscription: (user, plan_code, coupon_code, callback)->
logger.log user:user, plan_code:plan_code, coupon_code:coupon_code, "updating subscription"
LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
LimitationsManager.userHasV2Subscription user, (err, hasSubscription, subscription)->
if !hasSubscription
return callback()
else
@ -56,7 +56,7 @@ module.exports =
cancelSubscription: (user, callback) ->
LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
LimitationsManager.userHasV2Subscription user, (err, hasSubscription, subscription)->
if hasSubscription
RecurlyWrapper.cancelSubscription subscription.recurlySubscription_id, (error) ->
return callback(error) if error?
@ -73,7 +73,7 @@ module.exports =
callback()
reactivateSubscription: (user, callback) ->
LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
LimitationsManager.userHasV2Subscription user, (err, hasSubscription, subscription)->
if hasSubscription
RecurlyWrapper.reactivateSubscription subscription.recurlySubscription_id, (error) ->
return callback(error) if error?