2020-05-07 12:14:13 +00:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const bcrypt = require('bcryptjs');
|
2020-05-04 14:02:31 +00:00
|
|
|
const { v4: uuidv4 } = require('uuid');
|
|
|
|
|
2020-05-10 22:09:16 +00:00
|
|
|
const { authenticate } = require('../../middlewares/authenticate');
|
2020-05-10 22:18:09 +00:00
|
|
|
const { validateMeetingID } = require('../../middlewares/validateMeetingID');
|
2020-05-07 12:14:13 +00:00
|
|
|
const Meeting = require('../models/meetingModel');
|
2020-05-04 14:02:31 +00:00
|
|
|
|
2020-05-10 22:03:48 +00:00
|
|
|
router.post('/', authenticate, async (req, res) => {
|
2020-05-04 14:02:31 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-05-04 14:02:31 +00:00
|
|
|
|
|
|
|
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 });
|
2020-05-04 14:02:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-10 22:18:09 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-05-04 14:02:31 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
2020-05-04 14:02:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-10 22:18:09 +00:00
|
|
|
router.delete('/:id', authenticate, validateMeetingID, async (req, res) => {
|
2020-05-04 14:02:31 +00:00
|
|
|
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.`,
|
|
|
|
});
|
2020-05-04 14:02:31 +00:00
|
|
|
} 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-04 14:02:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-10 22:03:48 +00:00
|
|
|
router.get('/:id', authenticate, async (req, res) => {
|
2020-05-04 14:02:31 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-05-04 14:02:31 +00:00
|
|
|
} catch (error) {
|
2020-05-10 22:03:48 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: `Failed to fetch meeting with id ${id}`,
|
|
|
|
error,
|
|
|
|
});
|
2020-05-04 14:02:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-10 22:18:09 +00:00
|
|
|
router.get(
|
|
|
|
'/:id/participants',
|
|
|
|
authenticate,
|
|
|
|
validateMeetingID,
|
|
|
|
async (req, res) => {
|
|
|
|
const id = req.params.id;
|
2020-05-06 11:17:52 +00:00
|
|
|
|
2020-05-10 22:18:09 +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);
|
|
|
|
}
|
2020-05-10 22:18:09 +00:00
|
|
|
} 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-10 22:18:09 +00:00
|
|
|
},
|
|
|
|
);
|
2020-05-06 11:17:52 +00:00
|
|
|
|
2020-05-10 22:18:09 +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
|
|
|
|
2020-05-10 22:18:09 +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);
|
|
|
|
}
|
2020-05-10 22:18:09 +00:00
|
|
|
} 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-10 22:18:09 +00:00
|
|
|
},
|
|
|
|
);
|
2020-05-06 11:17:52 +00:00
|
|
|
|
2020-05-10 22:18:09 +00:00
|
|
|
router.get('/:id/availibility', authenticate, validateMeetingID, async (req, res) => {
|
2020-05-06 11:17:52 +00:00
|
|
|
const id = req.params.id;
|
|
|
|
|
|
|
|
try {
|
2020-05-10 22:18:09 +00:00
|
|
|
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}.`,
|
2020-05-10 22:03:48 +00:00
|
|
|
});
|
|
|
|
} else {
|
2020-05-10 22:18:09 +00:00
|
|
|
res.status(200).json(availibility);
|
2020-05-10 22:03:48 +00:00
|
|
|
}
|
2020-05-06 11:17:52 +00:00
|
|
|
} catch (error) {
|
2020-05-10 22:03:48 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: `Failed to fetch availibility for meeting with id ${id}`,
|
|
|
|
error,
|
|
|
|
});
|
2020-05-06 11:17:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-04 14:02:31 +00:00
|
|
|
module.exports = router;
|