From bf41216a9b5982792fd38877cdb9ea91d2f73c86 Mon Sep 17 00:00:00 2001 From: Brandon McPhail Date: Fri, 13 Dec 2013 15:48:18 -0800 Subject: [PATCH] Fixed challenge prize logic to avoid double paying --- src/controllers/challenges.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/controllers/challenges.js b/src/controllers/challenges.js index dc11abe5df..7f5014ec8e 100644 --- a/src/controllers/challenges.js +++ b/src/controllers/challenges.js @@ -106,20 +106,22 @@ api.create = function(req, res){ if (+req.body.prize > 0) { waterfall.push(function(cb){ var groupBalance = ((group.balance && group.leader==user._id) ? group.balance : 0); - if (req.body.prize > (user.balance*4 + groupBalance*4)) - return cb("Challenge.prize > (your gems + group balance). Purchase more gems or lower prize amount.s") + var prizeCost = req.body.prize/4; // I really should have stored user.balance as gems rather than dollars... stupid... + if (prizeCost > user.balance + groupBalance) + return cb("You can't afford this prize. Purchase more gems or lower the prize amount.") - var net = req.body.prize/4; // I really should have stored user.balance as gems rather than dollars... stupid... - - // user is group leader, and group has balance. Subtract from that first, then take the rest from user - if (groupBalance > 0) { - group.balance -= net; - if (group.balance < 0) { - net = Math.abs(group.balance); - group.balance = 0; - } - } - user.balance -= net; + if (groupBalance >= prizeCost) { + // Group pays for all of prize + group.balance -= prizeCost; + } else if (groupBalance > 0) { + // User pays remainder of prize cost after group + var remainder = prizeCost - group.balance; + group.balance = 0; + user.balance -= remainder; + } else { + // User pays for all of prize + user.balance -= prizeCost; + } cb(null) }); } @@ -344,4 +346,4 @@ api.unlink = function(req, res, next) { if (err) return res.json(500,{err:err}); res.send(200); }); -} \ No newline at end of file +}