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

Commit

Permalink
refactor: make deposit routes more intuitive
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwigoric committed Nov 23, 2023
1 parent b186fa5 commit 5c40597
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/routes/deposits.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ router.get('/', async (req, res, next) => {
*
* Get a deposit given its deposit ID
*/
router.get('/get/:depositid', async (req, res, next) => {
router.get('/:depositID', 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 deposit = await Deposit.findOne({ deleted: false, depositID: req.params.depositid })
const deposit = await Deposit.findOne({ deleted: false, depositID: req.params.depositID })
.select('-__v -_id')
.lean()

Expand All @@ -63,7 +63,7 @@ router.get('/get/:depositid', async (req, res, next) => {
*
* Get all deposits of a member given their username
*/
router.get('/:username', async (req, res, next) => {
router.get('/user/:username', async (req, res, next) => {
passport.authenticate('is-manager', { session: false }, async (err, manager, info) => {
try {
if (err) return next(err)
Expand Down Expand Up @@ -103,7 +103,7 @@ router.get('/:username', async (req, res, next) => {
* original deposit amount
* }
*/
router.put('/new/:username', async (req, res, next) => {
router.put('/user/:username', 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)
Expand Down Expand Up @@ -144,20 +144,22 @@ router.put('/new/:username', async (req, res, next) => {
})

/**
* POST /edit-deposit
* PATCH /:depositID
*
* Edit a deposit
*
* req.body contains the data of the deposit to edit. Finds a deposit in the database using depositID.
* NOTE: Does not edit deposit IDs.
*/
router.post('/edit-deposit', async (req, res, next) => {
router.patch('/:depositID', 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)

try {
const existingDeposit = await Deposit.findOne({ depositID: req.body.depositID })
const { depositID } = req.params

const existingDeposit = await Deposit.findOne({ depositID })
if (!existingDeposit) {
return res.status(400).json({ message: 'Deposit application does not exist' })
} else {
Expand All @@ -169,9 +171,7 @@ router.post('/edit-deposit', async (req, res, next) => {
delete depositInfo.depositID
delete depositInfo.approvalDate

await Deposit.updateOne({ loanID: req.body.loanID }, depositInfo, {
runValidators: true
})
await Deposit.updateOne({ depositID }, depositInfo, { runValidators: true })

return res.json({ message: 'Loan application successfully edited', error: false })
}
Expand All @@ -189,28 +189,26 @@ router.post('/edit-deposit', async (req, res, next) => {
})

/**
* POST /delete-deposit
* DELETE /:depositID
*
* Delete a deposit.
*
* Request body contains: {
* depositID: deposit ID of the deposit to be deleted
* }
*
* This functionality only soft deletes the deposit.
*/
router.post('/delete-deposit', async (req, res, next) => {
router.delete('/:depositID', 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)

try {
const existingDeposit = await Deposit.findOne({ depositID: req.body.depositID })
const { depositID } = req.params

const existingDeposit = await Deposit.findOne({ depositID })
if (!existingDeposit) {
return res.status(400).json({ message: 'Deposit does not exist' })
return res.status(404).json({ message: 'Deposit does not exist' })
} else {
await Deposit.updateOne(
{ DepositID: req.body.DepositID },
{ depositID },
{
deleted: true
}
Expand Down

0 comments on commit 5c40597

Please sign in to comment.