diff --git a/api/routes/accountRoute.js b/api/routes/accountRoute.js index fdf76de..4b9f458 100644 --- a/api/routes/accountRoute.js +++ b/api/routes/accountRoute.js @@ -26,11 +26,12 @@ router.put('/:id', authenticate, validateAccountID, async (req, res) => { const data = { ...req.body }; const id = req.params.id; + if (data.password) { + const hash = bcrypt.hashSync(data.password, 10); + data.password = hash; + } + try { - if (data.password) { - const hash = bcrypt.hashSync(data.password, 10); - data.password = hash; - } const account = await Account.updateAccount(data, id); res.status(200).json(...account); } catch (error) { @@ -57,25 +58,30 @@ router.delete('/:id', authenticate, validateAccountID, async (req, res) => { } }); -router.get('/:id/meetings', authenticate, validateAccountID, async (req, res) => { - const { id } = req.params; +router.get( + '/:id/meetings', + authenticate, + validateAccountID, + async (req, res) => { + const { id } = req.params; - try { - const meetings = await Account.getMeetingsByAccountId(id); - if (meetings.length == 0) { - res.status(200).json({ - message: `There are no meetings for account with id ${id}.`, + try { + const meetings = await Account.getMeetingsByAccountId(id); + if (meetings.length == 0) { + res.status(200).json({ + 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) => { const id = req.params.id; diff --git a/api/routes/meetingRoute.js b/api/routes/meetingRoute.js index 017db8f..c2912f5 100644 --- a/api/routes/meetingRoute.js +++ b/api/routes/meetingRoute.js @@ -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) => { - const id = req.params.id; +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 { + try { 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) => { - 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.`, + } catch (error) { + res.status(500).json({ + message: `Failed to fetch participants for meeting with id ${id}.`, + error, }); - } else { + } + }, +); + +router.get( + '/:id/possible-dates', + authenticate, + validateMeetingID, + async (req, res) => { + const id = req.params.id; + + try { const possibleDates = await Meeting.getPossibleDatesByMeetingId(id); if (possibleDates.length == 0) { res.status(200).json({ @@ -124,33 +121,26 @@ 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, + }); } - } 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.`, + const availibility = await Meeting.getAvailibilityByMeetingId(id); + if (availibility.length == 0) { + res.status(200).json({ + message: `There are no possibles dates for meeting with id ${id}.`, }); } else { - const availibility = await Meeting.getAvailibilityByMeetingId(id); - 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); - } + res.status(200).json(availibility); } } catch (error) { res.status(500).json({ diff --git a/middlewares/validateMeetingID.js b/middlewares/validateMeetingID.js new file mode 100644 index 0000000..7f21db4 --- /dev/null +++ b/middlewares/validateMeetingID.js @@ -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; \ No newline at end of file