Skip to content

Commit

Permalink
Merge 8254cc5 into 13604c0
Browse files Browse the repository at this point in the history
  • Loading branch information
shonubijerry committed May 2, 2019
2 parents 13604c0 + 8254cc5 commit c34ff18
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 2 deletions.
2 changes: 1 addition & 1 deletion server/controllers/loansController.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class LoansController {
* @returns {object} json response object
*/

static getAllLoans (req, res) {
static getUserLoans (req, res) {

const userEmail = req.token.user.email;
const allLoans = loansModel.getUserLoans(userEmail);
Expand Down
41 changes: 41 additions & 0 deletions server/controllers/repaymentsController.js
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;
62 changes: 62 additions & 0 deletions server/dummy/repayments.js
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;
1 change: 1 addition & 0 deletions server/helpers/errorStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const errorStrings = {
notAuthenticated: 'You must login to have access to this feature',
validTenor: 'Tenor must range from 1 to 12 months',
validAmount: 'Amount must be digits',
noRepayments: "There are currently no repayments for this loan",

}

Expand Down
33 changes: 33 additions & 0 deletions server/model/repaymentsModel.js
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;
4 changes: 3 additions & 1 deletion server/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import UsersController from '../controllers/usersController'
import LoansController from '../controllers/loansController'
import RepaymentsController from '../controllers/repaymentsController'
import ValidateUser from '../middleware/ValidateUser'
import Auth from '../middleware/Auth'
import ValidateLoans from '../middleware/ValidateLoans';
Expand All @@ -23,7 +24,8 @@ const routes = (app) => {
app.post('/api/v1/auth/signin', ValidateUser.validateSignin, UsersController.signin);
app.post('/api/v1/loans', Auth.authenticateUser, ValidateLoans.validateApplication, LoansController.createLoan);

app.get('/api/v1/loans', Auth.authenticateUser, LoansController.getAllLoans);
app.get('/api/v1/loans', Auth.authenticateUser, LoansController.getUserLoans);
app.get('/api/v1/loans/:loanId/repayments', Auth.authenticateUser, RepaymentsController.getLoanRepayments);

//declare 404 route
app.all('*', (req, res) =>
Expand Down
78 changes: 78 additions & 0 deletions server/tests/repaymentsTest.js
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();
});
});


});



});

0 comments on commit c34ff18

Please sign in to comment.