Send email when adding participant
- create invite email template - move email config - implement sendEmail functionality
This commit is contained in:
parent
ac2b6c2b88
commit
8614166d64
@ -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 });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -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.',
|
||||||
|
@ -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');
|
||||||
|
@ -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',
|
||||||
};
|
};
|
||||||
|
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() {
|
function logSmtpStatus() {
|
||||||
transporter.verify(function (error, success) {
|
transporter.verify(function (error, success) {
|
@ -1,5 +1,7 @@
|
|||||||
const transporter = require('./transporter');
|
const transporter = require('./transporter');
|
||||||
|
const nodemailer = require('nodemailer');
|
||||||
|
|
||||||
|
function sendEmail(message) {
|
||||||
transporter.sendMail(message, (error, info) => {
|
transporter.sendMail(message, (error, info) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log('Error occurred');
|
console.log('Error occurred');
|
||||||
@ -13,3 +15,6 @@ transporter.sendMail(message, (error, info) => {
|
|||||||
// only needed when using pooled connections
|
// only needed when using pooled connections
|
||||||
transporter.close();
|
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