use constants for messager
This commit is contained in:
parent
749c542bca
commit
80db21a6d5
|
@ -0,0 +1,2 @@
|
||||||
|
export const SERVICE_NAME = 'ad';
|
||||||
|
export const HEALTH_CRITICAL_LOGGING_KEY = 'logging.ad.health.crit';
|
|
@ -13,6 +13,7 @@ import { AD_REPOSITORY } from '@modules/ad/ad.di-tokens';
|
||||||
import { MESSAGE_PUBLISHER } from '@modules/messager/messager.di-tokens';
|
import { MESSAGE_PUBLISHER } from '@modules/messager/messager.di-tokens';
|
||||||
import { HealthModuleOptions } from '@mobicoop/health-module/dist/core/domain/types/health.types';
|
import { HealthModuleOptions } from '@mobicoop/health-module/dist/core/domain/types/health.types';
|
||||||
import { MessagePublisherPort } from '@mobicoop/ddd-library';
|
import { MessagePublisherPort } from '@mobicoop/ddd-library';
|
||||||
|
import { HEALTH_CRITICAL_LOGGING_KEY, SERVICE_NAME } from './app.constants';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
|
@ -47,8 +48,8 @@ import { MessagePublisherPort } from '@mobicoop/ddd-library';
|
||||||
adRepository: HealthRepositoryPort,
|
adRepository: HealthRepositoryPort,
|
||||||
messagePublisher: MessagePublisherPort,
|
messagePublisher: MessagePublisherPort,
|
||||||
): Promise<HealthModuleOptions> => ({
|
): Promise<HealthModuleOptions> => ({
|
||||||
serviceName: 'ad',
|
serviceName: SERVICE_NAME,
|
||||||
criticalLoggingKey: 'logging.ad.health.crit',
|
criticalLoggingKey: HEALTH_CRITICAL_LOGGING_KEY,
|
||||||
checkRepositories: [
|
checkRepositories: [
|
||||||
{
|
{
|
||||||
name: 'AdRepository',
|
name: 'AdRepository',
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
export const MESSAGER_AD_CREATED_ROUTING_KEY = 'ad.created';
|
|
@ -3,6 +3,7 @@ import { OnEvent } from '@nestjs/event-emitter';
|
||||||
import { AdCreatedDomainEvent } from '../../domain/events/ad-created.domain-events';
|
import { AdCreatedDomainEvent } from '../../domain/events/ad-created.domain-events';
|
||||||
import { MessagePublisherPort } from '@mobicoop/ddd-library';
|
import { MessagePublisherPort } from '@mobicoop/ddd-library';
|
||||||
import { AD_MESSAGE_PUBLISHER } from '@modules/ad/ad.di-tokens';
|
import { AD_MESSAGE_PUBLISHER } from '@modules/ad/ad.di-tokens';
|
||||||
|
import { MESSAGER_AD_CREATED_ROUTING_KEY } from '@modules/ad/ad.constants';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PublishMessageWhenAdIsCreatedDomainEventHandler {
|
export class PublishMessageWhenAdIsCreatedDomainEventHandler {
|
||||||
|
@ -13,6 +14,9 @@ export class PublishMessageWhenAdIsCreatedDomainEventHandler {
|
||||||
|
|
||||||
@OnEvent(AdCreatedDomainEvent.name, { async: true, promisify: true })
|
@OnEvent(AdCreatedDomainEvent.name, { async: true, promisify: true })
|
||||||
async handle(event: AdCreatedDomainEvent): Promise<any> {
|
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),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { PublishMessageWhenAdIsCreatedDomainEventHandler } from '@modules/ad/cor
|
||||||
import { AdCreatedDomainEvent } from '@modules/ad/core/domain/events/ad-created.domain-events';
|
import { AdCreatedDomainEvent } from '@modules/ad/core/domain/events/ad-created.domain-events';
|
||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { AD_MESSAGE_PUBLISHER } from '@modules/ad/ad.di-tokens';
|
import { AD_MESSAGE_PUBLISHER } from '@modules/ad/ad.di-tokens';
|
||||||
|
import { MESSAGER_AD_CREATED_ROUTING_KEY } from '@modules/ad/ad.constants';
|
||||||
|
|
||||||
const mockMessagePublisher = {
|
const mockMessagePublisher = {
|
||||||
publish: jest.fn().mockImplementation(),
|
publish: jest.fn().mockImplementation(),
|
||||||
|
@ -80,7 +81,7 @@ describe('Publish message when ad is created domain event handler', () => {
|
||||||
expect(publishMessageWhenAdIsCreatedDomainEventHandler).toBeDefined();
|
expect(publishMessageWhenAdIsCreatedDomainEventHandler).toBeDefined();
|
||||||
expect(mockMessagePublisher.publish).toHaveBeenCalledTimes(1);
|
expect(mockMessagePublisher.publish).toHaveBeenCalledTimes(1);
|
||||||
expect(mockMessagePublisher.publish).toHaveBeenCalledWith(
|
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"}}',
|
'{"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"}}',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
@ -6,6 +6,7 @@ import {
|
||||||
MessageBrokerPublisher,
|
MessageBrokerPublisher,
|
||||||
} from '@mobicoop/message-broker-module';
|
} from '@mobicoop/message-broker-module';
|
||||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
|
import { SERVICE_NAME } from '@src/app.constants';
|
||||||
|
|
||||||
const imports = [
|
const imports = [
|
||||||
MessageBrokerModule.forRootAsync({
|
MessageBrokerModule.forRootAsync({
|
||||||
|
@ -16,7 +17,7 @@ const imports = [
|
||||||
): Promise<MessageBrokerModuleOptions> => ({
|
): Promise<MessageBrokerModuleOptions> => ({
|
||||||
uri: configService.get<string>('MESSAGE_BROKER_URI'),
|
uri: configService.get<string>('MESSAGE_BROKER_URI'),
|
||||||
exchange: configService.get<string>('MESSAGE_BROKER_EXCHANGE'),
|
exchange: configService.get<string>('MESSAGE_BROKER_EXCHANGE'),
|
||||||
name: 'ad',
|
name: SERVICE_NAME,
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
|
|
Loading…
Reference in New Issue