use constants for messager

This commit is contained in:
sbriat 2023-10-10 16:54:01 +02:00
parent 749c542bca
commit 80db21a6d5
6 changed files with 15 additions and 5 deletions

2
src/app.constants.ts Normal file
View File

@ -0,0 +1,2 @@
export const SERVICE_NAME = 'ad';
export const HEALTH_CRITICAL_LOGGING_KEY = 'logging.ad.health.crit';

View File

@ -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<HealthModuleOptions> => ({
serviceName: 'ad',
criticalLoggingKey: 'logging.ad.health.crit',
serviceName: SERVICE_NAME,
criticalLoggingKey: HEALTH_CRITICAL_LOGGING_KEY,
checkRepositories: [
{
name: 'AdRepository',

View File

@ -0,0 +1 @@
export const MESSAGER_AD_CREATED_ROUTING_KEY = 'ad.created';

View File

@ -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<any> {
this.messagePublisher.publish('ad.created', JSON.stringify(event));
this.messagePublisher.publish(
MESSAGER_AD_CREATED_ROUTING_KEY,
JSON.stringify(event),
);
}
}

View File

@ -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"}}',
);
});

View File

@ -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<MessageBrokerModuleOptions> => ({
uri: configService.get<string>('MESSAGE_BROKER_URI'),
exchange: configService.get<string>('MESSAGE_BROKER_EXCHANGE'),
name: 'ad',
name: SERVICE_NAME,
}),
}),
];