codecombat/app/collections/Prepaids.coffee
2015-12-04 14:17:24 -08:00

23 lines
No EOL
635 B
CoffeeScript

CocoCollection = require 'collections/CocoCollection'
Prepaid = require 'models/Prepaid'
sum = (numbers) -> _.reduce(numbers, (a, b) -> a + b)
module.exports = class Prepaids extends CocoCollection
model: Prepaid
url: "/db/prepaid"
totalMaxRedeemers: ->
sum((prepaid.get('maxRedeemers') for prepaid in @models)) or 0
totalRedeemers: ->
sum((_.size(prepaid.get('redeemers')) for prepaid in @models)) or 0
totalAvailable: -> Math.max(@totalMaxRedeemers() - @totalRedeemers(), 0)
fetchByCreator: (creatorID, opts) ->
opts ?= {}
opts.data ?= {}
opts.data.creator = creatorID
@fetch opts