Skip to content

Commit

Permalink
feature(loans): show all loans for specific user
Browse files Browse the repository at this point in the history
- filter loans based on userid field
  • Loading branch information
chidimo committed May 28, 2019
1 parent a96df7e commit 6f72ea3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
20 changes: 15 additions & 5 deletions controllers/LoansController.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@ import

const loans_model = new Model('loans');
const repayments_model = new Model('repayments');
const rows = `id, userid, createdon, status, repaid, useremail,
amount, tenor, interest, balance, paymentinstallment`;

const LoansController = {

get_user_loans: async (req, res) => {
const { id } = req.params;
const clause = `WHERE userid=${id}`;
try {
const data = await loans_model.select(rows, clause);
return res.status(200).json({ data: data.rows });
}
catch (e) { return InternalServerError(res, e); }
},
get_all_loans: async (req, res) => {
const { status, repaid } = req.query;
const rows = `id, userid, createdon, status, repaid, useremail,
amount, tenor, interest, balance, paymentinstallment`;
// const rows = ;
const clause = `WHERE status='${status}' AND repaid='${repaid}'`;
dev_logger(`rep ${repaid}, ${typeof repaid}`);
try {
let data;
if (status && repaid) {
data = await loans_model.select(rows,
`WHERE status='${status}' AND repaid='${repaid}'`
);
data = await loans_model.select(rows, clause);
}
else {
data = await loans_model.select(rows);
Expand Down
6 changes: 6 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ router.get('/loans',
AuthenticationMiddleware.verifyToken,
LoansController.get_all_loans
);

router.get('/loans/user/:id',
AuthenticationMiddleware.verifyToken,
LoansController.get_user_loans
);

router.get('/loans/:id',
AuthenticationMiddleware.verifyToken,
LoansController.get_loan
Expand Down
14 changes: 14 additions & 0 deletions test/loans-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ describe('/loans', () => {
done();
});
});

it('should return all loans for specified user', done => {
const id = 1;
server
.get(`${BASE_URL}/loans/user/${id}`)
.expect(200)
.end((err, res) => {
res.status.should.equal(200);
for (const each of res.body.data) {
each.should.have.property('userid', id);
}
done();
});
});
});

describe('/loans: Get loan', () => {
Expand Down

0 comments on commit 6f72ea3

Please sign in to comment.