Skip to content

Commit

Permalink
refactered model and fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abarnhard committed Aug 11, 2014
1 parent 40fc7e6 commit 33e6908
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
4 changes: 2 additions & 2 deletions app/controllers/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ exports.index = function(req, res){
};

exports.show = function(req, res){
Account.findById(req.params.id, function(account){
Account.findById(req.params.id, {}, function(account){
res.render('accounts/show', {account:account, moment:moment, helper:accountHelper});
});
};

exports.transactionInit = function(req, res){
Account.findByIdLite(req.params.id, function(account){
Account.findById(req.params.id, {fields:{name:1, type:1}}, function(account){
res.render('accounts/transaction', {account:account});
});
};
Expand Down
21 changes: 13 additions & 8 deletions app/models/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ Account.findAll = function(cb){
});
};

Account.findById = function(id, cb){
Account.findById = function(id, fields, cb){
id = makeOid(id);
Account.collection.findOne({_id:id}, function(err, account){
// attach associated transfer objects for display
async.map(account.transferIds, function(tId, done){
makeTransfer(tId, done, account.name);
}, function(err, transfers){
account.transfers = transfers;
Account.collection.findOne({_id:id}, fields, function(err, account){
if(!Object.keys(fields).length){
// attach associated transfer objects for display
async.map(account.transferIds, function(tId, done){
makeTransfer(tId, done, account.name);
}, function(err, transfers){
account.transfers = transfers;
cb(account);
});
}else{
cb(account);
});
}
});
};

Expand Down Expand Up @@ -123,6 +127,7 @@ function makeOid(id){

function makeTransfer(tId, cb, name){
Transfer.findById(tId, function(err, transfer){
//
if(transfer.from === name){
transfer.from = '';
}else{
Expand Down
30 changes: 13 additions & 17 deletions test/unit/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('Account', function(){
});
describe('.findById', function(){
it('should return one account from database', function(done){
Account.findById(bobId, function(a){
Account.findById(bobId, {}, function(a){
expect(a.name).to.equal('Bob');
expect(a.transfers).to.have.length(4);
done();
Expand All @@ -78,14 +78,10 @@ describe('Account', function(){
});
});
});
describe('.deposit', function(){
});
describe('.withdraw', function(){
});
describe('.transaction', function(){
it('should deposit funds & increase account balance', function(done){
Account.transaction({id:bobId, type:'deposit', pin:'1234', amount:'500'}, function(){
Account.findById(bobId, function(a){
Account.findById(bobId, {}, function(a){
expect(a.balance).to.be.closeTo(1000, 0.1);
expect(a.numTransacts).to.equal(3);
expect(a.transactions).to.have.length(3);
Expand All @@ -97,7 +93,7 @@ describe('Account', function(){
});
it('should not deposit or increase account balance (incorrect PIN)', function(done){
Account.transaction({id:bobId, type:'deposit', pin:'1236', amount:'500'}, function(){
Account.findById(bobId, function(a){
Account.findById(bobId, {}, function(a){
expect(a.balance).to.be.closeTo(500, 0.1);
expect(a.numTransacts).to.equal(2);
expect(a.transactions).to.have.length(2);
Expand All @@ -107,7 +103,7 @@ describe('Account', function(){
});
it('should perform a withdrawal', function(done){
Account.transaction({id:bobId, type:'withdraw', pin:'1234', amount:'250'}, function(){
Account.findById(bobId, function(a){
Account.findById(bobId, {}, function(a){
expect(a.balance).to.be.closeTo(250, 0.1);
expect(a.numTransacts).to.equal(3);
expect(a.transactions).to.have.length(3);
Expand All @@ -120,7 +116,7 @@ describe('Account', function(){
});
it('should withdraw & reduce balance by amount', function(done){
Account.transaction({id:bobId, type:'withdraw', pin:'1234', amount:'250'}, function(){
Account.findById(bobId, function(a){
Account.findById(bobId, {}, function(a){
expect(a.balance).to.be.closeTo(250, 0.1);
expect(a.numTransacts).to.equal(3);
expect(a.transactions).to.have.length(3);
Expand All @@ -133,7 +129,7 @@ describe('Account', function(){
});
it('should not reduce balance by amount (incorrect PIN)', function(done){
Account.transaction({id:bobId, type:'withdraw', pin:'1235', amount:'250'}, function(){
Account.findById(bobId, function(a){
Account.findById(bobId, {}, function(a){
expect(a.balance).to.be.closeTo(500, 0.1);
expect(a.numTransacts).to.equal(2);
expect(a.transactions).to.have.length(2);
Expand All @@ -143,7 +139,7 @@ describe('Account', function(){
});
it('should reduce balance by amount & charge 50 overdraft', function(done){
Account.transaction({id:bobId, type:'withdraw', pin:'1234', amount:'550'}, function(){
Account.findById(bobId, function(a){
Account.findById(bobId, {}, function(a){
expect(a.balance).to.be.closeTo(-100, 0.1);
expect(a.numTransacts).to.equal(3);
expect(a.transactions).to.have.length(3);
Expand All @@ -158,10 +154,10 @@ describe('Account', function(){
describe('.transfer', function(){
it('should transfer funds from one account to another', function(done){
Account.transfer({from:'Bob', to:'Jim', pin:'1234', fromId:bobId, toId:jimId, amount:'250'}, function(){
Account.findById(bobId, function(a){
Account.findById(bobId, {}, function(a){
expect(a.balance).to.be.closeTo(225, 0.1);
expect(a.transferIds).to.have.length(5);
Account.findById(jimId, function(a2){
Account.findById(jimId, {}, function(a2){
expect(a2.balance).to.be.closeTo(350, 0.1);
expect(a2.transferIds).to.have.length(5);
done();
Expand All @@ -171,10 +167,10 @@ describe('Account', function(){
});
it('should not transfer funds from one account to another (wrong PIN)', function(done){
Account.transfer({from:'Bob', to:'Jim', pin:'1264', fromId:bobId, toId:jimId, amount:'250'}, function(){
Account.findById(bobId, function(a){
Account.findById(bobId, {}, function(a){
expect(a.balance).to.be.closeTo(500, 0.1);
expect(a.transferIds).to.have.length(4);
Account.findById(jimId, function(a2){
Account.findById(jimId, {}, function(a2){
expect(a2.balance).to.be.closeTo(100, 0.1);
expect(a2.transferIds).to.have.length(4);
done();
Expand All @@ -184,10 +180,10 @@ describe('Account', function(){
});
it('should not transfer funds from one account to another (Not enough money)', function(done){
Account.transfer({from:'Bob', to:'Jim', pin:'1234', fromId:bobId, toId:jimId, amount:'1000'}, function(){
Account.findById(bobId, function(a){
Account.findById(bobId, {}, function(a){
expect(a.balance).to.be.closeTo(500, 0.1);
expect(a.transferIds).to.have.length(4);
Account.findById(jimId, function(a2){
Account.findById(jimId, {}, function(a2){
expect(a2.balance).to.be.closeTo(100, 0.1);
expect(a2.transferIds).to.have.length(4);
done();
Expand Down

0 comments on commit 33e6908

Please sign in to comment.