Skip to content
Snippets Groups Projects
notification.js 2.92 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 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 } = await this.properties
    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,
      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 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 }