Skip to content
Snippets Groups Projects
notifications.js 1.23 KiB
Newer Older
const { Email, services } = require('pubsweet-component-helper-service')

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

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

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.accessTokens.passwordReset,
        }),
        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,
    token: user.accessTokens.unsubscribe,
  const { html, text } = email.getNotificationBody({
    emailBodyProps: getEmailCopy({
      emailType: 'user-forgot-password',
    }),
  })

  email.sendEmail({ html, text })
}