Skip to content
Snippets Groups Projects
helpers.js 1.79 KiB
Newer Older
const config = require('config')
const { services } = require('pubsweet-component-helper-service')

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

const unsubscribeSlug = config.get('unsubscribe.url')
const { name: journalName, staffEmail } = config.get('journal')

module.exports = {
  sendInvitedHEEmail: ({
    email,
    eicName,
    baseUrl,
    customId,
    titleText,
    isCanceled,
    handlingEditor,
  }) => {
    email.toUser = {
      email: handlingEditor.email,
    }

    email.content.subject = isCanceled
      ? `${customId}: Editor invitation cancelled`
      : `${customId}: Invitation to edit a manuscript`

    email.fromEmail = `${journalName} <${staffEmail}>`
    email.content.unsubscribeLink = services.createUrl(
      baseUrl,
      unsubscribeSlug,
      {
        id: handlingEditor.id,
      },
    )

    const { html, text } = email.getBody({
      body: getEmailCopy({
        titleText,
        targetUserName: eicName,
        emailType: isCanceled ? 'he-revoked' : 'he-assigned',
      }),
    })

    email.sendEmail({ html, text })
  },
  sendEiCEmail: async ({
    eic,
    email,
    baseUrl,
    comments,
    titleText,
    isAccepted,
    targetUserName,
  }) => {
    email.content.subject = isAccepted
      ? `${customId}: Editor invitation accepted`
      : `${customId}: Editor invitation declined`

    const emailType = isAccepted ? 'he-accepted' : 'he-declined'

    email.toUser = {
      email: eic.email,
    }

    email.content.unsubscribeLink = services.createUrl(
      baseUrl,
      unsubscribeSlug,
      {
        id: eic.id,
      },
    )

    const { html, text } = email.getBody({
      body: getEmailCopy({
        comments,
        emailType,
        titleText,
        targetUserName,
      }),
    })

    email.sendEmail({ html, text })
  },
}