Skip to content

Commit

Permalink
Merge branch 'Payment' of https://github.com/Rafsani/KAIZEN into Payment
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornplusplus committed Jul 28, 2021
2 parents 09d12f4 + b83fb9e commit eacc684
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
74 changes: 73 additions & 1 deletion server/controller/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const authInterface = require('../db/interfaces/authInterface');
const loanInterface = require('../db/interfaces/loanInterface');
const paymentInterface = require('../db/interfaces/paymentInterface');
const reportInterface = require('../db/interfaces/reportInterface');
const date = require('../util/date')


/**
Expand Down Expand Up @@ -462,6 +463,76 @@ const handleGETLenderInfo = async (req,res,next)=>{
}


/**
* @description this method returns the user's loan history
* @route - GET /api/user/:userId/loan
* @param {*} req
* @param {*} res
* @param {*} next
*/
const handleGETUserLoanHistory = async( req,res,next)=>{
try {
// make sure the user making the request has the same id as the one's data we are fetching
/** not yet implemented */
const loanQueryResult = await loanInterface.getAllLoansForUser({
Receiver : req.params.userId
});

let output = [];

if( loanQueryResult.status == 'OK' ){

loanQueryResult.data.forEach( loan => {

let contracts = [];

loan.contracts.forEach( contract => {
contracts.push({
contractId: contract._id,
contractStatus: contract.status,
lenderName: contract.lenderId.username,
contractSigningDate: date.contractSigningDate( contract.installmentDates ),
bkash: contract.lenderId.bkash,
installmentsCompleted: contract.installmentsCompleted,
loanAmount: contract.amount,
repaidAmount: contract.collectedAmount
});
});

output.push({
loanId: loan._id,
loanStatus: loan.Status,
receiverName: loan.Receiver.username,
type: loan.typeOfLoan,
details: loan.Details,
issuedDate: loan.issueDate,
requiredAmount: loan.Amount,
collectedAmount: loan.collectedAmount,
interestRate: ( loan.Receiver.verfiedStatus ) ? 5 : 8,
defaultedInstallments: loan.Receiver.loanDefaults,

contracts
});
});

return res.status(200).send({
data: output,
status: 'OK',
message: "All loans for this user have been found"
});
}

return res.status(400).send(loanQueryResult);

}catch(e){
return res.status(500).send({
status: 'EXCEPTION',
message: e.message
});
}
}


module.exports = {
handleGETUserById,
handleGETUserHistory,
Expand All @@ -470,5 +541,6 @@ module.exports = {
handleGETUserTransactionHistoryById,
handleGETUserSearchBase,
handlePOSTReport,
handleGETUserReportsById
handleGETUserReportsById,
handleGETUserLoanHistory
}
37 changes: 36 additions & 1 deletion server/db/interfaces/loanInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,41 @@ const LoanRequest = require('../models/LoanRequestModel');
}
}

/**
* @description list of all loans for a user
* @returns all loans
*/
const getAllLoansForUser = async( filter , populate, select )=> {
try {
const loans = await LoanRequest.find(filter)
.populate({
path: 'contracts',
populate: 'lenderId'
})
.populate('Receiver')
.sort({issueDate: -1});

if( loans.length !== 0 ){
return {
data: loans,
status: 'OK',
message: 'All loans for this user found from the database'
}
}

return {
data: null,
status: 'ERROR',
message: 'No loans found'
}
}catch( e ){
return {
data: null,
status: 'EXCEPTION',
message: e.message
};
}
}

module.exports = {
getAllLoans,
Expand All @@ -385,5 +419,6 @@ module.exports = {
acceptContractOffer,
denyContractOffer,
lastIssuedLoan,
repayLoan
repayLoan,
getAllLoansForUser
}
3 changes: 3 additions & 0 deletions server/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ router.route('/:userId/history')
router.route('/:userId/report')
.get( userController.handleGETUserReportsById );

router.route('/:userId/loan')
.get( userController.handleGETUserLoanHistory );

router.route('/search/:userId' )
.get( userController.handleGETUserSearchBase );

Expand Down

0 comments on commit eacc684

Please sign in to comment.