2016-05-09 18:16:54 -04:00
|
|
|
Promise = require 'bluebird'
|
2015-03-19 18:02:45 -04:00
|
|
|
mongoose = require 'mongoose'
|
2015-03-21 21:49:32 -04:00
|
|
|
config = require '../../server_config'
|
2015-11-10 17:33:27 -05:00
|
|
|
PrepaidSchema = new mongoose.Schema {
|
|
|
|
creator: mongoose.Schema.Types.ObjectId
|
|
|
|
}, {strict: false, minimize: false,read:config.mongo.readpref}
|
2016-05-09 18:16:54 -04:00
|
|
|
co = require 'co'
|
|
|
|
jsonSchema = require '../../app/schemas/models/prepaid.schema'
|
2015-03-19 18:02:45 -04:00
|
|
|
|
2015-09-25 13:03:44 -04:00
|
|
|
PrepaidSchema.index({code: 1}, { unique: true })
|
2015-11-02 16:35:47 -05:00
|
|
|
PrepaidSchema.index({'redeemers.userID': 1})
|
2016-05-09 18:16:54 -04:00
|
|
|
PrepaidSchema.index({owner: 1, endDate: 1}, { sparse: true })
|
|
|
|
|
|
|
|
PrepaidSchema.statics.DEFAULT_START_DATE = new Date(2016,4,15).toISOString()
|
|
|
|
PrepaidSchema.statics.DEFAULT_END_DATE = new Date(2017,5,1).toISOString()
|
2015-09-25 13:03:44 -04:00
|
|
|
|
2015-06-05 09:48:09 -04:00
|
|
|
PrepaidSchema.statics.generateNewCode = (done) ->
|
2016-05-09 18:16:54 -04:00
|
|
|
# Deprecated for not following Node callback convention. TODO: Remove
|
2015-06-05 09:48:09 -04:00
|
|
|
tryCode = ->
|
2015-10-07 17:06:09 -04:00
|
|
|
code = _.sample("abcdefghijklmnopqrstuvwxyz0123456789", 8).join('')
|
2015-06-05 09:48:09 -04:00
|
|
|
Prepaid.findOne code: code, (err, prepaid) ->
|
|
|
|
return done() if err
|
|
|
|
return done(code) unless prepaid
|
|
|
|
tryCode()
|
|
|
|
tryCode()
|
2016-05-09 18:16:54 -04:00
|
|
|
|
|
|
|
PrepaidSchema.statics.generateNewCodeAsync = co.wrap (done) ->
|
|
|
|
code = null
|
|
|
|
while true
|
|
|
|
code = _.sample("abcdefghijklmnopqrstuvwxyz0123456789", 8).join('')
|
|
|
|
prepaid = yield Prepaid.findOne({code: code})
|
|
|
|
break if not prepaid
|
|
|
|
return code
|
2015-11-12 15:16:40 -05:00
|
|
|
|
2015-11-02 16:06:58 -05:00
|
|
|
PrepaidSchema.pre('save', (next) ->
|
|
|
|
@set('exhausted', @get('maxRedeemers') <= _.size(@get('redeemers')))
|
2015-11-10 17:33:27 -05:00
|
|
|
if not @get('code')
|
|
|
|
Prepaid.generateNewCode (code) =>
|
|
|
|
@set('code', code)
|
|
|
|
next()
|
|
|
|
else
|
|
|
|
next()
|
2015-11-02 16:06:58 -05:00
|
|
|
)
|
2015-06-05 09:48:09 -04:00
|
|
|
|
2015-11-10 14:27:46 -05:00
|
|
|
PrepaidSchema.post 'init', (doc) ->
|
2016-05-09 18:16:54 -04:00
|
|
|
doc.set('maxRedeemers', parseInt(doc.get('maxRedeemers') ? 0))
|
|
|
|
if @get('type') is 'course'
|
|
|
|
if not @get('startDate')
|
|
|
|
@set('startDate', Prepaid.DEFAULT_START_DATE)
|
|
|
|
if not @get('endDate')
|
|
|
|
@set('endDate', Prepaid.DEFAULT_END_DATE)
|
2015-11-10 14:27:46 -05:00
|
|
|
|
2015-11-10 17:33:27 -05:00
|
|
|
PrepaidSchema.statics.postEditableProperties = [
|
2016-05-09 18:16:54 -04:00
|
|
|
'creator', 'maxRedeemers', 'properties', 'type', 'startDate', 'endDate'
|
2015-11-10 17:33:27 -05:00
|
|
|
]
|
2016-05-09 18:16:54 -04:00
|
|
|
PrepaidSchema.statics.editableProperties = []
|
|
|
|
PrepaidSchema.statics.jsonSchema = jsonSchema
|
2015-11-10 17:33:27 -05:00
|
|
|
|
2015-03-19 18:02:45 -04:00
|
|
|
module.exports = Prepaid = mongoose.model('prepaid', PrepaidSchema)
|