Add exhausted property to prepaid model

It is true when redeemers.length >= maxRedeemers
This commit is contained in:
Scott Erickson 2015-11-02 13:06:58 -08:00
parent e98d60fa7d
commit c3735ba0eb
3 changed files with 7 additions and 0 deletions

View file

@ -10,6 +10,7 @@ PrepaidSchema = c.object({title: 'Prepaid', required: ['creator', 'type']}, {
code: c.shortString(title: "Unique code to redeem")
type: { type: 'string' }
properties: {type: 'object'}
exhausted: { type: 'boolean' }
})
c.extendBasicProperties(PrepaidSchema, 'prepaid')

View file

@ -12,5 +12,10 @@ PrepaidSchema.statics.generateNewCode = (done) ->
return done(code) unless prepaid
tryCode()
tryCode()
PrepaidSchema.pre('save', (next) ->
@set('exhausted', @get('maxRedeemers') <= _.size(@get('redeemers')))
next()
)
module.exports = Prepaid = mongoose.model('prepaid', PrepaidSchema)

View file

@ -306,6 +306,7 @@ describe '/db/prepaid', ->
joeCode = prepaid.code
expect(prepaid.creator).toBeDefined()
expect(prepaid.maxRedeemers).toEqual(3)
expect(prepaid.exhausted).toBe(false)
expect(prepaid.properties).toBeDefined()
expect(prepaid.properties.months).toEqual(3)
done()