diff --git a/app/controllers/accounts.js b/app/controllers/accounts.js index 33fefa2..d1cc968 100644 --- a/app/controllers/accounts.js +++ b/app/controllers/accounts.js @@ -1,5 +1,5 @@ var mongoose = require('mongoose'), - ObjectId = mongoose.Schema.Types.ObjectId, + ObjectId = mongoose.Types.ObjectId, Account = mongoose.model('Account'), Entry = mongoose.model('Entry'), Handler = require('../helpers/handler'), @@ -65,6 +65,34 @@ var delete_account = function(account, callback) { }); }; +var get_balance = function(account_id, callback) { + Entry.aggregate() + .match({account_id: new ObjectId(account_id)}) + .group({_id : null, balance: { $sum: '$amount' }}) + .exec(callback); +}; + +var list_entries = function(account_id, entry, callback ) { + get_balance(account_id, function(errors, data) { + if( errors ) { + return callback(errors); + } + + Entry + .find({account_id: account_id}) + .sort({date: -1}) + .exec(function(errors, entries) { + if( errors ) { + return callback(errors); + } + return callback( null, { + entry: entry, + entries: entries, + balance: data[0].balance + }); + }); + }); +} module.exports = { create : function(request, response) { var user = request.user, @@ -142,18 +170,11 @@ module.exports = { if( errors ) { return Handler.errorHandler(errors, 400, response); } - - Entry - .find({account_id: account.id}) - .sort({date: -1}) - .exec(function(errors, entries) { - if( errors ) { - return Handler.errorHandler(errors, 500, response); - } - response.status(201).json({ - entry: entry, - entries: entries - }); + list_entries(account.id, entry, function(errors, data) { + if( errors ) { + return Handler.errorHandler(errors, 500, response); + } + return response.status(201).json(data); }); }); }); @@ -188,17 +209,11 @@ module.exports = { return Handler.errorHandler(errors, 400, response ); } - Entry - .find({account_id: account.id}) - .sort({date: -1}) - .exec(function(errors, entries) { + list_entries(account.id, entry, function(errors, data) { if( errors ) { return Handler.errorHandler(errors, 500, response); } - response.status(200).json({ - entry: entry, - entries: entries - }); + return response.status(200).json(data); }); }); }); @@ -224,18 +239,11 @@ module.exports = { if( errors ) { return Handler.errorHandler(errors, 500, response); } - - Entry - .find({account_id: account.id}) - .sort({date: -1}) - .exec(function(errors, entries) { + list_entries(account.id, entry, function(errors, data) { if( errors ) { return Handler.errorHandler(errors, 500, response); } - response.status(204).json({ - entry: entry, - entries: entries - }); + return response.status(200).json(data); }); }); }); diff --git a/test/accounts.js b/test/accounts.js index 7bf8d16..7a130b3 100644 --- a/test/accounts.js +++ b/test/accounts.js @@ -254,6 +254,8 @@ describe('API /accounts', function() { entries[0].type.should.be.equal('DEPOSIT'); entries[0].amount.should.be.equal(1000); + should.exist(result.body.balance); + done(); }); }); @@ -286,6 +288,7 @@ describe('API /accounts', function() { entries[0].type.should.be.equal('BILL'); entries[0].amount.should.be.equal(-1000); + should.exist(result.body.balance); done(); }); @@ -370,6 +373,8 @@ describe('API /accounts', function() { should.exist(entries); entries.should.be.instanceof(Array); + should.exist(result.body.balance); + done(); }); }); @@ -523,7 +528,11 @@ describe('API /accounts', function() { request(globalServer) .delete('/api/accounts/' + account_id + '/entries/' + entry_id) .set('Authorization', 'JWT ' + token) - .expect(204, done); + .expect(200) + .end(function(error, result) { + should.exist(result.body.balance); + done(); + }); }); });