codecombat/server/prepaids/prepaid_handler.coffee
Matt Lott 9c39bf7656 Update prepaids to support multiple redeemers for a single code
Will remove deprecated properties after updating production.
2015-08-12 15:51:18 -07:00

37 lines
1.3 KiB
CoffeeScript

Handler = require '../commons/Handler'
Prepaid = require './Prepaid'
# TODO: Should this happen on a save() call instead of a prepaid/-/create post?
# TODO: Probably a better way to create a unique 8 charactor string property using db voodoo
PrepaidHandler = class PrepaidHandler extends Handler
modelClass: Prepaid
jsonSchema: require '../../app/schemas/models/prepaid.schema'
allowedMethods: ['POST']
hasAccess: (req) ->
req.user?.isAdmin()
getByRelationship: (req, res, args...) ->
relationship = args[1]
return @createPrepaid(req, res) if relationship is 'create'
super arguments...
createPrepaid: (req, res) ->
return @sendForbiddenError(res) unless @hasAccess(req)
return @sendForbiddenError(res) unless req.body.type is 'subscription'
return @sendForbiddenError(res) unless req.body.maxRedeemers > 0
Prepaid.generateNewCode (code) =>
return @sendDatabaseError(res, 'Database error.') unless code
prepaid = new Prepaid
creator: req.user.id
type: req.body.type
code: code
maxRedeemers: req.body.maxRedeemers
properties:
couponID: 'free'
prepaid.save (err) =>
return @sendDatabaseError(res, err) if err
@sendSuccess(res, prepaid.toObject())
module.exports = new PrepaidHandler()