mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-03-14 07:00:01 -04:00
Remove extra Stripe call in sub prepaid redeem
This commit is contained in:
parent
11c03b3905
commit
c6caafb7cd
2 changed files with 14 additions and 26 deletions
|
@ -204,42 +204,29 @@ class SubscriptionHandler extends Handler
|
|||
if err
|
||||
@logSubscriptionError(req.user, "Redeem Prepaid Code find: #{JSON.stringify(err)}")
|
||||
return @sendDatabaseError(res, err)
|
||||
|
||||
return @sendForbiddenError(res) if prepaid is null
|
||||
unless prepaid
|
||||
@logSubscriptionError(req.user, "Could not find prepaid code #{req.body.ppc}")
|
||||
return @sendForbiddenError(res)
|
||||
|
||||
oldRedeemers = prepaid.get('redeemers') ? []
|
||||
return @sendForbiddenError(res) if oldRedeemers.length >= prepaid.get('maxRedeemers')
|
||||
|
||||
months = parseInt(prepaid.get('properties')?.months)
|
||||
return @sendForbiddenError(res) if isNaN(months) or months < 1
|
||||
|
||||
for redeemer in oldRedeemers
|
||||
return @sendForbiddenError(res) if redeemer.userID.equals(req.user._id)
|
||||
|
||||
customerID = req.user.get('stripe')?.customerID
|
||||
subscriptionID = req.user.get('stripe')?.subscriptionID
|
||||
findStripeSubscription customerID, subscriptionID: subscriptionID, (subscription) =>
|
||||
stripeSubscriptionPeriodEndDate = new Date(subscription.current_period_end * 1000) if subscription
|
||||
|
||||
unless customerID
|
||||
@redeemCode(req, res, oldRedeemers, months)
|
||||
else
|
||||
stripe.customers.retrieve customerID, (err, customer) =>
|
||||
@cancelSubscriptionImmediately req.user, subscription, (err) =>
|
||||
if err
|
||||
@logSubscriptionError(req.user, "Redeem Prepaid Code get customer: #{JSON.stringify(err)}")
|
||||
@logSubscriptionError(user, "Redeem Prepaid Code Stripe cancel subscription error: #{JSON.stringify(err)}")
|
||||
return @sendDatabaseError(res, err)
|
||||
@redeemPrepaidCode(req, res, oldRedeemers, months, stripeSubscriptionPeriodEndDate)
|
||||
|
||||
findStripeSubscription customer.id, subscriptionID: req.user.get('stripe')?.subscriptionID, (subscription) =>
|
||||
stripeSubscriptionPeriodEndDate = null
|
||||
if subscription
|
||||
stripeSubscriptionPeriodEndDate = new Date(subscription.current_period_end * 1000)
|
||||
|
||||
|
||||
@cancelSubscriptionImmediately req.user, subscription, (err) =>
|
||||
if err
|
||||
@logSubscriptionError(user, "Redeem Prepaid Code Stripe cancel subscription error: #{JSON.stringify(err)}")
|
||||
return @sendDatabaseError(res, err)
|
||||
|
||||
@redeemCode(req, res, oldRedeemers, months, stripeSubscriptionPeriodEndDate)
|
||||
|
||||
redeemCode: (req, res, oldRedeemers, months, startDate=null) =>
|
||||
redeemPrepaidCode: (req, res, oldRedeemers, months, startDate=null) =>
|
||||
return @sendForbiddenError(res) unless req.user?
|
||||
return @sendForbiddenError(res) unless req.body?.ppc
|
||||
return @sendForbiddenError(res) unless oldRedeemers
|
||||
|
@ -257,9 +244,8 @@ class SubscriptionHandler extends Handler
|
|||
|
||||
return @sendNotFoundError(res, "Error while updating prepaid redeemer") if num isnt 1
|
||||
|
||||
|
||||
# Add terminal subscription to User, extending existing subscriptions
|
||||
# TODO: refactor this into some form useable by both this and purchaseYearSale?
|
||||
# TODO: refactor this into some form useable by both this and purchaseYearSale
|
||||
stripeInfo = _.cloneDeep(req.user.get('stripe') ? {})
|
||||
endDate = new moment()
|
||||
if startDate
|
||||
|
|
|
@ -199,6 +199,7 @@ describe '/db/prepaid', ->
|
|||
expect(prepaid.type).toEqual('terminal_subscription')
|
||||
expect(prepaid.code).toBeDefined()
|
||||
# Saving this code for later tests
|
||||
# TODO: don't make tests dependent on each other
|
||||
joeCode = prepaid.code
|
||||
expect(prepaid.creator).toBeDefined()
|
||||
expect(prepaid.maxRedeemers).toEqual(3)
|
||||
|
@ -264,6 +265,8 @@ describe '/db/prepaid', ->
|
|||
expect(prepaid.properties?.months).toEqual(3)
|
||||
done()
|
||||
|
||||
# TODO: Move redeem subscription prepaid code tests to subscription tests file
|
||||
|
||||
it 'Creator can redeeem a prepaid code', (done) ->
|
||||
loginJoe (joe) ->
|
||||
expect(joeCode).not.toBeNull()
|
||||
|
@ -357,4 +360,3 @@ describe '/db/prepaid', ->
|
|||
expect(res.statusCode).not.toEqual(200)
|
||||
done()
|
||||
# TODO: add a bunch of parallel tests trying to redeem a code with a high maxRedeemers (50?) to see what happens
|
||||
|
||||
|
|
Loading…
Reference in a new issue