diff --git a/src/modules/cards/domain/events/CardAddedToLibraryEvent.ts b/src/modules/cards/domain/events/CardAddedToLibraryEvent.ts index c866e1e8..f6ac7f91 100644 --- a/src/modules/cards/domain/events/CardAddedToLibraryEvent.ts +++ b/src/modules/cards/domain/events/CardAddedToLibraryEvent.ts @@ -1,12 +1,11 @@ -import { IDomainEvent, IDomainEventClass } from '../../../../shared/domain/events/IDomainEvent'; +import { IDomainEvent } from '../../../../shared/domain/events/IDomainEvent'; import { UniqueEntityID } from '../../../../shared/domain/UniqueEntityID'; import { CardId } from '../value-objects/CardId'; import { CuratorId } from '../value-objects/CuratorId'; -import { CardTypeEnum } from '../value-objects/CardType'; import { EventNames } from '../../../../shared/infrastructure/events/EventConfig'; export class CardAddedToLibraryEvent implements IDomainEvent { - public static readonly eventName = EventNames.CARD_ADDED_TO_LIBRARY; + public readonly eventName = EventNames.CARD_ADDED_TO_LIBRARY; public readonly dateTimeOccurred: Date; constructor( diff --git a/src/shared/domain/events/IDomainEvent.ts b/src/shared/domain/events/IDomainEvent.ts index 57a124c2..8475fb1a 100644 --- a/src/shared/domain/events/IDomainEvent.ts +++ b/src/shared/domain/events/IDomainEvent.ts @@ -1,4 +1,4 @@ -import { EventName } from 'src/shared/infrastructure/events/EventConfig'; +import { EventName } from '../../infrastructure/events/EventConfig'; import { UniqueEntityID } from '../UniqueEntityID'; export interface IDomainEvent { diff --git a/src/shared/infrastructure/events/BullMQEventPublisher.ts b/src/shared/infrastructure/events/BullMQEventPublisher.ts index d901e5b8..3dbb4331 100644 --- a/src/shared/infrastructure/events/BullMQEventPublisher.ts +++ b/src/shared/infrastructure/events/BullMQEventPublisher.ts @@ -4,7 +4,7 @@ import { IEventPublisher } from '../../application/events/IEventPublisher'; import { IDomainEvent } from '../../domain/events/IDomainEvent'; import { Result, ok, err } from '../../core/Result'; import { QueueNames, QueueOptions } from './QueueConfig'; -import { EventName } from './EventConfig'; +import { EventMapper } from './EventMapper'; export class BullMQEventPublisher implements IEventPublisher { private queues: Map = new Map(); @@ -34,20 +34,10 @@ export class BullMQEventPublisher implements IEventPublisher { } const queue = this.queues.get(QueueNames.EVENTS)!; - const eventType = this.getEventType(event); - await queue.add(eventType, { - eventType: eventType, - aggregateId: event.getAggregateId().toString(), - dateTimeOccurred: event.dateTimeOccurred.toISOString(), - // Serialize the event data - cardId: (event as any).cardId?.getValue?.()?.toString(), - curatorId: (event as any).curatorId?.value, - }); + const serializedEvent = EventMapper.toSerialized(event); + await queue.add(serializedEvent.eventType, serializedEvent); } - private getEventType(event: IDomainEvent): EventName { - return event.eventName; - } async close(): Promise { await Promise.all( diff --git a/src/shared/infrastructure/events/BullMQEventSubscriber.ts b/src/shared/infrastructure/events/BullMQEventSubscriber.ts index f31a2fd7..08251723 100644 --- a/src/shared/infrastructure/events/BullMQEventSubscriber.ts +++ b/src/shared/infrastructure/events/BullMQEventSubscriber.ts @@ -5,11 +5,8 @@ import { IEventHandler, } from '../../application/events/IEventSubscriber'; import { IDomainEvent } from '../../domain/events/IDomainEvent'; -import { CardAddedToLibraryEvent } from '../../../modules/cards/domain/events/CardAddedToLibraryEvent'; -import { CardId } from '../../../modules/cards/domain/value-objects/CardId'; -import { CuratorId } from '../../../modules/cards/domain/value-objects/CuratorId'; import { QueueNames } from './QueueConfig'; -import { EventNames } from './EventConfig'; +import { EventMapper } from './EventMapper'; export class BullMQEventSubscriber implements IEventSubscriber { private workers: Worker[] = []; @@ -75,19 +72,6 @@ export class BullMQEventSubscriber implements IEventSubscriber { } private reconstructEvent(eventData: any): IDomainEvent { - switch (eventData.eventType) { - case EventNames.CARD_ADDED_TO_LIBRARY: { - const cardId = CardId.create(eventData.cardId).unwrap(); - const curatorId = CuratorId.create(eventData.curatorId).unwrap(); - - const event = new CardAddedToLibraryEvent(cardId, curatorId); - (event as any).dateTimeOccurred = new Date(eventData.dateTimeOccurred); - (event as any).eventName = EventNames.CARD_ADDED_TO_LIBRARY; - - return event; - } - default: - throw new Error(`Unknown event type: ${eventData.eventType}`); - } + return EventMapper.fromSerialized(eventData); } } diff --git a/src/shared/infrastructure/events/EventMapper.ts b/src/shared/infrastructure/events/EventMapper.ts new file mode 100644 index 00000000..cb7a2b23 --- /dev/null +++ b/src/shared/infrastructure/events/EventMapper.ts @@ -0,0 +1,50 @@ +import { IDomainEvent } from '../../domain/events/IDomainEvent'; +import { CardAddedToLibraryEvent } from '../../../modules/cards/domain/events/CardAddedToLibraryEvent'; +import { CardId } from '../../../modules/cards/domain/value-objects/CardId'; +import { CuratorId } from '../../../modules/cards/domain/value-objects/CuratorId'; +import { EventNames } from './EventConfig'; + +export interface SerializedEvent { + eventType: string; + aggregateId: string; + dateTimeOccurred: string; + [key: string]: any; +} + +export class EventMapper { + static toSerialized(event: IDomainEvent): SerializedEvent { + const baseData: SerializedEvent = { + eventType: event.eventName, + aggregateId: event.getAggregateId().toString(), + dateTimeOccurred: event.dateTimeOccurred.toISOString(), + }; + + // Add event-specific data based on event type + if (event instanceof CardAddedToLibraryEvent) { + return { + ...baseData, + cardId: event.cardId.getValue().toString(), + curatorId: event.curatorId.value, + }; + } + + throw new Error(`Unknown event type for serialization: ${event.constructor.name}`); + } + + static fromSerialized(eventData: SerializedEvent): IDomainEvent { + switch (eventData.eventType) { + case EventNames.CARD_ADDED_TO_LIBRARY: { + const cardId = CardId.create(eventData.cardId).unwrap(); + const curatorId = CuratorId.create(eventData.curatorId).unwrap(); + + const event = new CardAddedToLibraryEvent(cardId, curatorId); + // Override the dateTimeOccurred with the serialized value + (event as any).dateTimeOccurred = new Date(eventData.dateTimeOccurred); + + return event; + } + default: + throw new Error(`Unknown event type for deserialization: ${eventData.eventType}`); + } + } +}