Files
goodgo-platform/apps/api/src/modules/search/search.module.ts
Ho Ngoc Hai 0329455e9a feat(listings): add user-facing scam/abuse report flow (GOO-19)
- Add ListingFlag model with FlagReason enum (SCAM, DUPLICATE, WRONG_INFO, ALREADY_SOLD, INAPPROPRIATE)
- Add POST /listings/:id/report endpoint with rate limiting and duplicate prevention
- Auto-flag listings with ≥3 reports to PENDING_REVIEW for moderator review
- Add GET /admin/flagged-listings endpoint for admin moderation queue
- Add "Báo cáo" button + modal on listing detail page (Vietnamese UI)
- Add Prisma migration for listing_flags table with unique constraint per user/listing

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-23 00:19:12 +07:00

82 lines
4.1 KiB
TypeScript

import { Module, type OnModuleInit } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { makeCounterProvider } from '@willsoto/nestjs-prometheus';
import { LoggerService, TypesenseClientService } from '@modules/shared';
import { SubscriptionsModule } from '@modules/subscriptions';
import { CreateSavedSearchHandler } from './application/commands/create-saved-search/create-saved-search.handler';
import { DeleteSavedSearchHandler } from './application/commands/delete-saved-search/delete-saved-search.handler';
import { ReindexAllHandler } from './application/commands/reindex-all/reindex-all.handler';
import { SyncListingHandler } from './application/commands/sync-listing/sync-listing.handler';
import { UpdateSavedSearchHandler } from './application/commands/update-saved-search/update-saved-search.handler';
import { GeoSearchHandler } from './application/queries/geo-search/geo-search.handler';
import { GetSavedSearchHandler } from './application/queries/get-saved-search/get-saved-search.handler';
import { GetSavedSearchesHandler } from './application/queries/get-saved-searches/get-saved-searches.handler';
import { SearchPropertiesHandler } from './application/queries/search-properties/search-properties.handler';
import { SEARCH_REPOSITORY } from './domain/repositories/search.repository';
import { SavedSearchAlertCronService } from './infrastructure/cron/saved-search-alert-cron.service';
import { ListingApprovedEventHandler } from './infrastructure/event-handlers/listing-approved.handler';
import { ListingFeaturedExpiredHandler } from './infrastructure/event-handlers/listing-featured-expired.handler';
import { ListingStatusChangedHandler } from './infrastructure/event-handlers/listing-status-changed.handler';
import { SavedSearchAlertHandler } from './infrastructure/event-handlers/saved-search-alert.handler';
import { ListingIndexerService } from './infrastructure/services/listing-indexer.service';
import { PostgresSearchRepository } from './infrastructure/services/postgres-search.repository';
import { ResilientSearchRepository, SEARCH_DEGRADATION_TOTAL } from './infrastructure/services/resilient-search.repository';
import { TypesenseSearchRepository } from './infrastructure/services/typesense-search.repository';
import { SavedSearchController } from './presentation/controllers/saved-search.controller';
import { SearchController } from './presentation/controllers/search.controller';
const CommandHandlers = [SyncListingHandler, ReindexAllHandler, CreateSavedSearchHandler, DeleteSavedSearchHandler, UpdateSavedSearchHandler];
const QueryHandlers = [SearchPropertiesHandler, GeoSearchHandler, GetSavedSearchesHandler, GetSavedSearchHandler];
@Module({
imports: [CqrsModule, SubscriptionsModule],
controllers: [SearchController, SavedSearchController],
providers: [
// Infrastructure
TypesenseSearchRepository,
PostgresSearchRepository,
ResilientSearchRepository,
{ provide: SEARCH_REPOSITORY, useExisting: ResilientSearchRepository },
ListingIndexerService,
// Metrics
makeCounterProvider({
name: SEARCH_DEGRADATION_TOTAL,
help: 'Total search degradation events (Typesense circuit breaker)',
labelNames: ['service', 'event'],
}),
// Event handlers
ListingApprovedEventHandler,
ListingFeaturedExpiredHandler,
ListingStatusChangedHandler,
SavedSearchAlertHandler,
// Cron jobs
SavedSearchAlertCronService,
// CQRS
...CommandHandlers,
...QueryHandlers,
],
exports: [ListingIndexerService, SEARCH_REPOSITORY],
})
export class SearchModule implements OnModuleInit {
constructor(
private readonly searchRepo: ResilientSearchRepository,
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.warn(
`Typesense collection initialization failed: ${err instanceof Error ? err.message : String(err)} — PostgreSQL fallback is active`,
'SearchModule',
);
}
}
}