Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
feat: initial deduction calculations and oic
Browse files Browse the repository at this point in the history
  • Loading branch information
kndonetm committed Nov 22, 2023
1 parent 33f1146 commit 021343a
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/routes/loans.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const router = Router()
// Import models
import Loan from '../models/loan.js'
import Loanee from '../models/loanee.js'
import LoanSettings from '../models/loan_settings.js'

// Ledger routes
import ledgerRouter from './loan-ledgers.js'
Expand Down Expand Up @@ -184,8 +185,12 @@ router.put('/new/:username', async (req, res, next) => {
*
* req.body must be of the form:
* {
* loanID: String
* approved: boolean
* oic: {
* last:
* middle:
* first:
* }
* }
*/
router.post('/review-application/:loanID', async (req, res, next) => {
Expand All @@ -210,34 +215,48 @@ router.post('/review-application/:loanID', async (req, res, next) => {
}
}

console.log(existingLoan.ledger, !existingLoan.ledger)
const settings = await LoanSettings.findOne().lean()

if (!settings[existingLoan.loanType]) {
return res.status(400).json({
message: 'No loan settings exist for the current loan type',
error: true
})
}

let deductions = 0

for (const deductionType of ['service_fee', 'capital_build_up', 'savings']) {
const deductionSetting = settings[existingLoan.loanType][deductionType]

if (deductionSetting.enabled && deductionSetting.unit === 'percentage') {
deductions += deductionSetting.value * existingLoan.originalLoanAmount * 0.01
} else if (deductionSetting.enabled) {
deductions += deductionSetting.value
}
}

console.log(req.body)

if (existingLoan.ledger.length === 0) {
query.$set.ledger = [
{
transcationID: Date.now().toString(36).toUpperCase(),
transactionDate: Date.now(),
submissionDate: Date.now(),
amountPaid: 500,
balance: existingLoan.originalLoanAmount - 500,
amountPaid: deductions,
balance: existingLoan.originalLoanAmount - deductions,
interestPaid: 0,
finesPaid: 0,
officerInCharge: {
last: 'not',
given: 'implemented yet'
}
officerInCharge: req.body.oic
}
]
}

console.log(query)

const ret = await Loan.updateOne({ loanID: req.params.loanID }, query, {
await Loan.updateOne({ loanID: req.params.loanID }, query, {
runValidators: true
})

console.log(ret)

return res.status(200).json({
message: `Loan application ${req.body.approved ? 'approved' : 'rejected'}`,
error: false
Expand Down

0 comments on commit 021343a

Please sign in to comment.