This repository has been archived by the owner on Sep 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add job for extending due dates
- Loading branch information
Showing
3 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import loanInterests from './loan-interests.js' | ||
import loanDueDates from './loan-due-dates.js' | ||
|
||
export default { | ||
loanInterests | ||
loanInterests, | ||
loanDueDates | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Schema | ||
import Loan from '../../models/loan.js' | ||
|
||
const name = 'extend-loan-due-dates' | ||
|
||
const handler = async (job, done) => { | ||
const loans = await Loan.find({ dueDate: { $lt: new Date() } }).lean() | ||
|
||
// Iterate through loans | ||
for (const rawLoan of loans) { | ||
const loan = await rawLoan | ||
if (!loan.isPaidForCurrentPeriod) { | ||
// TODO: Add a transaction for penalty | ||
continue | ||
} | ||
|
||
// Extend due date | ||
const dueDate = new Date(loan.dueDate) | ||
dueDate.setDate(dueDate.getDate() + 1) | ||
if (loan.paymentFrequency === 'weekly') { | ||
dueDate.setDate(dueDate.getDate() + 6) | ||
} else if (loan.paymentFrequency === 'monthly') { | ||
dueDate.setMonth(dueDate.getMonth() + 1) | ||
dueDate.setDate(dueDate.getDate() - 1) | ||
} | ||
|
||
// Update loan | ||
await Loan.updateOne({ loanID: loan.loanID }, { dueDate, isPaidForCurrentPeriod: false }) | ||
} | ||
|
||
done() | ||
} | ||
|
||
const every = '0 0 * * *' // Every 12:00 AM | ||
|
||
export default { name, handler, every } |