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

Commit

Permalink
refactor: use $push operator in creating transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwigoric committed Nov 19, 2023
1 parent 319f95c commit 82acf04
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
15 changes: 8 additions & 7 deletions src/routes/deposit-ledgers.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,18 @@ router.put('/', async (req, res, next) => {

if (!deposit) return res.status(404).json({ error: true, message: 'Deposit not found' })

const { ledger } = deposit

// Add transaction to ledger
ledger.push({
// Construct transaction info
const transactionInfo = {
...req.body,
transactionID: Date.now().toString(36).toUpperCase()
})
}

try {
// Update deposit
await Deposit.updateOne({ deleted: false, depositID }, { $set: { ledger } })
// Add transaction to ledger
await Deposit.updateOne(
{ deleted: false, depositID },
{ $push: { ledger: transactionInfo } }
)

// Return transaction
return res.status(200).json({ error: false, message: 'Transaction successfully added' })
Expand Down
12 changes: 5 additions & 7 deletions src/routes/loan-ledgers.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,15 @@ router.put('/', (req, res, next) => {

if (!loan) return res.status(404).json({ error: true, message: 'Loan not found' })

const { ledger } = loan

// Add transaction to ledger
ledger.push({
// Construct transaction info
const transactionInfo = {
...req.body,
transactionID: Date.now().toString(36).toUpperCase()
})
}

try {
// Update loan
await Loan.updateOne({ deleted: false, loanID }, { $set: { ledger } })
// Add transaction to ledger
await Loan.updateOne({ deleted: false, loanID }, { $push: { ledger: transactionInfo } })

// Return a 200 response
return res.status(200).json({ error: false, message: 'Transaction successfully added' })
Expand Down

0 comments on commit 82acf04

Please sign in to comment.