2020-05-07 12:14:13 +00:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
2020-08-28 12:08:13 +00:00
|
|
|
const { v4: uuidv4 } = require('uuid');
|
2020-05-04 16:47:05 +00:00
|
|
|
|
2020-05-10 22:09:16 +00:00
|
|
|
const { authenticate } = require('../../middlewares/authenticate');
|
2020-08-28 12:08:13 +00:00
|
|
|
const {
|
|
|
|
validateParticipantID,
|
|
|
|
} = require('../../middlewares/validateParticipantID');
|
2020-05-07 12:14:13 +00:00
|
|
|
const Participant = require('../models/participantModel');
|
2020-09-04 10:41:09 +00:00
|
|
|
const { createInvite } = require('../../services/email/emailModels');
|
|
|
|
const sendEmail = require('../../services/email/sendEmail');
|
2020-05-04 16:47:05 +00:00
|
|
|
|
2020-08-28 12:34:37 +00:00
|
|
|
router.post('/', authenticate, async (req, res) => {
|
2020-08-28 12:08:13 +00:00
|
|
|
id = uuidv4();
|
2020-09-04 10:41:09 +00:00
|
|
|
|
|
|
|
let data = { ...req.body, id };
|
|
|
|
const {
|
|
|
|
account_id,
|
|
|
|
email,
|
|
|
|
meeting_id,
|
|
|
|
quorum,
|
|
|
|
mandatory,
|
|
|
|
host,
|
|
|
|
answered,
|
|
|
|
meetingTitle,
|
|
|
|
senderUsername,
|
|
|
|
} = data;
|
|
|
|
|
|
|
|
// Create invite message
|
|
|
|
const invite = createInvite({
|
|
|
|
receiver: email,
|
|
|
|
senderUsername,
|
|
|
|
meetingTitle,
|
|
|
|
meetingId: meeting_id,
|
|
|
|
});
|
|
|
|
|
|
|
|
const participantData = {
|
|
|
|
id,
|
|
|
|
account_id,
|
|
|
|
email,
|
|
|
|
meeting_id,
|
|
|
|
quorum,
|
|
|
|
mandatory,
|
|
|
|
host,
|
|
|
|
answered,
|
|
|
|
};
|
2020-05-04 16:47:05 +00:00
|
|
|
|
|
|
|
try {
|
2020-09-04 10:41:09 +00:00
|
|
|
const [participant] = await Participant.addParticipant(participantData);
|
|
|
|
|
|
|
|
// Send email
|
|
|
|
sendEmail(invite);
|
|
|
|
|
2020-05-04 16:47:05 +00:00
|
|
|
res.status(201).json(participant);
|
|
|
|
} catch (error) {
|
2020-09-04 10:41:09 +00:00
|
|
|
console.log(error);
|
2020-05-04 16:47:05 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: 'Failed to add new participant.',
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-28 12:08:13 +00:00
|
|
|
router.put('/:id', authenticate, validateParticipantID, async (req, res) => {
|
2020-05-04 16:47:05 +00:00
|
|
|
const data = { ...req.body };
|
2020-08-28 12:08:13 +00:00
|
|
|
const { id } = req.params;
|
2020-05-04 16:47:05 +00:00
|
|
|
|
|
|
|
try {
|
2020-08-28 12:08:13 +00:00
|
|
|
const participant = await Participant.updateParticipant(data, id);
|
2020-09-04 10:41:09 +00:00
|
|
|
res.status(200).json(participant);
|
2020-05-04 16:47:05 +00:00
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({
|
|
|
|
message: 'Failed to update participant.',
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-28 12:08:13 +00:00
|
|
|
router.delete('/:id', authenticate, validateParticipantID, async (req, res) => {
|
|
|
|
const { id } = req.params;
|
2020-05-04 16:47:05 +00:00
|
|
|
|
|
|
|
try {
|
2020-08-28 12:08:13 +00:00
|
|
|
const participant = await Participant.deleteParticipant(id);
|
2020-05-04 16:47:05 +00:00
|
|
|
res.status(200).json({
|
2020-08-28 12:08:13 +00:00
|
|
|
message: `Participant with id ${id} successfully deleted.`,
|
2020-05-04 16:47:05 +00:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({
|
2020-08-28 12:08:13 +00:00
|
|
|
message: `Failed to delete participant with id ${id}.`,
|
2020-05-04 16:47:05 +00:00
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-28 12:08:13 +00:00
|
|
|
router.get('/:id', authenticate, async (req, res) => {
|
|
|
|
const { id } = req.params;
|
2020-05-04 16:47:05 +00:00
|
|
|
|
|
|
|
try {
|
2020-08-28 12:08:13 +00:00
|
|
|
const participant = await Participant.getParticipantById(id);
|
2020-05-10 22:25:08 +00:00
|
|
|
if (typeof participant == 'undefined') {
|
|
|
|
res.status(404).json({
|
|
|
|
message: `Participant with id ${id} doesn't exist.`,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.status(200).json(participant);
|
|
|
|
}
|
2020-05-04 16:47:05 +00:00
|
|
|
} catch (error) {
|
2020-05-05 09:34:30 +00:00
|
|
|
res.status(500).json({
|
2020-08-28 12:08:13 +00:00
|
|
|
message: `Failed to get participant with id ${id}.`,
|
2020-05-05 09:34:30 +00:00
|
|
|
error,
|
|
|
|
});
|
2020-05-04 16:47:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|