From 007c45e3c7b888cceac5bbc8bb17ff4ae08efa33 Mon Sep 17 00:00:00 2001 From: Edouard Richard Date: Sun, 17 Mar 2024 12:08:33 +0100 Subject: [PATCH] feat: send emails on COMMENT_CARD notifications --- server/.env.sample | 8 +++ .../api/helpers/notifications/create-one.js | 51 +++++++++++++++++++ server/package-lock.json | 14 +++++ server/package.json | 1 + 4 files changed, 74 insertions(+) diff --git a/server/.env.sample b/server/.env.sample index 8d962b6..85b9889 100644 --- a/server/.env.sample +++ b/server/.env.sample @@ -35,6 +35,14 @@ SECRET_KEY=notsecretkey # OIDC_IGNORE_ROLES=true # OIDC_ENFORCED=true +# Email Notifications (https://nodemailer.com/smtp/) +SMTP_HOST= +SMTP_USER= +SMTP_PASSWORD= +SMTP_PORT=587 +SMTP_SECURE= +SMTP_FROM="Demo Demo" + ## Do not edit this TZ=UTC diff --git a/server/api/helpers/notifications/create-one.js b/server/api/helpers/notifications/create-one.js index cc6a0b6..5fc42d5 100644 --- a/server/api/helpers/notifications/create-one.js +++ b/server/api/helpers/notifications/create-one.js @@ -1,3 +1,18 @@ +const nodemailer = require('nodemailer'); + +const emailTransporter = + process.env.SMTP_HOST && + nodemailer.createTransport({ + pool: true, + host: process.env.SMTP_HOST, + port: process.env.SMTP_PORT, + secure: process.env.SMTP_SECURE === 'true', + auth: { + user: process.env.SMTP_USER, + pass: process.env.SMTP_PASSWORD, + }, + }); + const valuesValidator = (value) => { if (!_.isPlainObject(value)) { return false; @@ -14,6 +29,38 @@ const valuesValidator = (value) => { return true; }; +async function sendEmailNotification({ notification, action }) { + const actionUser = await sails.helpers.users.getOne(action.userId); + const actionCard = await Card.findOne(action.cardId); + const notificationUser = await sails.helpers.users.getOne(notification.userId); + const actionBoard = await Board.findOne(actionCard.boardId); + let email; + switch (action.type) { + case Action.Types.COMMENT_CARD: + email = { + subject: `${actionUser.name} commented the card ${actionCard.name} on ${actionBoard.name}`, + html: `

${actionUser.name} commented the card ${actionCard.name} on ${actionBoard.name}

${action.data.text}

`, + }; + break; + + default: + break; + } + if (!email) { + return; + } + emailTransporter.sendMail( + { from: process.env.SMTP_FROM, to: notificationUser.email, ...email }, + (error, info) => { + if (error) { + sails.log.error(error); + } else { + sails.log.info('Email sent: %s', info.messageId); + } + }, + ); +} + module.exports = { inputs: { values: { @@ -36,6 +83,10 @@ module.exports = { cardId: values.action.cardId, }).fetch(); + if (emailTransporter) { + sendEmailNotification({ notification, action: values.action }); + } + sails.sockets.broadcast(`user:${notification.userId}`, 'notificationCreate', { item: notification, }); diff --git a/server/package-lock.json b/server/package-lock.json index 05527a7..93453a8 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -15,6 +15,7 @@ "lodash": "^4.17.21", "moment": "^2.29.4", "move-file": "^2.1.0", + "nodemailer": "^6.9.12", "openid-client": "^5.6.1", "rimraf": "^5.0.5", "sails": "^1.5.7", @@ -5257,6 +5258,14 @@ } } }, + "node_modules/nodemailer": { + "version": "6.9.12", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.12.tgz", + "integrity": "sha512-pnLo7g37Br3jXbF0bl5DekBJihm2q+3bB3l2o/B060sWmb5l+VqeScAQCBqaQ+5ezRZFzW5SciZNGdRDEbq89w==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/nodemon": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.0.1.tgz", @@ -12852,6 +12861,11 @@ "whatwg-url": "^5.0.0" } }, + "nodemailer": { + "version": "6.9.12", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.12.tgz", + "integrity": "sha512-pnLo7g37Br3jXbF0bl5DekBJihm2q+3bB3l2o/B060sWmb5l+VqeScAQCBqaQ+5ezRZFzW5SciZNGdRDEbq89w==" + }, "nodemon": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.0.1.tgz", diff --git a/server/package.json b/server/package.json index 59bb2f7..67fc5e0 100644 --- a/server/package.json +++ b/server/package.json @@ -36,6 +36,7 @@ "lodash": "^4.17.21", "moment": "^2.29.4", "move-file": "^2.1.0", + "nodemailer": "^6.9.12", "openid-client": "^5.6.1", "rimraf": "^5.0.5", "sails": "^1.5.7",