From 399a8717d3b5a7ed0ec1a99edc5656e875c2e96a Mon Sep 17 00:00:00 2001 From: febbweiss Date: Fri, 14 Aug 2015 14:35:00 +0000 Subject: [PATCH] Feature: returns a list of entries when creating, editing or deleting an entry --- app/controllers/accounts.js | 41 ++++++++++++++++++++++++++++++---- app/models/entry.js | 3 +++ test/accounts.js | 44 +++++++++++++++++++++++++++---------- 3 files changed, 72 insertions(+), 16 deletions(-) diff --git a/app/controllers/accounts.js b/app/controllers/accounts.js index e24d39f..33fefa2 100644 --- a/app/controllers/accounts.js +++ b/app/controllers/accounts.js @@ -143,8 +143,19 @@ module.exports = { return Handler.errorHandler(errors, 400, response); } - response.status(201).json(entry); - }) + 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 + }); + }); + }); }); }, @@ -177,7 +188,18 @@ module.exports = { return Handler.errorHandler(errors, 400, response ); } - return response.json(entry); + Entry + .find({account_id: account.id}) + .sort({date: -1}) + .exec(function(errors, entries) { + if( errors ) { + return Handler.errorHandler(errors, 500, response); + } + response.status(200).json({ + entry: entry, + entries: entries + }); + }); }); }); }); @@ -203,7 +225,18 @@ module.exports = { return Handler.errorHandler(errors, 500, response); } - return response.status(204).end(); + Entry + .find({account_id: account.id}) + .sort({date: -1}) + .exec(function(errors, entries) { + if( errors ) { + return Handler.errorHandler(errors, 500, response); + } + response.status(204).json({ + entry: entry, + entries: entries + }); + }); }); }); }); diff --git a/app/models/entry.js b/app/models/entry.js index acea4d2..19ea251 100644 --- a/app/models/entry.js +++ b/app/models/entry.js @@ -16,5 +16,8 @@ var EntrySchema = new Schema({ created_at: {type: Date, default: Date.now} }); +EntrySchema.index({account_id: 1, date: -1, type: 1}); +EntrySchema.index({account_id: 1, date: -1, category: 1, sub_category: 1}); + var Entry = mongoose.model('Entry', EntrySchema); module.exports = Entry; \ No newline at end of file diff --git a/test/accounts.js b/test/accounts.js index 0221ce9..7bf8d16 100644 --- a/test/accounts.js +++ b/test/accounts.js @@ -231,7 +231,7 @@ describe('API /accounts', function() { .post('/api/accounts/' + account_id + '/entries') .send({ amount: 1000, - date: new Date('2014-12-08') + date: new Date('2015-08-14') }) .set('Authorization', 'JWT ' + token) .expect(201) @@ -239,13 +239,21 @@ describe('API /accounts', function() { .end(function(error, result) { should.not.exist(error); - var entry = result.body; + var entry = result.body.entry; should.exist(entry); entry.amount.should.be.equal(1000); - new Date(entry.date).should.eql(new Date(2014, 11, 8)); + new Date(entry.date).should.eql(new Date(2015, 7, 14)); entry.type.should.be.equal('DEPOSIT'); should.not.exist(entry.category); should.not.exist(entry.sub_category); + + var entries = result.body.entries; + should.exist(entries); + entries.should.be.instanceof(Array).and.have.lengthOf(2); + new Date(entries[0].date).should.eql(new Date('2015-08-14')) + entries[0].type.should.be.equal('DEPOSIT'); + entries[0].amount.should.be.equal(1000); + done(); }); }); @@ -255,7 +263,7 @@ describe('API /accounts', function() { .send({ label: 'test', amount: -1000, - date: new Date('2014-12-08') + date: new Date('2015-08-15') }) .set('Authorization', 'JWT ' + token) .expect(201) @@ -263,13 +271,22 @@ describe('API /accounts', function() { .end(function(error, result) { should.not.exist(error); - var entry = result.body; + var entry = result.body.entry; should.exist(entry); entry.amount.should.be.equal(-1000); - new Date(entry.date).should.eql(new Date(2014, 11, 8)); + new Date(entry.date).should.eql(new Date(2015, 7, 15)); entry.type.should.be.equal('BILL'); should.not.exist(entry.category); should.not.exist(entry.sub_category); + + var entries = result.body.entries; + should.exist(entries); + entries.should.be.instanceof(Array).and.have.lengthOf(3); + new Date(entries[0].date).should.eql(new Date('2015-08-15')) + entries[0].type.should.be.equal('BILL'); + entries[0].amount.should.be.equal(-1000); + + done(); }); }); @@ -329,7 +346,7 @@ describe('API /accounts', function() { }) .set('Authorization', 'JWT ' + token) .end(function(error, result) { - var entry_id = result.body._id; + var entry_id = result.body.entry._id; request(globalServer) .put('/api/accounts/' + account_id + '/entries/' + entry_id) .send({ @@ -343,12 +360,16 @@ describe('API /accounts', function() { .end( function(errors, result) { should.not.exist(errors); - var entry = result.body; + var entry = result.body.entry; should.exist(entry); entry.label.should.be.equal('modified'); entry.amount.should.be.equal(55); new Date(entry.date).should.eql(new Date(2014,11,9)); + var entries = result.body.entries; + should.exist(entries); + entries.should.be.instanceof(Array); + done(); }); }); @@ -364,7 +385,7 @@ describe('API /accounts', function() { }) .set('Authorization', 'JWT ' + token) .end(function(error, result) { - var entry_id = result.body._id; + var entry_id = result.body.entry._id; request(globalServer) .put('/api/accounts/' + account_id + '/entries/' + entry_id) .set('Authorization', 'JWT ' + token) @@ -382,7 +403,6 @@ describe('API /accounts', function() { }) .set('Authorization', 'JWT ' + token) .end(function(error, result) { - var entry_id = result.body._id; request(globalServer) .put('/api/accounts/' + account_id + '/entries/' + token) .send({ @@ -499,7 +519,7 @@ describe('API /accounts', function() { }) .set('Authorization', 'JWT ' + token) .end(function(error, result) { - var entry_id = result.body._id; + var entry_id = result.body.entry._id; request(globalServer) .delete('/api/accounts/' + account_id + '/entries/' + entry_id) .set('Authorization', 'JWT ' + token) @@ -531,7 +551,7 @@ describe('API /accounts', function() { }) .set('Authorization', 'JWT ' + token) .end(function(error, result) { - var entry_id = result.body._id; + var entry_id = result.body.entry._id; request(globalServer) .delete('/api/accounts/' + account_id + '/entries/' + entry_id) .set('Authorization', 'JWT ' + hacker_token)