-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Create loan repayment controller - Create loan repayment model - Create dummy data for loan repayments - Add API route to /server/routes/index.js - Create chai test cases for API endpoint
- Loading branch information
1 parent
13604c0
commit 8254cc5
Showing
7 changed files
with
219 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import RepaymentsModel from '../model/repaymentsModel' | ||
import ResponseHelper from '../helpers/responseHelper' | ||
import errorStrings from '../helpers/errorStrings'; | ||
|
||
/** | ||
* @fileOverview - class manages all loan repayments | ||
* @class - RepaymentsController | ||
* @requires - ../model/RepaymentsModel | ||
* @requires - ../helpers/ResponseHelper | ||
* @requires - ../helpers/errorStrings | ||
* @exports - repaymentsController.js | ||
**/ | ||
|
||
class RepaymentsController { | ||
|
||
/** | ||
* Get a single loan's repayments | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} json response object | ||
*/ | ||
|
||
static getLoanRepayments (req, res) { | ||
|
||
const loanId = parseInt(req.params.loanId, 10); | ||
const loanRepayments = RepaymentsModel.getLoanRepayments(loanId); | ||
|
||
if (loanRepayments.length > 0){ | ||
return res.status(200).send({ | ||
status: 200, | ||
data: loanRepayments | ||
}); | ||
} else { | ||
ResponseHelper.errorResponse(res, errorStrings.noRepayments); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
export default RepaymentsController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @fileOverview - This module holds dummy data for repayments | ||
* @exports - repayments array of objects | ||
*/ | ||
const repayments = [ | ||
{ | ||
id: 1, | ||
createdOn: '01/05/2018 2:16:17 PM', | ||
loanId: 1, | ||
amount: '8400.00' | ||
}, | ||
{ | ||
id: 2, | ||
createdOn: '01/06/2018 2:16:17 PM', | ||
loanId: 1, | ||
amount: '8400.00' | ||
}, | ||
{ | ||
id: 3, | ||
createdOn: '01/07/2018 2:16:17 PM', | ||
loanId: 1, | ||
amount: '8400.00' | ||
}, | ||
{ | ||
id: 4, | ||
createdOn: '01/02/2019 2:16:17 PM', | ||
loanId: 2, | ||
amount: '12000.00' | ||
}, | ||
{ | ||
id: 5, | ||
createdOn: '01/03/2019 2:16:17 PM', | ||
loanId: 2, | ||
amount: '12000.00' | ||
}, | ||
{ | ||
id: 6, | ||
createdOn: '01/03/2017 2:15:17 PM', | ||
loanId: 3, | ||
amount: '1881600.00' | ||
}, | ||
{ | ||
id: 7, | ||
createdOn: '01/04/2017 2:19:17 AM', | ||
loanId: 3, | ||
amount: '1881600.00' | ||
}, | ||
{ | ||
id: 8, | ||
createdOn: '21/06/2017 3:15:47 PM', | ||
loanId: 3, | ||
amount: '1881600.00' | ||
}, | ||
{ | ||
id: 9, | ||
createdOn: '11/07/2017 11:45:14 AM', | ||
loanId: 3, | ||
amount: '1881600.00' | ||
} | ||
]; | ||
|
||
export default repayments; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import repayments from '../dummy/repayments' | ||
|
||
/** | ||
* @fileOverview - class manages all users data storage | ||
* @class - RepaymentsModel | ||
* @exports - repaymentsModel.js | ||
* @requires - ../helpers/loanHelper | ||
* @requires - ../dummy/repayments | ||
**/ | ||
|
||
class RepaymentsModel { | ||
|
||
/** | ||
* Get a single loan repayments | ||
* @param {object} req | ||
* @returns {object} an object with all repayments | ||
*/ | ||
|
||
static getLoanRepayments (loanId) { | ||
|
||
const loanRepayments = []; | ||
|
||
for ( let singleRepayment of repayments ){ | ||
if (singleRepayment.loanId === loanId ){ | ||
loanRepayments.push(singleRepayment); | ||
} | ||
} | ||
return loanRepayments; | ||
} | ||
|
||
} | ||
|
||
export default RepaymentsModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* eslint-disable no-undef */ | ||
import chaiHttp from 'chai-http'; | ||
import chai from 'chai'; | ||
import app from '../../app'; | ||
import testDb from './testDb' | ||
import errorStrings from '../helpers/errorStrings' | ||
|
||
chai.use(chaiHttp); | ||
chai.should(); | ||
|
||
let currentToken; | ||
const signinUrl = '/api/v1/auth/signin'; | ||
const repaymentsUrl = '/api/v1/loans/3/repayments'; | ||
|
||
describe('Loans Controller', () => { | ||
|
||
it('it should return authentication error', (done) => { | ||
chai.request(app) | ||
.get(repaymentsUrl) | ||
.end((error, res) => { | ||
res.should.have.status(401); | ||
res.body.should.be.a('object'); | ||
res.body.should.have.property('error'); | ||
res.body.error.should.equal(errorStrings.notAuthenticated); | ||
done(); | ||
}); | ||
}); | ||
|
||
before((done) => { | ||
chai.request(app) | ||
.post(signinUrl) | ||
.send(testDb.testUsers[7]) | ||
.end((error, res) => { | ||
currentToken = res.body.data.token; | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('GET /api/v1/loans', () => { | ||
|
||
it(`it should get loan repayments`, (done) => { | ||
chai.request(app) | ||
.get(repaymentsUrl) | ||
.set('token', currentToken) | ||
.end((error, res) => { | ||
res.should.have.status(200); | ||
res.body.should.be.a('object'); | ||
res.body.should.have.property('data'); | ||
res.body.data.should.be.a('array'); | ||
res.body.data.should.not.be.empty; | ||
res.body.data[0].should.be.a('object'); | ||
res.body.data[0].should.have.property('id'); | ||
res.body.data[0].should.have.property('createdOn'); | ||
res.body.data[0].should.have.property('loanId'); | ||
res.body.data[0].should.have.property('amount'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it(`it should return error if no repayment is found`, (done) => { | ||
chai.request(app) | ||
.get('/api/v1/loans/6/repayments') | ||
.set('token', currentToken) | ||
.end((error, res) => { | ||
res.should.have.status(406); | ||
res.body.should.be.a('object'); | ||
res.body.should.have.property('error'); | ||
res.body.error.should.equal(errorStrings.noRepayments); | ||
done(); | ||
}); | ||
}); | ||
|
||
|
||
}); | ||
|
||
|
||
|
||
}); |