Course instance redeem API returns 200 for double redeem

If a user has already redeemed course prepaid code, return 200 and the
related course instances.  Simplifies client-side experience.
This commit is contained in:
Matt Lott 2015-10-07 15:14:26 -07:00
parent 440bf9c6e0
commit d95cbc0627
2 changed files with 31 additions and 2 deletions

View file

@ -142,6 +142,9 @@ CourseInstanceHandler = class CourseInstanceHandler extends Handler
return @sendDatabaseError(res, err) if err return @sendDatabaseError(res, err) if err
return @sendForbiddenError(res) if prepaid.get('redeemers')?.length >= prepaid.get('maxRedeemers') return @sendForbiddenError(res) if prepaid.get('redeemers')?.length >= prepaid.get('maxRedeemers')
if _.find((prepaid.get('redeemers') ? []), (a) -> a.userID.equals(req.user.id))
return @sendSuccess(res, courseInstances)
# Add to prepaid redeemers # Add to prepaid redeemers
query = query =
_id: prepaid.get('_id') _id: prepaid.get('_id')

View file

@ -222,7 +222,7 @@ describe 'CourseInstance', ->
describe 'Redeem prepaid code', -> describe 'Redeem prepaid code', ->
it 'Redeem prepaid code an instance of max 2', (done) -> it 'Redeem prepaid code for an instance of max 2', (done) ->
stripe.tokens.create { stripe.tokens.create {
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
}, (err, token) -> }, (err, token) ->
@ -264,7 +264,7 @@ describe 'CourseInstance', ->
expect(usersFound).toEqual(2) expect(usersFound).toEqual(2)
done() done()
it 'Redeem full prepaid code on instance of max 1', (done) -> it 'Redeem full prepaid code for on instance of max 1', (done) ->
stripe.tokens.create { stripe.tokens.create {
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' } card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
}, (err, token) -> }, (err, token) ->
@ -326,3 +326,29 @@ describe 'CourseInstance', ->
return done(err) if err return done(err) if err
expect(prepaid.get('redeemers')?.length).toEqual(prepaid.get('maxRedeemers')) expect(prepaid.get('redeemers')?.length).toEqual(prepaid.get('maxRedeemers'))
done() done()
it 'Redeem prepaid code twice', (done) ->
stripe.tokens.create {
card: { number: '4242424242424242', exp_month: 12, exp_year: 2020, cvc: '123' }
}, (err, token) ->
loginNewUser (user1) ->
createCourse 0, (err, course) ->
expect(err).toBeNull()
return done(err) if err
createCourseInstances user1, course.get('_id'), 2, token.id, (err, courseInstances) ->
expect(err).toBeNull()
return done(err) if err
expect(courseInstances.length).toEqual(1)
Prepaid.findById courseInstances[0].get('prepaidID'), (err, prepaid) ->
expect(err).toBeNull()
return done(err) if err
loginNewUser (user2) ->
# Redeem once
request.post {uri: courseInstanceRedeemURL, json: {prepaidCode: prepaid.get('code')} }, (err, res) ->
expect(err).toBeNull()
expect(res.statusCode).toBe(200)
# Redeem twice
request.post {uri: courseInstanceRedeemURL, json: {prepaidCode: prepaid.get('code')} }, (err, res) ->
expect(err).toBeNull()
expect(res.statusCode).toBe(200)
done()