Skip to content
Snippets Groups Projects
helpers.js 3.38 KiB
Newer Older
const querystring = require('querystring')
const fs = require('fs')
const handlebars = require('handlebars')

const createUrl = (baseUrl, slug, queryParams = null) =>
  !queryParams
    ? `${baseUrl}${slug}`
    : `${baseUrl}${slug}?${querystring.encode(queryParams)}`

const getEmailBody = (emailType, replacements) => {
  handlePartial('header', replacements)
  handlePartial('footer', replacements)
  handlePartial('mainButton', replacements)
  handlePartial('mainBody', replacements)

  return getMainTemplate(emailType, replacements)
}

const getNotificationBody = (emailType, replacements) => {
  handlePartial('notificationHeader', replacements)
  handlePartial('footer', replacements)
  handlePartial('signature', replacements)
  if (replacements.hasLink) handlePartial('manuscriptDetailsLink', replacements)
  handlePartial('notificationBody', replacements)

  return getMainTemplate(emailType, replacements)
}

const getInvitationBody = (emailType, replacements) => {
  handlePartial('invitationHeader', replacements)
  handlePartial('footer', replacements)
  handlePartial('invitationUpperContent', replacements)
  handlePartial('invitationButtons', replacements)
  handlePartial('manuscriptData', replacements)
  handlePartial('signature', replacements)
  handlePartial('invitationLowerContent', replacements)

  return getMainTemplate(emailType, replacements)
}

const getBody = (emailType, replacements) => {
  const simplePartials = ['header', 'footer', 'mainButton', 'mainBody']
  const notificationPartials = [
    'notificationHeader',
    'footer',
    'signature',
    'manuscriptDetailsLink',
    'notificationBody',
  ]
  const invitationPartials = [
    'invitationHeader',
    'footer',
    'invitationUpperContent',
    'invitationButtons',
    'manuscriptData',
    'signature',
    'invitationLowerContent',
  ]

  switch (emailType) {
    case 'simpleCTA':
    case 'noCTA':
      simplePartials.forEach(partial => handlePartial(partial, replacements))
      break
    case 'invitation':
      invitationPartials.forEach(partial =>
        handlePartial(partial, replacements),
      )
      break
    case 'notification':
      notificationPartials.forEach(partial =>
        handlePartial(partial, replacements),
      )
      break
    default:
      break
  }
  return getMainTemplate(emailType, replacements)
}

const readFile = path =>
  fs.readFileSync(path, { encoding: 'utf-8' }, (err, file) => {
    if (err) {
      throw err
    } else {
      return file
    }
  })

const handlePartial = (partialName = 'signature', context = {}) => {
  let partial = readFile(`${__dirname}/templates/partials/${partialName}.hbs`)
  const template = handlebars.compile(partial)
  partial = template(context)
  handlebars.registerPartial(partialName, partial)
}

const getMainTemplate = (fileName, context) => {
  const htmlFile = readFile(`${__dirname}/templates/${fileName}.html`)
  const htmlTemplate = handlebars.compile(htmlFile)
  const htmlBody = htmlTemplate(context)
  return htmlBody
}

const getExpectedDate = (timestamp, daysExpected) => {
  const date = new Date(timestamp)
  let expectedDate = date.getDate() + daysExpected
  date.setDate(expectedDate)
  expectedDate = date.toLocaleDateString('en-US', {
    day: 'numeric',
    month: 'long',
    year: 'numeric',
  })
  return expectedDate
}

module.exports = {
  getBody,
  createUrl,
  getEmailBody,
  getExpectedDate,
  getNotificationBody,
  getInvitationBody,
}