Hooked up Level handler to deny creating new LevelSessions when the level requires a subscription and the user doesn't have one.

This commit is contained in:
Scott Erickson 2014-12-03 13:32:04 -08:00
parent 4701ba22af
commit eb98cf4952
5 changed files with 11 additions and 1 deletions

View file

@ -41,9 +41,11 @@ class CocoModel extends Backbone.Model
clone.set($.extend(true, {}, if withChanges then @attributes else @_revertAttributes))
clone
onError: ->
onError: (level, jqxhr) ->
@loading = false
@jqxhr = null
if jqxhr.status is 402
Backbone.Mediator.publish 'level:subscription-required'
onLoaded: ->
@loaded = true

View file

@ -150,6 +150,7 @@ module.exports = class CocoView extends Backbone.View
# Error handling for loading
onResourceLoadFailed: (e) ->
r = e.resource
return if r.jqxhr?.status is 402 # payment-required failures are handled separately
if r.jqxhr?.status is 0
r.retries ?= 0
r.retries += 1

View file

@ -79,6 +79,7 @@ module.exports = class Handler
sendNotFoundError: (res, message) -> errors.notFound(res, message)
sendMethodNotAllowed: (res, message) -> errors.badMethod(res, @allowedMethods, message)
sendBadInputError: (res, message) -> errors.badInput(res, message)
sendPaymentRequiredError: (res, message) -> errors.paymentRequired(res, message)
sendDatabaseError: (res, err) ->
return @sendError(res, err.code, err.response) if err.response and err.code
log.error "Database error, #{err}"

View file

@ -15,6 +15,11 @@ module.exports.forbidden = (res, message='Forbidden') ->
log.debug "403: #{message}"
res.send 403, message
res.end()
module.exports.paymentRequired = (res, message='Payment required') ->
log.debug "402: #{message}"
res.send 402, message
res.end()
module.exports.notFound = (res, message='Not found.') ->
res.send 404, message

View file

@ -72,6 +72,7 @@ LevelHandler = class LevelHandler extends Handler
Session.findOne(sessionQuery).exec (err, doc) =>
return @sendDatabaseError(res, err) if err
return @sendSuccess(res, doc) if doc?
return @sendPaymentRequiredError(res, err) if (not req.user.get('stripe')?.subscriptionID) and level.get('requiresSubscription')
@createAndSaveNewSession sessionQuery, req, res
createAndSaveNewSession: (sessionQuery, req, res) =>