From f86bd3ed7f691ac4f16d8d2ad345b824f597b5b7 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sun, 4 Feb 2018 13:43:55 +0100 Subject: [PATCH] Added transaction backend support --- README.md | 3 +- lib/routes/UserIdTransaction.post.js | 90 ++++++++++++++++++++-------- 2 files changed, 67 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 8196957..8f03505 100644 --- a/README.md +++ b/README.md @@ -242,7 +242,8 @@ The following data structure describes the transaction: ```` { value: , - comment: + comment: , + toUserId: } ```` diff --git a/lib/routes/UserIdTransaction.post.js b/lib/routes/UserIdTransaction.post.js index 5ca89bf..cf9e176 100644 --- a/lib/routes/UserIdTransaction.post.js +++ b/lib/routes/UserIdTransaction.post.js @@ -27,6 +27,7 @@ TransactionCreate.prototype.route = function (req, res, next) { var value = parseFloat(req.body.value); var userId = req.params.userId; var comment = req.body.comment; + var toUserId = req.body.toUserId; if (isNaN(value)) { return next(new errors.InvalidRequestError('not a number: ' + req.body.value)); @@ -38,40 +39,79 @@ TransactionCreate.prototype.route = function (req, res, next) { return next(new errors.InternalServerError('previous stage result missing')); } - if (value < configuration.boundaries.transaction.lower) { + if (value < configuration.boundaries.transaction.lower || toUserId && ((value * -1) < configuration.boundaries.transaction.lower)) { return next(new errors.ForbiddenError('transaction value of ' + value + ' falls below the transaction minimum of ' + configuration.boundaries.transaction.lower)); } - if (value > configuration.boundaries.transaction.upper) { + if (value > configuration.boundaries.transaction.upper || toUserId && ((value * -1) > configuration.boundaries.transaction.upper)) { return next(new errors.ForbiddenError('transaction value of ' + value + ' exceeds the transaction maximum of ' + configuration.boundaries.transaction.upper)); } - var user = req.strichliste.result.content(); - var newBalance = user.balance + value; - if (value < 0 && newBalance < configuration.boundaries.account.lower) { - return next(new errors.ForbiddenError('transaction value of ' + value + ' leads to an overall account balance of ' + newBalance + ' which goes below the lower account limit of ' + configuration.boundaries.account.lower)); + if (toUserId) { + that._persistence.loadUserById(toUserId, function (error, targetUser) { + if (error) return next(new errors.InternalServerError(error.message)); + if (!targetUser) return next(new errors.NotFoundError('user ' + toUserId + ' not found')); + + var newTargetUserBalance = targetUser.balance - value; + + if (value > 0 && newTargetUserBalance < configuration.boundaries.account.lower) { + return next(new errors.ForbiddenError('transaction value of ' + (-1 * value) + ' leads to an overall account balance of ' + newTargetUserBalance + 'which goes below the lower account limit of ' + configuration.boundaries.account.lower)); + } + if (value < 0 && newTargetUserBalance > configuration.boundaries.account.upper) { + return next(new errors.ForbiddenError('transaction value of ' + (-1 * value) + ' leads to an overall account balance of ' + newTargetUserBalance + ' which goes beyond the upper account limit of ' + configuration.boundaries.account.upper)); + } + + seq() + .seq(function () { + that._persistence.createTransaction(toUserId, -1 * value, comment, this); + }) + .seq(function (transactionId) { + that._persistence.loadTransaction(transactionId, function (error, result) { + if (error) return next(new errors.InternalServerError('error retrieving transaction: ' + transactionId)); + + that._mqttWrapper.publishTransactionValue(value); + + req.strichliste.result = new Result(result, Result.CONTENT_TYPE_JSON, 201); + end(); + }); + }) + .catch(function (error) { + next(new errors.InternalServerError('unexpected: ' + error.message)); + }); + }); } - - if (value > 0 && newBalance > configuration.boundaries.account.upper) { - return next(new errors.ForbiddenError('transaction value of ' + value + ' leads to an overall account balance of ' + newBalance + ' which goes beyond the upper account limit of ' + configuration.boundaries.account.upper)); + else { + end(); } - seq() - .seq(function () { - that._persistence.createTransaction(userId, value, comment, this); - }) - .seq(function (transactionId) { - that._persistence.loadTransaction(transactionId, function (error, result) { - if (error) return next(new errors.InternalServerError('error retrieving transaction: ' + transactionId)); - - that._mqttWrapper.publishTransactionValue(value); - - req.strichliste.result = new Result(result, Result.CONTENT_TYPE_JSON, 201); - next(); + function end (){ + var user = req.strichliste.result.content(); + var newBalance = user.balance + value; + if (value < 0 && newBalance < configuration.boundaries.account.lower) { + return next(new errors.ForbiddenError('transaction value of ' + value + ' leads to an overall account balance of ' + newBalance + ' which goes below the lower account limit of ' + configuration.boundaries.account.lower)); + } + + if (value > 0 && newBalance > configuration.boundaries.account.upper) { + return next(new errors.ForbiddenError('transaction value of ' + value + ' leads to an overall account balance of ' + newBalance + ' which goes beyond the upper account limit of ' + configuration.boundaries.account.upper)); + } + + seq() + .seq(function () { + that._persistence.createTransaction(userId, value, comment, this); + }) + .seq(function (transactionId) { + that._persistence.loadTransaction(transactionId, function (error, result) { + if (error) return next(new errors.InternalServerError('error retrieving transaction: ' + transactionId)); + + that._mqttWrapper.publishTransactionValue(value); + + req.strichliste.result = new Result(result, Result.CONTENT_TYPE_JSON, 201); + next(); + }); + }) + .catch(function (error) { + next(new errors.InternalServerError('unexpected: ' + error.message)); }); - }) - .catch(function (error) { - next(new errors.InternalServerError('unexpected: ' + error.message)); - }); + }; }; TransactionCreate.routeName = 'UserIdTransaction.post';