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

refactor(component-manuscript-manager): send reviewers email

parent 60959239
No related branches found
No related tags found
2 merge requests!21Sprint #16 features,!17Email helper refactor
......@@ -126,6 +126,29 @@ class Fragment {
),
)
}
async getSubmittedReviewers({ UserModel }) {
const submittedInvitations = await this.getInvitationsForSubmittingReviewers()
return Promise.all(
submittedInvitations.map(inv => UserModel.find(inv.userId)),
)
}
async getAcceptedReviewers({ UserModel }) {
const agreedInvitations = this.getReviewerInvitations({
agree: true,
})
return Promise.all(agreedInvitations.map(inv => UserModel.find(inv.userId)))
}
async getPendingReviewers({ UserModel }) {
const pendingInvitations = this.getReviewerInvitations({
agree: false,
})
return Promise.all(
pendingInvitations.map(inv => UserModel.find(inv.userId)),
)
}
}
module.exports = Fragment
......@@ -151,121 +151,6 @@ class Email {
)
}
async setupAuthorsEmail({
requestToRevision = false,
publish = false,
FragmentModel,
}) {
const {
baseUrl,
UserModel,
collection,
parsedFragment: { heRecommendation, id, title, newComments },
authors: {
submittingAuthor: {
id: submittingAuthorId,
email,
firstName,
lastName,
},
},
} = this
let comments = get(heRecommendation, 'comments', [])
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'
let toAuthors = null
if (emailType === 'author-request-to-revision') {
toAuthors = [
{
id: submittingAuthorId,
email,
name: `${firstName} ${lastName}`,
},
]
} else {
const fragment = await FragmentModel.find(id)
const userHelper = new User({ UserModel })
const activeAuthors = await userHelper.getActiveAuthors(fragment.authors)
toAuthors = activeAuthors.map(author => ({
id: author.id,
email: author.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}`,
},
},
})
})
}
async setupHandlingEditorEmail({
publish = false,
returnWithComments = false,
reviewSubmitted = false,
reviewerName = '',
}) {
const {
baseUrl,
UserModel,
collection,
parsedFragment: { eicComments = '', title, id },
authors: { submittingAuthor: { firstName = '', lastName = '' } },
} = this
const user = get(collection, 'handlingEditor', {})
if (!get(user, 'notifications.email.user')) return
const userHelper = new User({ UserModel })
const eic = await userHelper.getEditorInChief()
let emailType = publish
? 'he-manuscript-published'
: 'he-manuscript-rejected'
if (reviewSubmitted) emailType = 'review-submitted'
if (returnWithComments) emailType = 'he-manuscript-return-with-comments'
mailService.sendNotificationEmail({
user,
toId: user.id,
toEmail: user.email,
emailType,
meta: {
baseUrl,
collection,
reviewerName,
eicComments,
eicName: `${eic.firstName} ${eic.lastName}`,
emailSubject: `${collection.customId}: Manuscript Decision`,
handlingEditorName: user.name || '',
fragment: {
id,
title,
authorName: `${firstName} ${lastName}`,
},
},
})
}
async setupEiCEmail({ recommendation, comments }) {
const {
baseUrl,
......
......@@ -68,17 +68,16 @@ module.exports = models => async (req, res) => {
const parsedFragment = await fragmentHelper.getFragmentData({
handlingEditor: collection.handlingEditor,
})
const fragmentAuthors = await fragmentHelper.getAuthorData({ UserModel })
const baseUrl = services.getBaseUrl(req)
const authors = await fragmentHelper.getAuthorData({ UserModel })
const email = new Email({
type: 'user',
content: {
subject: `${collection.customId}: Manuscript`,
titleText: `the manuscript titled "${parsedFragment.title}" by ${
authors.submittingAuthor.firstName
} ${authors.submittingAuthor.firstName}`,
fragmentAuthors.submittingAuthor.firstName
} ${fragmentAuthors.submittingAuthor.firstName}`,
},
signatureName: get(collection, 'handlingEditor.name'),
comments: parsedFragment.newComments,
......@@ -107,43 +106,35 @@ module.exports = models => async (req, res) => {
collectionHelper.updateFinalStatusByRecommendation({
recommendation,
})
sendAuthorsEmail({ recommendation, email, baseUrl, authors })
// email.parsedFragment.recommendations = fragment.recommendations
// email.setupReviewersEmail({
// recommendation,
// isSubmitted: true,
// agree: true,
// FragmentModel,
// })
}
} else if (recommendationType === 'editorRecommendation') {
collectionHelper.updateStatusByRecommendation({
recommendation,
isHandlingEditor: true,
})
// email.setupReviewersEmail({
// recommendation,
// agree: true,
// FragmentModel,
// })
// email.setupReviewersEmail({ agree: false, FragmentModel: models.Fragment })
// email.setupEiCEmail({
// recommendation,
// comments: newRecommendation.comments,
// })
if (['minor', 'major'].includes(recommendation)) {
fragment.revision = pick(fragment, ['authors', 'files', 'metadata'])
// email.parsedFragment.newComments = newRecommendation.comments
// email.setupAuthorsEmail({
// requestToRevision: true,
// })
}
}
if (recommendationType === 'editor-recommendation') {
sendAuthorsEmail({
recommendation,
email,
baseUrl,
isEditorInChief: reqUser.editorInChief || reqUser.admin,
fragmentAuthors,
})
sendReviewersEmail({
email,
baseUrl,
fragmentHelper,
recommendation,
isEditorInChief: reqUser.editorInChief || reqUser.admin,
})
}
fragment.recommendations.push(newRecommendation)
await fragment.save()
return res.status(200).json(newRecommendation)
......@@ -184,14 +175,30 @@ const sendHandlingEditorEmail = ({
email.sendEmail({ html, text })
}
const sendAuthorsEmail = ({ recommendation, email, baseUrl, authors }) => {
let emailType
if (recommendation === 'publish') {
emailType = 'author-manuscript-published'
email.content.subject = `${email.content.subject} Published`
const sendAuthorsEmail = async ({
recommendation,
email,
baseUrl,
isEditorInChief,
fragmentAuthors,
}) => {
let emailType, authors
if (isEditorInChief) {
if (recommendation === 'publish') {
emailType = 'author-manuscript-published'
email.content.subject = `${email.content.subject} Published`
} else {
emailType = 'author-manuscript-rejected'
email.content.subject = `${email.content.subject} Rejected`
}
authors = fragmentAuthors.authorsList.map(author => ({
...author,
emailType,
}))
} else {
emailType = 'author-manuscript-rejected'
email.content.subject = `${email.content.subject} Rejected`
email.content.comments = newComments
emailType = 'author-request-to-revision'
authors = [{ ...fragmentAuthors.submittingAuthor, emailType }]
}
authors.forEach(author => {
......@@ -206,7 +213,77 @@ const sendAuthorsEmail = ({ recommendation, email, baseUrl, authors }) => {
id: author.id,
},
)
const { html, text } = email.getBody(emailType)
const { html, text } = email.getBody(author.emailType)
email.sendEmail({ html, text })
})
}
const sendReviewersEmail = async ({
email,
baseUrl,
eicName,
UserModel,
fragmentHelper,
recommendation,
isEditorInChief,
handlingEditorName,
}) => {
let reviewers
if (isEditorInChief) {
email.content.subject = `${email.content.subject} Decision`
reviewers = (await fragmentHelper.getSubmittedReviewers({
UserModel,
})).map(rev => ({
...rev,
emailType: 'submitted-reviewers-after-recommendation',
}))
email.content.signatureName = eicName
} else {
email.content.signatureName = handlingEditorName
email.contet.subject = `${
email.content.subject
} ${getSubjectByRecommendation(recommendation)}`
const acceptedReviewers = (await fragmentHelper.getAcceptedReviewers({
UserModel,
})).map(rev => ({
...rev,
emailType: 'accepted-reviewers-after-recommendation',
}))
const pendingReviewers = (await fragmentHelper.getPendingReviewers({
UserModel,
})).map(rev => ({
...rev,
emailType: 'pending-reviewers-after-recommendation',
}))
reviewers = [...acceptedReviewers, ...pendingReviewers]
}
reviewers = reviewers
.filter(rev => rev.isActive)
.filter(rev => get(rev, 'notifications.email.user'))
reviewers.forEach(reviewer => {
email.toUser = {
email: reviewer.email,
name: `${reviewer.firstName} ${reviewer.lastName}`,
}
email.content.unsubscribeLink = services.createUrl(
baseUrl,
unsubscribeSlug,
{
id: reviewer.id,
},
)
const { html, text } = email.getBody(reviewer.emailType)
email.sendEmail({ html, text })
})
}
const getSubjectByRecommendation = recommendation =>
['minor', 'major'].includes(recommendation)
? 'Revision Requested'
: 'Recommendation Submitted'
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