diff --git a/src/models/deposit.js b/src/models/deposit.js index d91e216..8368e43 100644 --- a/src/models/deposit.js +++ b/src/models/deposit.js @@ -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 @@ -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], diff --git a/src/models/depositTransactionSchema.js b/src/models/depositTransactionSchema.js index 103e48b..c467874 100644 --- a/src/models/depositTransactionSchema.js +++ b/src/models/depositTransactionSchema.js @@ -1,5 +1,5 @@ // Import packages -import { Schema } from 'mongoose' +import { Schema, Decimal128 } from 'mongoose' import NameSchema from './nameSchema.js' @@ -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: { diff --git a/src/routes/deposit-ledgers.js b/src/routes/deposit-ledgers.js index 41b060f..6140450 100644 --- a/src/routes/deposit-ledgers.js +++ b/src/routes/deposit-ledgers.js @@ -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) }) @@ -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) @@ -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 diff --git a/src/routes/deposits.js b/src/routes/deposits.js index ea65ffe..9afca2c 100644 --- a/src/routes/deposits.js +++ b/src/routes/deposits.js @@ -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) }) @@ -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 }) @@ -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) @@ -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 diff --git a/src/routes/loans.js b/src/routes/loans.js index c73097d..4b0c897 100644 --- a/src/routes/loans.js +++ b/src/routes/loans.js @@ -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() }