diff --git a/src/modules/search/application/eventHandlers/CardAddedToLibraryEventHandler.ts b/src/modules/search/application/eventHandlers/CardAddedToLibraryEventHandler.ts index 330eaf2c..8ef7656a 100644 --- a/src/modules/search/application/eventHandlers/CardAddedToLibraryEventHandler.ts +++ b/src/modules/search/application/eventHandlers/CardAddedToLibraryEventHandler.ts @@ -40,7 +40,6 @@ export class CardAddedToLibraryEventHandler // Index the URL for search const indexResult = await this.indexUrlForSearchUseCase.execute({ url: card.url.value, - cardId: event.cardId.getStringValue(), }); if (indexResult.isErr()) { diff --git a/src/modules/search/application/eventHandlers/ConnectionCreatedEventHandler.ts b/src/modules/search/application/eventHandlers/ConnectionCreatedEventHandler.ts new file mode 100644 index 00000000..ca85c1e1 --- /dev/null +++ b/src/modules/search/application/eventHandlers/ConnectionCreatedEventHandler.ts @@ -0,0 +1,69 @@ +import { ConnectionCreatedEvent } from '../../../cards/domain/events/ConnectionCreatedEvent'; +import { IEventHandler } from '../../../../shared/application/events/IEventSubscriber'; +import { Result, ok } from '../../../../shared/core/Result'; +import { IndexUrlForSearchUseCase } from '../useCases/commands/IndexUrlForSearchUseCase'; +import { IConnectionRepository } from '../../../cards/domain/IConnectionRepository'; + +export class ConnectionCreatedEventHandler + implements IEventHandler +{ + constructor( + private indexUrlForSearchUseCase: IndexUrlForSearchUseCase, + private connectionRepository: IConnectionRepository, + ) {} + + async handle(event: ConnectionCreatedEvent): Promise> { + // Get connection details + const connectionResult = await this.connectionRepository.findById( + event.connectionId, + ); + if (connectionResult.isErr()) { + console.error( + 'Failed to find connection for search indexing:', + connectionResult.error, + ); + return ok(undefined); // Don't fail the event processing + } + + const connection = connectionResult.value; + if (!connection) { + console.warn( + 'Connection not found for search indexing:', + event.connectionId.getStringValue(), + ); + return ok(undefined); + } + + // Index source URL if it exists + if (connection.source.url) { + const sourceIndexResult = await this.indexUrlForSearchUseCase.execute({ + url: connection.source.url.value, + }); + + if (sourceIndexResult.isErr()) { + console.error( + 'Failed to index source URL for search:', + sourceIndexResult.error, + ); + // Don't fail the event processing - search indexing is not critical + } + } + + // Index target URL if it exists + if (connection.target.url) { + const targetIndexResult = await this.indexUrlForSearchUseCase.execute({ + url: connection.target.url.value, + }); + + if (targetIndexResult.isErr()) { + console.error( + 'Failed to index target URL for search:', + targetIndexResult.error, + ); + // Don't fail the event processing - search indexing is not critical + } + } + + return ok(undefined); + } +} diff --git a/src/modules/search/application/useCases/commands/IndexUrlForSearchUseCase.ts b/src/modules/search/application/useCases/commands/IndexUrlForSearchUseCase.ts index 1da80aa3..7aedfb12 100644 --- a/src/modules/search/application/useCases/commands/IndexUrlForSearchUseCase.ts +++ b/src/modules/search/application/useCases/commands/IndexUrlForSearchUseCase.ts @@ -7,7 +7,6 @@ import { SearchService } from '../../../domain/services/SearchService'; export interface IndexUrlForSearchDTO { url: string; - cardId: string; } export interface IndexUrlForSearchResponseDTO { diff --git a/src/shared/infrastructure/events/BullMQEventPublisher.ts b/src/shared/infrastructure/events/BullMQEventPublisher.ts index 767ac48a..5a4489cd 100644 --- a/src/shared/infrastructure/events/BullMQEventPublisher.ts +++ b/src/shared/infrastructure/events/BullMQEventPublisher.ts @@ -94,7 +94,7 @@ export class BullMQEventPublisher implements IEventPublisher { case EventNames.USER_UNFOLLOWED_TARGET: return [QueueNames.NOTIFICATIONS]; case EventNames.CONNECTION_CREATED: - return [QueueNames.FEEDS, QueueNames.NOTIFICATIONS]; + return [QueueNames.FEEDS, QueueNames.NOTIFICATIONS, QueueNames.SEARCH]; case EventNames.CONNECTION_REMOVED: return [QueueNames.NOTIFICATIONS]; default: diff --git a/src/shared/infrastructure/processes/SearchWorkerProcess.ts b/src/shared/infrastructure/processes/SearchWorkerProcess.ts index 0d67c24c..32770fcb 100644 --- a/src/shared/infrastructure/processes/SearchWorkerProcess.ts +++ b/src/shared/infrastructure/processes/SearchWorkerProcess.ts @@ -5,6 +5,7 @@ import { } from '../http/factories/ServiceFactory'; import { UseCaseFactory } from '../http/factories/UseCaseFactory'; import { CardAddedToLibraryEventHandler } from '../../../modules/search/application/eventHandlers/CardAddedToLibraryEventHandler'; +import { ConnectionCreatedEventHandler } from '../../../modules/search/application/eventHandlers/ConnectionCreatedEventHandler'; import { QueueNames } from '../events/QueueConfig'; import { EventNames } from '../events/EventConfig'; import { BaseWorkerProcess } from './BaseWorkerProcess'; @@ -51,5 +52,15 @@ export class SearchWorkerProcess extends BaseWorkerProcess { EventNames.CARD_ADDED_TO_LIBRARY, cardAddedToLibraryHandler, ); + + const connectionCreatedHandler = new ConnectionCreatedEventHandler( + useCases.indexUrlForSearchUseCase, + repositories.connectionRepository, + ); + + await subscriber.subscribe( + EventNames.CONNECTION_CREATED, + connectionCreatedHandler, + ); } }