Merge pull request 'Send email when adding participant' (#2) from feat-send-email into master

Reviewed-on: https://git.armada.digital/meeting-planner/backend/pulls/2
This commit is contained in:
Rui Hildt 2020-09-04 11:08:30 +00:00
commit dda9b88428
9 changed files with 90 additions and 55 deletions

View File

@ -21,7 +21,6 @@ router.post('/', authenticate, async (req, res) => {
const [meeting] = await Meeting.addMeeting(data); const [meeting] = await Meeting.addMeeting(data);
res.status(201).json(meeting); res.status(201).json(meeting);
} catch (error) { } catch (error) {
console.log(error);
res.status(500).json({ message: 'Failed to add new meeting.', error }); res.status(500).json({ message: 'Failed to add new meeting.', error });
} }
}); });

View File

@ -7,15 +7,53 @@ const {
validateParticipantID, validateParticipantID,
} = require('../../middlewares/validateParticipantID'); } = require('../../middlewares/validateParticipantID');
const Participant = require('../models/participantModel'); const Participant = require('../models/participantModel');
const { createInvite } = require('../../services/email/emailModels');
const sendEmail = require('../../services/email/sendEmail');
router.post('/', authenticate, async (req, res) => { router.post('/', authenticate, async (req, res) => {
id = uuidv4(); 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 { try {
const [participant] = await Participant.addParticipant(data); const [participant] = await Participant.addParticipant(participantData);
// Send email
sendEmail(invite);
res.status(201).json(participant); res.status(201).json(participant);
} catch (error) { } catch (error) {
console.log(error);
res.status(500).json({ res.status(500).json({
message: 'Failed to add new participant.', message: 'Failed to add new participant.',
error, error,
@ -29,7 +67,7 @@ router.put('/:id', authenticate, validateParticipantID, async (req, res) => {
try { try {
const participant = await Participant.updateParticipant(data, id); const participant = await Participant.updateParticipant(data, id);
res.status(200).json(...participant); res.status(200).json(participant);
} catch (error) { } catch (error) {
res.status(500).json({ res.status(500).json({
message: 'Failed to update participant.', message: 'Failed to update participant.',

View File

@ -4,7 +4,7 @@ const bodyParser = require('body-parser');
const cors = require('cors'); const cors = require('cors');
const helmet = require('helmet'); const helmet = require('helmet');
const logSmtpStatus = require('../helpers/logSmtpStatus'); const logSmtpStatus = require('../services/email/logSmtpStatus');
const accountsRoute = require('./routes/accountRoute'); const accountsRoute = require('./routes/accountRoute');
const meetingsRoute = require('./routes/meetingRoute'); const meetingsRoute = require('./routes/meetingRoute');

View File

@ -19,4 +19,9 @@ module.exports = {
smtpRequireTLS: true, // True to force use of StartTLS smtpRequireTLS: true, // True to force use of StartTLS
smtpSecure: false, // False to force use of StartTLS smtpSecure: false, // False to force use of StartTLS
smtpPool: true, // Pool connections to server smtpPool: true, // Pool connections to server
// EMAIL
appURL: 'https://meetingplanner.com',
sender: 'no-reply@ruihildt.xyz',
signature: 'The Meeting Planner Team',
}; };

View File

@ -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;
}

View File

@ -1,4 +1,4 @@
const transporter = require('../services/email/transporter'); const transporter = require('./transporter');
function logSmtpStatus() { function logSmtpStatus() {
transporter.verify(function (error, success) { transporter.verify(function (error, success) {

View File

@ -1,6 +1,8 @@
const transporter = require('./transporter'); const transporter = require('./transporter');
const nodemailer = require('nodemailer');
transporter.sendMail(message, (error, info) => { function sendEmail(message) {
transporter.sendMail(message, (error, info) => {
if (error) { if (error) {
console.log('Error occurred'); console.log('Error occurred');
console.log(error.message); console.log(error.message);
@ -12,4 +14,7 @@ transporter.sendMail(message, (error, info) => {
// only needed when using pooled connections // only needed when using pooled connections
transporter.close(); transporter.close();
}); });
}
module.exports = sendEmail;

View File

@ -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',
};

View File

@ -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
`,
};