Add validateMeetingID middleware to meeting routes

This commit is contained in:
rui hildt 2020-05-11 00:18:09 +02:00
parent e8d8d9e42f
commit 7a0b2b09ba
3 changed files with 88 additions and 69 deletions

View File

@ -26,11 +26,12 @@ router.put('/:id', authenticate, validateAccountID, async (req, res) => {
const data = { ...req.body };
const id = req.params.id;
try {
if (data.password) {
const hash = bcrypt.hashSync(data.password, 10);
data.password = hash;
}
try {
const account = await Account.updateAccount(data, id);
res.status(200).json(...account);
} catch (error) {
@ -57,7 +58,11 @@ router.delete('/:id', authenticate, validateAccountID, async (req, res) => {
}
});
router.get('/:id/meetings', authenticate, validateAccountID, async (req, res) => {
router.get(
'/:id/meetings',
authenticate,
validateAccountID,
async (req, res) => {
const { id } = req.params;
try {
@ -75,7 +80,8 @@ router.get('/:id/meetings', authenticate, validateAccountID, async (req, res) =>
error,
});
}
});
},
);
router.get('/:id', authenticate, async (req, res) => {
const id = req.params.id;

View File

@ -4,6 +4,7 @@ const bcrypt = require('bcryptjs');
const { v4: uuidv4 } = require('uuid');
const { authenticate } = require('../../middlewares/authenticate');
const { validateMeetingID } = require('../../middlewares/validateMeetingID');
const Meeting = require('../models/meetingModel');
router.post('/', authenticate, async (req, res) => {
@ -23,7 +24,7 @@ router.post('/', authenticate, async (req, res) => {
}
});
router.put('/:id', authenticate, async (req, res) => {
router.put('/:id', authenticate, validateMeetingID, async (req, res) => {
const data = { ...req.body };
const id = req.params.id;
@ -43,7 +44,7 @@ router.put('/:id', authenticate, async (req, res) => {
}
});
router.delete('/:id', authenticate, async (req, res) => {
router.delete('/:id', authenticate, validateMeetingID, async (req, res) => {
const id = req.params.id;
try {
@ -79,16 +80,14 @@ router.get('/:id', authenticate, async (req, res) => {
}
});
router.get('/:id/participants', authenticate, async (req, res) => {
router.get(
'/:id/participants',
authenticate,
validateMeetingID,
async (req, res) => {
const id = req.params.id;
try {
const meeting = await Meeting.getMeetingById(id);
if (typeof meeting == 'undefined') {
res.status(404).json({
message: `Meeting with id ${id} could not be found.`,
});
} else {
const participants = await Meeting.getParticipantsByMeetingId(id);
if (participants.length == 0) {
res.status(200).json({
@ -97,25 +96,23 @@ router.get('/:id/participants', authenticate, async (req, res) => {
} else {
res.status(200).json(participants);
}
}
} catch (error) {
res.status(500).json({
message: `Failed to fetch participants for meeting with id ${id}.`,
error,
});
}
});
},
);
router.get('/:id/possible-dates', authenticate, async (req, res) => {
router.get(
'/:id/possible-dates',
authenticate,
validateMeetingID,
async (req, res) => {
const id = req.params.id;
try {
const meeting = await Meeting.getMeetingById(id);
if (typeof meeting == 'undefined') {
res.status(404).json({
message: `Meeting with id ${id} could not be found.`,
});
} else {
const possibleDates = await Meeting.getPossibleDatesByMeetingId(id);
if (possibleDates.length == 0) {
res.status(200).json({
@ -124,25 +121,19 @@ router.get('/:id/possible-dates', authenticate, async (req, res) => {
} else {
res.status(200).json(possibleDates);
}
}
} catch (error) {
res.status(500).json({
message: `Failed to fetch possible dates for meeting with id ${id}`,
error,
});
}
});
},
);
router.get('/:id/availibility', authenticate, async (req, res) => {
router.get('/:id/availibility', authenticate, validateMeetingID, async (req, res) => {
const id = req.params.id;
try {
const meeting = await Meeting.getMeetingById(id);
if (typeof meeting == 'undefined') {
res.status(404).json({
message: `Meeting with id ${id} could not be found.`,
});
} else {
const availibility = await Meeting.getAvailibilityByMeetingId(id);
if (availibility.length == 0) {
res.status(200).json({
@ -151,7 +142,6 @@ router.get('/:id/availibility', authenticate, async (req, res) => {
} else {
res.status(200).json(availibility);
}
}
} catch (error) {
res.status(500).json({
message: `Failed to fetch availibility for meeting with id ${id}`,

View File

@ -0,0 +1,23 @@
const Meeting = require('../api/models/meetingModel');
async function validateMeetingID(req, res, next) {
const { id } = req.params;
try {
const meeting = await Meeting.getMeetingById(id);
if (typeof meeting == 'undefined') {
return res.status(404).json({
message: `Meeting with id ${id} doesn't exist.`,
});
} else {
next();
}
} catch (error) {
res.status(500).json({
message: `Failed to fetch meeting with id ${id}.`,
error,
});
}
}
exports.validateMeetingID = validateMeetingID;