- TypesenseClient service with configurable connection - Collection schema for listings with facets, geo-point, and Vietnamese text - ListingIndexer service with PostGIS coordinate extraction for geo search - CQRS commands: SyncListing, ReindexAll (batch with pagination) - CQRS queries: SearchProperties (filters, sorting), GeoSearch (radius) - Event handlers for listing.approved/updated/deactivated auto-sync - REST endpoints: GET /search, GET /search/geo, POST /search/reindex (admin) - DTOs with class-validator validation and pagination Co-Authored-By: Paperclip <noreply@paperclip.ing>
64 lines
2.3 KiB
TypeScript
64 lines
2.3 KiB
TypeScript
import { Module, type OnModuleInit } from '@nestjs/common';
|
|
import { CqrsModule } from '@nestjs/cqrs';
|
|
|
|
// Domain
|
|
import { SEARCH_REPOSITORY } from './domain/repositories/search.repository';
|
|
|
|
// Infrastructure
|
|
import { TypesenseClientService } from './infrastructure/services/typesense-client.service';
|
|
import { TypesenseSearchRepository } from './infrastructure/services/typesense-search.repository';
|
|
import { ListingIndexerService } from './infrastructure/services/listing-indexer.service';
|
|
import { ListingApprovedEventHandler } from './infrastructure/event-handlers/listing-approved.handler';
|
|
|
|
// Application
|
|
import { SyncListingHandler } from './application/commands/sync-listing/sync-listing.handler';
|
|
import { ReindexAllHandler } from './application/commands/reindex-all/reindex-all.handler';
|
|
import { SearchPropertiesHandler } from './application/queries/search-properties/search-properties.handler';
|
|
import { GeoSearchHandler } from './application/queries/geo-search/geo-search.handler';
|
|
|
|
// Presentation
|
|
import { SearchController } from './presentation/controllers/search.controller';
|
|
|
|
import { LoggerService } from '@modules/shared/infrastructure/logger.service';
|
|
|
|
const CommandHandlers = [SyncListingHandler, ReindexAllHandler];
|
|
const QueryHandlers = [SearchPropertiesHandler, GeoSearchHandler];
|
|
|
|
@Module({
|
|
imports: [CqrsModule],
|
|
controllers: [SearchController],
|
|
providers: [
|
|
// Infrastructure
|
|
TypesenseClientService,
|
|
{ provide: SEARCH_REPOSITORY, useClass: TypesenseSearchRepository },
|
|
ListingIndexerService,
|
|
|
|
// Event handlers
|
|
ListingApprovedEventHandler,
|
|
|
|
// CQRS
|
|
...CommandHandlers,
|
|
...QueryHandlers,
|
|
],
|
|
exports: [ListingIndexerService, SEARCH_REPOSITORY],
|
|
})
|
|
export class SearchModule implements OnModuleInit {
|
|
constructor(
|
|
private readonly typesenseClient: TypesenseClientService,
|
|
private readonly searchRepo: TypesenseSearchRepository,
|
|
private readonly logger: LoggerService,
|
|
) {}
|
|
|
|
async onModuleInit(): Promise<void> {
|
|
try {
|
|
await this.searchRepo.ensureCollection();
|
|
this.logger.log('Search module initialized — Typesense collection ready', 'SearchModule');
|
|
} catch (err) {
|
|
this.logger.error(
|
|
`Failed to initialize Typesense collection: ${err instanceof Error ? err.message : String(err)}`,
|
|
'SearchModule',
|
|
);
|
|
}
|
|
}
|
|
}
|