From ba921a4021060d6f870ef1c0fa98cfdb61c26505 Mon Sep 17 00:00:00 2001
From: Sebastian Mihalache <sebi.mihalache@gmail.com>
Date: Tue, 16 Oct 2018 13:41:18 +0300
Subject: [PATCH] feat(manuscript-manager): send proper emails on EQA accept

---
 .../notifications/notifications.js            |   7 +-
 .../notifications/notifications.js            | 162 +-----------------
 .../src/routes/technicalChecks/patch.js       |  46 +++--
 3 files changed, 49 insertions(+), 166 deletions(-)

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 eee5ba2fc..9bb9c3a6c 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notifications.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notifications.js
@@ -60,7 +60,11 @@ module.exports = {
 
     // the EiC recommends to publish so an email to the EQA needs to be sent,
     // one requesting approval or one informing them that the manuscript has been published
-    if (isEditorInChief && recommendation === 'publish') {
+    if (
+      isEditorInChief &&
+      recommendation === 'publish' &&
+      collection.technicalChecks.token
+    ) {
       sendEQAEmail({
         email,
         eicName,
@@ -77,7 +81,6 @@ module.exports = {
 
     // send HE emails when a review is submitted
     // or when the EiC makes a recommendation after peer review
-
     if (
       recommendationType === 'review' ||
       (isEditorInChief &&
diff --git a/packages/component-manuscript-manager/src/routes/technicalChecks/notifications/notifications.js b/packages/component-manuscript-manager/src/routes/technicalChecks/notifications/notifications.js
index 631f67193..8380da030 100644
--- a/packages/component-manuscript-manager/src/routes/technicalChecks/notifications/notifications.js
+++ b/packages/component-manuscript-manager/src/routes/technicalChecks/notifications/notifications.js
@@ -1,5 +1,4 @@
 const config = require('config')
-const { get } = require('lodash')
 const Email = require('@pubsweet/component-email-templating')
 
 const {
@@ -9,12 +8,10 @@ const {
 } = require('pubsweet-component-helper-service')
 const { getEmailCopy } = require('./emailCopy')
 
-const unsubscribeSlug = config.get('unsubscribe.url')
 const { name: journalName, staffEmail } = config.get('journal')
 
 module.exports = {
   async sendNotifications({
-    isEQA,
     agree,
     baseUrl,
     collection,
@@ -28,10 +25,7 @@ module.exports = {
       handlingEditor: collection.handlingEditor,
     })
 
-    const {
-      activeAuthors: authors,
-      submittingAuthor,
-    } = await fragmentHelper.getAuthorData({
+    const { submittingAuthor } = await fragmentHelper.getAuthorData({
       UserModel,
     })
 
@@ -60,42 +54,14 @@ module.exports = {
       },
     })
 
-    if (agree && isEQA) {
-      email.content.signatureName = await userHelper.getEiCName()
-
-      sendHandlingEditorEmail({
-        email,
-        baseUrl,
-        titleText,
-        subjectBaseText,
-        handlingEditor: get(collection, 'handlingEditor', {}),
-      })
-      sendSubmittedReviewersEmail({
-        email,
-        baseUrl,
-        titleText,
-        UserModel,
-        fragmentHelper,
-        subjectBaseText,
-      })
-
-      sendAuthorsEmail({
-        email,
-        baseUrl,
-        titleText,
-        subjectBaseText,
-        fragmentAuthors: authors,
-      })
-    } else {
-      sendEditorsEmail({
-        email,
-        agree,
-        customId,
-        comments,
-        titleText,
-        userHelper,
-      })
-    }
+    sendEditorsEmail({
+      email,
+      agree,
+      customId,
+      comments,
+      titleText,
+      userHelper,
+    })
   },
 }
 
@@ -137,113 +103,3 @@ const sendEditorsEmail = async ({
     email.sendEmail({ html, text })
   })
 }
-
-const sendHandlingEditorEmail = ({
-  email,
-  baseUrl,
-  titleText,
-  handlingEditor,
-  subjectBaseText,
-}) => {
-  email.content.subject = `${subjectBaseText} Decision`
-  const emailType = 'he-manuscript-published'
-
-  email.toUser = {
-    email: handlingEditor.email,
-    name: handlingEditor.name,
-  }
-  email.content.unsubscribeLink = services.createUrl(baseUrl, unsubscribeSlug, {
-    id: handlingEditor.id,
-  })
-
-  const { html, text } = email.getNotificationBody({
-    emailBodyProps: getEmailCopy({
-      emailType,
-      titleText,
-    }),
-  })
-  email.sendEmail({ html, text })
-}
-
-const sendSubmittedReviewersEmail = async ({
-  email,
-  baseUrl,
-  titleText,
-  UserModel,
-  fragmentHelper,
-  subjectBaseText,
-}) => {
-  email.content.subject = `${subjectBaseText} Decision`
-
-  const reviewers = (await fragmentHelper.getReviewers({
-    UserModel,
-    type: 'submitted',
-  })).map(rev => ({
-    ...rev,
-    ...getEmailCopy({
-      emailType: 'submitted-reviewers-after-publish',
-      titleText,
-    }),
-  }))
-
-  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.getNotificationBody({
-      emailBodyProps: {
-        paragraph: reviewer.paragraph,
-        hasLink: reviewer.hasLink,
-      },
-    })
-    email.sendEmail({ html, text })
-  })
-}
-
-const sendAuthorsEmail = ({
-  email,
-  baseUrl,
-  titleText,
-  subjectBaseText,
-  fragmentAuthors,
-}) => {
-  const emailType = 'author-manuscript-published'
-  email.content.subject = `${subjectBaseText} Published`
-
-  const authors = fragmentAuthors.map(author => ({
-    ...author,
-    ...getEmailCopy({
-      emailType,
-      titleText,
-    }),
-  }))
-
-  authors.forEach(author => {
-    email.toUser = {
-      email: author.email,
-      name: `${author.firstName} ${author.lastName}`,
-    }
-    email.content.unsubscribeLink = services.createUrl(
-      baseUrl,
-      unsubscribeSlug,
-      {
-        id: author.id,
-      },
-    )
-    const { html, text } = email.getNotificationBody({
-      emailBodyProps: {
-        paragraph: author.paragraph,
-        hasLink: author.hasLink,
-      },
-    })
-    email.sendEmail({ html, text })
-  })
-}
diff --git a/packages/component-manuscript-manager/src/routes/technicalChecks/patch.js b/packages/component-manuscript-manager/src/routes/technicalChecks/patch.js
index 4afb91c9d..8d82bb688 100644
--- a/packages/component-manuscript-manager/src/routes/technicalChecks/patch.js
+++ b/packages/component-manuscript-manager/src/routes/technicalChecks/patch.js
@@ -1,7 +1,13 @@
-const { get, find, isEmpty } = require('lodash')
+const { get, find, isEmpty, has, last } = require('lodash')
 const { services } = require('pubsweet-component-helper-service')
 
-const { sendNotifications } = require('./notifications/notifications')
+const {
+  sendNotifications: sendEQSNotifications,
+} = require('./notifications/notifications')
+
+const {
+  sendNotifications: sendEQANotifications,
+} = require('../fragmentsRecommendations/notifications/notifications')
 
 const TECHNICAL_STEPS = {
   EQS: 'eqs',
@@ -64,15 +70,33 @@ module.exports = ({ Collection, Fragment, User }) => async (req, res) => {
     collection.status = setNewStatus(step, agree)
     await collection.save()
 
-    sendNotifications({
-      User,
-      agree,
-      comments,
-      Fragment,
-      collection,
-      baseUrl: services.getBaseUrl(req),
-      isEQA: step === TECHNICAL_STEPS.EQA,
-    })
+    const isEQA = get(collection, 'technicalChecks.eqa', false)
+    const baseUrl = services.getBaseUrl(req)
+    if (isEQA) {
+      const fragment = await Fragment.find(last(collection.fragments))
+
+      sendEQANotifications({
+        baseUrl,
+        fragment,
+        collection,
+        hasEQA: true,
+        UserModel: User,
+        isEditorInChief: true,
+        newRecommendation: {
+          recommendation: 'publish',
+          recommendationType: 'editorRecommendation',
+        },
+      })
+    } else {
+      sendEQSNotifications({
+        User,
+        agree,
+        comments,
+        Fragment,
+        collection,
+        baseUrl,
+      })
+    }
 
     return res.status(200).json(collection)
   } catch (e) {
-- 
GitLab