From 8614166d6405eeb7b7ed498e59101bfd83d15839 Mon Sep 17 00:00:00 2001 From: rui hildt Date: Fri, 4 Sep 2020 12:41:09 +0200 Subject: [PATCH] Send email when adding participant - create invite email template - move email config - implement sendEmail functionality --- api/routes/meetingRoute.js | 1 - api/routes/participantRoute.js | 44 ++++++++++++++++++-- api/server.js | 2 +- config/config.js | 5 +++ services/email/emailModels.js | 26 ++++++++++++ {helpers => services/email}/logSmtpStatus.js | 2 +- services/email/sendEmail.js | 27 +++++++----- services/email/tempConfig.js | 11 ----- services/email/templates/meetingInvite.js | 27 ------------ 9 files changed, 90 insertions(+), 55 deletions(-) create mode 100644 services/email/emailModels.js rename {helpers => services/email}/logSmtpStatus.js (81%) delete mode 100644 services/email/tempConfig.js delete mode 100644 services/email/templates/meetingInvite.js diff --git a/api/routes/meetingRoute.js b/api/routes/meetingRoute.js index c58d23b..f6cb1d7 100644 --- a/api/routes/meetingRoute.js +++ b/api/routes/meetingRoute.js @@ -21,7 +21,6 @@ router.post('/', authenticate, async (req, res) => { const [meeting] = await Meeting.addMeeting(data); res.status(201).json(meeting); } catch (error) { - console.log(error); res.status(500).json({ message: 'Failed to add new meeting.', error }); } }); diff --git a/api/routes/participantRoute.js b/api/routes/participantRoute.js index d9dc063..d6e4fdb 100644 --- a/api/routes/participantRoute.js +++ b/api/routes/participantRoute.js @@ -7,15 +7,53 @@ const { validateParticipantID, } = require('../../middlewares/validateParticipantID'); const Participant = require('../models/participantModel'); +const { createInvite } = require('../../services/email/emailModels'); +const sendEmail = require('../../services/email/sendEmail'); router.post('/', authenticate, async (req, res) => { id = uuidv4(); - const data = { ...req.body, id }; + + 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, + }; try { - const [participant] = await Participant.addParticipant(data); + const [participant] = await Participant.addParticipant(participantData); + + // Send email + sendEmail(invite); + res.status(201).json(participant); } catch (error) { + console.log(error); res.status(500).json({ message: 'Failed to add new participant.', error, @@ -29,7 +67,7 @@ router.put('/:id', authenticate, validateParticipantID, async (req, res) => { try { const participant = await Participant.updateParticipant(data, id); - res.status(200).json(...participant); + res.status(200).json(participant); } catch (error) { res.status(500).json({ message: 'Failed to update participant.', diff --git a/api/server.js b/api/server.js index a15b5a4..0fc5913 100644 --- a/api/server.js +++ b/api/server.js @@ -4,7 +4,7 @@ const bodyParser = require('body-parser'); const cors = require('cors'); const helmet = require('helmet'); -const logSmtpStatus = require('../helpers/logSmtpStatus'); +const logSmtpStatus = require('../services/email/logSmtpStatus'); const accountsRoute = require('./routes/accountRoute'); const meetingsRoute = require('./routes/meetingRoute'); diff --git a/config/config.js b/config/config.js index 8a178b5..f615cb7 100644 --- a/config/config.js +++ b/config/config.js @@ -19,4 +19,9 @@ module.exports = { smtpRequireTLS: true, // True to force use of StartTLS smtpSecure: false, // False to force use of StartTLS smtpPool: true, // Pool connections to server + + // EMAIL + appURL: 'https://meetingplanner.com', + sender: 'no-reply@ruihildt.xyz', + signature: 'The Meeting Planner Team', }; diff --git a/services/email/emailModels.js b/services/email/emailModels.js new file mode 100644 index 0000000..213cddf --- /dev/null +++ b/services/email/emailModels.js @@ -0,0 +1,26 @@ +const { sender, signature, appURL } = require('../../config/config'); + +module.exports = { + createInvite, +}; + +function createInvite({ receiver, senderUsername, meetingTitle, meetingId }) { + const invite = { + from: sender, + to: receiver, + subject: `Invitation | ${meetingTitle}`, + text: `Hi, + + ${senderUsername} invites you to participate to the poll '${meetingTitle}'. + + To participate, please fill in you availability: ${appURL}/meetings/${meetingId} + + Have a beautiful day, + + ${signature} | ${appURL} + + `, + }; + + return invite; +} diff --git a/helpers/logSmtpStatus.js b/services/email/logSmtpStatus.js similarity index 81% rename from helpers/logSmtpStatus.js rename to services/email/logSmtpStatus.js index a65dd02..93b8a75 100644 --- a/helpers/logSmtpStatus.js +++ b/services/email/logSmtpStatus.js @@ -1,4 +1,4 @@ -const transporter = require('../services/email/transporter'); +const transporter = require('./transporter'); function logSmtpStatus() { transporter.verify(function (error, success) { diff --git a/services/email/sendEmail.js b/services/email/sendEmail.js index 8b59149..9d9d6ab 100644 --- a/services/email/sendEmail.js +++ b/services/email/sendEmail.js @@ -1,15 +1,20 @@ const transporter = require('./transporter'); +const nodemailer = require('nodemailer'); -transporter.sendMail(message, (error, info) => { - if (error) { - console.log('Error occurred'); - console.log(error.message); - return process.exit(1); - } +function sendEmail(message) { + transporter.sendMail(message, (error, info) => { + if (error) { + console.log('Error occurred'); + console.log(error.message); + return process.exit(1); + } - console.log('Message sent successfully!'); - console.log(nodemailer.getTestMessageUrl(info)); + console.log('Message sent successfully!'); + console.log(nodemailer.getTestMessageUrl(info)); - // only needed when using pooled connections - transporter.close(); -}); + // only needed when using pooled connections + transporter.close(); + }); +} + +module.exports = sendEmail; \ No newline at end of file diff --git a/services/email/tempConfig.js b/services/email/tempConfig.js deleted file mode 100644 index ee7b5a7..0000000 --- a/services/email/tempConfig.js +++ /dev/null @@ -1,11 +0,0 @@ -module.exports = { - appURL: 'https://meetingplanner.com', - - sender: 'no-reply@ruihildt.xyz', - receiver: 'rui@armada.digital', - - senderUsername: 'Rui', - meetingTitle: 'Simple Title meeting', - meetingID: 'zef-zefzefzefzefze-zefzefzef', - signature: 'The Meeting Planner Team', -}; diff --git a/services/email/templates/meetingInvite.js b/services/email/templates/meetingInvite.js deleted file mode 100644 index 1364fd9..0000000 --- a/services/email/templates/meetingInvite.js +++ /dev/null @@ -1,27 +0,0 @@ -const { - sender, - receiver, - senderUsername, - receiver, - meetingTitle, - meetingID, - signature, - appURL, -} = require('../tempConfig'); - -let meetingInvite = { - from: sender, - to: receiver, - subject: `Invitation | ${meetingTitle}`, - text: `Hi, - \n - ${senderUsername} invites you to participate to the poll '${meetingTitle}'. - \n - To participate, please fill in you availability: ${meetingURL}/meetings/${meetingID} - \n - Have a beautiful day, - \n - ${signature} | ${appURL} - \n - `, -};