From f807ac82487fa49bc3fb167e4da8b59bc550a63d Mon Sep 17 00:00:00 2001
From: Sebastian Mihalache <sebastian.mihalache@gmail.con>
Date: Wed, 23 May 2018 15:26:14 +0300
Subject: [PATCH] feat(component-manuscript-manager): send emails after HE
 revisions

---
 packages/component-mail-service/src/Mail.js   | 39 ++++----
 .../src/helpers/Collection.js                 | 12 +--
 .../src/helpers/User.js                       | 88 ++++++++++++++++---
 .../routes/fragmentsRecommendations/post.js   | 34 ++++---
 4 files changed, 130 insertions(+), 43 deletions(-)

diff --git a/packages/component-mail-service/src/Mail.js b/packages/component-mail-service/src/Mail.js
index 05f85e808..49ac9fcbc 100644
--- a/packages/component-mail-service/src/Mail.js
+++ b/packages/component-mail-service/src/Mail.js
@@ -256,10 +256,7 @@ module.exports = {
     meta = { privateNote: '' },
   }) => {
     let subject, textBody
-    const privateNoteText =
-      meta.privateNote !== undefined
-        ? `Private note: "${meta.privateNote}"`
-        : ''
+
     const emailTemplate = 'notification'
     const replacements = {
       detailsUrl: helpers.createUrl(
@@ -375,10 +372,8 @@ module.exports = {
           replacements.signatureName
         }`
         break
-      case 'after-he-recommendation':
-        subject = `${
-          meta.collection.customId
-        }: Manuscript Recommendation Submitted`
+      case 'agreed-reviewers-after-recommendation':
+        subject = meta.emailSubject
         replacements.hasLink = false
         replacements.previewText =
           'a manuscript has received a recommendation based on reviews'
@@ -402,15 +397,29 @@ module.exports = {
         replacements.previewText =
           'a handling editor has submitted a recommendation'
         replacements.intro = `Dear Dr. ${meta.eicName}`
-        replacements.paragraph = `It is my recommendation, based on the reviews I have received for the manuscript titled "${
+        replacements.paragraph = meta.paragraph
+        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}, that ${
-          meta.heRecommendation
-        }. <br/><br/>
-        ${privateNoteText}<br/><br/>`
-
+        }" by ${
+          meta.fragment.authorName
+        } to proceed to publication, there needs to be a revision. <br/><br/>
+        ${meta.authorNoteText}<br/><br/>`
         replacements.beforeAnchor =
-          'For more information, and to see the full review, please visit the '
+          'For more information about what is required, please visit the '
         replacements.signatureName = meta.handlingEditorName
         textBody = `${replacements.intro} ${replacements.paragraph} ${
           replacements.beforeAnchor
diff --git a/packages/component-manuscript-manager/src/helpers/Collection.js b/packages/component-manuscript-manager/src/helpers/Collection.js
index 166dd37ab..7717c3395 100644
--- a/packages/component-manuscript-manager/src/helpers/Collection.js
+++ b/packages/component-manuscript-manager/src/helpers/Collection.js
@@ -3,7 +3,10 @@ const last = require('lodash/last')
 
 const statuses = config.get('statuses')
 
-const updateStatus = async (collection, newStatus) => {
+const updateStatusByRecommendation = async (collection, recommendation) => {
+  let newStatus = 'pendingApproval'
+  if (['minor', 'major'].includes(recommendation))
+    newStatus = 'revisionRequested'
   collection.status = newStatus
   collection.visibleStatus = statuses[collection.status].private
   await collection.save()
@@ -22,15 +25,14 @@ const getFragmentAndAuthorData = async ({
   const submittingAuthorData = authors.find(
     author => author.isSubmitting === true,
   )
-  const author = await UserModel.find(submittingAuthorData.userId)
-  const authorName = `${author.firstName} ${author.lastName}`
+  const submittingAuthor = await UserModel.find(submittingAuthorData.userId)
   const authorsPromises = authors.map(async author => {
     const user = await UserModel.find(author.userId)
     return `${user.firstName} ${user.lastName}`
   })
   const authorsList = await Promise.all(authorsPromises)
   const { id } = fragment
-  return { title, authorName, id, abstract, authorsList }
+  return { title, submittingAuthor, id, abstract, authorsList }
 }
 
 const getAgreedReviewerInvitation = (invitations = []) =>
@@ -42,7 +44,7 @@ const getAgreedReviewerInvitation = (invitations = []) =>
   )
 
 module.exports = {
-  updateStatus,
+  updateStatusByRecommendation,
   getFragmentAndAuthorData,
   getAgreedReviewerInvitation,
 }
diff --git a/packages/component-manuscript-manager/src/helpers/User.js b/packages/component-manuscript-manager/src/helpers/User.js
index 28290936f..c5e4e828f 100644
--- a/packages/component-manuscript-manager/src/helpers/User.js
+++ b/packages/component-manuscript-manager/src/helpers/User.js
@@ -41,13 +41,14 @@ const setupReviewSubmittedEmailData = async ({
   })
 }
 
-const setupAfterRecommendationEmailData = async ({
+const setupAgreedReviewersEmailData = async ({
   baseUrl,
   fragment: { title, authorName },
   collection,
   mailService,
   UserModel,
   FragmentModel,
+  recommendation,
 }) => {
   const agreedReviewerInvitations = collectionHelper.getAgreedReviewerInvitation(
     collection.invitations,
@@ -64,16 +65,20 @@ const setupAfterRecommendationEmailData = async ({
   })
   let users = await Promise.all(userPromises)
   users = users.filter(Boolean)
+  const subject = `${collection.customId}: Manuscript ${getSubject(
+    recommendation,
+  )}`
   users.forEach(user =>
     mailService.sendNotificationEmail({
       toEmail: user.email,
       user,
-      emailType: 'after-he-recommendation',
+      emailType: 'agreed-reviewers-after-recommendation',
       meta: {
         collection: { customId: collection.customId },
         fragment: { title, authorName },
         handlingEditorName: collection.handlingEditor.name,
         baseUrl,
+        emailSubject: subject,
       },
     }),
   )
@@ -85,15 +90,34 @@ const setupEiCRecommendationEmailData = async ({
   fragment: { id, title, authorName },
   collection,
   mailService,
-  publish,
+  recommendation,
   comments,
 }) => {
   // to do: get private note from recommendation
   const privateNote = comments.find(comm => comm.public === false)
   const content = get(privateNote, 'content')
-  const heRecommendation = publish
-    ? 'we should proceed to publication'
-    : 'we should reject it for publication'
+  const privateNoteText =
+    content !== undefined ? `Private note: "${content}"` : ''
+  let paragraph
+  const heRecommendation = getHeRecommendation(recommendation)
+  switch (heRecommendation) {
+    case 'publish':
+      paragraph = `It is my recommendation, based on the reviews I have received for the manuscript titled "${title}" by ${authorName}, that we should proceed to publication. <br/><br/>
+      ${privateNoteText}<br/><br/>`
+      break
+    case 'reject':
+      paragraph = `It is my recommendation, based on the reviews I have received for the manuscript titled "${title}" by ${authorName}, that we should reject it for publication. <br/><br/>
+      ${privateNoteText}<br/><br/>`
+      break
+    case 'revision':
+      paragraph = `In order for the manuscript titled "${title}" by ${authorName} to proceed to publication, there needs to be a revision. <br/><br/>
+      ${privateNoteText}<br/><br/>`
+      break
+
+    default:
+      throw new Error('undefined he recommentation type')
+  }
+
   const eic = await getEditorInChief(UserModel)
   const toEmail = eic.email
   await mailService.sendNotificationEmail({
@@ -101,19 +125,63 @@ const setupEiCRecommendationEmailData = async ({
     emailType: 'eic-recommendation',
     meta: {
       collection: { customId: collection.customId, id: collection.id },
-      fragment: { id, title, authorName },
+      fragment: { id },
       handlingEditorName: collection.handlingEditor.name,
       baseUrl,
       eicName: `${eic.firstName} ${eic.lastName}`,
-      heRecommendation,
-      privateNote: content,
+      paragraph,
+    },
+  })
+}
+
+const setupAuthorRequestToRevisionEmailData = async ({
+  baseUrl,
+  UserModel,
+  fragment: { id, title, submittingAuthor },
+  collection,
+  mailService,
+  comments,
+}) => {
+  const authorNote = comments.find(comm => comm.public === true)
+  const content = get(authorNote, 'content')
+  const authorNoteText =
+    content !== undefined ? `Reason & Details: "${content}"` : ''
+  await mailService.sendNotificationEmail({
+    toEmail: submittingAuthor.email,
+    emailType: 'author-request-to-revision',
+    meta: {
+      collection: { customId: collection.customId, id: collection.id },
+      fragment: {
+        id,
+        title,
+        authorName: `${submittingAuthor.firstName} ${
+          submittingAuthor.lastName
+        }`,
+      },
+      handlingEditorName: collection.handlingEditor.name,
+      baseUrl,
+      authorNoteText,
     },
   })
 }
 
+const getSubject = recommendation =>
+  ['minor', 'major'].includes(recommendation)
+    ? 'Revision Requested'
+    : 'Recommendation Submitted'
+
+const getHeRecommendation = recommendation => {
+  let heRecommendation = recommendation === 'reject' ? 'reject' : 'publish'
+  if (['minor', 'major'].includes(recommendation)) {
+    heRecommendation = 'revision'
+  }
+  return heRecommendation
+}
+
 module.exports = {
   getEditorInChief,
   setupReviewSubmittedEmailData,
-  setupAfterRecommendationEmailData,
+  setupAgreedReviewersEmailData,
   setupEiCRecommendationEmailData,
+  setupAuthorRequestToRevisionEmailData,
 }
diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js
index 454626197..7f52662bc 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js
@@ -53,44 +53,52 @@ module.exports = models => async (req, res) => {
   newRecommendation.recommendation = recommendation || undefined
   newRecommendation.comments = comments || undefined
 
-  if (
-    ['reject', 'publish'].includes(recommendation) &&
-    recommendationType === 'editorRecommendation'
-  ) {
+  if (recommendationType === 'editorRecommendation') {
+    await collectionHelper.updateStatusByRecommendation(
+      collection,
+      recommendation,
+    )
     const {
       title,
-      authorName,
+      submittingAuthor,
       id,
     } = await collectionHelper.getFragmentAndAuthorData({
       UserModel: models.User,
       FragmentModel: models.Fragment,
       collection,
     })
+    const authorName = `${submittingAuthor.firstName} ${
+      submittingAuthor.lastName
+    }`
     const baseUrl = helpers.getBaseUrl(req)
-    await userHelper.setupAfterRecommendationEmailData({
+    await userHelper.setupAgreedReviewersEmailData({
       baseUrl,
       UserModel: models.User,
       FragmentModel: models.Fragment,
       collection,
       mailService,
       fragment: { title, authorName },
+      recommendation,
     })
-    await collectionHelper.updateStatus(collection, 'pendingApproval')
     await userHelper.setupEiCRecommendationEmailData({
       baseUrl,
       UserModel: models.User,
       collection,
       mailService,
-      publish: recommendation === 'publish',
+      recommendation,
       fragment: { title, id, authorName },
       comments: newRecommendation.comments,
     })
+    await userHelper.setupAuthorRequestToRevisionEmailData({
+      baseUrl,
+      UserModel: models.User,
+      collection,
+      mailService,
+      recommendation,
+      fragment: { title, id, submittingAuthor },
+      comments: newRecommendation.comments,
+    })
   }
-  if (
-    ['minor', 'major'].includes(recommendation) &&
-    recommendationType === 'editorRecommendation'
-  )
-    await collectionHelper.updateStatus(collection, 'revisionRequested')
 
   fragment.recommendations.push(newRecommendation)
   await fragment.save()
-- 
GitLab