Skip to content

Commit

Permalink
Merge pull request hackerspace-bootstrap#276 from NicoHood/transfer_fix
Browse files Browse the repository at this point in the history
Added transaction backend support
  • Loading branch information
schinken authored Feb 8, 2018
2 parents 6a755ed + f86bd3e commit 4ff5eda
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ The following data structure describes the transaction:
````
{
value: <float>,
comment: <string>
comment: <string>,
toUserId: <int>
}
````

Expand Down
90 changes: 65 additions & 25 deletions lib/routes/UserIdTransaction.post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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';
Expand Down

0 comments on commit 4ff5eda

Please sign in to comment.