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

Commit

Permalink
feat: add job for extending due dates
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwigoric committed Nov 24, 2023
1 parent 859c7d1 commit 49f9caa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/schedules/agenda.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ process.on('SIGINT', graceful)

async function start() {
await agenda.start()
for (const jobInfo of Object.values(jobs)) await agenda.every(jobInfo.every, jobInfo.name)
for (const jobInfo of Object.values(jobs))
if (jobInfo.every) await agenda.every(jobInfo.every, jobInfo.name)
}
await start()

Expand Down
4 changes: 3 additions & 1 deletion src/schedules/jobs/index.js
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
}
36 changes: 36 additions & 0 deletions src/schedules/jobs/loan-due-dates.js
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 }

0 comments on commit 49f9caa

Please sign in to comment.