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

Commit

Permalink
feat: deposit ledger routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwigoric committed Nov 14, 2023
1 parent aa74724 commit 1044e94
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 5 deletions.
163 changes: 163 additions & 0 deletions src/routes/deposit-ledgers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Packages
import { Router } from 'express'
import passport from 'passport'

// Initialize router
const router = Router()

// Models
import Deposit from '../models/deposit.js'

// Routes

/**
* GET /
*
* Get all deposits
*/
router.get('/', async (req, res, next) => {
passport.authenticate('is-manager', { session: false }, async (err, manager, info) => {
if (err) return next(err)
if (!manager) return res.status(401).json(info)

const { depositID } = req

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

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

const { ledger } = deposit
return res.status(200).json({ ledger, error: false })
})(req, res, next)
})

/**
* GET /:txID
*
* Get information of a transaction
*/
router.get('/:txID', async (req, res, next) => {
passport.authenticate('is-manager', { session: false }, async (err, manager, info) => {
if (err) return next(err)
if (!manager) return res.status(401).json(info)

const { depositID } = req
const { txID } = req.params

// Retrieve deposit
const deposit = await Deposit.findOne({ deleted: false, depositID }).lean()

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

const { ledger } = deposit

// Find transaction
const transaction = ledger.find((tx) => tx.transactionID === txID)

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

// Return transaction
return res.status(200).json({ transaction, error: false })
})(req, res, next)
})

/**
* PUT /
*
* Create a new transaction
*/
router.put('/', async (req, res, next) => {
passport.authenticate('is-manager', { session: false }, async (err, manager, info) => {
if (err) return next(err)
if (!manager) return res.status(401).json(info)

const { depositID } = req

// Retrieve deposit
const deposit = await Deposit.findOne({ deleted: false, depositID }).lean()

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

const { ledger } = deposit

// Add transaction to ledger
ledger.push(req.body)

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

// Return transaction
return res.status(200).json({ error: false, message: 'Transaction successfully added' })
} catch (error) {
if (error.name === 'ValidationError') {
return res.status(400).json({
message: error.errors[Object.keys(error.errors)[0]].message,
error: true
})
}
return next(error)
}
})(req, res, next)
})

/**
* PATCH /:txID
*
* Update a transaction
*/
router.patch('/:txID', async (req, res, next) => {
passport.authenticate('is-manager', { session: false }, async (err, manager, info) => {
if (err) return next(err)
if (!manager) return res.status(401).json(info)

const { depositID } = req
const { txID } = req.params

// Retrieve deposit
const deposit = await Deposit.findOne({ deleted: false, depositID }).lean()

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

const { ledger } = deposit

// Find transaction
const transaction = ledger.find((tx) => tx.transactionID === txID)

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

const query = {
'ledger.$.ORNumber': req.body.ORNumber,
'ledger.$.transactionDate': req.body.transactionDate,
'ledger.$.amountReceived': req.body.amountReceived,
'ledger.$.amountWithdrawn': req.body.amountWithdrawn,
'ledger.$.interest': req.body.interest,
'ledger.$.balance': req.body.balance
}

try {
// Update deposit
await Deposit.updateOne(
{ deleted: false, depositID, 'ledger.transactionID': txID },
{ $set: query }
)

// Return transaction
return res
.status(200)
.json({ error: false, message: 'Transaction successfully updated' })
} catch (error) {
if (error.name === 'ValidationError') {
return res.status(400).json({
message: error.errors[Object.keys(error.errors)[0]].message,
error: true
})
}
return next(error)
}
})(req, res, next)
})

export default router
11 changes: 11 additions & 0 deletions src/routes/deposits.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ const router = Router()
import Loanee from '../models/loanee.js'
import Deposit from '../models/deposit.js'

// Ledger routes
import ledgerRouter from './deposit-ledgers.js'
router.use(
'/:depositID/ledger',
(req, res, next) => {
req.depositID = req.params.depositID
next()
},
ledgerRouter
)

/**
* GET /
*
Expand Down
8 changes: 3 additions & 5 deletions src/routes/loan-ledgers.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ router.patch('/:txID', (req, res, next) => {
if (!transaction)
return res.status(404).json({ error: true, message: 'Transaction not found' })

let query = {
const query = {
'ledger.$.ORNumber': req.body.ORNumber,
'ledger.$.paymentDate': req.body.paymentDate,
'ledger.$.submissionDate': req.body.submissionDate,
Expand All @@ -132,11 +132,9 @@ router.patch('/:txID', (req, res, next) => {

try {
// Update transaction
const k = await Loan.updateOne(
await Loan.updateOne(
{ deleted: false, loanID, 'ledger.transactionID': txID },
{
$set: query
}
{ $set: query }
)

// Return a 200 response
Expand Down

0 comments on commit 1044e94

Please sign in to comment.