diff --git a/src/Auth/Domain/UseCases/Auth/ForgotPasswordUseCase.ts b/src/Auth/Domain/UseCases/Auth/ForgotPasswordUseCase.ts index dd9a87da..30555511 100644 --- a/src/Auth/Domain/UseCases/Auth/ForgotPasswordUseCase.ts +++ b/src/Auth/Domain/UseCases/Auth/ForgotPasswordUseCase.ts @@ -2,7 +2,6 @@ import MainConfig from '../../../../Config/MainConfig'; import ForgotPasswordPayload from '../../Payloads/Auth/ForgotPasswordPayload'; import IUserRepository from '../../../Infrastructure/Repositories/User/IUserRepository'; import { REPOSITORIES } from '../../../../Config/Injects'; -import ForgotPasswordEvent from '../../../Infrastructure/Events/ForgotPasswordEvent'; import SendEmailService from '../../../../Notification/Domain/Services/SendEmailService'; import TypeNotificationEnum from '../../../../Notification/Domain/Enum/TypeNotificationEnum'; import Locales from '../../../../Shared/Utils/Locales'; @@ -36,7 +35,6 @@ class ForgotPasswordUseCase const urlConfirmationToken = `${urlWeb}/change-forgot-password?token=${confirmationToken}`; void await SendEmailService.handle({ - event: ForgotPasswordEvent.name, type: TypeNotificationEnum.FORGOT_PASSWORD, to: user.email, name: 'Forgot password', @@ -48,7 +46,8 @@ class ForgotPasswordUseCase EMAIL_USER: user.email, URL_CONFIRMATION_TOKEN: urlConfirmationToken }, - external: true + external: true, + templatePathNameFile: 'auth/forgotPassword.hbs' }); const locales = Locales.getInstance().getLocales(); diff --git a/src/Auth/Domain/UseCases/Auth/RegisterUseCase.ts b/src/Auth/Domain/UseCases/Auth/RegisterUseCase.ts index 8b69b6d1..f3ff226a 100644 --- a/src/Auth/Domain/UseCases/Auth/RegisterUseCase.ts +++ b/src/Auth/Domain/UseCases/Auth/RegisterUseCase.ts @@ -1,5 +1,4 @@ import SendEmailService from '../../../../Notification/Domain/Services/SendEmailService'; -import RegisterEvent from '../../../Infrastructure/Events/RegisterEvent'; import TypeNotificationEnum from '../../../../Notification/Domain/Enum/TypeNotificationEnum'; import Locales from '../../../../Shared/Utils/Locales'; import RegisterPayload from '../../Payloads/Auth/RegisterPayload'; @@ -45,7 +44,6 @@ class RegisterUseCase const urlConfirmationToken = `${urlWeb}/verify-your-account?token=${confirmationToken}`; void await SendEmailService.handle({ - event: RegisterEvent.name, type: TypeNotificationEnum.VERIFY_ACCOUNT, to: payload.email, name: 'Verify your account', @@ -57,7 +55,8 @@ class RegisterUseCase EMAIL_USER: payload.email, URL_CONFIRMATION_TOKEN: urlConfirmationToken }, - external: true + external: true, + templatePathNameFile: 'auth/register.hbs' }); const locales = Locales.getInstance().getLocales(); diff --git a/src/Auth/Domain/UseCases/Auth/VerifyYourAccountUseCase.ts b/src/Auth/Domain/UseCases/Auth/VerifyYourAccountUseCase.ts index fce7a965..28062415 100644 --- a/src/Auth/Domain/UseCases/Auth/VerifyYourAccountUseCase.ts +++ b/src/Auth/Domain/UseCases/Auth/VerifyYourAccountUseCase.ts @@ -8,7 +8,6 @@ import IAuthRepository from '../../../Infrastructure/Repositories/Auth/IAuthRepo import IUserRepository from '../../../Infrastructure/Repositories/User/IUserRepository'; import IUserDomain from '../../Entities/IUserDomain'; import SendEmailService from '../../../../Notification/Domain/Services/SendEmailService'; -import VerifiedAccountEvent from '../../../Infrastructure/Events/VerifiedAccountEvent'; import TypeNotificationEnum from '../../../../Notification/Domain/Enum/TypeNotificationEnum'; import ValidatorSchema from '../../../../Shared/Utils/ValidatorSchema'; import VerifyYourAccountSchemaValidation @@ -41,7 +40,6 @@ class VerifyYourAccountUseCase await this.repository.verifyAccount({ id: user.getId() }); void await SendEmailService.handle({ - event: VerifiedAccountEvent.name, type: TypeNotificationEnum.VERIFIED_ACCOUNT, to: user.email, name: 'Verified account', @@ -51,7 +49,8 @@ class VerifyYourAccountUseCase data: { EMAIL_USER: user.email }, - external: true + external: true, + templatePathNameFile: 'auth/verifiedAccount.hbs' }); const locales = Locales.getInstance().getLocales(); diff --git a/src/Auth/Infrastructure/Events/ForgotPasswordEvent.ts b/src/Auth/Infrastructure/Events/EmailEvent.ts similarity index 66% rename from src/Auth/Infrastructure/Events/ForgotPasswordEvent.ts rename to src/Auth/Infrastructure/Events/EmailEvent.ts index ca83fdf1..94bde7c0 100644 --- a/src/Auth/Infrastructure/Events/ForgotPasswordEvent.ts +++ b/src/Auth/Infrastructure/Events/EmailEvent.ts @@ -2,9 +2,9 @@ import { FACTORIES } from '../../../Config/Injects'; import NotifierFactory from '../../../Notification/Shared/NotifierFactory'; import IEvent from '../../../Shared/Infrastructure/Events/IEvent'; -class ForgotPasswordEvent implements IEvent +class EmailEvent implements IEvent { - public name: string = ForgotPasswordEvent.name; + public name: string = EmailEvent.name; public handle = async(props: any) => { @@ -13,11 +13,11 @@ class ForgotPasswordEvent implements IEvent const emailNotificator: any = NotifierFactory.create(FACTORIES.EmailStrategy); emailNotificator.emailNotification = emailNotification; - emailNotificator.templatePathNameFile = 'auth/forgotPassword.hbs'; + emailNotificator.templatePathNameFile = args.templatePathNameFile; emailNotificator.data = args; - await emailNotificator.send(); + await emailNotificator.send(emailNotificator.templatePathNameFile); }; } -export default ForgotPasswordEvent; +export default EmailEvent; diff --git a/src/Auth/Infrastructure/Events/RegisterEvent.ts b/src/Auth/Infrastructure/Events/RegisterEvent.ts deleted file mode 100644 index 39048c2d..00000000 --- a/src/Auth/Infrastructure/Events/RegisterEvent.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { FACTORIES } from '../../../Config/Injects'; -import NotifierFactory from '../../../Notification/Shared/NotifierFactory'; -import IEvent from '../../../Shared/Infrastructure/Events/IEvent'; - -class RegisterEvent implements IEvent -{ - public name = RegisterEvent.name; - - public handle = async(props: any) => - { - const { emailNotification, args } = props; - - const emailNotifier: any = NotifierFactory.create(FACTORIES.EmailStrategy); - - emailNotifier.emailNotification = emailNotification; - emailNotifier.templatePathNameFile = 'auth/register.hbs'; - emailNotifier.data = args; - - await emailNotifier.send(); - }; -} - -export default RegisterEvent; diff --git a/src/Auth/Infrastructure/Events/VerifiedAccountEvent.ts b/src/Auth/Infrastructure/Events/VerifiedAccountEvent.ts deleted file mode 100644 index 5cdb8289..00000000 --- a/src/Auth/Infrastructure/Events/VerifiedAccountEvent.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { FACTORIES } from '../../../Config/Injects'; -import NotifierFactory from '../../../Notification/Shared/NotifierFactory'; -import IEvent from '../../../Shared/Infrastructure/Events/IEvent'; - -class VerifiedAccountEvent implements IEvent -{ - public name = VerifiedAccountEvent.name; - - public handle = async(props: any) => - { - const { emailNotification, args } = props; - - const emailNotifier: any = NotifierFactory.create(FACTORIES.EmailStrategy); - - emailNotifier.emailNotification = emailNotification; - emailNotifier.templatePathNameFile = 'auth/verifiedAccount.hbs'; - emailNotifier.data = args; - - await emailNotifier.send(); - }; -} - -export default VerifiedAccountEvent; diff --git a/src/Notification/Domain/Entities/EmailNotification.ts b/src/Notification/Domain/Entities/EmailNotification.ts index b343123a..d3e359c5 100644 --- a/src/Notification/Domain/Entities/EmailNotification.ts +++ b/src/Notification/Domain/Entities/EmailNotification.ts @@ -18,6 +18,7 @@ class EmailNotification extends Notification bcc: string; subject: string; external: boolean; + _templatePathNameFile?: string; attachedFiles?: IFileVersionDomain[]; data?: IEmailNotificationData; tempFilesAttachments: IFilesAttachments[]; diff --git a/src/Notification/Domain/Entities/ISendEmailParams.ts b/src/Notification/Domain/Entities/ISendEmailParams.ts index 2f5f0ea5..055724aa 100644 --- a/src/Notification/Domain/Entities/ISendEmailParams.ts +++ b/src/Notification/Domain/Entities/ISendEmailParams.ts @@ -5,7 +5,6 @@ import IFileVersionDomain from '../../../File/Domain/Entities/IFileVersionDomain interface ISendEmailParams { type: TypeNotificationEnum; - event: string; args: Record name: string; subject?: string; @@ -15,6 +14,7 @@ interface ISendEmailParams cc?: string[]; bcc?: string[]; external?: boolean; + templatePathNameFile?: string; } export default ISendEmailParams; diff --git a/src/Notification/Domain/Services/SendEmailService.ts b/src/Notification/Domain/Services/SendEmailService.ts index bd6aa7da..e40e1124 100644 --- a/src/Notification/Domain/Services/SendEmailService.ts +++ b/src/Notification/Domain/Services/SendEmailService.ts @@ -4,9 +4,11 @@ import EmailNotification from '../Entities/EmailNotification'; class SendEmailService { + private static emailEvent = 'EmailEvent'; public static async handle(params: ISendEmailParams): Promise { - const { type, args, event, name, files, data, cc, bcc, to, subject, external } = params; + const { type, args, name, files, + data, cc, bcc, to, subject, external, templatePathNameFile } = params; const emailNotification = new EmailNotification(); @@ -19,10 +21,11 @@ class SendEmailService emailNotification.attachedFiles = files ?? []; emailNotification.type = type; emailNotification.external = external ?? false; + args.templatePathNameFile = templatePathNameFile; const eventHandler = EventHandler.getInstance(); - eventHandler.execute(event, { emailNotification, args }); + eventHandler.execute(this.emailEvent, { emailNotification, args }); } } diff --git a/src/Notification/Shared/EmailStrategy.ts b/src/Notification/Shared/EmailStrategy.ts index f3ef8bb3..bfdd1295 100644 --- a/src/Notification/Shared/EmailStrategy.ts +++ b/src/Notification/Shared/EmailStrategy.ts @@ -67,7 +67,7 @@ class EmailStrategy implements INotifierStrategy this._save = save; } - public async send() + public async send(templatePathNameFile) { if (!this._emailNotification && !this._templatePathNameFile && !this._data) { @@ -82,6 +82,7 @@ class EmailStrategy implements INotifierStrategy const port: number = config.mail.port; const secure: boolean = config.mail.secure; const templateRoot: string = config.mail.templateDir; + this._templatePathNameFile = templatePathNameFile; const templateDir = `${process.cwd()}/${templateRoot}/${this._templatePathNameFile}`; const smtp_config = { host, port, secure }; diff --git a/src/Notification/Shared/INotifierStrategy.ts b/src/Notification/Shared/INotifierStrategy.ts index f8098b32..2d55f471 100644 --- a/src/Notification/Shared/INotifierStrategy.ts +++ b/src/Notification/Shared/INotifierStrategy.ts @@ -1,7 +1,7 @@ interface INotifierStrategy { - send(): void; + send(templatePathNameFile: string): void; } export default INotifierStrategy; diff --git a/src/index.ts b/src/index.ts index 49cf5531..6d58f1cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,10 +14,8 @@ import ICreateConnection from './Shared/Infrastructure/Database/ICreateConnectio import Logger from './Shared/Helpers/Logger/Logger'; import closedApplication from './closed'; import UserCreatedEvent from './Auth/Infrastructure/Events/UserCreatedEvent'; -import ForgotPasswordEvent from './Auth/Infrastructure/Events/ForgotPasswordEvent'; -import RegisterEvent from './Auth/Infrastructure/Events/RegisterEvent'; -import VerifiedAccountEvent from './Auth/Infrastructure/Events/VerifiedAccountEvent'; import SendMessageEvent from './Notification/Infrastructure/Events/SendMessageEvent'; +import EmailEvent from './Auth/Infrastructure/Events/EmailEvent'; void (async() => { @@ -50,9 +48,7 @@ void (async() => // Set EventHandler and all events const eventHandler = EventHandler.getInstance(); eventHandler.setEvent(new UserCreatedEvent()); - eventHandler.setEvent(new ForgotPasswordEvent()); - eventHandler.setEvent(new RegisterEvent()); - eventHandler.setEvent(new VerifiedAccountEvent()); + eventHandler.setEvent(new EmailEvent()); eventHandler.setEvent(new SendMessageEvent()); // Create cron diff --git a/src/initTestServer.ts b/src/initTestServer.ts index afb65e97..dd2694f0 100644 --- a/src/initTestServer.ts +++ b/src/initTestServer.ts @@ -22,10 +22,8 @@ import UserMockRepository from './Auth/Tests/UserMockRepository'; import RoleMockRepository from './Auth/Tests/RoleMockRepository'; import { Lifecycle } from 'tsyringe'; import UserCreatedEvent from './Auth/Infrastructure/Events/UserCreatedEvent'; -import ForgotPasswordEvent from './Auth/Infrastructure/Events/ForgotPasswordEvent'; -import RegisterEvent from './Auth/Infrastructure/Events/RegisterEvent'; -import VerifiedAccountEvent from './Auth/Infrastructure/Events/VerifiedAccountEvent'; import SendMessageEvent from './Notification/Infrastructure/Events/SendMessageEvent'; +import EmailEvent from './Auth/Infrastructure/Events/EmailEvent'; type TestServerData = { request: supertest.SuperAgentTest, @@ -47,9 +45,7 @@ const initTestServer = async(): Promise => const eventHandler = EventHandler.getInstance(); eventHandler.setEvent(new UserCreatedEvent()); - eventHandler.setEvent(new ForgotPasswordEvent()); - eventHandler.setEvent(new RegisterEvent()); - eventHandler.setEvent(new VerifiedAccountEvent()); + eventHandler.setEvent(new EmailEvent()); eventHandler.setEvent(new SendMessageEvent()); void Locales.getInstance();