2015-09-25 13:03:44 -04:00
|
|
|
CocoModel = require './CocoModel'
|
|
|
|
schema = require 'schemas/models/prepaid.schema'
|
|
|
|
|
|
|
|
module.exports = class Prepaid extends CocoModel
|
|
|
|
@className: "Prepaid"
|
|
|
|
urlRoot: '/db/prepaid'
|
|
|
|
|
|
|
|
openSpots: ->
|
2015-11-24 13:32:15 -05:00
|
|
|
return @get('maxRedeemers') - @get('redeemers')?.length if @get('redeemers')?
|
|
|
|
@get('maxRedeemers')
|
2015-09-25 13:03:44 -04:00
|
|
|
|
|
|
|
userHasRedeemed: (userID) ->
|
|
|
|
for redeemer in @get('redeemers')
|
|
|
|
return redeemer.date if redeemer.userID is userID
|
|
|
|
return null
|
2015-11-24 13:32:15 -05:00
|
|
|
|
2015-11-09 20:58:40 -05:00
|
|
|
initialize: ->
|
|
|
|
@listenTo @, 'add', ->
|
|
|
|
maxRedeemers = @get('maxRedeemers')
|
|
|
|
if _.isString(maxRedeemers)
|
|
|
|
@set 'maxRedeemers', parseInt(maxRedeemers)
|
2016-08-23 13:43:31 -04:00
|
|
|
super(arguments...)
|
2016-05-09 18:16:54 -04:00
|
|
|
|
|
|
|
status: ->
|
|
|
|
endDate = @get('endDate')
|
|
|
|
if endDate and new Date(endDate) < new Date()
|
|
|
|
return 'expired'
|
|
|
|
|
|
|
|
startDate = @get('startDate')
|
|
|
|
if startDate and new Date(startDate) > new Date()
|
|
|
|
return 'pending'
|
|
|
|
|
|
|
|
if @openSpots() <= 0
|
|
|
|
return 'empty'
|
|
|
|
|
|
|
|
return 'available'
|
|
|
|
|
|
|
|
redeem: (user, options={}) ->
|
|
|
|
options.url = _.result(@, 'url')+'/redeemers'
|
|
|
|
options.type = 'POST'
|
|
|
|
options.data ?= {}
|
|
|
|
options.data.userID = user.id or user
|
|
|
|
@fetch(options)
|