Add validateParticipantID to participant routes

This commit is contained in:
rui hildt 2020-05-11 00:25:08 +02:00
parent 7a0b2b09ba
commit 03815bd2f6
2 changed files with 36 additions and 3 deletions

View File

@ -2,6 +2,7 @@ const express = require('express');
const router = express.Router(); const router = express.Router();
const { authenticate } = require('../../middlewares/authenticate'); const { authenticate } = require('../../middlewares/authenticate');
const { validateParticipantID } = require('../../middlewares/validateParticipantID');
const Participant = require('../models/participantModel'); const Participant = require('../models/participantModel');
router.post('/', authenticate, async (req, res) => { 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 data = { ...req.body };
const { account_id, meeting_id } = req.params; 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; const { account_id, meeting_id } = req.params;
try { try {
@ -64,7 +65,13 @@ router.get('/:account_id-:meeting_id', authenticate, async (req, res) => {
account_id, account_id,
meeting_id, meeting_id,
); );
if (typeof participant == 'undefined') {
res.status(404).json({
message: `Participant with id ${id} doesn't exist.`,
});
} else {
res.status(200).json(participant); res.status(200).json(participant);
}
} catch (error) { } catch (error) {
res.status(500).json({ res.status(500).json({
message: `Failed to get participant with id ${account_id}-${meeting_id}.`, message: `Failed to get participant with id ${account_id}-${meeting_id}.`,

View File

@ -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;