From 6c97fa300f87d96c9d86e8bc4d3a43b033e29421 Mon Sep 17 00:00:00 2001 From: Joshua Permito Date: Thu, 23 Nov 2023 11:36:30 +0800 Subject: [PATCH] feat: set up flow for marking loan as released --- src/models/loan.js | 7 +++++++ src/routes/loans.js | 44 +++++++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/models/loan.js b/src/models/loan.js index 4d82a8e..ab0b63c 100644 --- a/src/models/loan.js +++ b/src/models/loan.js @@ -61,6 +61,13 @@ const LoanSchema = new Schema({ dueDate: { type: Date }, + releaseDate: { + type: Date + }, + isPaidForCurrentPeriod: { + type: Boolean, + default: false + }, coborrower: { name: NameSchema, birthday: Date, diff --git a/src/routes/loans.js b/src/routes/loans.js index e31975b..ca1285a 100644 --- a/src/routes/loans.js +++ b/src/routes/loans.js @@ -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({