Feature: returns a list of entries when creating, editing or deleting an entry

This commit is contained in:
2015-08-14 14:35:00 +00:00
parent ffbc4f4980
commit 399a8717d3
3 changed files with 72 additions and 16 deletions

View File

@@ -143,8 +143,19 @@ module.exports = {
return Handler.errorHandler(errors, 400, response); 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 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 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
});
});
}); });
}); });
}); });

View File

@@ -16,5 +16,8 @@ var EntrySchema = new Schema({
created_at: {type: Date, default: Date.now} 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); var Entry = mongoose.model('Entry', EntrySchema);
module.exports = Entry; module.exports = Entry;

View File

@@ -231,7 +231,7 @@ describe('API /accounts', function() {
.post('/api/accounts/' + account_id + '/entries') .post('/api/accounts/' + account_id + '/entries')
.send({ .send({
amount: 1000, amount: 1000,
date: new Date('2014-12-08') date: new Date('2015-08-14')
}) })
.set('Authorization', 'JWT ' + token) .set('Authorization', 'JWT ' + token)
.expect(201) .expect(201)
@@ -239,13 +239,21 @@ describe('API /accounts', function() {
.end(function(error, result) { .end(function(error, result) {
should.not.exist(error); should.not.exist(error);
var entry = result.body; var entry = result.body.entry;
should.exist(entry); should.exist(entry);
entry.amount.should.be.equal(1000); 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'); entry.type.should.be.equal('DEPOSIT');
should.not.exist(entry.category); should.not.exist(entry.category);
should.not.exist(entry.sub_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(); done();
}); });
}); });
@@ -255,7 +263,7 @@ describe('API /accounts', function() {
.send({ .send({
label: 'test', label: 'test',
amount: -1000, amount: -1000,
date: new Date('2014-12-08') date: new Date('2015-08-15')
}) })
.set('Authorization', 'JWT ' + token) .set('Authorization', 'JWT ' + token)
.expect(201) .expect(201)
@@ -263,13 +271,22 @@ describe('API /accounts', function() {
.end(function(error, result) { .end(function(error, result) {
should.not.exist(error); should.not.exist(error);
var entry = result.body; var entry = result.body.entry;
should.exist(entry); should.exist(entry);
entry.amount.should.be.equal(-1000); 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'); entry.type.should.be.equal('BILL');
should.not.exist(entry.category); should.not.exist(entry.category);
should.not.exist(entry.sub_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(); done();
}); });
}); });
@@ -329,7 +346,7 @@ describe('API /accounts', function() {
}) })
.set('Authorization', 'JWT ' + token) .set('Authorization', 'JWT ' + token)
.end(function(error, result) { .end(function(error, result) {
var entry_id = result.body._id; var entry_id = result.body.entry._id;
request(globalServer) request(globalServer)
.put('/api/accounts/' + account_id + '/entries/' + entry_id) .put('/api/accounts/' + account_id + '/entries/' + entry_id)
.send({ .send({
@@ -343,12 +360,16 @@ describe('API /accounts', function() {
.end( function(errors, result) { .end( function(errors, result) {
should.not.exist(errors); should.not.exist(errors);
var entry = result.body; var entry = result.body.entry;
should.exist(entry); should.exist(entry);
entry.label.should.be.equal('modified'); entry.label.should.be.equal('modified');
entry.amount.should.be.equal(55); entry.amount.should.be.equal(55);
new Date(entry.date).should.eql(new Date(2014,11,9)); 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(); done();
}); });
}); });
@@ -364,7 +385,7 @@ describe('API /accounts', function() {
}) })
.set('Authorization', 'JWT ' + token) .set('Authorization', 'JWT ' + token)
.end(function(error, result) { .end(function(error, result) {
var entry_id = result.body._id; var entry_id = result.body.entry._id;
request(globalServer) request(globalServer)
.put('/api/accounts/' + account_id + '/entries/' + entry_id) .put('/api/accounts/' + account_id + '/entries/' + entry_id)
.set('Authorization', 'JWT ' + token) .set('Authorization', 'JWT ' + token)
@@ -382,7 +403,6 @@ describe('API /accounts', function() {
}) })
.set('Authorization', 'JWT ' + token) .set('Authorization', 'JWT ' + token)
.end(function(error, result) { .end(function(error, result) {
var entry_id = result.body._id;
request(globalServer) request(globalServer)
.put('/api/accounts/' + account_id + '/entries/' + token) .put('/api/accounts/' + account_id + '/entries/' + token)
.send({ .send({
@@ -499,7 +519,7 @@ describe('API /accounts', function() {
}) })
.set('Authorization', 'JWT ' + token) .set('Authorization', 'JWT ' + token)
.end(function(error, result) { .end(function(error, result) {
var entry_id = result.body._id; var entry_id = result.body.entry._id;
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)
@@ -531,7 +551,7 @@ describe('API /accounts', function() {
}) })
.set('Authorization', 'JWT ' + token) .set('Authorization', 'JWT ' + token)
.end(function(error, result) { .end(function(error, result) {
var entry_id = result.body._id; var entry_id = result.body.entry._id;
request(globalServer) request(globalServer)
.delete('/api/accounts/' + account_id + '/entries/' + entry_id) .delete('/api/accounts/' + account_id + '/entries/' + entry_id)
.set('Authorization', 'JWT ' + hacker_token) .set('Authorization', 'JWT ' + hacker_token)