Skip to content
Snippets Groups Projects
Commit 46d07119 authored by Andrei Cioromila's avatar Andrei Cioromila
Browse files

refactor(user): Refactor new user flows to build a new EmailTemplate for each notification

parent dbfef05f
No related branches found
No related tags found
3 merge requests!160Update staging with master features,!156Develop,!149Hin 1089
......@@ -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 = {
......
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 })
},
}
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()
},
}
......@@ -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({})
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment