refactor(core): Use an IoC container to manage singleton classes [Part-2] (no-changelog) (#5690)

* use typedi for UserManagementMailer

* use typedi for SamlService

* fix typos

* use typedi for Queue

* use typedi for License

* convert some more code to use typedi
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-03-16 15:34:13 +01:00
committed by GitHub
parent c07f838ce6
commit 9bd7529193
39 changed files with 154 additions and 178 deletions

View File

@@ -3,6 +3,7 @@
import { In } from 'typeorm';
import type express from 'express';
import { compare, genSaltSync, hash } from 'bcryptjs';
import Container from 'typedi';
import * as Db from '@/Db';
import * as ResponseHelper from '@/ResponseHelper';
@@ -13,7 +14,7 @@ import type { Role } from '@db/entities/Role';
import type { AuthenticatedRequest } from '@/requests';
import config from '@/config';
import { getWebhookBaseUrl } from '@/WebhookHelpers';
import { getLicense } from '@/License';
import { License } from '@/License';
import { RoleService } from '@/role/role.service';
import type { PostHogClient } from '@/posthog';
@@ -55,7 +56,7 @@ export function isUserManagementEnabled(): boolean {
}
export function isSharingEnabled(): boolean {
const license = getLicense();
const license = Container.get(License);
return (
isUserManagementEnabled() &&
(config.getEnv('enterprise.features.sharing') || license.isSharingEnabled())

View File

@@ -1,9 +1,3 @@
export interface UserManagementMailerImplementation {
init: () => Promise<void>;
sendMail: (mailData: MailData) => Promise<SendEmailResult>;
verifyConnection: () => Promise<void>;
}
export type InviteEmailData = {
email: string;
firstName?: string;

View File

@@ -3,9 +3,9 @@ import type { Transporter } from 'nodemailer';
import { createTransport } from 'nodemailer';
import { ErrorReporterProxy as ErrorReporter, LoggerProxy as Logger } from 'n8n-workflow';
import config from '@/config';
import type { MailData, SendEmailResult, UserManagementMailerImplementation } from './Interfaces';
import type { MailData, SendEmailResult } from './Interfaces';
export class NodeMailer implements UserManagementMailerImplementation {
export class NodeMailer {
private transport?: Transporter;
async init(): Promise<void> {

View File

@@ -2,13 +2,9 @@ import { existsSync } from 'fs';
import { readFile } from 'fs/promises';
import Handlebars from 'handlebars';
import { join as pathJoin } from 'path';
import { Service } from 'typedi';
import config from '@/config';
import type {
InviteEmailData,
PasswordResetData,
SendEmailResult,
UserManagementMailerImplementation,
} from './Interfaces';
import type { InviteEmailData, PasswordResetData, SendEmailResult } from './Interfaces';
import { NodeMailer } from './NodeMailer';
type Template = HandlebarsTemplateDelegate<unknown>;
@@ -36,8 +32,9 @@ async function getTemplate(
return template;
}
@Service()
export class UserManagementMailer {
private mailer: UserManagementMailerImplementation | undefined;
private mailer: NodeMailer | undefined;
constructor() {
// Other implementations can be used in the future.
@@ -81,12 +78,3 @@ export class UserManagementMailer {
return result ?? { emailSent: false };
}
}
let mailerInstance: UserManagementMailer | undefined;
export function getInstance(): UserManagementMailer {
if (mailerInstance === undefined) {
mailerInstance = new UserManagementMailer();
}
return mailerInstance;
}

View File

@@ -1,3 +1,3 @@
import { getInstance, UserManagementMailer } from './UserManagementMailer';
import { UserManagementMailer } from './UserManagementMailer';
export { getInstance, UserManagementMailer };
export { UserManagementMailer };