mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-25 08:38:09 -05:00
22 lines
736 B
CoffeeScript
22 lines
736 B
CoffeeScript
mongoose = require 'mongoose'
|
|
config = require '../../server_config'
|
|
PrepaidSchema = new mongoose.Schema {}, {strict: false, minimize: false,read:config.mongo.readpref}
|
|
|
|
PrepaidSchema.index({code: 1}, { unique: true })
|
|
PrepaidSchema.index({'redeemers.userID': 1})
|
|
|
|
PrepaidSchema.statics.generateNewCode = (done) ->
|
|
tryCode = ->
|
|
code = _.sample("abcdefghijklmnopqrstuvwxyz0123456789", 8).join('')
|
|
Prepaid.findOne code: code, (err, prepaid) ->
|
|
return done() if err
|
|
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)
|