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

Commit

Permalink
feat: set up flow for marking loan as released
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwigoric committed Nov 23, 2023
1 parent 2802cb6 commit 6c97fa3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/models/loan.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ const LoanSchema = new Schema({
dueDate: {
type: Date
},
releaseDate: {
type: Date
},
isPaidForCurrentPeriod: {
type: Boolean,
default: false
},
coborrower: {
name: NameSchema,
birthday: Date,
Expand Down
44 changes: 23 additions & 21 deletions src/routes/loans.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,31 +301,33 @@ router.patch('/:loanID', async (req, res, next) => {
const { loanID } = req.params

const existingLoan = await Loan.findOne({ loanID })
if (!existingLoan) {
if (!existingLoan)
return res.status(404).json({ message: 'Loan application does not exist' })
} else {
// Do not edit loan ledgers, loan IDs, submission dates, or approval dates.
const loanInfo = { ...req.body }
if (loanInfo.ledger) {
delete loanInfo.ledger
}
delete loanInfo.loanID
delete loanInfo.submissionDate
delete loanInfo.approvalDate
delete loanInfo.originalLoanAmount

if (
Object.entries(loanInfo.coborrower.name).every(([, val]) => {
return val === '' || val === null
})
) {
loanInfo.coborrower = null
}

await Loan.updateOne({ loanID }, loanInfo, { runValidators: true })
// Do not edit loan ledgers, loan IDs, submission dates, or approval dates.
const loanInfo = { ...req.body }
if (loanInfo.ledger) {
delete loanInfo.ledger
}
delete loanInfo.loanID
delete loanInfo.submissionDate
delete loanInfo.approvalDate
delete loanInfo.originalLoanAmount

if (existingLoan.status === 'approved' && loanInfo.status === 'released')
loanInfo.releaseDate = Date.now()

return res.json({ message: 'Loan application successfully edited', error: false })
if (
Object.entries(loanInfo.coborrower.name).every(([, val]) => {
return val === '' || val === null
})
) {
loanInfo.coborrower = null
}

await Loan.updateOne({ loanID }, loanInfo, { runValidators: true })

return res.json({ message: 'Loan application successfully edited', error: false })
} catch (error) {
if (error.name === 'ValidationError') {
return res.status(400).json({
Expand Down

0 comments on commit 6c97fa3

Please sign in to comment.