159 lines
3.6 KiB
JavaScript
159 lines
3.6 KiB
JavaScript
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');
|
|
const Meeting = require('../models/meetingModel');
|
|
|
|
router.post('/', authenticate, async (req, res) => {
|
|
id = uuidv4();
|
|
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) {
|
|
res.status(500).json({ message: 'Failed to add new meeting.', error });
|
|
}
|
|
});
|
|
|
|
router.put('/:id', authenticate, validateMeetingID, async (req, res) => {
|
|
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) {
|
|
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);
|
|
res.status(200).json({
|
|
message: `Meeting with id ${id} successfully deleted.`,
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
message: `Failed to delete meeting with id ${id}.`,
|
|
error,
|
|
});
|
|
}
|
|
});
|
|
|
|
router.get('/:id', 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 {
|
|
res.status(200).json(meeting);
|
|
}
|
|
} catch (error) {
|
|
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;
|
|
|
|
try {
|
|
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,
|
|
});
|
|
}
|
|
},
|
|
);
|
|
|
|
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({
|
|
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,
|
|
});
|
|
}
|
|
},
|
|
);
|
|
|
|
router.get(
|
|
'/:id/availability',
|
|
authenticate,
|
|
validateMeetingID,
|
|
async (req, res) => {
|
|
const id = req.params.id;
|
|
|
|
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,
|
|
});
|
|
}
|
|
},
|
|
);
|
|
|
|
module.exports = router;
|