mirror of
https://github.com/Febbweiss/CloudBudget.git
synced 2026-03-04 22:35:38 +00:00
Feature: add balance in entry creation, modification and deletion response
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
var mongoose = require('mongoose'),
|
var mongoose = require('mongoose'),
|
||||||
ObjectId = mongoose.Schema.Types.ObjectId,
|
ObjectId = mongoose.Types.ObjectId,
|
||||||
Account = mongoose.model('Account'),
|
Account = mongoose.model('Account'),
|
||||||
Entry = mongoose.model('Entry'),
|
Entry = mongoose.model('Entry'),
|
||||||
Handler = require('../helpers/handler'),
|
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 = {
|
module.exports = {
|
||||||
create : function(request, response) {
|
create : function(request, response) {
|
||||||
var user = request.user,
|
var user = request.user,
|
||||||
@@ -142,18 +170,11 @@ module.exports = {
|
|||||||
if( errors ) {
|
if( errors ) {
|
||||||
return Handler.errorHandler(errors, 400, response);
|
return Handler.errorHandler(errors, 400, response);
|
||||||
}
|
}
|
||||||
|
list_entries(account.id, entry, function(errors, data) {
|
||||||
Entry
|
|
||||||
.find({account_id: account.id})
|
|
||||||
.sort({date: -1})
|
|
||||||
.exec(function(errors, entries) {
|
|
||||||
if( errors ) {
|
if( errors ) {
|
||||||
return Handler.errorHandler(errors, 500, response);
|
return Handler.errorHandler(errors, 500, response);
|
||||||
}
|
}
|
||||||
response.status(201).json({
|
return response.status(201).json(data);
|
||||||
entry: entry,
|
|
||||||
entries: entries
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -188,17 +209,11 @@ module.exports = {
|
|||||||
return Handler.errorHandler(errors, 400, response );
|
return Handler.errorHandler(errors, 400, response );
|
||||||
}
|
}
|
||||||
|
|
||||||
Entry
|
list_entries(account.id, entry, function(errors, data) {
|
||||||
.find({account_id: account.id})
|
|
||||||
.sort({date: -1})
|
|
||||||
.exec(function(errors, entries) {
|
|
||||||
if( errors ) {
|
if( errors ) {
|
||||||
return Handler.errorHandler(errors, 500, response);
|
return Handler.errorHandler(errors, 500, response);
|
||||||
}
|
}
|
||||||
response.status(200).json({
|
return response.status(200).json(data);
|
||||||
entry: entry,
|
|
||||||
entries: entries
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -224,18 +239,11 @@ module.exports = {
|
|||||||
if( errors ) {
|
if( errors ) {
|
||||||
return Handler.errorHandler(errors, 500, response);
|
return Handler.errorHandler(errors, 500, response);
|
||||||
}
|
}
|
||||||
|
list_entries(account.id, entry, function(errors, data) {
|
||||||
Entry
|
|
||||||
.find({account_id: account.id})
|
|
||||||
.sort({date: -1})
|
|
||||||
.exec(function(errors, entries) {
|
|
||||||
if( errors ) {
|
if( errors ) {
|
||||||
return Handler.errorHandler(errors, 500, response);
|
return Handler.errorHandler(errors, 500, response);
|
||||||
}
|
}
|
||||||
response.status(204).json({
|
return response.status(200).json(data);
|
||||||
entry: entry,
|
|
||||||
entries: entries
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -254,6 +254,8 @@ describe('API /accounts', function() {
|
|||||||
entries[0].type.should.be.equal('DEPOSIT');
|
entries[0].type.should.be.equal('DEPOSIT');
|
||||||
entries[0].amount.should.be.equal(1000);
|
entries[0].amount.should.be.equal(1000);
|
||||||
|
|
||||||
|
should.exist(result.body.balance);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -286,6 +288,7 @@ describe('API /accounts', function() {
|
|||||||
entries[0].type.should.be.equal('BILL');
|
entries[0].type.should.be.equal('BILL');
|
||||||
entries[0].amount.should.be.equal(-1000);
|
entries[0].amount.should.be.equal(-1000);
|
||||||
|
|
||||||
|
should.exist(result.body.balance);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -370,6 +373,8 @@ describe('API /accounts', function() {
|
|||||||
should.exist(entries);
|
should.exist(entries);
|
||||||
entries.should.be.instanceof(Array);
|
entries.should.be.instanceof(Array);
|
||||||
|
|
||||||
|
should.exist(result.body.balance);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -523,7 +528,11 @@ describe('API /accounts', function() {
|
|||||||
request(globalServer)
|
request(globalServer)
|
||||||
.delete('/api/accounts/' + account_id + '/entries/' + entry_id)
|
.delete('/api/accounts/' + account_id + '/entries/' + entry_id)
|
||||||
.set('Authorization', 'JWT ' + token)
|
.set('Authorization', 'JWT ' + token)
|
||||||
.expect(204, done);
|
.expect(200)
|
||||||
|
.end(function(error, result) {
|
||||||
|
should.exist(result.body.balance);
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user