backend/api/routes/meetingRoute.js

159 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-05-07 12:14:13 +00:00
const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const { v4: uuidv4 } = require('uuid');
const { authenticate } = require('../../middlewares/authenticate');
const { validateMeetingID } = require('../../middlewares/validateMeetingID');
2020-05-07 12:14:13 +00:00
const Meeting = require('../models/meetingModel');
2020-05-10 22:03:48 +00:00
router.post('/', authenticate, async (req, res) => {
id = uuidv4();
2020-05-07 12:14:13 +00:00
data = { id, ...req.body };
if (data.password) {
const hash = bcrypt.hashSync(data.password, 14);
data.password = hash;
}
try {
const [meeting] = await Meeting.addMeeting(data);
res.status(201).json(meeting);
} catch (error) {
2020-05-10 22:03:48 +00:00
res.status(500).json({ message: 'Failed to add new meeting.', error });
}
});
router.put('/:id', authenticate, validateMeetingID, async (req, res) => {
2020-05-07 12:14:13 +00:00
const data = { ...req.body };
const id = req.params.id;
if (data.password) {
const hash = bcrypt.hashSync(data.password, 14);
data.password = hash;
}
try {
const meeting = await Meeting.updateMeeting(data, id);
res.status(200).json(meeting);
} catch (error) {
2020-05-07 12:14:13 +00:00
res.status(500).json({
message: `Failed to update meeting with id ${id}.`,
error,
});
}
});
router.delete('/:id', authenticate, validateMeetingID, async (req, res) => {
const id = req.params.id;
try {
const meeting = await Meeting.deleteMeeting(id);
2020-05-07 12:14:13 +00:00
res.status(200).json({
message: `Meeting with id ${id} successfully deleted.`,
});
} catch (error) {
2020-05-07 12:14:13 +00:00
res.status(500).json({
message: `Failed to delete meeting with id ${id}.`,
error,
});
}
});
2020-05-10 22:03:48 +00:00
router.get('/:id', authenticate, async (req, res) => {
const id = req.params.id;
try {
const meeting = await Meeting.getMeetingById(id);
2020-05-10 22:03:48 +00:00
if (typeof meeting == 'undefined') {
res.status(404).json({
message: `Meeting with id ${id} could not be found.`,
});
} else {
res.status(200).json(meeting);
}
} catch (error) {
2020-05-10 22:03:48 +00:00
res.status(500).json({
message: `Failed to fetch meeting with id ${id}`,
error,
});
}
});
router.get(
'/:id/participants',
authenticate,
validateMeetingID,
async (req, res) => {
const id = req.params.id;
2020-05-06 11:17:52 +00:00
try {
2020-05-10 22:03:48 +00:00
const participants = await Meeting.getParticipantsByMeetingId(id);
if (participants.length == 0) {
res.status(200).json({
message: `There are no participants for meeting with id ${id}.`,
});
} else {
res.status(200).json(participants);
}
} catch (error) {
res.status(500).json({
message: `Failed to fetch participants for meeting with id ${id}.`,
error,
});
2020-05-10 22:03:48 +00:00
}
},
);
2020-05-06 11:17:52 +00:00
router.get(
'/:id/possible-dates',
authenticate,
validateMeetingID,
async (req, res) => {
const id = req.params.id;
2020-05-06 11:17:52 +00:00
try {
2020-05-10 22:03:48 +00:00
const possibleDates = await Meeting.getPossibleDatesByMeetingId(id);
if (possibleDates.length == 0) {
res.status(200).json({
message: `There are no possibles dates for meeting with id ${id}.`,
});
} else {
res.status(200).json(possibleDates);
}
} catch (error) {
res.status(500).json({
message: `Failed to fetch possible dates for meeting with id ${id}`,
error,
});
2020-05-10 22:03:48 +00:00
}
},
);
2020-05-06 11:17:52 +00:00
router.get(
'/:id/availability',
authenticate,
validateMeetingID,
async (req, res) => {
const id = req.params.id;
2020-05-06 11:17:52 +00:00
try {
const availability = await Meeting.getAvailabilityByMeetingId(id);
if (availability.length == 0) {
res.status(200).json({
message: `There are no possibles dates for meeting with id ${id}.`,
});
} else {
res.status(200).json(availability);
}
} catch (error) {
res.status(500).json({
message: `Failed to fetch availability for meeting with id ${id}`,
error,
2020-05-10 22:03:48 +00:00
});
}
},
);
2020-05-06 11:17:52 +00:00
module.exports = router;