Enqueue confirmation email
This commit is contained in:
26
packages/api/src/jobs/send_email.ts
Normal file
26
packages/api/src/jobs/send_email.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { env } from '../env'
|
||||
import { sendWithMailJet } from '../services/send_emails'
|
||||
import { sendEmail } from '../utils/sendEmail'
|
||||
|
||||
export const SEND_CONFIRMATION_EMAIL_JOB = 'send-confirmation-email'
|
||||
|
||||
export interface SendConfirmationEmailData {
|
||||
emailAddress: string
|
||||
link: string
|
||||
templateData: Record<string, any>
|
||||
}
|
||||
|
||||
export const sendConfirmationEmail = async (
|
||||
data: SendConfirmationEmailData
|
||||
) => {
|
||||
if (process.env.USE_MAILJET) {
|
||||
return sendWithMailJet(data.emailAddress, data.link)
|
||||
}
|
||||
|
||||
return sendEmail({
|
||||
from: env.sender.message,
|
||||
to: data.emailAddress,
|
||||
templateId: env.sendgrid.confirmationTemplateId,
|
||||
dynamicTemplateData: data.templateData,
|
||||
})
|
||||
}
|
||||
@ -36,6 +36,10 @@ import {
|
||||
import { refreshAllFeeds } from './jobs/rss/refreshAllFeeds'
|
||||
import { refreshFeed } from './jobs/rss/refreshFeed'
|
||||
import { savePageJob } from './jobs/save_page'
|
||||
import {
|
||||
sendConfirmationEmail,
|
||||
SEND_CONFIRMATION_EMAIL_JOB,
|
||||
} from './jobs/send_email'
|
||||
import {
|
||||
syncReadPositionsJob,
|
||||
SYNC_READ_POSITIONS_JOB_NAME,
|
||||
@ -157,6 +161,8 @@ export const createWorker = (connection: ConnectionOptions) =>
|
||||
return processYouTubeTranscript(job.data)
|
||||
case EXPORT_ALL_ITEMS_JOB_NAME:
|
||||
return exportAllItems(job.data)
|
||||
case SEND_CONFIRMATION_EMAIL_JOB:
|
||||
return sendConfirmationEmail(job.data)
|
||||
default:
|
||||
logger.warning(`[queue-processor] unhandled job: ${job.name}`)
|
||||
}
|
||||
|
||||
@ -142,9 +142,7 @@ export const createUser = async (input: {
|
||||
})
|
||||
|
||||
if (input.pendingConfirmation) {
|
||||
if (!(await sendConfirmationEmail(user))) {
|
||||
return Promise.reject({ errorCode: SignupErrorCode.InvalidEmail })
|
||||
}
|
||||
await sendConfirmationEmail(user)
|
||||
}
|
||||
|
||||
return [user, profile]
|
||||
|
||||
@ -1,14 +1,15 @@
|
||||
import mailjet from 'node-mailjet'
|
||||
import { env } from '../env'
|
||||
import { generateVerificationToken } from '../utils/auth'
|
||||
import { enqueueSendConfirmationEmail } from '../utils/createTask'
|
||||
import { logger } from '../utils/logger'
|
||||
import { sendEmail } from '../utils/sendEmail'
|
||||
import mailjet from 'node-mailjet'
|
||||
|
||||
export const sendConfirmationEmail = async (user: {
|
||||
id: string
|
||||
name: string
|
||||
email: string
|
||||
}): Promise<boolean> => {
|
||||
}) => {
|
||||
// generate confirmation link
|
||||
const token = generateVerificationToken({ id: user.id })
|
||||
const link = `${env.client.url}/auth/confirm-email/${token}`
|
||||
@ -18,19 +19,14 @@ export const sendConfirmationEmail = async (user: {
|
||||
link,
|
||||
}
|
||||
|
||||
if (process.env.USE_MAILJET) {
|
||||
return sendWithMailJet(user.email, link)
|
||||
}
|
||||
|
||||
return sendEmail({
|
||||
from: env.sender.message,
|
||||
to: user.email,
|
||||
templateId: env.sendgrid.confirmationTemplateId,
|
||||
dynamicTemplateData,
|
||||
await enqueueSendConfirmationEmail({
|
||||
emailAddress: user.email,
|
||||
link,
|
||||
templateData: dynamicTemplateData,
|
||||
})
|
||||
}
|
||||
|
||||
const sendWithMailJet = async (
|
||||
export const sendWithMailJet = async (
|
||||
email: string,
|
||||
link: string
|
||||
): Promise<boolean> => {
|
||||
|
||||
@ -35,6 +35,10 @@ import {
|
||||
REFRESH_ALL_FEEDS_JOB_NAME,
|
||||
REFRESH_FEED_JOB_NAME,
|
||||
} from '../jobs/rss/refreshAllFeeds'
|
||||
import {
|
||||
SendConfirmationEmailData,
|
||||
SEND_CONFIRMATION_EMAIL_JOB,
|
||||
} from '../jobs/send_email'
|
||||
import { SYNC_READ_POSITIONS_JOB_NAME } from '../jobs/sync_read_positions'
|
||||
import { TriggerRuleJobData, TRIGGER_RULE_JOB_NAME } from '../jobs/trigger_rule'
|
||||
import {
|
||||
@ -69,6 +73,7 @@ export const getJobPriority = (jobName: string): number => {
|
||||
case UPDATE_LABELS_JOB:
|
||||
case UPDATE_HIGHLIGHT_JOB:
|
||||
case SYNC_READ_POSITIONS_JOB_NAME:
|
||||
case SEND_CONFIRMATION_EMAIL_JOB:
|
||||
return 1
|
||||
case TRIGGER_RULE_JOB_NAME:
|
||||
case CALL_WEBHOOK_JOB_NAME:
|
||||
@ -839,4 +844,21 @@ export const enqueueExportItem = async (jobData: ExportItemJobData) => {
|
||||
})
|
||||
}
|
||||
|
||||
export const enqueueSendConfirmationEmail = async (
|
||||
jobData: SendConfirmationEmailData
|
||||
) => {
|
||||
const queue = await getBackendQueue()
|
||||
if (!queue) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return queue.add(SEND_CONFIRMATION_EMAIL_JOB, jobData, {
|
||||
attempts: 1, // only try once
|
||||
priority: getJobPriority(SEND_CONFIRMATION_EMAIL_JOB),
|
||||
jobId: `${SEND_CONFIRMATION_EMAIL_JOB}_${jobData.emailAddress}_${JOB_VERSION}`, // deduplication
|
||||
removeOnComplete: true,
|
||||
removeOnFail: true,
|
||||
})
|
||||
}
|
||||
|
||||
export default createHttpTaskWithToken
|
||||
|
||||
Reference in New Issue
Block a user