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

Commit

Permalink
feat: add notification settings routes
Browse files Browse the repository at this point in the history
  • Loading branch information
kndonetm committed Nov 20, 2023
1 parent 07e96d2 commit 4e6db3a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/routes/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import passport from 'passport'
// Schema
import LoanSettings from '../models/loan_settings.js'
import DepositSettings from '../models/deposit_settings.js'
import NotificationSettings from '../models/notificationSettings.js'

// Initialize router
const router = Router()
Expand Down Expand Up @@ -75,4 +76,37 @@ router.patch('/deposits', async (req, res, next) => {
})(req, res, next)
})

// Routes for Notifications
router.get('/notifications', 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({ message: info.message })

const settings = await NotificationSettings.findOne().select('-_id -__v')

return res.status(200).json({ settings })
})(req, res, next)
})

router.patch('/notifications', async (req, res, next) => {
passport.authenticate('admin', { session: false }, async (err, admin, info) => {
if (err) return next(err)
if (!admin) return res.status(401).json({ message: info.message })

try {
await NotificationSettings.findOneAndUpdate({}, req.body).select('-_id -__v')

return res.status(200).json({ error: false, message: 'Updated deposit settings' })
} 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

0 comments on commit 4e6db3a

Please sign in to comment.