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

Commit

Permalink
feat: deposit rounding errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kndonetm committed Nov 23, 2023
1 parent 1bda438 commit 24e27e4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/models/deposit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import packages
import { Schema, model } from 'mongoose'
import { Schema, model, Decimal128 } from 'mongoose'
import { v5 as uuidV5 } from 'uuid'

// Import schema
Expand All @@ -21,11 +21,11 @@ const DepositSchema = new Schema({
immutable: true
},
interestRate: {
type: Number,
type: Decimal128,
required: true
},
originalDepositAmount: {
type: Number,
type: Decimal128,
required: true
},
ledger: [DepositTransactionSchema],
Expand Down
8 changes: 4 additions & 4 deletions src/models/depositTransactionSchema.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import packages
import { Schema } from 'mongoose'
import { Schema, Decimal128 } from 'mongoose'

import NameSchema from './nameSchema.js'

Expand Down Expand Up @@ -33,15 +33,15 @@ const DepositTransactionSchema = new Schema({
}
},
amount: {
type: Number,
type: Decimal128,
required: true
},
interest: {
type: Number,
type: Decimal128,
required: true
},
balance: {
type: Number,
type: Decimal128,
required: true
},
officerInCharge: {
Expand Down
16 changes: 16 additions & 0 deletions src/routes/deposit-ledgers.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ router.get('/', async (req, res, next) => {
if (!deposit) return res.status(404).json({ error: true, message: 'Deposit not found' })

const { ledger } = deposit

parseDecimal(ledger)
return res.status(200).json({ ledger, error: false })
})(req, res, next)
})
Expand Down Expand Up @@ -59,6 +61,8 @@ router.get('/:txID', async (req, res, next) => {
if (!transaction)
return res.status(404).json({ error: true, message: 'Transaction not found' })

parseDecimal(transaction)

// Return transaction
return res.status(200).json({ transaction, error: false })
})(req, res, next)
Expand Down Expand Up @@ -174,4 +178,16 @@ router.patch('/:txID', async (req, res, next) => {
})(req, res, next)
})

// Helper functions
// https://stackoverflow.com/questions/53369688/extract-decimal-from-decimal128-with-mongoose-mongodb
const parseDecimal = (v, i, prev) => {
if (v !== null && typeof v === 'object') {
if (v.constructor.name === 'Decimal128') prev[i] = parseFloat(v)
else
Object.entries(v).forEach(([key, value]) =>
parseDecimal(value, key, prev ? prev[i] : v)
)
}
}

export default router
17 changes: 17 additions & 0 deletions src/routes/deposits.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ router.get('/', async (req, res, next) => {

const deposits = await Deposit.find({ deleted: false }).select('-__v -_id').lean()

parseDecimal(deposits)

return res.status(200).json({ deposits, error: false })
})(req, res, next)
})
Expand All @@ -51,6 +53,7 @@ router.get('/:depositID', async (req, res, next) => {
.lean()

if (deposit) {
parseDecimal(deposit)
return res.status(200).json({ deposit, error: false })
} else {
return res.status(400).json({ message: 'Deposit ID does not exist', error: true })
Expand Down Expand Up @@ -81,6 +84,8 @@ router.get('/user/:username', async (req, res, next) => {
.select('-__v -_id')
.lean()

parseDecimal(deposits)

return res.status(200).json({ deposits, error: false })
} catch (error) {
console.error(error)
Expand Down Expand Up @@ -232,4 +237,16 @@ router.delete('/:depositID', async (req, res, next) => {
})(req, res, next)
})

// Helper functions
// https://stackoverflow.com/questions/53369688/extract-decimal-from-decimal128-with-mongoose-mongodb
const parseDecimal = (v, i, prev) => {
if (v !== null && typeof v === 'object') {
if (v.constructor.name === 'Decimal128') prev[i] = parseFloat(v)
else
Object.entries(v).forEach(([key, value]) =>
parseDecimal(value, key, prev ? prev[i] : v)
)
}
}

export default router
6 changes: 0 additions & 6 deletions src/routes/loans.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,6 @@ router.post('/:loanID/review', async (req, res, next) => {
]
}

console.log(
deductions.neg(),
deductions.neg().add(existingLoan.balance),
deductions.neg().add(existingLoan.balance).toString()
)

if (existingLoan.balance && req.body.approved) {
query.$set.balance = deductions.neg().add(existingLoan.balance).toString()
}
Expand Down

0 comments on commit 24e27e4

Please sign in to comment.