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:
commit
dda9b88428
@ -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 });
|
||||
}
|
||||
});
|
||||
|
@ -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.',
|
||||
|
@ -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');
|
||||
|
@ -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',
|
||||
};
|
||||
|
26
services/email/emailModels.js
Normal file
26
services/email/emailModels.js
Normal 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;
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
const transporter = require('../services/email/transporter');
|
||||
const transporter = require('./transporter');
|
||||
|
||||
function logSmtpStatus() {
|
||||
transporter.verify(function (error, success) {
|
@ -1,5 +1,7 @@
|
||||
const transporter = require('./transporter');
|
||||
const nodemailer = require('nodemailer');
|
||||
|
||||
function sendEmail(message) {
|
||||
transporter.sendMail(message, (error, info) => {
|
||||
if (error) {
|
||||
console.log('Error occurred');
|
||||
@ -13,3 +15,6 @@ transporter.sendMail(message, (error, info) => {
|
||||
// only needed when using pooled connections
|
||||
transporter.close();
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = sendEmail;
|
@ -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',
|
||||
};
|
@ -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
|
||||
`,
|
||||
};
|
Loading…
Reference in New Issue
Block a user