Skip to content

Commit

Permalink
refactor(nexp-240): refactor event handler
Browse files Browse the repository at this point in the history
* refactor(nexp): eventhandler as injectable dependency
  • Loading branch information
Rutito2010 authored Mar 17, 2024
1 parent 6b9f1ac commit 66bc8e6
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 8 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ services:
restart: always
image: mailhog/mailhog:latest
ports:
- "1025:1025"
- "8025:8025"
networks:
- experiencenet
Expand Down
2 changes: 1 addition & 1 deletion src/Notification/Domain/Events/SendMessageEvent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FACTORIES } from '../../../Shared/DI/Injects';
import NotifierFactory from '../../Shared/NotifierFactory';
import { IEvent } from '@digichanges/shared-experience';
import { IEvent } from '../../Infrastructure/events';

class SendMessageEvent implements IEvent
{
Expand Down
6 changes: 4 additions & 2 deletions src/Notification/Domain/Services/NotificationService.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import NotificationRepPayload from '../Payloads/NotificationRepPayload';
import PushNotification from '../Entities/PushNotification';
import { EventHandler } from '@digichanges/shared-experience';
import SendMessageEvent from '../Events/SendMessageEvent';
import NotificationSendMessagePayload from '../Payloads/NotificationSendMessagePayload';
import INotificationResponse from '../Entities/INotificationResponse';
import { IEventHandler } from '../../Infrastructure/events';
import DependencyInjector from '../../../Shared/DI/DependencyInjector';

class NotificationService
{
private eventHandler = EventHandler.getInstance();
private eventHandler: IEventHandler;

async execute(pushNotification: PushNotification, payload: NotificationRepPayload, message: string, name: string): Promise<INotificationResponse>
{
this.eventHandler = DependencyInjector.inject<IEventHandler>('IEventHandler');
pushNotification.subscription = payload.getSubscription();
pushNotification.name = name;
this.eventHandler.execute(SendMessageEvent.name, { push_notification: pushNotification, message });
Expand Down
71 changes: 71 additions & 0 deletions src/Notification/Infrastructure/events/EventHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Subject } from 'rxjs';
import { IEvent } from './IEvent';
export interface IEventHandler
{
execute(eventName: string, args: any): void;
setEvent(_event: IEvent): void;
removeListeners(): void;
}

type SubscribeEventProps =
{
eventName: string;
args: Record<string, unknown>;
}

export class EventHandler implements IEventHandler
{
private eventSubject: Subject<any>;
private events: Map<string, (args: any) => Promise<void>>;

constructor()
{
console.log(1);
this.events = new Map<string, (args: any) => Promise<void>>();
this.eventSubject = new Subject<any>();

this.eventSubject.subscribe((event: SubscribeEventProps) =>
{
const { eventName, args } = event;
const eventHandler = this.events.get(eventName);

if (eventHandler)
{
void (async() =>
{
try
{
await eventHandler(args).then();
}
catch (error)
{
console.log(error);
}
})();
}
});
}


public execute(eventName: string, args: any)
{
this.eventSubject.next({ eventName, args });
}

public setEvent(_event: IEvent)
{
if (this.events.has(_event.name))
{
return;
}

this.events.set(_event.name, _event.handle);
}

public async removeListeners()
{
this.eventSubject.complete();
}
}

export default EventHandler;
5 changes: 5 additions & 0 deletions src/Notification/Infrastructure/events/IEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface IEvent
{
name: string;
handle(props: any): Promise<void>;
}
2 changes: 2 additions & 0 deletions src/Notification/Infrastructure/events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './EventHandler';
export * from './IEvent';
2 changes: 1 addition & 1 deletion src/Shared/DI/Injects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export enum REPOSITORIES {
}

export enum SERVICES {
AuthorizeService = 'AuthorizeService'
AuthorizeService = 'AuthorizeService',
}

export enum FACTORIES {
Expand Down
5 changes: 5 additions & 0 deletions src/Shared/DI/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import DatabaseFactory from '../../Main/Infrastructure/Factories/DatabaseFactory
import { IMessageBroker } from '../Infrastructure/IMessageBroker';
import RabbitMQMessageBroker from '../Infrastructure/RabbitMQMessageBroker';
import CronService, { ICronService } from '../../Main/Infrastructure/Factories/CronService';
import EventHandler, { IEventHandler } from '../../Notification/Infrastructure/events/EventHandler';

const config = MainConfig.getInstance().getConfig();
const defaultDbConfig = config.dbConfig.default;
Expand Down Expand Up @@ -81,6 +82,10 @@ container.register(SERVICES.AuthorizeService, {

container.register<IMessageBroker>('IMessageBroker', { useClass: RabbitMQMessageBroker }, { lifecycle: Lifecycle.Singleton });

container.register<IEventHandler>('IEventHandler',
{ useClass: EventHandler },
{ lifecycle: Lifecycle.Singleton });

container.register<ICronService>('ICronService', {
// @ts-ignore
useFactory: instanceCachingFactory(() =>
Expand Down
4 changes: 2 additions & 2 deletions src/closed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { createTerminus } from '@godaddy/terminus';
import ICacheDataAccess from './Main/Infrastructure/Repositories/ICacheDataAccess';
import Logger from './Shared/Helpers/Logger';
import ICreateConnection from './Main/Infrastructure/Database/ICreateConnection';
import { EventHandler } from '@digichanges/shared-experience';
import { Server } from 'http';
import { IMessageBroker } from './Shared/Infrastructure/IMessageBroker';
import { IEventHandler } from './Notification/Infrastructure/events';

interface ClosedApplicationParams
{
server?: Server,
eventHandler?: EventHandler
eventHandler?: IEventHandler
messageBroker: IMessageBroker
cache: ICacheDataAccess,
createConnection: ICreateConnection,
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import EmailEvent from './Auth/Infrastructure/Events/EmailEvent';
import ICacheDataAccess from './Main/Infrastructure/Repositories/ICacheDataAccess';
import { IMessageBroker } from './Shared/Infrastructure/IMessageBroker';
import crons from './crons';
import { IEventHandler } from './Notification/Infrastructure/events';

void (async() =>
{
Expand Down Expand Up @@ -54,7 +55,9 @@ void (async() =>
}

// Set EventHandler and all events
const eventHandler = EventHandler.getInstance();
// const eventHandler = EventHandler.getInstance();

const eventHandler = DependencyInjector.inject<IEventHandler>('IEventHandler');
eventHandler.setEvent(new EmailEvent());
eventHandler.setEvent(new SendMessageEvent());

Expand Down
4 changes: 3 additions & 1 deletion src/initTestServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { REPOSITORIES } from './Shared/DI/Injects';
import { Lifecycle } from 'tsyringe';
import SendMessageEvent from './Notification/Domain/Events/SendMessageEvent';
import AuthMockRepository from './Auth/Tests/AuthMockRepository';
import DependencyInjector from './Shared/DI/DependencyInjector';
import { IEventHandler } from './Notification/Infrastructure/events';

type TestServerData = {
request: supertest.SuperAgentTest,
Expand All @@ -34,7 +36,7 @@ const initTestServer = async(): Promise<TestServerData> =>
await dbConnection.create();
await dbConnection.synchronize();

const eventHandler = EventHandler.getInstance();
const eventHandler = DependencyInjector.inject<IEventHandler>('IEventHandler');
eventHandler.setEvent(new SendMessageEvent());

// @ts-ignore
Expand Down

0 comments on commit 66bc8e6

Please sign in to comment.