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-09-01 16:57:33 +00:00
|
|
|
|
|
|
|
data = { ...req.body, id };
|
2020-05-07 12:14:13 +00:00
|
|
|
|
|
|
|
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 };
|
2020-09-01 16:57:33 +00:00
|
|
|
const { id } = req.params;
|
2020-05-07 12:14:13 +00:00
|
|
|
|
|
|
|
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-09-01 16:57:33 +00:00
|
|
|
const { id } = req.params;
|
2020-05-04 14:02:31 +00:00
|
|
|
|
|
|
|
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-09-01 16:57:33 +00:00
|
|
|
const { id } = req.params;
|
2020-05-04 14:02:31 +00:00
|
|
|
|
|
|
|
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) => {
|
2020-09-01 16:57:33 +00:00
|
|
|
const { id } = req.params;
|
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) => {
|
2020-09-01 16:57:33 +00:00
|
|
|
const { id } = req.params;
|
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-06-02 14:51:42 +00:00
|
|
|
router.get(
|
|
|
|
'/:id/availability',
|
|
|
|
authenticate,
|
|
|
|
validateMeetingID,
|
|
|
|
async (req, res) => {
|
2020-09-01 16:57:33 +00:00
|
|
|
const { id } = req.params;
|
2020-05-06 11:17:52 +00:00
|
|
|
|
2020-06-02 14:51:42 +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-06-02 14:51:42 +00:00
|
|
|
},
|
|
|
|
);
|
2020-05-06 11:17:52 +00:00
|
|
|
|
2020-05-04 14:02:31 +00:00
|
|
|
module.exports = router;
|