chore: Refactoring event processors and service architecture (#2495)
This commit is contained in:
163
server/mailer.js
163
server/mailer.js
@@ -23,15 +23,15 @@ import {
|
||||
import { SigninEmail, signinEmailText } from "./emails/SigninEmail";
|
||||
import { WelcomeEmail, welcomeEmailText } from "./emails/WelcomeEmail";
|
||||
import { baseStyles } from "./emails/components/EmailLayout";
|
||||
import { createQueue } from "./utils/queue";
|
||||
import { emailsQueue } from "./queues";
|
||||
|
||||
const log = debug("emails");
|
||||
const useTestEmailService =
|
||||
process.env.NODE_ENV === "development" && !process.env.SMTP_USERNAME;
|
||||
|
||||
type Emails = "welcome" | "export";
|
||||
export type EmailTypes = "welcome" | "export" | "invite" | "signin";
|
||||
|
||||
type SendMailType = {
|
||||
export type EmailSendOptions = {
|
||||
to: string,
|
||||
properties?: any,
|
||||
title: string,
|
||||
@@ -42,13 +42,6 @@ type SendMailType = {
|
||||
attachments?: Object[],
|
||||
};
|
||||
|
||||
type EmailJob = {
|
||||
data: {
|
||||
type: Emails,
|
||||
opts: SendMailType,
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Mailer
|
||||
*
|
||||
@@ -63,7 +56,61 @@ type EmailJob = {
|
||||
export class Mailer {
|
||||
transporter: ?any;
|
||||
|
||||
sendMail = async (data: SendMailType): ?Promise<*> => {
|
||||
constructor() {
|
||||
this.loadTransport();
|
||||
}
|
||||
|
||||
async loadTransport() {
|
||||
if (process.env.SMTP_HOST) {
|
||||
let smtpConfig = {
|
||||
host: process.env.SMTP_HOST,
|
||||
port: process.env.SMTP_PORT,
|
||||
secure:
|
||||
"SMTP_SECURE" in process.env
|
||||
? process.env.SMTP_SECURE === "true"
|
||||
: process.env.NODE_ENV === "production",
|
||||
auth: undefined,
|
||||
tls:
|
||||
"SMTP_TLS_CIPHERS" in process.env
|
||||
? { ciphers: process.env.SMTP_TLS_CIPHERS }
|
||||
: undefined,
|
||||
};
|
||||
|
||||
if (process.env.SMTP_USERNAME) {
|
||||
smtpConfig.auth = {
|
||||
user: process.env.SMTP_USERNAME,
|
||||
pass: process.env.SMTP_PASSWORD,
|
||||
};
|
||||
}
|
||||
|
||||
this.transporter = nodemailer.createTransport(smtpConfig);
|
||||
return;
|
||||
}
|
||||
|
||||
if (useTestEmailService) {
|
||||
log("SMTP_USERNAME not provided, generating test account…");
|
||||
|
||||
try {
|
||||
let testAccount = await nodemailer.createTestAccount();
|
||||
|
||||
const smtpConfig = {
|
||||
host: "smtp.ethereal.email",
|
||||
port: 587,
|
||||
secure: false,
|
||||
auth: {
|
||||
user: testAccount.user,
|
||||
pass: testAccount.pass,
|
||||
},
|
||||
};
|
||||
|
||||
this.transporter = nodemailer.createTransport(smtpConfig);
|
||||
} catch (err) {
|
||||
log(`Could not generate test account: ${err.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sendMail = async (data: EmailSendOptions): ?Promise<*> => {
|
||||
const { transporter } = this;
|
||||
|
||||
if (transporter) {
|
||||
@@ -164,87 +211,23 @@ export class Mailer {
|
||||
});
|
||||
};
|
||||
|
||||
constructor() {
|
||||
this.loadTransport();
|
||||
}
|
||||
|
||||
async loadTransport() {
|
||||
if (process.env.SMTP_HOST) {
|
||||
let smtpConfig = {
|
||||
host: process.env.SMTP_HOST,
|
||||
port: process.env.SMTP_PORT,
|
||||
secure:
|
||||
"SMTP_SECURE" in process.env
|
||||
? process.env.SMTP_SECURE === "true"
|
||||
: process.env.NODE_ENV === "production",
|
||||
auth: undefined,
|
||||
tls:
|
||||
"SMTP_TLS_CIPHERS" in process.env
|
||||
? { ciphers: process.env.SMTP_TLS_CIPHERS }
|
||||
: undefined,
|
||||
};
|
||||
|
||||
if (process.env.SMTP_USERNAME) {
|
||||
smtpConfig.auth = {
|
||||
user: process.env.SMTP_USERNAME,
|
||||
pass: process.env.SMTP_PASSWORD,
|
||||
};
|
||||
sendTemplate = async (type: EmailTypes, opts?: Object = {}) => {
|
||||
await emailsQueue.add(
|
||||
{
|
||||
type,
|
||||
opts,
|
||||
},
|
||||
{
|
||||
attempts: 5,
|
||||
removeOnComplete: true,
|
||||
backoff: {
|
||||
type: "exponential",
|
||||
delay: 60 * 1000,
|
||||
},
|
||||
}
|
||||
|
||||
this.transporter = nodemailer.createTransport(smtpConfig);
|
||||
return;
|
||||
}
|
||||
|
||||
if (useTestEmailService) {
|
||||
log("SMTP_USERNAME not provided, generating test account…");
|
||||
|
||||
try {
|
||||
let testAccount = await nodemailer.createTestAccount();
|
||||
|
||||
const smtpConfig = {
|
||||
host: "smtp.ethereal.email",
|
||||
port: 587,
|
||||
secure: false,
|
||||
auth: {
|
||||
user: testAccount.user,
|
||||
pass: testAccount.pass,
|
||||
},
|
||||
};
|
||||
|
||||
this.transporter = nodemailer.createTransport(smtpConfig);
|
||||
} catch (err) {
|
||||
log(`Could not generate test account: ${err.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
const mailer = new Mailer();
|
||||
export default mailer;
|
||||
|
||||
export const mailerQueue = createQueue("email");
|
||||
|
||||
mailerQueue.process(async (job: EmailJob) => {
|
||||
// $FlowIssue flow doesn't like dynamic values
|
||||
await mailer[job.data.type](job.data.opts);
|
||||
});
|
||||
|
||||
export const sendEmail = (type: Emails, to: string, options?: Object = {}) => {
|
||||
mailerQueue.add(
|
||||
{
|
||||
type,
|
||||
opts: {
|
||||
to,
|
||||
...options,
|
||||
},
|
||||
},
|
||||
{
|
||||
attempts: 5,
|
||||
removeOnComplete: true,
|
||||
backoff: {
|
||||
type: "exponential",
|
||||
delay: 60 * 1000,
|
||||
},
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user