Add checks to keep User purchased.gems from becoming NaN

Also sought to more thoroughly protect earned.gems.
This commit is contained in:
Scott Erickson 2016-06-20 16:44:07 -07:00
parent d829d15528
commit 38d19a142a
6 changed files with 13 additions and 6 deletions

View file

@ -377,6 +377,9 @@ PaymentHandler = class PaymentHandler extends Handler
#- Incrementing/recalculating gems #- Incrementing/recalculating gems
incrementGemsFor: (user, gems, done) -> incrementGemsFor: (user, gems, done) ->
if not gems
return done()
purchased = _.clone(user.get('purchased')) purchased = _.clone(user.get('purchased'))
if not purchased?.gems if not purchased?.gems
purchased ?= {} purchased ?= {}

View file

@ -176,7 +176,7 @@ class SubscriptionHandler extends Handler
purchased = _.clone(req.user.get('purchased')) purchased = _.clone(req.user.get('purchased'))
purchased ?= {} purchased ?= {}
purchased.gems ?= 0 purchased.gems ?= 0
purchased.gems += parseInt(charge.metadata.gems) purchased.gems += parseInt(charge.metadata.gems) if charge.metadata.gems
req.user.set('purchased', purchased) req.user.set('purchased', purchased)
req.user.save (err, user) => req.user.save (err, user) =>
@ -257,7 +257,7 @@ class SubscriptionHandler extends Handler
purchased = _.clone(req.user.get('purchased')) purchased = _.clone(req.user.get('purchased'))
purchased ?= {} purchased ?= {}
purchased.gems ?= 0 purchased.gems ?= 0
purchased.gems += product.get('gems') * months purchased.gems += product.get('gems') * months if product.get('gems')
req.user.set('purchased', purchased) req.user.set('purchased', purchased)
req.user.save (err, user) => req.user.save (err, user) =>
@ -440,7 +440,7 @@ class SubscriptionHandler extends Handler
purchased = _.clone(user.get('purchased')) purchased = _.clone(user.get('purchased'))
purchased ?= {} purchased ?= {}
purchased.gems ?= 0 purchased.gems ?= 0
purchased.gems += product.get('gems') purchased.gems += product.get('gems') if product.get('gems')
user.set('purchased', purchased) user.set('purchased', purchased)
user.save (err) => user.save (err) =>
@ -550,7 +550,7 @@ class SubscriptionHandler extends Handler
purchased = _.clone(recipient.get('purchased')) purchased = _.clone(recipient.get('purchased'))
purchased ?= {} purchased ?= {}
purchased.gems ?= 0 purchased.gems ?= 0
purchased.gems += product.get('gems') purchased.gems += product.get('gems') if product.get('gems')
recipient.set('purchased', purchased) recipient.set('purchased', purchased)
recipient.save (err) => recipient.save (err) =>
if err if err

View file

@ -59,7 +59,7 @@ EarnedAchievementSchema.statics.createForAchievement = (achievement, doc, origin
earned.achievedAmount = newAmount earned.achievedAmount = newAmount
#console.log 'earnedPoints is', (expFunction(newAmount) - expFunction(originalAmount)) * pointWorth, 'was', earned.earnedPoints, earned.previouslyAchievedAmount, 'got exp function for new amount', newAmount, expFunction(newAmount), 'for original amount', originalAmount, expFunction(originalAmount), 'with point worth', pointWorth #console.log 'earnedPoints is', (expFunction(newAmount) - expFunction(originalAmount)) * pointWorth, 'was', earned.earnedPoints, earned.previouslyAchievedAmount, 'got exp function for new amount', newAmount, expFunction(newAmount), 'for original amount', originalAmount, expFunction(originalAmount), 'with point worth', pointWorth
earnedPoints = earned.earnedPoints = (expFunction(newAmount) - expFunction(originalAmount)) * pointWorth earnedPoints = earned.earnedPoints = (expFunction(newAmount) - expFunction(originalAmount)) * pointWorth
earnedGems = earned.earnedGems = (expFunction(newAmount) - expFunction(originalAmount)) * gemWorth earnedGems = earned.earnedGems = (expFunction(newAmount) - expFunction(originalAmount)) * gemWorth ? 0
earned.previouslyAchievedAmount = originalAmount earned.previouslyAchievedAmount = originalAmount
EarnedAchievement.update {achievement: earned.achievement, user: earned.user}, earned, {upsert: true}, (err) -> EarnedAchievement.update {achievement: earned.achievement, user: earned.user}, earned, {upsert: true}, (err) ->
return log.error err if err? return log.error err if err?

View file

@ -9,6 +9,7 @@ AnalyticsUsersActive = require './AnalyticsUsersActive'
Classroom = require '../models/Classroom' Classroom = require '../models/Classroom'
languages = require '../routes/languages' languages = require '../routes/languages'
_ = require 'lodash' _ = require 'lodash'
errors = require '../commons/errors'
config = require '../../server_config' config = require '../../server_config'
stripe = require('stripe')(config.stripe.secretKey) stripe = require('stripe')(config.stripe.secretKey)
@ -347,6 +348,8 @@ UserSchema.methods.saveActiveUser = (event, done=null) ->
done?() done?()
UserSchema.pre('save', (next) -> UserSchema.pre('save', (next) ->
if _.isNaN(@get('purchased')?.gems)
return next(new errors.InternalServerError('Attempting to save NaN to user'))
Classroom = require './Classroom' Classroom = require './Classroom'
if @isTeacher() and not @wasTeacher if @isTeacher() and not @wasTeacher
Classroom.update({members: @_id}, {$pull: {members: @_id}}, {multi: true}).exec (err, res) -> Classroom.update({members: @_id}, {$pull: {members: @_id}}, {multi: true}).exec (err, res) ->

View file

@ -47,6 +47,7 @@ updateUserProperty = (userID, userProperty, answer) ->
return log.error err if err return log.error err if err
updateUserGems = (userID, gemDelta) -> updateUserGems = (userID, gemDelta) ->
return unless gemDelta
update = $inc: {'earned.gems': gemDelta} update = $inc: {'earned.gems': gemDelta}
User.update {_id: mongoose.Types.ObjectId(userID)}, update, (err, result) -> User.update {_id: mongoose.Types.ObjectId(userID)}, update, (err, result) ->
return log.error err if err return log.error err if err

View file

@ -101,7 +101,7 @@ module.exports.setup = (app) ->
# Update purchased gems # Update purchased gems
# TODO: is this correct for a resub? # TODO: is this correct for a resub?
Payment.find({recipient: recipient._id, gems: {$exists: true}}).select('gems').exec (err, payments) -> Payment.find({recipient: recipient._id, gems: {$exists: true}}).select('gems').exec (err, payments) ->
gems = _.reduce payments, ((sum, p) -> sum + p.get('gems')), 0 gems = _.reduce payments, ((sum, p) -> sum + (p.get('gems') or 0)), 0
purchased = _.clone(recipient.get('purchased')) purchased = _.clone(recipient.get('purchased'))
purchased ?= {} purchased ?= {}
purchased.gems = gems purchased.gems = gems