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 data = { ...req.body };
const id = req.params.id; const id = req.params.id;
if (data.password) {
const hash = bcrypt.hashSync(data.password, 10);
data.password = hash;
}
try { try {
if (data.password) {
const hash = bcrypt.hashSync(data.password, 10);
data.password = hash;
}
const account = await Account.updateAccount(data, id); const account = await Account.updateAccount(data, id);
res.status(200).json(...account); res.status(200).json(...account);
} catch (error) { } catch (error) {
@ -57,25 +58,30 @@ router.delete('/:id', authenticate, validateAccountID, async (req, res) => {
} }
}); });
router.get('/:id/meetings', authenticate, validateAccountID, async (req, res) => { router.get(
const { id } = req.params; '/:id/meetings',
authenticate,
validateAccountID,
async (req, res) => {
const { id } = req.params;
try { try {
const meetings = await Account.getMeetingsByAccountId(id); const meetings = await Account.getMeetingsByAccountId(id);
if (meetings.length == 0) { if (meetings.length == 0) {
res.status(200).json({ res.status(200).json({
message: `There are no meetings for account with id ${id}.`, message: `There are no meetings for account with id ${id}.`,
});
} else {
res.status(200).json(meetings);
}
} catch (error) {
res.status(500).json({
message: `Failed to fetch meetings with account id ${id}.`,
error,
}); });
} else {
res.status(200).json(meetings);
} }
} catch (error) { },
res.status(500).json({ );
message: `Failed to fetch meetings with account id ${id}.`,
error,
});
}
});
router.get('/:id', authenticate, async (req, res) => { router.get('/:id', authenticate, async (req, res) => {
const id = req.params.id; const id = req.params.id;

View File

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

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;