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

const unsubscribeSlug = config.get('unsubscribe.url')

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

// const { getEmailCopy } = require('./emailCopy')
const helpers = require('./helpers')
const reviewHelpers = require('./reviewerSubmitsReport/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
    this.properties = this._getNotificationProperties()
  }

  async heMakesRecommendation() {
    const notificationProperties = this.getNotificationProperties()
  }

  async eicMakesDecision() {
    const notificationProperties = this.getNotificationProperties()
  }

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

    const { collection } = this
    const handlingEditor = get(collection, 'handlingEditor', {})

    const { customId } = collection
    const collHelper = new Collection({ collection })

    const email = new Email({
      type: 'user',
      toUser: {
        email: handlingEditor.email,
        name: collHelper.getHELastName(),
      },
      fromEmail: `${eicName} <${staffEmail}>`,
      content: {
        signatureName: eicName,
        ctaText: 'MANUSCRIPT DETAILS',
        subject: `${customId}: A review has been submitted`,
        unsubscribeLink: services.createUrl(this.baseUrl, unsubscribeSlug, {
          id: handlingEditor.id,
          token: handlingEditor.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 _getNotificationProperties() {
    const fragmentHelper = new Fragment({ fragment: this.fragment })
    const parsedFragment = await fragmentHelper.getFragmentData({
      handlingEditor: this.collection.handlingEditor,
    })
    const { submittingAuthor } = await fragmentHelper.getAuthorData({
      UserModel: this.UserModel,
    })

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

    const { recommendation, recommendationType } = this.newRecommendation

    return { recommendation, recommendationType, titleText }
  }
}

module.exports = Notification