From af526181cfd5f63d7cbc6450d132c28ecca5d35f Mon Sep 17 00:00:00 2001
From: Sebastian Mihalache <sebastian.mihalache@gmail.con>
Date: Fri, 21 Sep 2018 14:58:23 +0300
Subject: [PATCH] feat(emails): send email on EiC reject

---
 .../src/services/Fragment.js                  |  4 +++-
 .../src/services/User.js                      | 24 +++++++++++++------
 .../src/services/email/Email.js               |  4 +---
 .../notifications/emailCopy.js                |  6 +++++
 .../notifications/helpers.js                  |  9 +++++--
 .../notifications/notifications.js            | 21 ++++++++++------
 .../src/routes/fragmentsUsers/get.js          |  4 +++-
 7 files changed, 51 insertions(+), 21 deletions(-)

diff --git a/packages/component-helper-service/src/services/Fragment.js b/packages/component-helper-service/src/services/Fragment.js
index a0e8494bb..bf7013772 100644
--- a/packages/component-helper-service/src/services/Fragment.js
+++ b/packages/component-helper-service/src/services/Fragment.js
@@ -76,7 +76,9 @@ class Fragment {
       )
 
       const userHelper = new User({ UserModel })
-      const activeAuthors = await userHelper.getActiveAuthors(authors)
+      const activeAuthors = await userHelper.getActiveAuthors({
+        fragmentAuthors: authors,
+      })
 
       return {
         activeAuthors,
diff --git a/packages/component-helper-service/src/services/User.js b/packages/component-helper-service/src/services/User.js
index e37a835dc..83c8809d2 100644
--- a/packages/component-helper-service/src/services/User.js
+++ b/packages/component-helper-service/src/services/User.js
@@ -62,15 +62,25 @@ class User {
     user.save()
   }
 
-  async getActiveAuthors(authors) {
-    const activeUsers = (await Promise.all(
-      authors.map(author => this.UserModel.find(author.id)),
+  async getActiveAuthors({ fragmentAuthors }) {
+    const userData = (await Promise.all(
+      fragmentAuthors.map(author => this.UserModel.find(author.id)),
     ))
-      .filter(user => user.isActive)
-      .filter(user => get(user, 'notifications.email.user'))
-      .map(user => user.id)
+      .filter(user => user.isActive && get(user, 'notifications.email.user'))
+      .map(user => ({ id: user.id, accessTokens: user.accessTokens }))
 
-    return authors.filter(author => activeUsers.includes(author.id))
+    return fragmentAuthors
+      .map(fAuthor => {
+        const matchingAuthor = userData.find(user => user.id === fAuthor.id)
+        if (matchingAuthor) {
+          return {
+            ...fAuthor,
+            accessTokens: matchingAuthor.accessTokens,
+          }
+        }
+        return false
+      })
+      .filter(Boolean)
   }
 
   async getEiCName() {
diff --git a/packages/component-helper-service/src/services/email/Email.js b/packages/component-helper-service/src/services/email/Email.js
index 0a7c2028b..5fdd6b42c 100644
--- a/packages/component-helper-service/src/services/email/Email.js
+++ b/packages/component-helper-service/src/services/email/Email.js
@@ -85,9 +85,7 @@ class Email {
     logger.info(
       `EMAIL: Sent email from ${from} to ${to} with subject '${subject}'`,
     )
-    logger.debug(
-      `EMAIL: Sent email from ${from} to ${to} with subject '${subject}'`,
-    )
+
     SendEmail.send(mailData)
   }
 }
diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/emailCopy.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/emailCopy.js
index c2f633897..fbb64a10e 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/emailCopy.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/emailCopy.js
@@ -109,6 +109,12 @@ const getEmailCopy = ({
       paragraph = `${titleText} has been accepted for publication by ${eicName}. <br/><br/>
         Please complete QA screening so that manuscript can be sent to production.`
       break
+    case 'authors-manuscript-rejected-before-review':
+      paragraph = `I regret to inform you that your manuscript has been rejected for publication in ${journalName} for the following reason:<br/><br/>
+        ${comments}<br/><br/>
+        Thank you for your submission, and please do consider submitting again in the future.`
+      hasLink = false
+      break
     default:
       throw new Error(`The ${emailType} email type is not defined.`)
   }
diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/helpers.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/helpers.js
index ffeb610de..3dc8b8d77 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/helpers.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/helpers.js
@@ -378,14 +378,19 @@ module.exports = {
 
     return email
   },
-  getEmailTypeByRecommendationForAuthors: ({ recommendation }) => {
+  getEmailTypeByRecommendationForAuthors: ({
+    recommendation,
+    hasPeerReview,
+  }) => {
     let emailType
     switch (recommendation) {
       case 'publish':
         emailType = 'author-manuscript-published'
         break
       case 'reject':
-        emailType = 'author-manuscript-rejected'
+        emailType = hasPeerReview
+          ? 'author-manuscript-rejected'
+          : 'authors-manuscript-rejected-before-review'
         break
       default:
         throw new Error(`Undefined recommendation: ${recommendation}`)
diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notifications.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notifications.js
index 9f6c43f5e..c930744e7 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notifications.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notifications.js
@@ -68,8 +68,7 @@ module.exports = {
       })
     }
 
-    const hasPeerReview = (collection = {}) =>
-      !isEmpty(collection.handlingEditor)
+    const hasPeerReview = !isEmpty(collection.handlingEditor)
     const { customId } = collection
     const collHelper = new Collection({ collection })
 
@@ -77,7 +76,7 @@ module.exports = {
     // or when the EiC makes a recommendation after peer review
     if (
       (isEditorInChief || recommendationType === 'review') &&
-      hasPeerReview(collection) &&
+      hasPeerReview &&
       (recommendation !== 'publish' || hasEQA)
     ) {
       const handlingEditor = get(collection, 'handlingEditor', {})
@@ -118,10 +117,18 @@ module.exports = {
       // send all authors email
       const emailType = helpers.getEmailTypeByRecommendationForAuthors({
         recommendation,
+        hasPeerReview,
       })
-      const comments = helpers.getHEComments({
-        heRecommendation: parsedFragment.heRecommendation,
-      })
+
+      let comments
+      if (hasPeerReview) {
+        comments = helpers.getHEComments({
+          heRecommendation: parsedFragment.heRecommendation,
+        })
+      } else {
+        comments = newRecommendation.comments[0].content
+      }
+
       const authors = helpers.getAllAuthors({
         comments,
         emailType,
@@ -156,7 +163,7 @@ module.exports = {
       helpers.sendSubmittingAuthorEmail({ email, author, baseUrl })
     }
 
-    if (!hasPeerReview(collection)) {
+    if (!hasPeerReview) {
       return
     }
 
diff --git a/packages/component-user-manager/src/routes/fragmentsUsers/get.js b/packages/component-user-manager/src/routes/fragmentsUsers/get.js
index 6798a5f55..1931adfbe 100644
--- a/packages/component-user-manager/src/routes/fragmentsUsers/get.js
+++ b/packages/component-user-manager/src/routes/fragmentsUsers/get.js
@@ -13,7 +13,9 @@ module.exports = models => async (req, res) => {
     const { authors = [] } = await models.Fragment.find(fragmentId)
 
     const userHelper = new User({ UserModel: models.User })
-    const activeAuthors = await userHelper.getActiveAuthors(authors)
+    const activeAuthors = await userHelper.getActiveAuthors({
+      fragmentAuthors: authors,
+    })
 
     return res.status(200).json(activeAuthors)
   } catch (e) {
-- 
GitLab