From 80db21a6d53d7870834890ba7d5dabb9d3fcc826 Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 10 Oct 2023 16:54:01 +0200 Subject: [PATCH] use constants for messager --- src/app.constants.ts | 2 ++ src/app.module.ts | 5 +++-- src/modules/ad/ad.constants.ts | 1 + ...blish-message-when-ad-is-created.domain-event-handler.ts | 6 +++++- ...-message-when-ad-is-created.domain-event-handler.spec.ts | 3 ++- src/modules/messager/messager.module.ts | 3 ++- 6 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 src/app.constants.ts create mode 100644 src/modules/ad/ad.constants.ts diff --git a/src/app.constants.ts b/src/app.constants.ts new file mode 100644 index 0000000..76bbc49 --- /dev/null +++ b/src/app.constants.ts @@ -0,0 +1,2 @@ +export const SERVICE_NAME = 'ad'; +export const HEALTH_CRITICAL_LOGGING_KEY = 'logging.ad.health.crit'; diff --git a/src/app.module.ts b/src/app.module.ts index fc7d7b7..e09025f 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -13,6 +13,7 @@ import { AD_REPOSITORY } from '@modules/ad/ad.di-tokens'; import { MESSAGE_PUBLISHER } from '@modules/messager/messager.di-tokens'; import { HealthModuleOptions } from '@mobicoop/health-module/dist/core/domain/types/health.types'; import { MessagePublisherPort } from '@mobicoop/ddd-library'; +import { HEALTH_CRITICAL_LOGGING_KEY, SERVICE_NAME } from './app.constants'; @Module({ imports: [ @@ -47,8 +48,8 @@ import { MessagePublisherPort } from '@mobicoop/ddd-library'; adRepository: HealthRepositoryPort, messagePublisher: MessagePublisherPort, ): Promise => ({ - serviceName: 'ad', - criticalLoggingKey: 'logging.ad.health.crit', + serviceName: SERVICE_NAME, + criticalLoggingKey: HEALTH_CRITICAL_LOGGING_KEY, checkRepositories: [ { name: 'AdRepository', diff --git a/src/modules/ad/ad.constants.ts b/src/modules/ad/ad.constants.ts new file mode 100644 index 0000000..415925e --- /dev/null +++ b/src/modules/ad/ad.constants.ts @@ -0,0 +1 @@ +export const MESSAGER_AD_CREATED_ROUTING_KEY = 'ad.created'; diff --git a/src/modules/ad/core/application/event-handlers/publish-message-when-ad-is-created.domain-event-handler.ts b/src/modules/ad/core/application/event-handlers/publish-message-when-ad-is-created.domain-event-handler.ts index 70c0d7f..24f72fd 100644 --- a/src/modules/ad/core/application/event-handlers/publish-message-when-ad-is-created.domain-event-handler.ts +++ b/src/modules/ad/core/application/event-handlers/publish-message-when-ad-is-created.domain-event-handler.ts @@ -3,6 +3,7 @@ import { OnEvent } from '@nestjs/event-emitter'; import { AdCreatedDomainEvent } from '../../domain/events/ad-created.domain-events'; import { MessagePublisherPort } from '@mobicoop/ddd-library'; import { AD_MESSAGE_PUBLISHER } from '@modules/ad/ad.di-tokens'; +import { MESSAGER_AD_CREATED_ROUTING_KEY } from '@modules/ad/ad.constants'; @Injectable() export class PublishMessageWhenAdIsCreatedDomainEventHandler { @@ -13,6 +14,9 @@ export class PublishMessageWhenAdIsCreatedDomainEventHandler { @OnEvent(AdCreatedDomainEvent.name, { async: true, promisify: true }) async handle(event: AdCreatedDomainEvent): Promise { - this.messagePublisher.publish('ad.created', JSON.stringify(event)); + this.messagePublisher.publish( + MESSAGER_AD_CREATED_ROUTING_KEY, + JSON.stringify(event), + ); } } diff --git a/src/modules/ad/tests/unit/core/publish-message-when-ad-is-created.domain-event-handler.spec.ts b/src/modules/ad/tests/unit/core/publish-message-when-ad-is-created.domain-event-handler.spec.ts index 144c1b0..3d0386d 100644 --- a/src/modules/ad/tests/unit/core/publish-message-when-ad-is-created.domain-event-handler.spec.ts +++ b/src/modules/ad/tests/unit/core/publish-message-when-ad-is-created.domain-event-handler.spec.ts @@ -3,6 +3,7 @@ import { PublishMessageWhenAdIsCreatedDomainEventHandler } from '@modules/ad/cor import { AdCreatedDomainEvent } from '@modules/ad/core/domain/events/ad-created.domain-events'; import { Test, TestingModule } from '@nestjs/testing'; import { AD_MESSAGE_PUBLISHER } from '@modules/ad/ad.di-tokens'; +import { MESSAGER_AD_CREATED_ROUTING_KEY } from '@modules/ad/ad.constants'; const mockMessagePublisher = { publish: jest.fn().mockImplementation(), @@ -80,7 +81,7 @@ describe('Publish message when ad is created domain event handler', () => { expect(publishMessageWhenAdIsCreatedDomainEventHandler).toBeDefined(); expect(mockMessagePublisher.publish).toHaveBeenCalledTimes(1); expect(mockMessagePublisher.publish).toHaveBeenCalledWith( - 'ad.created', + MESSAGER_AD_CREATED_ROUTING_KEY, '{"id":"some-domain-event-id","aggregateId":"some-aggregate-id","userId":"some-user-id","driver":false,"passenger":true,"frequency":"PUNCTUAL","fromDate":"2023-06-28","toDate":"2023-06-28","schedule":[{"day":3,"time":"07:15","margin":900}],"seatsProposed":3,"seatsRequested":1,"strict":false,"waypoints":[{"position":0,"houseNumber":"5","street":"Avenue Foch","locality":"Nancy","postalCode":"54000","country":"France","lat":48.689445,"lon":6.1765102},{"position":1,"locality":"Paris","postalCode":"75000","country":"France","lat":48.8566,"lon":2.3522}],"metadata":{"timestamp":1687928400000,"correlationId":"some-correlation-id"}}', ); }); diff --git a/src/modules/messager/messager.module.ts b/src/modules/messager/messager.module.ts index a70717a..06dbf73 100644 --- a/src/modules/messager/messager.module.ts +++ b/src/modules/messager/messager.module.ts @@ -6,6 +6,7 @@ import { MessageBrokerPublisher, } from '@mobicoop/message-broker-module'; import { ConfigModule, ConfigService } from '@nestjs/config'; +import { SERVICE_NAME } from '@src/app.constants'; const imports = [ MessageBrokerModule.forRootAsync({ @@ -16,7 +17,7 @@ const imports = [ ): Promise => ({ uri: configService.get('MESSAGE_BROKER_URI'), exchange: configService.get('MESSAGE_BROKER_EXCHANGE'), - name: 'ad', + name: SERVICE_NAME, }), }), ];