From 8d29f2a43370f0153781a1d397f416dec1ad8fe0 Mon Sep 17 00:00:00 2001 From: Leonardo Camargo Date: Tue, 31 Oct 2023 10:39:57 -0300 Subject: [PATCH] feat: create customer events --- .../customer-change-address-event.spec.ts | 36 ++++++++++++++++++ .../customer/customer-change-address-event.ts | 11 ++++++ .../customer/customer-created-event.spec.ts | 38 +++++++++++++++++++ .../event/customer/customer-created-event.ts | 11 ++++++ .../handlers/send-console-log-handler.ts | 9 +++++ .../handlers/send-console-log1-handler.ts | 9 +++++ .../handlers/send-console-log2-handler.ts | 9 +++++ 7 files changed, 123 insertions(+) create mode 100644 src/domain/event/customer/customer-change-address-event.spec.ts create mode 100644 src/domain/event/customer/customer-change-address-event.ts create mode 100644 src/domain/event/customer/customer-created-event.spec.ts create mode 100644 src/domain/event/customer/customer-created-event.ts create mode 100644 src/domain/event/customer/handlers/send-console-log-handler.ts create mode 100644 src/domain/event/customer/handlers/send-console-log1-handler.ts create mode 100644 src/domain/event/customer/handlers/send-console-log2-handler.ts diff --git a/src/domain/event/customer/customer-change-address-event.spec.ts b/src/domain/event/customer/customer-change-address-event.spec.ts new file mode 100644 index 0000000..d917025 --- /dev/null +++ b/src/domain/event/customer/customer-change-address-event.spec.ts @@ -0,0 +1,36 @@ +import { EventDispatcher } from '../@shared/event-dispatcher' +import { CustomerChangeAddressEvent } from './customer-change-address-event' +import { SendConsoleLogHandler } from './handlers/send-console-log-handler' + +describe('Customer change address events tests', () => { + it('should register an event handler', () => { + const eventDispatcher = new EventDispatcher() + const eventHandler = new SendConsoleLogHandler() + eventDispatcher.register('CustomerChangeAddressEvent', eventHandler) + expect(eventDispatcher.getEventHandlers.CustomerChangeAddressEvent).toBeDefined() + expect(eventDispatcher.getEventHandlers.CustomerChangeAddressEvent.length).toBe(1) + expect(eventDispatcher.getEventHandlers.CustomerChangeAddressEvent[0]).toMatchObject(eventHandler) + }) + + it('should notify all event handlers', () => { + const eventDispatcher = new EventDispatcher() + const eventHandler = new SendConsoleLogHandler() + const spyEventHandler = jest.spyOn(eventHandler, 'handler') + + eventDispatcher.register('CustomerChangeAddressEvent', eventHandler) + expect(eventDispatcher.getEventHandlers.CustomerChangeAddressEvent[0]).toMatchObject(eventHandler) + + const customerCreatedEvent = new CustomerChangeAddressEvent({ + id: '123', + name: 'John', + street: 'Street 1', + number: 123, + zip: '12345-678', + city: 'São Paulo', + state: 'SP' + }) + + eventDispatcher.notify(customerCreatedEvent) + expect(spyEventHandler).toHaveBeenCalled() + }) +}) diff --git a/src/domain/event/customer/customer-change-address-event.ts b/src/domain/event/customer/customer-change-address-event.ts new file mode 100644 index 0000000..3c84728 --- /dev/null +++ b/src/domain/event/customer/customer-change-address-event.ts @@ -0,0 +1,11 @@ +import { EventInterface } from '../@shared/event-interface' + +export class CustomerChangeAddressEvent implements EventInterface { + readonly dataTimeOccurred: Date + readonly eventData: any + + constructor (eventData: any) { + this.dataTimeOccurred = new Date() + this.eventData = eventData + } +} diff --git a/src/domain/event/customer/customer-created-event.spec.ts b/src/domain/event/customer/customer-created-event.spec.ts new file mode 100644 index 0000000..a229801 --- /dev/null +++ b/src/domain/event/customer/customer-created-event.spec.ts @@ -0,0 +1,38 @@ +import { EventDispatcher } from '../@shared/event-dispatcher' +import { CustomerCreatedEvent } from './customer-created-event' +import { SendConsoleLog1Handler } from './handlers/send-console-log1-handler' +import { SendConsoleLog2Handler } from './handlers/send-console-log2-handler' + +describe('Customer created events tests', () => { + it('should register an event handler', () => { + const eventDispatcher = new EventDispatcher() + const eventHandler = new SendConsoleLog1Handler() + eventDispatcher.register('CustomerCreatedEvent', eventHandler) + expect(eventDispatcher.getEventHandlers.CustomerCreatedEvent).toBeDefined() + expect(eventDispatcher.getEventHandlers.CustomerCreatedEvent.length).toBe(1) + expect(eventDispatcher.getEventHandlers.CustomerCreatedEvent[0]).toMatchObject(eventHandler) + }) + + it('should notify all event handlers', () => { + const eventDispatcher = new EventDispatcher() + const eventHandler1 = new SendConsoleLog1Handler() + const eventHandler2 = new SendConsoleLog2Handler() + + const spyEventHandler1 = jest.spyOn(eventHandler1, 'handler') + const spyEventHandler2 = jest.spyOn(eventHandler2, 'handler') + + eventDispatcher.register('CustomerCreatedEvent', eventHandler1) + eventDispatcher.register('CustomerCreatedEvent', eventHandler2) + + expect(eventDispatcher.getEventHandlers.CustomerCreatedEvent[0]).toMatchObject(eventHandler1) + expect(eventDispatcher.getEventHandlers.CustomerCreatedEvent[1]).toMatchObject(eventHandler2) + + const customerCreatedEvent = new CustomerCreatedEvent({ + name: 'John' + }) + + eventDispatcher.notify(customerCreatedEvent) + expect(spyEventHandler1).toHaveBeenCalled() + expect(spyEventHandler2).toHaveBeenCalled() + }) +}) diff --git a/src/domain/event/customer/customer-created-event.ts b/src/domain/event/customer/customer-created-event.ts new file mode 100644 index 0000000..959edc4 --- /dev/null +++ b/src/domain/event/customer/customer-created-event.ts @@ -0,0 +1,11 @@ +import { EventInterface } from '../@shared/event-interface' + +export class CustomerCreatedEvent implements EventInterface { + readonly dataTimeOccurred: Date + readonly eventData: any + + constructor (eventData: any) { + this.dataTimeOccurred = new Date() + this.eventData = eventData + } +} diff --git a/src/domain/event/customer/handlers/send-console-log-handler.ts b/src/domain/event/customer/handlers/send-console-log-handler.ts new file mode 100644 index 0000000..27195a3 --- /dev/null +++ b/src/domain/event/customer/handlers/send-console-log-handler.ts @@ -0,0 +1,9 @@ +import { EventHandlerInterface } from '../../@shared/event-handler-interface' +import { EventInterface } from '../../@shared/event-interface' +import { CustomerCreatedEvent } from '../customer-created-event' + +export class SendConsoleLogHandler implements EventHandlerInterface { + handler (event: EventInterface): void { + console.log(`endereço do cliente: ${event.eventData.id as string}, ${event.eventData.name as string} alterado para: ${event.eventData.street as string}, ${event.eventData.number.toString() as string}`) + } +} diff --git a/src/domain/event/customer/handlers/send-console-log1-handler.ts b/src/domain/event/customer/handlers/send-console-log1-handler.ts new file mode 100644 index 0000000..86b1a09 --- /dev/null +++ b/src/domain/event/customer/handlers/send-console-log1-handler.ts @@ -0,0 +1,9 @@ +import { EventHandlerInterface } from '../../@shared/event-handler-interface' +import { EventInterface } from '../../@shared/event-interface' +import { CustomerCreatedEvent } from '../customer-created-event' + +export class SendConsoleLog1Handler implements EventHandlerInterface { + handler (event: EventInterface): void { + console.log('esse é o primeiro console.log do evento: CustomerCreated') + } +} diff --git a/src/domain/event/customer/handlers/send-console-log2-handler.ts b/src/domain/event/customer/handlers/send-console-log2-handler.ts new file mode 100644 index 0000000..3e9509a --- /dev/null +++ b/src/domain/event/customer/handlers/send-console-log2-handler.ts @@ -0,0 +1,9 @@ +import { EventHandlerInterface } from '../../@shared/event-handler-interface' +import { EventInterface } from '../../@shared/event-interface' +import { CustomerCreatedEvent } from '../customer-created-event' + +export class SendConsoleLog2Handler implements EventHandlerInterface { + handler (event: EventInterface): void { + console.log('esse é o segundo console.log do evento: CustomerCreated') + } +}