From 99a49623c34753a3ba5238fdbd939148fcfa11d9 Mon Sep 17 00:00:00 2001
From: Sebastian Mihalache <sebi.mihalache@gmail.com>
Date: Fri, 19 Oct 2018 11:56:25 +0300
Subject: [PATCH] feat(recommendations): build eqa emails

---
 .../notifications/emailCopy.js                |   4 +-
 .../notifications/notification.js             | 120 ++++++++++++++++--
 .../notifications/notifications.js            |   2 +-
 .../routes/fragmentsRecommendations/post.js   |  42 ++++--
 4 files changed, 146 insertions(+), 22 deletions(-)

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 8fadac625..ffaf1f511 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/emailCopy.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/emailCopy.js
@@ -1,4 +1,5 @@
 const config = require('config')
+const { upperFirst } = require('lodash')
 
 const staffEmail = config.get('journal.staffEmail')
 const journalName = config.get('journal.name')
@@ -106,7 +107,8 @@ const getEmailCopy = ({
       hasLink = false
       hasIntro = false
       hasSignature = false
-      paragraph = `${titleText} has been accepted for publication by ${eicName}. <br/><br/>
+      paragraph = `${upperFirst(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':
diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notification.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notification.js
index 77dc6a820..1260ae7dc 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notification.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notification.js
@@ -3,6 +3,7 @@ const { get } = require('lodash')
 const Email = require('@pubsweet/component-email-templating')
 
 const unsubscribeSlug = config.get('unsubscribe.url')
+const { getEmailCopy } = require('./emailCopy')
 
 const {
   User,
@@ -30,19 +31,48 @@ class Notification {
     this.UserModel = UserModel
     this.collection = collection
     this.newRecommendation = newRecommendation
-    this.properties = this._getNotificationProperties()
   }
 
-  async heMakesRecommendation() {
-    const notificationProperties = this.getNotificationProperties()
-  }
+  async notifyHEWhenReviewerSubmitsReport(reviewerLastName) {
+    const { eicName, titleText } = await this.getNotificationProperties()
+
+    const handlingEditorId = get(this.collection, 'handlingEditor.id')
+    const heUser = await this.UserModel.find(handlingEditorId)
 
-  async eicMakesDecision() {
-    const notificationProperties = this.getNotificationProperties()
+    const email = new Email({
+      type: 'user',
+      toUser: {
+        email: heUser.email,
+        name: heUser.lastName,
+      },
+      fromEmail: `${eicName} <${staffEmail}>`,
+      content: {
+        signatureName: eicName,
+        ctaText: 'MANUSCRIPT DETAILS',
+        subject: `${this.collection.customId}: A review has been submitted`,
+        unsubscribeLink: services.createUrl(this.baseUrl, unsubscribeSlug, {
+          id: heUser.id,
+          token: heUser.accessTokens.unsubscribe,
+        }),
+        ctaLink: services.createUrl(
+          this.baseUrl,
+          `/projects/${this.collection.id}/versions/${
+            this.fragment.id
+          }/details`,
+        ),
+      },
+    })
+
+    helpers.sendHandlingEditorEmail({
+      email,
+      titleText,
+      reviewerLastName,
+      emailType: 'he-review-submitted',
+    })
   }
 
-  async notifyHEWhenReviewerSubmitsReport(reviewerLastName) {
-    const { eicName, titleText } = await this.properties
+  async notifyHEWhenEiCMakesDecision() {
+    const { eicName, titleText } = this.props
 
     const handlingEditorId = get(this.collection, 'handlingEditor.id')
     const heUser = await this.UserModel.find(handlingEditorId)
@@ -79,7 +109,79 @@ class Notification {
     })
   }
 
-  async _getNotificationProperties() {
+  notifyEAWhenEiCMakesFinalDecision() {
+    const { eicName, titleText } = this.props
+    const subjectBaseText = `${this.collection.customId}: Manuscript`
+
+    const email = new Email({
+      type: 'system',
+      toUser: {
+        email: staffEmail,
+      },
+      fromEmail: `${journalName} <${staffEmail}>`,
+      content: {
+        subject: `${subjectBaseText} decision finalized`,
+        unsubscribeLink: this.baseUrl,
+      },
+    })
+
+    const { html, text } = email.getNotificationBody({
+      emailBodyProps: getEmailCopy({
+        eicName,
+        titleText,
+        emailType: 'eqa-manuscript-published',
+      }),
+    })
+    email.sendEmail({ html, text })
+  }
+
+  notifyEAWhenEiCRequestsEQAApproval() {
+    const { eicName } = this.props
+    const subjectBaseText = `${this.collection.customId}: Manuscript`
+
+    const email = new Email({
+      type: 'system',
+      toUser: {
+        email: staffEmail,
+      },
+      fromEmail: `${journalName} <${staffEmail}>`,
+      content: {
+        subject: `${subjectBaseText} Request for EQA approval`,
+        unsubscribeLink: this.baseUrl,
+        ctaText: 'MAKE DECISION',
+        ctaLink: services.createUrl(
+          this.baseUrl,
+          config.get('eqa-decision.url'),
+          {
+            collectionId: this.collection.id,
+            customId: this.collection.customId,
+            token: this.collection.technicalChecks.token,
+          },
+        ),
+      },
+    })
+
+    const { html, text } = email.getNotificationBody({
+      emailBodyProps: getEmailCopy({
+        eicName,
+        customId: this.collection.customId,
+        emailType: 'eqa-manuscript-request-for-approval',
+      }),
+    })
+    email.sendEmail({ html, text })
+  }
+
+  notifyAuthorsWhenEiCMakesDecision() {}
+
+  notifyReviewersWhenEiCMakesDecision() {}
+
+  notifySAWhenHERequestsRevision() {}
+
+  notifyReviewersWhenHEMakesRecommendation() {}
+
+  notifyEiCWhenHEMakesRecommendation() {}
+
+  async getNotificationProperties() {
     const fragmentHelper = new Fragment({ fragment: this.fragment })
     const parsedFragment = await fragmentHelper.getFragmentData({
       handlingEditor: this.collection.handlingEditor,
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 f177dfa48..10034e07b 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notifications.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notifications.js
@@ -266,7 +266,7 @@ 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 (recommendation === 'publish' && collection.technicalChecks.token) {
-      sendEQAEmail({
+      helpers.sendEQAEmail({
         email,
         eicName,
         baseUrl,
diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js
index 9c27a871f..f9df5c8b9 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js
@@ -30,7 +30,8 @@ const sendMTSPackage = async (collection, fragment) => {
   await MTS.sendPackage({ fragment: packageFragment })
 }
 
-const notifications = require('./notifications/notifications')
+// const notifications = require('./notifications/notifications')
+const Notification = require('./notifications/notification')
 
 module.exports = models => async (req, res) => {
   const { recommendation, comments, recommendationType } = req.body
@@ -135,23 +136,42 @@ module.exports = models => async (req, res) => {
     }
 
     if (isEditorInChief) {
-      notifications.sendNotificationsWhenEiCMakesDecision({
-        hasEQA,
+      // notifications.sendNotificationsWhenEiCMakesDecision({
+      //   hasEQA,
+      //   fragment,
+      //   collection,
+      //   newRecommendation,
+      //   UserModel: models.User,
+      //   targetUserName: reqUser.lastName,
+      //   baseUrl: services.getBaseUrl(req),
+      // })
+      const notification = new Notification({
         fragment,
         collection,
         newRecommendation,
         UserModel: models.User,
-        targetUserName: reqUser.lastName,
         baseUrl: services.getBaseUrl(req),
       })
+
+      if (
+        recommendation === 'publish' &&
+        collection.technicalChecks.token &&
+        ['accepted', 'inQA'].includes(this.collection.status)
+      ) {
+        if (this.collection.status === 'inQA') {
+          notification.notifyEAWhenEiCRequestsEQAApproval()
+        } else {
+          notification.notifyEAWhenEiCMakesFinalDecision()
+        }
+      }
     } else {
-      notifications.sendNotificationsWhenHEMakesRecommendation({
-        fragment,
-        collection,
-        newRecommendation,
-        UserModel: models.User,
-        baseUrl: services.getBaseUrl(req),
-      })
+      // notifications.sendNotificationsWhenHEMakesRecommendation({
+      //   fragment,
+      //   collection,
+      //   newRecommendation,
+      //   UserModel: models.User,
+      //   baseUrl: services.getBaseUrl(req),
+      // })
     }
   }
 
-- 
GitLab