diff --git a/packages/component-email/src/routes/emails/emailCopy.js b/packages/component-email/src/routes/emails/emailCopy.js index d14178be9815e574a83f2e16aefff8495e59affd..34900efe02066d5f4eaff9a081dfc670bfefad4a 100644 --- a/packages/component-email/src/routes/emails/emailCopy.js +++ b/packages/component-email/src/routes/emails/emailCopy.js @@ -3,8 +3,6 @@ const config = require('config') const journalName = config.get('journal.name') const getEmailCopy = ({ emailType, role }) => { let paragraph - const hasIntro = true - const hasSignature = true switch (emailType) { case 'user-signup': paragraph = `Thank you for creating an account on Hindawi’s review system. @@ -23,7 +21,7 @@ const getEmailCopy = ({ emailType, role }) => { throw new Error(`The ${emailType} email type is not defined.`) } - return { paragraph, hasLink: true, hasIntro, hasSignature } + return { paragraph, hasLink: true, hasIntro: true, hasSignature: true } } module.exports = { diff --git a/packages/component-email/src/routes/emails/helpers.js b/packages/component-email/src/routes/emails/helpers.js deleted file mode 100644 index 35395e3da3bb1fac36c7d2006ca49122f955cb3e..0000000000000000000000000000000000000000 --- a/packages/component-email/src/routes/emails/helpers.js +++ /dev/null @@ -1,39 +0,0 @@ -const config = require('config') -const { services } = require('pubsweet-component-helper-service') - -const { getEmailCopy } = require('./emailCopy') - -const confirmSignUp = config.get('confirm-signup.url') - -module.exports = { - sendNewUserEmail: ({ email, role }) => { - email.content.subject = 'Confirm your account' - - const emailType = - role === 'Handling Editor' ? 'he-added-by-admin' : 'user-added-by-admin' - - const { html, text } = email.getNotificationBody({ - emailBodyProps: getEmailCopy({ - role, - emailType, - }), - }) - - email.sendEmail({ html, text }) - }, - sendSignupEmail: ({ email, baseUrl, user }) => { - email.content.subject = 'Confirm your email address' - email.content.ctaLink = services.createUrl(baseUrl, confirmSignUp, { - userId: user.id, - confirmationToken: user.accessTokens.confirmation, - }) - - const { html, text } = email.getNotificationBody({ - emailBodyProps: getEmailCopy({ - emailType: 'user-signup', - }), - }) - - email.sendEmail({ html, text }) - }, -} diff --git a/packages/component-email/src/routes/emails/notifications.js b/packages/component-email/src/routes/emails/notifications.js index 5fe6fc1183bef9386e5b8f811a0ee6084e1387b3..469737f6096bdf80da0011060734d0100ab58db2 100644 --- a/packages/component-email/src/routes/emails/notifications.js +++ b/packages/component-email/src/routes/emails/notifications.js @@ -1,15 +1,23 @@ const config = require('config') const unsubscribeSlug = config.get('unsubscribe.url') -const resetPath = config.get('invite-reset-password.url') const { staffEmail, name: journalName } = config.get('journal') const Email = require('@pubsweet/component-email-templating') const { services } = require('pubsweet-component-helper-service') - -const { sendNewUserEmail, sendSignupEmail } = require('./helpers') +const { getEmailCopy } = require('./emailCopy') module.exports = { - async sendNotifications({ user, baseUrl, role }) { + sendNewUserEmail: ({ user, baseUrl, role }) => { + const resetPath = config.get('invite-reset-password.url') + + const emailType = + role === 'Handling Editor' ? 'he-added-by-admin' : 'user-added-by-admin' + + const { paragraph, ...bodyProps } = getEmailCopy({ + role, + emailType, + }) + const email = new Email({ type: 'user', fromEmail: `${journalName} <${staffEmail}>`, @@ -18,6 +26,7 @@ module.exports = { name: `${user.lastName}`, }, content: { + subject: 'Confirm your account', ctaLink: services.createUrl(baseUrl, resetPath, { email: user.email, token: user.accessTokens.passwordReset, @@ -28,18 +37,49 @@ module.exports = { country: user.country, }), ctaText: 'CONFIRM ACCOUNT', + paragraph, + signatureName: 'Hindawi', + unsubscribeLink: services.createUrl(baseUrl, unsubscribeSlug, { + id: user.id, + token: user.accessTokens.unsubscribe, + }), + }, + bodyProps, + }) + + email.sendEmail() + }, + sendSignupEmail: ({ user, baseUrl }) => { + const confirmSignUp = config.get('confirm-signup.url') + + const { paragraph, ...bodyProps } = getEmailCopy({ + emailType: 'user-signup', + }) + + const email = new Email({ + type: 'user', + fromEmail: `${journalName} <${staffEmail}>`, + toUser: { + email: user.email, + name: `${user.lastName}`, + }, + content: { + subject: 'Confirm your email address', + ctaLink: services.createUrl(baseUrl, confirmSignUp, { + userId: user.id, + confirmationToken: user.accessTokens.confirmation, + }), + ctaText: 'CONFIRM ACCOUNT', + paragraph, signatureName: 'Hindawi', unsubscribeLink: services.createUrl(baseUrl, unsubscribeSlug, { id: user.id, token: user.accessTokens.unsubscribe, }), }, + bodyProps, }) - if (role) { - sendNewUserEmail({ email, role }) - } else { - sendSignupEmail({ email, baseUrl, user }) - } + email.sendEmail() }, } diff --git a/packages/component-email/src/routes/emails/post.js b/packages/component-email/src/routes/emails/post.js index 246322fa3a7da378a16d3ade2b684c405b6ec5b8..67e071e283188b1ed038a3bdf02941ceff7ca862 100644 --- a/packages/component-email/src/routes/emails/post.js +++ b/packages/component-email/src/routes/emails/post.js @@ -16,53 +16,49 @@ module.exports = models => async (req, res) => { return res.status(400).json({ error: `Email type ${type} is not defined.` }) } - const UserModel = models.User - + let user try { - const user = await UserModel.findByEmail(email) + user = await models.User.findByEmail(email) + } catch (e) { + const notFoundError = await services.handleNotFoundError(e, 'User') + return res.status(notFoundError.status).json({ + error: notFoundError.message, + }) + } - if (type === 'signup') { - if (!user.accessTokens.confirmation) { - return res - .status(400) - .json({ error: 'User does not have a confirmation token.' }) - } + if (type === 'signup') { + if (!user.accessTokens.confirmation) { + return res + .status(400) + .json({ error: 'User does not have a confirmation token.' }) } - + notifications.sendSignupEmail({ user, baseUrl: services.getBaseUrl(req) }) + } else if (type === 'invite') { let emailRole - if (type === 'invite') { - switch (role) { - case 'handlingEditor': - emailRole = 'Handling Editor' - break - case 'editorInChief': - emailRole = 'Editor in Chief' - break - case 'admin': - emailRole = 'Administrator' - break - case 'author': - emailRole = 'Author' - break - default: - return res.status(400).json({ - error: `Role ${role} is not defined.`, - }) - } + switch (role) { + case 'handlingEditor': + emailRole = 'Handling Editor' + break + case 'editorInChief': + emailRole = 'Editor in Chief' + break + case 'admin': + emailRole = 'Administrator' + break + case 'author': + emailRole = 'Author' + break + default: + return res.status(400).json({ + error: `Role ${role} is not defined.`, + }) } - - notifications.sendNotifications({ + notifications.sendNewUserEmail({ user, - role: emailRole, - UserModel: models.User, baseUrl: services.getBaseUrl(req), - }) - - return res.status(200).json({}) - } catch (e) { - const notFoundError = await services.handleNotFoundError(e, 'User') - return res.status(notFoundError.status).json({ - error: notFoundError.message, + role: emailRole, }) } + + return res.status(200).json({}) }