From e843edeb131a8017f3ef078608b133cdba4b84b1 Mon Sep 17 00:00:00 2001 From: Alina Beck Date: Thu, 19 Sep 2019 01:15:52 +0100 Subject: [PATCH] unite email templates in templateBuilder --- .../src/middleware/email/emailMiddleware.js | 14 +-- .../email/templates/passwordReset.js | 38 --------- .../src/middleware/email/templates/signup.js | 24 ------ .../email/templates/templateBuilder.js | 48 +++++++++++ .../resolvers/passwordReset/emailTemplates.js | 85 ------------------- 5 files changed, 57 insertions(+), 152 deletions(-) delete mode 100644 backend/src/middleware/email/templates/passwordReset.js delete mode 100644 backend/src/middleware/email/templates/signup.js create mode 100644 backend/src/middleware/email/templates/templateBuilder.js delete mode 100644 backend/src/schema/resolvers/passwordReset/emailTemplates.js diff --git a/backend/src/middleware/email/emailMiddleware.js b/backend/src/middleware/email/emailMiddleware.js index af3d69c4a..e5b0678b8 100644 --- a/backend/src/middleware/email/emailMiddleware.js +++ b/backend/src/middleware/email/emailMiddleware.js @@ -1,13 +1,16 @@ import CONFIG from '../../config' import nodemailer from 'nodemailer' import { htmlToText } from 'nodemailer-html-to-text' -import { resetPasswordTemplate } from './templates/passwordReset' -import { signupTemplate } from './templates/signup' +import { + signupTemplate, + resetPasswordTemplate, + wrongAccountTemplate, +} from './templates/templateBuilder' const hasEmailConfig = CONFIG.SMTP_HOST && CONFIG.SMTP_PORT const hasAuthData = CONFIG.SMTP_USERNAME && CONFIG.SMTP_PASSWORD -let sendMail = () => { } +let sendMail = () => {} if (!hasEmailConfig) { if (process.env.NODE_ENV !== 'test') { // eslint-disable-next-line no-console @@ -48,8 +51,9 @@ const sendSignupMail = async (resolve, root, args, context, resolveInfo) => { const sendPasswordResetMail = async (resolve, root, args, context, resolveInfo) => { const { email } = args - const { email: emailFound, nonce, name } = await resolve(root, args, context, resolveInfo) - await sendMail(resetPasswordTemplate({ emailFound, email, nonce, name })) + const { email: userFound, nonce, name } = await resolve(root, args, context, resolveInfo) + const template = userFound ? resetPasswordTemplate : wrongAccountTemplate + await sendMail(template({ email, nonce, name })) return true } diff --git a/backend/src/middleware/email/templates/passwordReset.js b/backend/src/middleware/email/templates/passwordReset.js deleted file mode 100644 index d9da7a180..000000000 --- a/backend/src/middleware/email/templates/passwordReset.js +++ /dev/null @@ -1,38 +0,0 @@ -import fs from 'fs' -import path from 'path' -import mustache from 'mustache' -import CONFIG from '../../../config' - -const passwordResetHtml = fs.readFileSync(path.join(__dirname, './resetPassword.html'), 'utf-8') -const wrongAccountHtml = fs.readFileSync(path.join(__dirname, './wrongAccount.html'), 'utf-8') - -const supportUrl = 'https://human-connection.org/en/contact/' -const from = '"Human Connection" ' - -export const resetPasswordTemplate = ({ - name, - email, - emailFound, - nonce, -}) => { - let subject, htmlTemplate, actionUrl - - if (emailFound) { - subject = 'Neues Passwort | Reset Password' - htmlTemplate = passwordResetHtml - actionUrl = new URL('/password-reset/change-password', CONFIG.CLIENT_URI) - actionUrl.searchParams.set('nonce', nonce) - actionUrl.searchParams.set('email', email) - } else { - subject = 'Falsche Mailadresse? | Wrong Email?' - htmlTemplate = wrongAccountHtml - actionUrl = new URL('/password-reset/request', CONFIG.CLIENT_URI) - } - - return { - from, - to: email, - subject, - html: mustache.render(htmlTemplate, { actionUrl, name, nonce, supportUrl }), - } -} diff --git a/backend/src/middleware/email/templates/signup.js b/backend/src/middleware/email/templates/signup.js deleted file mode 100644 index d67c8f7ae..000000000 --- a/backend/src/middleware/email/templates/signup.js +++ /dev/null @@ -1,24 +0,0 @@ -import fs from 'fs' -import path from 'path' -import mustache from 'mustache' -import CONFIG from '../../../config' - -const signupHtml = fs.readFileSync(path.join(__dirname, './signup.html'), 'utf-8') - -export const signupTemplate = ({ - email, - nonce, - subject = 'Willkommen, Bienvenue, Welcome to Human Connection!', - supportUrl = 'https://human-connection.org/en/contact', -}) => { - const actionUrl = new URL('/registration/create-user-account', CONFIG.CLIENT_URI) - actionUrl.searchParams.set('nonce', nonce) - actionUrl.searchParams.set('email', email) - - return { - from: '"Human Connection" ', - to: email, - subject, - html: mustache.render(signupHtml, { actionUrl, nonce, supportUrl }), - } -} diff --git a/backend/src/middleware/email/templates/templateBuilder.js b/backend/src/middleware/email/templates/templateBuilder.js new file mode 100644 index 000000000..8bf7dc438 --- /dev/null +++ b/backend/src/middleware/email/templates/templateBuilder.js @@ -0,0 +1,48 @@ +import fs from 'fs' +import path from 'path' +import mustache from 'mustache' +import CONFIG from '../../../config' + +const from = '"Human Connection" ' +const supportUrl = 'https://human-connection.org/en/contact' + +const signupHtml = fs.readFileSync(path.join(__dirname, './signup.html'), 'utf-8') +const passwordResetHtml = fs.readFileSync(path.join(__dirname, './resetPassword.html'), 'utf-8') +const wrongAccountHtml = fs.readFileSync(path.join(__dirname, './wrongAccount.html'), 'utf-8') + +export const signupTemplate = ({ email, nonce }) => { + const actionUrl = new URL('/registration/create-user-account', CONFIG.CLIENT_URI) + actionUrl.searchParams.set('nonce', nonce) + actionUrl.searchParams.set('email', email) + + return { + from, + to: email, + subject: 'Willkommen, Bienvenue, Welcome to Human Connection!', + html: mustache.render(signupHtml, { actionUrl, supportUrl }), + } +} + +export const resetPasswordTemplate = ({ email, nonce, name }) => { + const actionUrl = new URL('/password-reset/change-password', CONFIG.CLIENT_URI) + actionUrl.searchParams.set('nonce', nonce) + actionUrl.searchParams.set('email', email) + + return { + from, + to: email, + subject: 'Neues Passwort | Reset Password', + html: mustache.render(passwordResetHtml, { actionUrl, name, nonce, supportUrl }), + } +} + +export const wrongAccountTemplate = ({ email }) => { + const actionUrl = new URL('/password-reset/request', CONFIG.CLIENT_URI) + + return { + from, + to: email, + subject: 'Falsche Mailadresse? | Wrong Email?', + html: mustache.render(wrongAccountHtml, { actionUrl, supportUrl }), + } +} diff --git a/backend/src/schema/resolvers/passwordReset/emailTemplates.js b/backend/src/schema/resolvers/passwordReset/emailTemplates.js deleted file mode 100644 index 8508adccc..000000000 --- a/backend/src/schema/resolvers/passwordReset/emailTemplates.js +++ /dev/null @@ -1,85 +0,0 @@ -import CONFIG from '../../../config' - -export const from = '"Human Connection" ' - -export const resetPasswordMail = options => { - const { - name, - email, - code, - subject = 'Use this link to reset your password. The link is only valid for 24 hours.', - supportUrl = 'https://human-connection.org/en/contact/', - } = options - const actionUrl = new URL('/password-reset/change-password', CONFIG.CLIENT_URI) - actionUrl.searchParams.set('code', code) - actionUrl.searchParams.set('email', email) - - return { - to: email, - subject, - text: ` -Hi ${name}! - -You recently requested to reset your password for your Human Connection account. -Use the link below to reset it. This password reset is only valid for the next -24 hours. - -${actionUrl} - -If you did not request a password reset, please ignore this email or contact -support if you have questions: - -${supportUrl} - -Thanks, -The Human Connection Team - -If you're having trouble with the link above, you can manually copy and -paste the following code into your browser window: - -${code} - -Human Connection gemeinnützige GmbH -Bahnhofstr. 11 -73235 Weilheim / Teck -Deutschland - `, - } -} - -export const wrongAccountMail = options => { - const { - email, - subject = `We received a request to reset your password with this email address (${email})`, - supportUrl = 'https://human-connection.org/en/contact/', - } = options - const actionUrl = new URL('/password-reset/request', CONFIG.CLIENT_URI) - return { - to: email, - subject, - text: ` -We received a request to reset the password to access Human Connection with your -email address, but we were unable to find an account associated with this -address. - -If you use Human Connection and were expecting this email, consider trying to -request a password reset using the email address associated with your account. -Try a different email: - -${actionUrl} - -If you do not use Human Connection or did not request a password reset, please -ignore this email. Feel free to contact support if you have further questions: - -${supportUrl} - -Thanks, -The Human Connection Team - -Human Connection gemeinnützige GmbH -Bahnhofstr. 11 -73235 Weilheim / Teck -Deutschland - `, - } -}