Skip to content

Commit

Permalink
Resolves CjS77#4
Browse files Browse the repository at this point in the history
  • Loading branch information
sudsy committed Oct 5, 2018
1 parent 189a442 commit 038e1c3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/handlers/getBalance.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports.handler = function getBalance(req, res, next) {
throw new AleError(`Book with id ${id} does not exist`, codes.BookDoesNotExist);
}
const { account, perPage, page } = req.query;
const inQuote = req.query.inQuoteCurrency !== "false";
const inQuote = req.query.inQuoteCurrency !== false;
return book.getBalance({ account, perPage, page }, inQuote);
}).then(balance => {
res.json(balance);
Expand Down
9 changes: 7 additions & 2 deletions models/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,19 @@ Book.prototype.getJournalEntries = function(query) {
*/
Book.prototype.getBalance = function(query, inQuoteCurrency = false) {
query = parseQuery(this.getDataValue('id'), query);
const credit = inQuoteCurrency ? sequelize.literal('credit * "exchangeRate"') : sequelize.col('credit');
const debit = inQuoteCurrency ? sequelize.literal('debit * "exchangeRate"') : sequelize.col('debit');
const delim= sequelize.getDialect() == 'mysql' ? '`' : '"';
const credit = inQuoteCurrency ? sequelize.literal(`credit * ${delim}exchangeRate${delim}`) : sequelize.col('credit');
const debit = inQuoteCurrency ? sequelize.literal(`debit * ${delim}exchangeRate${delim}`) : sequelize.col('debit');

query.attributes = [
[sequelize.fn('SUM', credit), 'creditTotal'],
[sequelize.fn('SUM', debit), 'debitTotal'],
[sequelize.fn('COUNT', sequelize.col('id')), 'numTransactions'],
[sequelize.fn('MAX', sequelize.col('currency')), 'currency']
];



return Transaction.findAll(query).then(result => {
result = result.shift();
if (!result) {
Expand Down

0 comments on commit 038e1c3

Please sign in to comment.