diff --git a/api/routes/participantRoute.js b/api/routes/participantRoute.js index d22aa43..ad84250 100644 --- a/api/routes/participantRoute.js +++ b/api/routes/participantRoute.js @@ -2,6 +2,7 @@ const express = require('express'); const router = express.Router(); const { authenticate } = require('../../middlewares/authenticate'); +const { validateParticipantID } = require('../../middlewares/validateParticipantID'); const Participant = require('../models/participantModel'); router.post('/', authenticate, async (req, res) => { @@ -18,7 +19,7 @@ router.post('/', authenticate, async (req, res) => { } }); -router.put('/:account_id-:meeting_id', authenticate, async (req, res) => { +router.put('/:account_id-:meeting_id', authenticate, validateParticipantID, async (req, res) => { const data = { ...req.body }; const { account_id, meeting_id } = req.params; @@ -37,7 +38,7 @@ router.put('/:account_id-:meeting_id', authenticate, async (req, res) => { } }); -router.delete('/:account_id-:meeting_id', authenticate, async (req, res) => { +router.delete('/:account_id-:meeting_id', authenticate, validateParticipantID, async (req, res) => { const { account_id, meeting_id } = req.params; try { @@ -64,7 +65,13 @@ router.get('/:account_id-:meeting_id', authenticate, async (req, res) => { account_id, meeting_id, ); - res.status(200).json(participant); + if (typeof participant == 'undefined') { + res.status(404).json({ + message: `Participant with id ${id} doesn't exist.`, + }); + } else { + res.status(200).json(participant); + } } catch (error) { res.status(500).json({ message: `Failed to get participant with id ${account_id}-${meeting_id}.`, diff --git a/middlewares/validateParticipantID.js b/middlewares/validateParticipantID.js new file mode 100644 index 0000000..de9e25b --- /dev/null +++ b/middlewares/validateParticipantID.js @@ -0,0 +1,26 @@ +const Participant = require('../api/models/participantModel'); + +async function validateParticipantID(req, res, next) { + const { account_id, meeting_id } = req.params; + + try { + const participant = await Participant.getParticipantById( + account_id, + meeting_id, + ); + if (typeof participant == 'undefined') { + res.status(404).json({ + message: `Participant with id ${id} doesn't exist.`, + }); + } else { + next(); + } + } catch (error) { + res.status(500).json({ + message: `Failed to get participant with id ${account_id}-${meeting_id}.`, + error, + }); + } +} + +exports.validateParticipantID = validateParticipantID; \ No newline at end of file