chore: Email + mailer refactor (#3342)
* Huge email refactor * fix: One rename too many * comments
This commit is contained in:
@@ -9,6 +9,12 @@ export enum TaskPriority {
|
||||
}
|
||||
|
||||
export default abstract class BaseTask<T> {
|
||||
/**
|
||||
* Schedule this task type to be processed asyncronously by a worker.
|
||||
*
|
||||
* @param props Properties to be used by the task
|
||||
* @returns A promise that resolves once the job is placed on the task queue
|
||||
*/
|
||||
public static schedule<T>(props: T) {
|
||||
// @ts-expect-error cannot create an instance of an abstract class, we wont
|
||||
const task = new this();
|
||||
@@ -21,8 +27,17 @@ export default abstract class BaseTask<T> {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the task.
|
||||
*
|
||||
* @param props Properties to be used by the task
|
||||
* @returns A promise that resolves once the task has completed.
|
||||
*/
|
||||
public abstract perform(props: T): Promise<void>;
|
||||
|
||||
/**
|
||||
* Job options such as priority and retry strategy, as defined by Bull.
|
||||
*/
|
||||
public get options(): JobOptions {
|
||||
return {
|
||||
priority: TaskPriority.Normal,
|
||||
|
||||
@@ -1,15 +1,23 @@
|
||||
import emails from "@server/emails/templates";
|
||||
import { APM } from "@server/logging/tracing";
|
||||
import mailer, { EmailSendOptions, EmailTypes } from "../../mailer";
|
||||
import BaseTask from "./BaseTask";
|
||||
|
||||
type Props = {
|
||||
type: EmailTypes;
|
||||
options: EmailSendOptions;
|
||||
templateName: string;
|
||||
props: Record<string, any>;
|
||||
};
|
||||
|
||||
@APM.trace()
|
||||
export default class EmailTask extends BaseTask<Props> {
|
||||
public async perform(props: Props) {
|
||||
await mailer[props.type](props.options);
|
||||
public async perform({ templateName, props }: Props) {
|
||||
const EmailClass = emails[templateName];
|
||||
if (!EmailClass) {
|
||||
throw new Error(
|
||||
`Email task "${templateName}" template does not exist. Check the file name matches the class name.`
|
||||
);
|
||||
}
|
||||
|
||||
const email = new EmailClass(props);
|
||||
return email.send();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user