Skip to content
Snippets Groups Projects
Commit 618cb409 authored by Sebastian Mihalache's avatar Sebastian Mihalache
Browse files

refactor notification email

parent 8a59f0a3
No related branches found
No related tags found
2 merge requests!21Sprint #16 features,!17Email helper refactor
Showing
with 131 additions and 304 deletions
const Fragment = require('./Fragment')
const User = require('./User')
const services = require('./services')
const get = require('lodash/get')
const config = require('config')
const mailService = require('pubsweet-component-mail-service')
const mailService = require('pubsweet-component-mail-template-service')
const unsubscribeSlug = config.get('unsubscribe.url')
const manuscriptTypes = config.get('manuscript-types')
......@@ -160,7 +163,7 @@ class Email {
baseUrl,
UserModel,
collection,
parsedFragment: { heRecommendation, id, title, newComments },
parsedFragment: { heRecommendation, id: fragmentId, title, newComments },
authors: {
submittingAuthor: {
id: submittingAuthorId,
......@@ -174,11 +177,20 @@ class Email {
if (requestToRevision) comments = newComments
const authorNote = comments.find(comm => comm.public)
const content = get(authorNote, 'content')
const authorNoteText = content ? `Reason & Details: "${content}"` : ''
let emailType = requestToRevision
? 'author-request-to-revision'
: 'author-manuscript-rejected'
if (publish) emailType = 'author-manuscript-published'
const notes = content ? `Reason & Details: "${content}"` : ''
let emailType, subject
if (requestToRevision) {
emailType = 'author-request-to-revision'
subject = `${collection.customId}: Manuscript Recommendation`
} else if (publish) {
emailType = 'author-manuscript-published'
subject = `${collection.customId}: Manuscript Published`
} else {
emailType = 'author-manuscript-rejected'
subject = `${collection.customId}: Manuscript Rejected`
}
let toAuthors = null
if (emailType === 'author-request-to-revision') {
toAuthors = [
......@@ -189,7 +201,7 @@ class Email {
},
]
} else {
const fragment = await FragmentModel.find(id)
const fragment = await FragmentModel.find(fragmentId)
const userHelper = new User({ UserModel })
const activeAuthors = await userHelper.getActiveAuthors(fragment.authors)
......@@ -199,23 +211,39 @@ class Email {
name: `${author.firstName} ${author.lastName}`,
}))
}
toAuthors.forEach(toAuthor => {
mailService.sendNotificationEmail({
emailType,
toId: toAuthor.id,
toEmail: toAuthor.email,
meta: {
handlingEditorName: get(collection, 'handlingEditor.name'),
baseUrl,
collection,
authorNoteText,
fragment: {
id,
title,
authorName: toAuthor.name,
submittingAuthorName: `${firstName} ${lastName}`,
},
notes,
subject,
hasLink: false,
manuscriptTitleText: `the manuscript titled "${title}" by ${firstName} ${lastName}`,
replacements: {
signatureName: get(collection, 'handlingEditor.name'),
toUserName: toAuthor.name,
beforeAnchor: 'Please visit the',
afterAnchor: 'to see the full review',
detailsUrl: services.createUrl(
baseUrl,
`/projects/${collection.id}/versions/${fragmentId}/details`,
),
unsubscribeLink: services.createUrl(baseUrl, unsubscribeSlug, {
id: toAuthor.id,
}),
},
// meta: {
// handlingEditorName: get(collection, 'handlingEditor.name'),
// baseUrl,
// collection,
// fragment: {
// id,
// title,
// authorName: toAuthor.name,
// submittingAuthorName: `${firstName} ${lastName}`,
// },
// },
})
})
}
......
const logger = require('@pubsweet/logger')
const querystring = require('querystring')
const checkForUndefinedParams = (...params) => {
if (params.includes(undefined)) {
......@@ -75,9 +76,15 @@ const handleNotFoundError = async (error, item) => {
const getBaseUrl = req => `${req.protocol}://${req.get('host')}`
const createUrl = (baseUrl, slug, queryParams = null) =>
!queryParams
? `${baseUrl}${slug}`
: `${baseUrl}${slug}?${querystring.encode(queryParams)}`
module.exports = {
checkForUndefinedParams,
validateEmailAndToken,
handleNotFoundError,
getBaseUrl,
createUrl,
}
module.exports = require('./src/Mail')
module.exports = require('./src/MailTemplate')
{
"name": "pubsweet-component-mail-service",
"name": "pubsweet-component-mail-template-service",
"version": "0.0.1",
"description": "mail service component for pubsweet",
"description": "mail template service component for pubsweet",
"license": "MIT",
"author": "Collaborative Knowledge Foundation",
"files": [
......@@ -10,8 +10,7 @@
"main": "index.js",
"repository": {
"type": "git",
"url": "https://gitlab.coko.foundation/xpub/xpub",
"path": "component-mail-service"
"url": "https://gitlab.coko.foundation/xpub/xpub"
},
"dependencies": {
"@pubsweet/component-send-email": "0.2.1",
......
......@@ -320,368 +320,161 @@ module.exports = {
return Email.send(mailData)
},
sendNotificationEmail: async ({
user,
toId,
notes,
toEmail,
subject,
emailType,
meta = { privateNote: '' },
timestamp,
reviewerName,
replacements,
manuscriptTitleText,
}) => {
let subject, textBody
// const replacements = {
// hasLink: true,
// paragraph,
// signatureName,
// intro: `Dear Dr. ${toUser.name}`,
// beforeAnchor: 'Please visit the',
// afterAnchor: 'to see the full review',
// detailsUrl: helpers.createUrl(
// baseUrl,
// `/projects/${collectionId}/versions/${fragmentId}/details`,
// ),
// unsubscribeLink: helpers.createUrl(baseUrl, unsubscribeSlug, {
// id: toUser.id,
// }),
// }
// const manuscriptTitleText = `the manuscript titled "${title}" by ${authorName}`
const emailTemplate = 'notification'
const detailsUrl = meta.baseUrl
? helpers.createUrl(
meta.baseUrl,
`/projects/${meta.collection.id}/versions/${
meta.fragment.id
}/details`,
)
: ''
const replacements = {
detailsUrl,
hasLink: true,
afterAnchor: '',
beforeAnchor: '',
unsubscribeLink: helpers.createUrl(meta.baseUrl, unsubscribeSlug, {
id: toId,
}),
}
switch (emailType) {
case 'unassign-reviewer':
subject = `${meta.collection.customId}: Review Unassigned`
replacements.previewText = 'notification from Hindawi'
replacements.intro = `Dear ${user.firstName} ${user.lastName}`
replacements.paragraph = `You are no longer needed to review the article titled "${
meta.fragment.title
}" by ${
meta.fragment.authorName
}. If you have comments on this manuscript you believe the Editor should
replacements.paragraph = `You are no longer needed to review ${manuscriptTitleText}. If you have comments on this manuscript you believe the Editor should
see, please email them to ${config.get(
'mailer.from',
)} as soon as possible. Thank you for your
time and I hope you will consider reviewing for Hindawi again.`
replacements.beforeAnchor = 'Please visit the'
replacements.afterAnchor = 'to see the full review'
replacements.signatureName = meta.handlingEditorName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.beforeAnchor
} ${replacements.detailsUrl} ${replacements.afterAnchor} ${
replacements.signatureName
}`
break
case 'review-submitted':
subject = `${meta.collection.customId}: Manuscript Review`
replacements.previewText = 'a new review has been submitted'
replacements.intro = `Dear ${meta.handlingEditorName}`
replacements.paragraph = `We are pleased to inform you that Dr. ${
meta.reviewerName
} has submitted a review for the manuscript titled "${
meta.fragment.title
}" by ${meta.fragment.authorName}.`
replacements.beforeAnchor = 'Please visit the'
replacements.afterAnchor = 'to see the full review'
replacements.signatureName = meta.eicName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.beforeAnchor
} ${replacements.detailsUrl} ${replacements.afterAnchor} ${
replacements.signatureName
}`
// subject = `${customId}: Manuscript Review`
replacements.paragraph = `We are pleased to inform you that Dr. ${reviewerName} has submitted a review for ${manuscriptTitleText}.`
break
case 'reviewer-agreed':
subject = `${meta.collection.customId}: Manuscript Reviews`
replacements.previewText = 'a user has agreed to review'
replacements.intro = `Dear ${meta.handlingEditorName}`
replacements.paragraph = `We are pleased to inform you that Dr. ${
user.firstName
} ${user.lastName} has agreed to review the manuscript titled "${
meta.fragment.title
}" by ${
meta.fragment.authorName
}. You should receive the report by Dr. ${user.firstName} ${
user.lastName
} before ${helpers.getExpectedDate(meta.timestamp, 14)}.`
// subject = `${meta.collection.customId}: Manuscript Reviews`
replacements.paragraph = `We are pleased to inform you that Dr. ${reviewerName} has agreed to review ${manuscriptTitleText}. <br/>
You should receive the report by Dr. ${reviewerName} before ${helpers.getExpectedDate(
timestamp,
14,
)}.`
replacements.beforeAnchor =
'If you have any queries, or would like to send a reminder if you no report has been submitted, then please visit the'
replacements.afterAnchor = 'to see the full review'
replacements.signatureName = meta.eicName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.beforeAnchor
} ${replacements.detailsUrl} ${replacements.afterAnchor} ${
replacements.signatureName
}`
break
case 'reviewer-declined':
subject = `${meta.collection.customId}: Manuscript Reviews`
replacements.previewText = 'a user has declined to review'
replacements.intro = `Dear ${meta.handlingEditorName}`
replacements.paragraph = `We regret to inform you that Dr. ${
user.firstName
} ${user.lastName} has declined to review the manuscript titled "${
meta.fragment.title
}" by ${meta.fragment.authorName}.`
replacements.beforeAnchor = 'Please visit the'
// subject = `${meta.collection.customId}: Manuscript Reviews`
replacements.paragraph = `We regret to inform you that Dr. ${reviewerName} has declined to review ${manuscriptTitleText}.`
replacements.afterAnchor =
'to see if you need to invite any additional reviewers in order to reach a decision on the manuscript'
replacements.signatureName = meta.eicName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.beforeAnchor
} ${replacements.detailsUrl} ${replacements.afterAnchor} ${
replacements.signatureName
}`
break
case 'reviewer-thank-you':
subject = `${meta.collection.customId}: Manuscript Review`
replacements.previewText = 'Hindawi says thank you'
replacements.intro = `Dear Dr. ${user.firstName} ${user.lastName}`
replacements.paragraph = `Thank you for agreeing to review the manuscript titled "${
meta.fragment.title
}" by ${meta.fragment.authorName}.`
// subject = `${meta.collection.customId}: Manuscript Review`
replacements.paragraph = `Thank you for agreeing to review ${manuscriptTitleText}.`
replacements.beforeAnchor =
'You can view the full PDF file of the manuscript and post your review report using the following URL:'
replacements.signatureName = meta.handlingEditorName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.beforeAnchor
} ${replacements.detailsUrl} ${replacements.afterAnchor} ${
replacements.signatureName
}`
replacements.afterAnchor = ''
break
case 'agreed-reviewers-after-recommendation':
subject = meta.emailSubject
case 'agreed-reviewers-after-he-recommendation':
replacements.hasLink = false
replacements.previewText =
'a manuscript has received a recommendation based on reviews'
replacements.intro = `Dear Dr. ${meta.reviewerName}`
replacements.paragraph = `I appreciate any time you may have spent reviewing manuscript "${
meta.fragment.title
}" by ${
meta.fragment.authorName
}. However, an editorial decision has been made and the review of this manuscript is now complete. I apologize for any inconvenience. <br/>
replacements.paragraph = `I appreciate any time you may have spent reviewing ${manuscriptTitleText}. However, an editorial decision has been made and the review of this manuscript is now complete. I apologize for any inconvenience. <br/>
If you have comments on this manuscript you believe the Editor should see, please email them to Hindawi as soon as possible. <br/>
Thank you for your interest and I hope you will consider reviewing for Hindawi again.`
delete replacements.detailsUrl
replacements.signatureName = meta.editorName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.signatureName
}`
break
case 'no-response-reviewers-after-recommendation':
subject = meta.emailSubject
case 'pending-reviewers-after-he-recommendation':
replacements.hasLink = false
replacements.previewText = 'a manuscript has reached a decision'
replacements.intro = `Dear Dr. ${meta.reviewerName}`
replacements.paragraph = `An editorial decision has been made regarding the ${
meta.manuscriptType
} titled "${meta.fragment.title}" by ${
meta.fragment.authorName
}. So, you do not need to proceed with the review of this manuscript. <br/><br/>
replacements.paragraph = `An editorial decision has been made regarding ${manuscriptTitleText}. So, you do not need to proceed with the review of this manuscript. <br/><br/>
If you have comments on this manuscript you believe the Editor should see, please email them to Hindawi as soon as possible.`
delete replacements.detailsUrl
replacements.signatureName = meta.editorName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.signatureName
}`
break
case 'eic-recommendation':
subject = `${meta.collection.customId}: Manuscript Recommendation`
replacements.previewText =
'a handling editor has submitted a recommendation'
replacements.intro = `Dear Dr. ${meta.eicName}`
replacements.paragraph = meta.paragraph
// subject = `${meta.collection.customId}: Manuscript Recommendation`
replacements.beforeAnchor =
'For more information about what is required, please visit the '
replacements.signatureName = meta.handlingEditorName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.beforeAnchor
} ${replacements.detailsUrl} ${replacements.afterAnchor} ${
replacements.signatureName
}`
break
case 'author-request-to-revision':
subject = `${meta.collection.customId}: Manuscript Recommendation`
replacements.previewText =
'a handling editor has submitted a recommendation'
replacements.intro = `Dear Dr. ${meta.fragment.authorName}`
replacements.paragraph = `In order for the manuscript titled "${
meta.fragment.title
}" by ${
meta.fragment.authorName
} to proceed to publication, there needs to be a revision. <br/><br/>
${meta.authorNoteText}<br/><br/>`
// subject = `${meta.collection.customId}: Manuscript Recommendation`
replacements.paragraph = `In order for ${manuscriptTitleText} to proceed to publication, there needs to be a revision. <br/><br/>
${notes}<br/><br/>`
replacements.beforeAnchor =
'For more information about what is required, please visit the '
replacements.signatureName = meta.handlingEditorName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.beforeAnchor
} ${replacements.detailsUrl} ${replacements.signatureName}`
break
case 'author-manuscript-rejected':
subject = `${meta.collection.customId}: Manuscript Rejected`
replacements.previewText = 'a manuscript has been rejected'
replacements.intro = `Dear Dr. ${meta.fragment.authorName}`
replacements.paragraph = `I am sorry to inform you that manuscript titled "${
meta.fragment.title
}" by ${
meta.fragment.submittingAuthorName
} has been rejected for publication. <br/><br/>
${meta.authorNoteText}<br/><br/>`
// subject = `${meta.collection.customId}: Manuscript Rejected`
replacements.paragraph = `I am sorry to inform you that ${manuscriptTitleText} has been rejected for publication. <br/><br/>
${notes}<br/><br/>`
replacements.hasLink = false
delete replacements.detailsUrl
replacements.signatureName = meta.handlingEditorName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.signatureName
}`
break
case 'author-manuscript-published':
subject = `${meta.collection.customId}: Manuscript Decision`
replacements.previewText = 'a manuscript has been published'
replacements.intro = `Dear Dr. ${meta.fragment.authorName}`
replacements.paragraph = `I am delighted to inform you that manuscript titled "${
meta.fragment.title
}" by ${
meta.fragment.submittingAuthorName
} has passed through the review process and will be published in Hindawi.<br/><br/>
${meta.authorNoteText}<br/><br/>
replacements.paragraph = `I am delighted to inform you that ${manuscriptTitleText} has passed through the review process and will be published in Hindawi.<br/><br/>
${notes}<br/><br/>
Thanks again for choosing to publish with us.`
replacements.hasLink = false
delete replacements.detailsUrl
replacements.signatureName = meta.handlingEditorName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.signatureName
}`
break
case 'he-manuscript-rejected':
subject = meta.emailSubject
// subject = meta.emailSubject
replacements.hasLink = false
replacements.previewText = 'a manuscript has been rejected'
replacements.intro = `Dear Dr. ${meta.handlingEditorName}`
replacements.paragraph = `Thank you for your recommendation to reject the manuscript titled "${
meta.fragment.title
}" by ${
meta.fragment.authorName
} based on the reviews you received.<br/><br/>
replacements.paragraph = `Thank you for your recommendation to reject ${manuscriptTitleText} based on the reviews you received.<br/><br/>
I can confirm this article has now been rejected.`
delete replacements.detailsUrl
replacements.signatureName = meta.eicName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.signatureName
}`
break
case 'he-manuscript-published':
subject = meta.emailSubject
// subject = meta.emailSubject
replacements.hasLink = false
replacements.previewText = 'a manuscript has been published'
replacements.intro = `Dear Dr. ${meta.handlingEditorName}`
replacements.paragraph = `Thank you for your recommendation to publish the manuscript titled "${
meta.fragment.title
}" by ${
meta.fragment.authorName
} based on the reviews you received.<br/><br/>
replacements.paragraph = `Thank you for your recommendation to publish the manuscript titled ${manuscriptTitleText} based on the reviews you received.<br/><br/>
I can confirm this article will now go through to publication.`
delete replacements.detailsUrl
replacements.signatureName = meta.eicName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.signatureName
}`
break
case 'he-manuscript-return-with-comments':
subject = meta.emailSubject
// subject = meta.emailSubject
replacements.hasLink = false
replacements.previewText =
'a manuscript has been returned with comments'
replacements.intro = `Dear Dr. ${meta.handlingEditorName}`
replacements.paragraph = `Thank you for your recommendation for the manuscript titled "${
meta.fragment.title
}" by ${
meta.fragment.authorName
} based on the reviews you received.<br/><br/>
${meta.eicComments}<br/><br/>`
delete replacements.detailsUrl
replacements.signatureName = meta.eicName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.signatureName
}`
replacements.paragraph = `Thank you for your recommendation for ${manuscriptTitleText} based on the reviews you received.<br/><br/>
${notes}<br/><br/>`
break
case 'submitting-reviewers-after-decision':
subject = meta.emailSubject
// subject = meta.emailSubject
replacements.hasLink = false
replacements.previewText = 'a decision has been made on a manuscript'
replacements.intro = `Dear Dr. ${meta.reviewerName}`
replacements.paragraph = `Thank you for your review on the manuscript titled "${
meta.fragment.title
}" by ${
meta.fragment.authorName
}. After taking into account the reviews and the recommendation of the Handling Editor, I can confirm this article ${
meta.emailText
}.<br/><br/>
replacements.paragraph = `Thank you for your review on ${manuscriptTitleText}. After taking into account the reviews and the recommendation of the Handling Editor, I can confirm this article ${notes}.<br/><br/>
If you have any queries about this decision, then please email them to Hindawi as soon as possible.`
delete replacements.detailsUrl
replacements.signatureName = meta.editorName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.signatureName
}`
break
case 'new-version-submitted':
subject = `${meta.collection.customId}: Manuscript Update`
replacements.previewText = 'A manuscript has been updated'
replacements.intro = `Dear Dr. ${meta.handlingEditorName}`
replacements.paragraph = `A new version of the manuscript titled "${
meta.fragment.title
}" by ${meta.fragment.authorName} has been submitted.`
// subject = `${meta.collection.customId}: Manuscript Update`
replacements.paragraph = `A new version of ${manuscriptTitleText} has been submitted.`
replacements.beforeAnchor =
'Previous reviewers have been automatically invited to review the manuscript again. Please visit the'
replacements.afterAnchor =
'to see the latest version and any other actions you may need to take'
replacements.signatureName = meta.eicName
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.beforeAnchor
} ${replacements.detailsUrl} ${replacements.afterAnchor} ${
replacements.signatureName
}`
break
case 'submitting-reviewers-after-revision':
subject = `${meta.collection.customId}: Manuscript Update`
replacements.previewText = 'A manuscript has been updated'
replacements.intro = `Dear Dr. ${meta.reviewerName}`
replacements.paragraph = `A new version of the manuscript titled "${
meta.fragment.title
}" by ${meta.fragment.authorName} has been submitted.`
// subject = `${meta.collection.customId}: Manuscript Update`
replacements.paragraph = `A new version of ${manuscriptTitleText} has been submitted.`
replacements.beforeAnchor = `As you have reviewed the previous version of this manuscript, I would be grateful if you can review this revised version and submit a review report by ${helpers.getExpectedDate(
meta.timestamp,
timestamp,
14,
)}. You can download the PDF of the revised version and submit your new review from the following URL:`
replacements.signatureName = meta.collection.handlingEditor.name
textBody = `${replacements.intro} ${replacements.paragraph} ${
replacements.beforeAnchor
} ${replacements.detailsUrl} ${replacements.signatureName}`
break
default:
subject = 'Hindawi Notification!'
break
}
const htmlBody = helpers.getNotificationBody(emailTemplate, replacements)
const htmlBody = helpers.getNotificationBody('notification', replacements)
const mailData = {
from: config.get('mailer.from'),
to: toEmail,
subject,
text: textBody,
text: `${replacements.intro} ${replacements.paragraph} ${
replacements.beforeAnchor
} ${replacements.detailsUrl} ${replacements.afterAnchor} ${
replacements.signatureName
}`,
html: htmlBody,
}
return Email.send(mailData)
Email.send(mailData)
},
}
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