diff --git a/src/app.module.ts b/src/app.module.ts index a457cad..a2b6bc2 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -53,7 +53,5 @@ import { HealthModule } from '@modules/health/health.module'; HealthModule, AdModule, ], - controllers: [], - providers: [], }) export class AppModule {} diff --git a/src/modules/ad/ad.module.ts b/src/modules/ad/ad.module.ts index 15bc8fb..5a5e3ce 100644 --- a/src/modules/ad/ad.module.ts +++ b/src/modules/ad/ad.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { Module, Provider } from '@nestjs/common'; import { CreateAdGrpcController } from './interface/grpc-controllers/create-ad.grpc.controller'; import { CqrsModule } from '@nestjs/cqrs'; import { @@ -25,40 +25,66 @@ import { FindAdByIdQueryHandler } from './core/queries/find-ad-by-id/find-ad-by- import { PublishMessageWhenAdIsCreatedDomainEventHandler } from './core/event-handlers/publish-message-when-ad-is-created.domain-event-handler'; import { PublishLogMessageWhenAdIsCreatedDomainEventHandler } from './core/event-handlers/publish-log-message-when-ad-is-created.domain-event-handler'; +const grpcControllers = [CreateAdGrpcController, FindAdByIdGrpcController]; + +const eventHandlers: Provider[] = [ + PublishMessageWhenAdIsCreatedDomainEventHandler, + PublishLogMessageWhenAdIsCreatedDomainEventHandler, +]; + +const commandHandlers: Provider[] = [CreateAdService]; + +const queryHandlers: Provider[] = [FindAdByIdQueryHandler]; + +const mappers: Provider[] = [AdMapper]; + +const repositories: Provider[] = [ + { + provide: AD_REPOSITORY, + useClass: AdRepository, + }, +]; + +const messageBrokers: Provider[] = [ + { + provide: MESSAGE_BROKER_PUBLISHER, + useClass: MessageBrokerPublisher, + }, + { + provide: MESSAGE_PUBLISHER, + useClass: MessagePublisher, + }, +]; + +const orms: Provider[] = [PrismaService]; + +const utilities: Provider[] = [ + { + provide: PARAMS_PROVIDER, + useClass: DefaultParamsProvider, + }, + { + provide: TIMEZONE_FINDER, + useClass: TimezoneFinder, + }, + { + provide: TIME_CONVERTER, + useClass: TimeConverter, + }, +]; + @Module({ imports: [CqrsModule], - controllers: [CreateAdGrpcController, FindAdByIdGrpcController], + controllers: [...grpcControllers], providers: [ - CreateAdService, - FindAdByIdQueryHandler, - PublishMessageWhenAdIsCreatedDomainEventHandler, - PublishLogMessageWhenAdIsCreatedDomainEventHandler, - PrismaService, - AdMapper, - { - provide: AD_REPOSITORY, - useClass: AdRepository, - }, - { - provide: PARAMS_PROVIDER, - useClass: DefaultParamsProvider, - }, - { - provide: MESSAGE_BROKER_PUBLISHER, - useClass: MessageBrokerPublisher, - }, - { - provide: MESSAGE_PUBLISHER, - useClass: MessagePublisher, - }, - { - provide: TIMEZONE_FINDER, - useClass: TimezoneFinder, - }, - { - provide: TIME_CONVERTER, - useClass: TimeConverter, - }, + ...eventHandlers, + ...commandHandlers, + ...queryHandlers, + ...mappers, + ...repositories, + ...messageBrokers, + ...orms, + ...utilities, ], exports: [ PrismaService, diff --git a/src/modules/health/health.module.ts b/src/modules/health/health.module.ts index 6b04c26..988cd14 100644 --- a/src/modules/health/health.module.ts +++ b/src/modules/health/health.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { Module, Provider } from '@nestjs/common'; import { HealthHttpController } from './interface/http-controllers/health.http.controller'; import { TerminusModule } from '@nestjs/terminus'; import { MESSAGE_BROKER_PUBLISHER, MESSAGE_PUBLISHER } from 'src/app.constants'; @@ -10,23 +10,33 @@ import { AD_REPOSITORY } from './health.di-tokens'; import { HealthGrpcController } from './interface/grpc-controllers/health.grpc.controller'; import { AdModule } from '@modules/ad/ad.module'; +const grpcControllers = [HealthGrpcController]; + +const httpControllers = [HealthHttpController]; + +const useCases: Provider[] = [RepositoriesHealthIndicatorUseCase]; + +const repositories: Provider[] = [ + { + provide: AD_REPOSITORY, + useClass: AdRepository, + }, +]; + +const messageBrokers: Provider[] = [ + { + provide: MESSAGE_BROKER_PUBLISHER, + useClass: MessageBrokerPublisher, + }, + { + provide: MESSAGE_PUBLISHER, + useClass: MessagePublisher, + }, +]; + @Module({ imports: [TerminusModule, AdModule], - controllers: [HealthGrpcController, HealthHttpController], - providers: [ - RepositoriesHealthIndicatorUseCase, - { - provide: AD_REPOSITORY, - useClass: AdRepository, - }, - { - provide: MESSAGE_BROKER_PUBLISHER, - useClass: MessageBrokerPublisher, - }, - { - provide: MESSAGE_PUBLISHER, - useClass: MessagePublisher, - }, - ], + controllers: [...grpcControllers, ...httpControllers], + providers: [...useCases, ...repositories, ...messageBrokers], }) export class HealthModule {}