Skip to content
Snippets Groups Projects
notification.js 9.29 KiB
Newer Older
const config = require('config')
const { get, isEmpty } = require('lodash')
const Email = require('@pubsweet/component-email-templating')

const unsubscribeSlug = config.get('unsubscribe.url')
const { getEmailCopy } = require('./emailCopy')

const {
  User,
  services,
  Fragment,
  Collection,
} = require('pubsweet-component-helper-service')

// const { getEmailCopy } = require('./emailCopy')
const helpers = require('./helpers')

// const editorialAssistantEmail = config.get('journal.staffEmail')
const { name: journalName, staffEmail } = config.get('journal')

class Notification {
  constructor({
    baseUrl = '',
    fragment = {},
    UserModel = {},
    collection = {},
    newRecommendation = {},
  }) {
    this.baseUrl = baseUrl
    this.fragment = fragment
    this.UserModel = UserModel
    this.collection = collection
    this.newRecommendation = newRecommendation
  }

  async notifyHEWhenReviewerSubmitsReport(reviewerLastName) {
    const { eicName, titleText } = await this.getNotificationProperties()

    const handlingEditorId = get(this.collection, 'handlingEditor.id')
    const heUser = await this.UserModel.find(handlingEditorId)
    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,
      emailType: 'he-review-submitted',
    })
  async notifyHEWhenEiCMakesDecision() {
    const { eicName, titleText } = this.props
    const handlingEditorId = get(this.collection, 'handlingEditor.id')
    const heUser = await this.UserModel.find(handlingEditorId)

    const email = new Email({
      type: 'user',
      toUser: {
      },
      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,
  async notifyEAWhenEiCMakesFinalDecision() {
    const { eicName, titleText } = await this.getNotificationProperties()
    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 })
  }

  async notifyEAWhenEiCRequestsEQAApproval() {
    const { eicName } = await this.getNotificationProperties()
    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 })
  }

  async notifyAuthorsWhenEiCMakesDecision() {
    const {
      eicName,
      titleText,
      activeAuthors,
      recommendation,
      parsedFragment,
    } = await this.getNotificationProperties()

    const subjectOpts = {
      publish: `${this.collection.customId}: Manuscript accepted`,
      reject: `${this.collection.customId}: Manuscript rejected`,
    }
    const subject = subjectOpts[recommendation]
    if (isEmpty(subject)) {
      throw new Error(`Undefined recommendation: ${recommendation}`)
    }
    const hasPeerReview = !isEmpty(this.collection.handlingEditor)
    const emailType = helpers.getEmailTypeByRecommendationForAuthors({
      recommendation,
      hasPeerReview,
    })
    const comments = hasPeerReview
      ? helpers.getHEComments({
          heRecommendation: parsedFragment.heRecommendation,
        })
      : this.newRecommendation.comments[0].content

    activeAuthors.forEach(async author => {
      const email = new Email({
        type: 'user',
        toUser: {
          email: author.email,
          name: author.lastName,
        },
        fromEmail: `${eicName} <${staffEmail}>`,
        content: {
          signatureName: eicName,
          ctaText: 'MANUSCRIPT DETAILS',
          subject,
          unsubscribeLink: services.createUrl(this.baseUrl, unsubscribeSlug, {
            id: author.id,
            token: author.accessTokens.unsubscribe,
          }),
          ctaLink: services.createUrl(
            this.baseUrl,
            `/projects/${this.collection.id}/versions/${
              this.fragment.id
            }/details`,
          ),
          signatureJournal: journalName,
        },
      })

      const { html, text } = email.getNotificationBody({
        emailBodyProps: getEmailCopy({
          titleText,
          emailType,
          comments,
        }),
      })
  async notifyReviewersWhenEiCMakesDecision() {
    const {
      eicName,
      titleText,
      recommendation,
      fragmentHelper,
    } = await this.getNotificationProperties()

    const emailType =
      recommendation === 'publish'
        ? 'submitted-reviewers-after-publish'
        : 'submitted-reviewers-after-reject'

    const subject =
      recommendation === 'publish'
        ? 'A manuscript you reviewed has been accepted'
        : 'A manuscript you reviewed has been rejected'

    const reviewers = await fragmentHelper.getReviewers({
      UserModel: this.UserModel,
      type: 'submitted',
    })

    const emailBodyProps = getEmailCopy({
      emailType,
      titleText,
    })

    reviewers.forEach(reviewer => {
      const email = new Email({
        type: 'user',
        toUser: {
          email: reviewer.email,
          name: reviewer.lastName,
        },
        fromEmail: `${eicName} <${staffEmail}>`,
        content: {
          signatureName: eicName,
          ctaText: 'MANUSCRIPT DETAILS',
          subject: `${this.collection.customId}: ${subject}`,
          unsubscribeLink: services.createUrl(this.baseUrl, unsubscribeSlug, {
            id: reviewer.id,
            token: reviewer.accessTokens.unsubscribe,
          }),
          ctaLink: services.createUrl(
            this.baseUrl,
            `/projects/${this.collection.id}/versions/${
              this.fragment.id
            }/details`,
          ),
          signatureJournal: journalName,
        },
      })

      const { html, text } = email.getNotificationBody({ emailBodyProps })
      email.sendEmail({ html, text })
    })
  }

  notifySAWhenHERequestsRevision() {}

  notifyReviewersWhenHEMakesRecommendation() {}

  notifyEiCWhenHEMakesRecommendation() {}

  async getNotificationProperties() {
    const fragmentHelper = new Fragment({ fragment: this.fragment })
    const parsedFragment = await fragmentHelper.getFragmentData({
      handlingEditor: this.collection.handlingEditor,
    })
    const {
      submittingAuthor,
      activeAuthors,
    } = await fragmentHelper.getAuthorData({
    const userHelper = new User({ UserModel: this.UserModel })
    const eicName = await userHelper.getEiCName()

    const titleText = `the manuscript titled "${parsedFragment.title}" by ${
      submittingAuthor.firstName
    } ${submittingAuthor.lastName}`

    const { recommendation, recommendationType } = this.newRecommendation

    return {
      recommendation,
      recommendationType,
      eicName,
      titleText,
      submittingAuthor,
      activeAuthors,
      parsedFragment,