29 lines
851 B
TypeScript
29 lines
851 B
TypeScript
|
|
import { Worker } from "bullmq";
|
||
|
|
import { redisConnectionOpts } from "../connection.js";
|
||
|
|
import { sendVerification, sendPasswordReset } from "../../lib/email.js";
|
||
|
|
|
||
|
|
export function startMailWorker(): Worker {
|
||
|
|
const worker = new Worker(
|
||
|
|
"mail",
|
||
|
|
async (job) => {
|
||
|
|
switch (job.name) {
|
||
|
|
case "sendVerification":
|
||
|
|
await sendVerification(job.data.email as string, job.data.token as string);
|
||
|
|
break;
|
||
|
|
case "sendPasswordReset":
|
||
|
|
await sendPasswordReset(job.data.email as string, job.data.token as string);
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
throw new Error(`Unknown mail job: ${job.name}`);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
{ connection: redisConnectionOpts },
|
||
|
|
);
|
||
|
|
|
||
|
|
worker.on("failed", (job, err) => {
|
||
|
|
console.error(`Mail job ${job?.id} (${job?.name}) failed:`, err.message);
|
||
|
|
});
|
||
|
|
|
||
|
|
return worker;
|
||
|
|
}
|