refactor modules

This commit is contained in:
sbriat 2023-06-29 10:47:47 +02:00
parent aa06b8c54c
commit 74a855c1b9
3 changed files with 85 additions and 51 deletions

View File

@ -53,7 +53,5 @@ import { HealthModule } from '@modules/health/health.module';
HealthModule,
AdModule,
],
controllers: [],
providers: [],
})
export class AppModule {}

View File

@ -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,24 +25,27 @@ 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';
@Module({
imports: [CqrsModule],
controllers: [CreateAdGrpcController, FindAdByIdGrpcController],
providers: [
CreateAdService,
FindAdByIdQueryHandler,
const grpcControllers = [CreateAdGrpcController, FindAdByIdGrpcController];
const eventHandlers: Provider[] = [
PublishMessageWhenAdIsCreatedDomainEventHandler,
PublishLogMessageWhenAdIsCreatedDomainEventHandler,
PrismaService,
AdMapper,
];
const commandHandlers: Provider[] = [CreateAdService];
const queryHandlers: Provider[] = [FindAdByIdQueryHandler];
const mappers: Provider[] = [AdMapper];
const repositories: Provider[] = [
{
provide: AD_REPOSITORY,
useClass: AdRepository,
},
{
provide: PARAMS_PROVIDER,
useClass: DefaultParamsProvider,
},
];
const messageBrokers: Provider[] = [
{
provide: MESSAGE_BROKER_PUBLISHER,
useClass: MessageBrokerPublisher,
@ -51,6 +54,15 @@ import { PublishLogMessageWhenAdIsCreatedDomainEventHandler } from './core/event
provide: MESSAGE_PUBLISHER,
useClass: MessagePublisher,
},
];
const orms: Provider[] = [PrismaService];
const utilities: Provider[] = [
{
provide: PARAMS_PROVIDER,
useClass: DefaultParamsProvider,
},
{
provide: TIMEZONE_FINDER,
useClass: TimezoneFinder,
@ -59,6 +71,20 @@ import { PublishLogMessageWhenAdIsCreatedDomainEventHandler } from './core/event
provide: TIME_CONVERTER,
useClass: TimeConverter,
},
];
@Module({
imports: [CqrsModule],
controllers: [...grpcControllers],
providers: [
...eventHandlers,
...commandHandlers,
...queryHandlers,
...mappers,
...repositories,
...messageBrokers,
...orms,
...utilities,
],
exports: [
PrismaService,

View File

@ -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,15 +10,20 @@ import { AD_REPOSITORY } from './health.di-tokens';
import { HealthGrpcController } from './interface/grpc-controllers/health.grpc.controller';
import { AdModule } from '@modules/ad/ad.module';
@Module({
imports: [TerminusModule, AdModule],
controllers: [HealthGrpcController, HealthHttpController],
providers: [
RepositoriesHealthIndicatorUseCase,
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,
@ -27,6 +32,11 @@ import { AdModule } from '@modules/ad/ad.module';
provide: MESSAGE_PUBLISHER,
useClass: MessagePublisher,
},
],
];
@Module({
imports: [TerminusModule, AdModule],
controllers: [...grpcControllers, ...httpControllers],
providers: [...useCases, ...repositories, ...messageBrokers],
})
export class HealthModule {}