Skip to content
Snippets Groups Projects
reminders.js 1.21 KiB
Newer Older
const moment = require('moment')
const { cloneDeep } = require('lodash')
const Job = require('pubsweet-component-jobs')
const { getEmailCopy } = require('../emails/emailCopy')
const Email = require('@pubsweet/component-email-templating')

const scheduleReminderJob = async ({
  email,
  const { days, emailType } = reminder

  const executionDate = moment()
    .add(days, timeUnit)
    .toISOString()

  const queue = `reminders-${userId}-${invitationId}`

  const { paragraph, ...bodyProps } = getEmailCopy({
    titleText,
    expectedDate,
  })

  email.bodyProps = bodyProps
  email.content.subject = subject

  await Job.schedule({ queue, params, executionDate, jobHandler })
}

const jobHandler = async job => {
  const { days, timeUnit, executionDate, emailProps } = job.data
  const email = new Email(emailProps)

  await email.sendEmail()
  return `Job ${job.id}: the ${days} ${timeUnit} reminder has been sent to ${
    email.toUser.email
  } at ${executionDate}`
module.exports = { scheduleReminderJob }