2014-11-01 17:15:57 -04:00
|
|
|
Purchase = require './Purchase'
|
|
|
|
User = require '../users/User'
|
|
|
|
Handler = require '../commons/Handler'
|
|
|
|
{handlers} = require '../commons/mapping'
|
|
|
|
mongoose = require 'mongoose'
|
|
|
|
log = require 'winston'
|
|
|
|
sendwithus = require '../sendwithus'
|
|
|
|
hipchat = require '../hipchat'
|
|
|
|
|
|
|
|
PurchaseHandler = class PurchaseHandler extends Handler
|
|
|
|
modelClass: Purchase
|
|
|
|
editableProperties: []
|
|
|
|
postEditableProperties: ['purchased']
|
|
|
|
jsonSchema: require '../../app/schemas/models/purchase.schema'
|
|
|
|
|
|
|
|
makeNewInstance: (req) ->
|
|
|
|
purchase = super(req)
|
|
|
|
purchase.set 'purchaser', req.user._id
|
|
|
|
purchase.set 'recipient', req.user._id
|
|
|
|
purchase.set 'created', new Date().toISOString()
|
|
|
|
purchase
|
|
|
|
|
|
|
|
post: (req, res) ->
|
|
|
|
purchased = req.body.purchased
|
|
|
|
purchaser = req.user._id
|
|
|
|
purchasedOriginal = purchased?.original
|
|
|
|
|
|
|
|
Handler = require '../commons/Handler'
|
|
|
|
return @sendBadInputError(res) if not Handler.isID(purchasedOriginal)
|
|
|
|
|
|
|
|
collection = purchased?.collection
|
|
|
|
return @sendBadInputError(res) if not collection in @jsonSchema.properties.purchased.properties.collection.enum
|
|
|
|
|
|
|
|
handler = require('../' + handlers[collection])
|
2014-11-05 19:41:03 -05:00
|
|
|
criteria = { 'original': purchasedOriginal }
|
2014-11-01 17:15:57 -04:00
|
|
|
sort = { 'version.major': -1, 'version.minor': -1 }
|
|
|
|
|
|
|
|
handler.modelClass.findOne(criteria).sort(sort).exec (err, purchasedItem) =>
|
|
|
|
gemsOwned = req.user.get('earned')?.gems or 0
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
return @sendNotFoundError(res) unless purchasedItem
|
|
|
|
return @sendBadInputError(res, 'This cannot be purchased.') if not cost = purchasedItem.get('gems')
|
2014-11-10 18:24:05 -05:00
|
|
|
return @sendForbiddenError(res, 'Not enough gems.') if cost > req.user.gems()
|
2014-11-01 17:15:57 -04:00
|
|
|
req.purchasedItem = purchasedItem # for safekeeping
|
|
|
|
|
|
|
|
criteria = {
|
2014-11-05 19:41:03 -05:00
|
|
|
'purchased.original': purchasedOriginal
|
2014-11-01 17:15:57 -04:00
|
|
|
'recipient': purchaser
|
|
|
|
}
|
|
|
|
Purchase.findOne criteria, (err, purchase) =>
|
|
|
|
if purchase
|
|
|
|
@addPurchaseToUser(req, res)
|
|
|
|
return @sendSuccess(res, @formatEntity(req, purchase))
|
|
|
|
|
|
|
|
else
|
|
|
|
super(req, res)
|
|
|
|
|
|
|
|
onPostSuccess: (req) ->
|
|
|
|
@addPurchaseToUser(req)
|
|
|
|
|
|
|
|
addPurchaseToUser: (req) ->
|
|
|
|
user = req.user
|
|
|
|
purchased = user.get('purchased') or {}
|
2014-11-11 17:52:43 -05:00
|
|
|
purchased = _.cloneDeep purchased
|
2014-11-01 17:15:57 -04:00
|
|
|
item = req.purchasedItem
|
|
|
|
|
|
|
|
group = switch item.get('kind')
|
|
|
|
when 'Item' then 'items'
|
|
|
|
when 'Hero' then 'heroes'
|
|
|
|
else 'levels'
|
|
|
|
|
2014-11-10 18:24:05 -05:00
|
|
|
original = item.get('original') + ''
|
2014-11-01 17:15:57 -04:00
|
|
|
purchased[group] ?= []
|
|
|
|
unless original in purchased[group]
|
|
|
|
#- add the purchase to the list of purchases
|
2014-11-05 19:41:03 -05:00
|
|
|
purchased[group].push(original+'')
|
2014-11-01 17:15:57 -04:00
|
|
|
user.set('purchased', purchased)
|
|
|
|
|
|
|
|
#- deduct the gems from the user
|
|
|
|
spent = hadSpent = user.get('spent') ? 0
|
|
|
|
spent += item.get('gems')
|
|
|
|
user.set('spent', spent)
|
|
|
|
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
module.exports = new PurchaseHandler()
|