Skip to content

Commit

Permalink
feat: nse-145 unique event to send emails added
Browse files Browse the repository at this point in the history
  • Loading branch information
KMery authored and nrusso committed Oct 28, 2023
1 parent 5ec09e7 commit 51c6467
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 77 deletions.
5 changes: 2 additions & 3 deletions src/Auth/Domain/UseCases/Auth/ForgotPasswordUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions src/Auth/Domain/UseCases/Auth/RegisterUseCase.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions src/Auth/Domain/UseCases/Auth/VerifyYourAccountUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -51,7 +49,8 @@ class VerifyYourAccountUseCase
data: {
EMAIL_USER: user.email
},
external: true
external: true,
templatePathNameFile: 'auth/verifiedAccount.hbs'
});

const locales = Locales.getInstance().getLocales();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
{
Expand All @@ -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;
23 changes: 0 additions & 23 deletions src/Auth/Infrastructure/Events/RegisterEvent.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/Auth/Infrastructure/Events/VerifiedAccountEvent.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/Notification/Domain/Entities/EmailNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class EmailNotification extends Notification
bcc: string;
subject: string;
external: boolean;
_templatePathNameFile?: string;
attachedFiles?: IFileVersionDomain[];
data?: IEmailNotificationData;
tempFilesAttachments: IFilesAttachments[];
Expand Down
2 changes: 1 addition & 1 deletion src/Notification/Domain/Entities/ISendEmailParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import IFileVersionDomain from '../../../File/Domain/Entities/IFileVersionDomain
interface ISendEmailParams
{
type: TypeNotificationEnum;
event: string;
args: Record<string, any>
name: string;
subject?: string;
Expand All @@ -15,6 +14,7 @@ interface ISendEmailParams
cc?: string[];
bcc?: string[];
external?: boolean;
templatePathNameFile?: string;
}

export default ISendEmailParams;
7 changes: 5 additions & 2 deletions src/Notification/Domain/Services/SendEmailService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import EmailNotification from '../Entities/EmailNotification';

class SendEmailService
{
private static emailEvent = 'EmailEvent';
public static async handle(params: ISendEmailParams): Promise<void>
{
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();

Expand All @@ -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 });
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Notification/Shared/EmailStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion src/Notification/Shared/INotifierStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

interface INotifierStrategy
{
send(): void;
send(templatePathNameFile: string): void;
}

export default INotifierStrategy;
8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() =>
{
Expand Down Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions src/initTestServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -47,9 +45,7 @@ const initTestServer = async(): Promise<TestServerData> =>

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();
Expand Down

0 comments on commit 51c6467

Please sign in to comment.