Skip to content
Snippets Groups Projects
reminders.js 1.23 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 ({
  expectedDate,
}) => {
  const executionDate = moment()
    .add(day, timeUnit)
  const queue = `reminders-${userId}-${invitationId}`

  const { paragraph, ...bodyProps } = getEmailCopy({
    emailType: `reviewer-resend-invitation-${order}-reminder`,
    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 }