Skip to content
Snippets Groups Projects
notifications.js 1.16 KiB
Newer Older
const config = require('config')

const forgotPath = config.get('forgot-password.url')
const { name: journalName, staffEmail } = config.get('journal')

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

const { Email, services } = require('pubsweet-component-helper-service')

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

module.exports = {
  async sendNotifications({ user, baseUrl }) {
    const email = new Email({
      type: 'system',
      fromEmail: `${journalName} <${staffEmail}>`,
      content: {
        ctaLink: services.createUrl(baseUrl, forgotPath, {
          email: user.email,
          token: user.passwordResetToken,
        }),
        ctaText: 'RESET PASSWORD',
      },
    })

    sendForgotPasswordEmail({ email, baseUrl, user })
  },
}

const sendForgotPasswordEmail = ({ email, baseUrl, user }) => {
  email.toUser = {
    email: user.email,
  }

  email.content.subject = 'Forgot Password'
  email.content.unsubscribeLink = services.createUrl(baseUrl, unsubscribeSlug, {
    id: user.id,
  })

  const { html, text } = email.getBody({
    body: getEmailCopy({
      emailType: 'user-forgot-password',
    }),
  })

  email.sendEmail({ html, text })
}