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

Commit

Permalink
feat: automatic deposit interest calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
kndonetm committed Nov 26, 2023
1 parent ff0615c commit 98d7571
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/models/depositTransactionSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const DepositTransactionSchema = new Schema({
immutable: true
},
ORNumber: {
type: String,
required: true
type: String
},
transactionDate: {
type: Date,
Expand Down
4 changes: 2 additions & 2 deletions src/schedules/agenda.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { DEFAULT_MONGODB_URI } from '../db/default_uri.js'
// Configure Agenda
const agenda = new Agenda({
ensureIndex: true,
// processEvery: '10 minutes',
processEvery: '5 seconds',
processEvery: '10 minutes',
// processEvery: '5 seconds',
db: { address: process.env.MONGODB_URI || DEFAULT_MONGODB_URI }
// TODO: Use existing mongoose connection
})
Expand Down
77 changes: 74 additions & 3 deletions src/schedules/jobs/deposit-interests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,78 @@ const name = 'process-deposit-interests'
const handler = async (job, done) => {
console.log('Updating deposit interests...')

const deposits = await Deposit.find({})
const deposits = await Deposit.find({
nextInterestDate: {
$lte: Date.now()
}
}).lean()
parseDecimal(deposits)

const allSettings = await DepositSettings.findOne().lean()
parseDecimal(allSettings)

for (const deposit of deposits) {
const depositSettings = allSettings[deposit.category]
let interest = Decimal('0')

if (depositSettings.interest_rate.unit === 'Fixed') {
interest = interest.add(depositSettings.interest_rate.value)
} else {
interest = interest.add(
Decimal(depositSettings.interest_rate.value).mul('0.01').mul(deposit.runningAmount)
)
}

const newBalance = round2(interest.add(deposit.runningAmount))
interest = round2(interest)
const submissionDate = Date.now()

const transaction = {
transactionID: Date.now().toString(36).toUpperCase(),
transactionDate: Date.now(),
submissionDate: submissionDate,
transactionType: 'Deposit',
amount: 0,
interest: parseFloat(interest),
balance: parseFloat(newBalance),
officerInCharge: {
given: 'Admin',
middle: '',
last: ' '
}
}

const timeSetting = depositSettings.time

const timeConversions = {
days: 1,
months: 30,
years: 365
}
const nextInterestDate = moment(deposit.nextInterestDate)
.add(timeSetting.value * timeConversions[timeSetting.type], 'days')
.set({
hour: 0,
minute: 0,
second: 0,
millisecond: 0
})
.toDate()

const query = {
$push: { ledger: transaction },
$set: {
runningAmount: parseFloat(newBalance),
nextInterestDate: nextInterestDate
}
}

const ret = await Deposit.updateOne({ depositID: deposit.depositID }, query, {
runValidators: true
})
}

console.log(`Successfully updated ${deposits.length} deposit interests.`)

done()
}
Expand All @@ -30,6 +101,6 @@ const round2 = function (decimal) {
return decimal.mul('100').round().mul('0.01')
}

// const every = '0 1 * * *' // Every 1:00 AM (to avoid desync problems with date checks)
const every = '5 seconds'
const every = '0 1 * * *' // Every 1:00 AM (to avoid desync problems with date checks)
// const every = '5 seconds'
export default { name, handler, every }
4 changes: 2 additions & 2 deletions src/schedules/jobs/loan-interests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ moment().format()
const name = 'process-loan-interests'

const handler = async (job, done) => {
console.log('Updating loan interests...')

const loans = await Loan.find({
nextInterestDate: {
$lte: Date.now()
Expand All @@ -18,8 +20,6 @@ const handler = async (job, done) => {
const allSettings = await LoanSettings.findOne().lean()
parseDecimal(allSettings)

console.log('Updating loan interests...')

for (const loan of loans) {
const loanSettings = allSettings[loan.loanType]
let interest = Decimal('0')
Expand Down

0 comments on commit 98d7571

Please sign in to comment.