mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-12-05 05:11:17 -05:00
36 lines
1.1 KiB
CoffeeScript
36 lines
1.1 KiB
CoffeeScript
mongoose = require 'mongoose'
|
|
config = require '../../server_config'
|
|
PrepaidSchema = new mongoose.Schema {
|
|
creator: mongoose.Schema.Types.ObjectId
|
|
}, {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')))
|
|
if not @get('code')
|
|
Prepaid.generateNewCode (code) =>
|
|
@set('code', code)
|
|
next()
|
|
else
|
|
next()
|
|
)
|
|
|
|
PrepaidSchema.post 'init', (doc) ->
|
|
doc.set('maxRedeemers', parseInt(doc.get('maxRedeemers')))
|
|
|
|
PrepaidSchema.statics.postEditableProperties = [
|
|
'creator', 'maxRedeemers', 'properties', 'type'
|
|
]
|
|
|
|
module.exports = Prepaid = mongoose.model('prepaid', PrepaidSchema)
|