Newer
Older
const moment = require('moment')
const { cloneDeep } = require('lodash')
const logger = require('@pubsweet/logger')
const { jobs: { connectToJobQueue } } = require('pubsweet-server/src')
const { getEmailCopy } = require('../emails/emailCopy')
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const Email = require('@pubsweet/component-email-templating')
const scheduleReminderJob = async ({
days,
email,
subject,
timeUnit,
titleText,
expectedDate,
}) => {
const executionDate = moment()
.add(days, timeUnit)
.toISOString()
const queue = `reviewer-reminders-${days}-${executionDate}`
const jobQueue = await connectToJobQueue()
const { paragraph, ...bodyProps } = getEmailCopy({
emailType: 'reviewer-resend-invitation',
titleText,
expectedDate,
})
email.bodyProps = bodyProps
email.content.subject = subject
// Add job to the queue
const jobId = await jobQueue.publishAfter(
queue,
{
days,
timeUnit,
executionDate,
emailProps: cloneDeep(email),
},
{},
executionDate,
)
console.log(`published job ${jobId} for the ${days} ${timeUnit} reminder`)
// Subscribe to the job queue with an async handler
await jobQueue.subscribe(queue, jobHandler)
await jobQueue.onComplete(queue, job => {
console.log(job.data.response.value)
logger.info(job.data.response.value)
})
}
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 }