simplify broker

This commit is contained in:
sbriat
2023-07-03 14:15:27 +02:00
parent 3f72737b09
commit 9f50c3b2f1
9 changed files with 182 additions and 293 deletions

View File

@@ -1,2 +1 @@
export const MESSAGE_BROKER_PUBLISHER = Symbol();
export const MESSAGE_PUBLISHER = Symbol();

View File

@@ -1,10 +1,6 @@
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AdModule } from './modules/ad/ad.module';
import {
MessageBrokerModule,
MessageBrokerModuleOptions,
} from '@mobicoop/message-broker-module';
import {
ConfigurationModule,
ConfigurationModuleOptions,
@@ -12,6 +8,10 @@ import {
import { EventEmitterModule } from '@nestjs/event-emitter';
import { RequestContextModule } from 'nestjs-request-context';
import { HealthModule } from '@modules/health/health.module';
import {
MessageBrokerModule,
MessageBrokerModuleOptions,
} from '@mobicoop/message-broker-module';
@Module({
imports: [

View File

@@ -7,14 +7,9 @@ import {
TIMEZONE_FINDER,
TIME_CONVERTER,
} from './ad.di-tokens';
import {
MESSAGE_BROKER_PUBLISHER,
MESSAGE_PUBLISHER,
} from '@src/app.constants';
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
import { MESSAGE_PUBLISHER } from '@src/app.constants';
import { AdRepository } from './infrastructure/ad.repository';
import { DefaultParamsProvider } from './infrastructure/default-params-provider';
import { MessagePublisher } from './infrastructure/message-publisher';
import { AdMapper } from './ad.mapper';
import { CreateAdService } from './core/application/commands/create-ad/create-ad.service';
import { TimezoneFinder } from './infrastructure/timezone-finder';
@@ -24,6 +19,7 @@ import { FindAdByIdQueryHandler } from './core/application/queries/find-ad-by-id
import { PublishMessageWhenAdIsCreatedDomainEventHandler } from './core/application/event-handlers/publish-message-when-ad-is-created.domain-event-handler';
import { PublishLogMessageWhenAdIsCreatedDomainEventHandler } from './core/application/event-handlers/publish-log-message-when-ad-is-created.domain-event-handler';
import { PrismaService } from './infrastructure/prisma.service';
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
const grpcControllers = [CreateAdGrpcController, FindAdByIdGrpcController];
@@ -46,13 +42,9 @@ const repositories: Provider[] = [
];
const messageBrokers: Provider[] = [
{
provide: MESSAGE_BROKER_PUBLISHER,
useClass: MessageBrokerPublisher,
},
{
provide: MESSAGE_PUBLISHER,
useClass: MessagePublisher,
useClass: MessageBrokerPublisher,
},
];

View File

@@ -1,16 +0,0 @@
import { Inject, Injectable } from '@nestjs/common';
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
import { MESSAGE_BROKER_PUBLISHER } from '@src/app.constants';
import { MessagePublisherPort } from '@mobicoop/ddd-library';
@Injectable()
export class MessagePublisher implements MessagePublisherPort {
constructor(
@Inject(MESSAGE_BROKER_PUBLISHER)
private readonly messageBrokerPublisher: MessageBrokerPublisher,
) {}
publish = (routingKey: string, message: string): void => {
this.messageBrokerPublisher.publish(routingKey, message);
};
}

View File

@@ -1,36 +0,0 @@
import { MessagePublisher } from '@modules/ad/infrastructure/message-publisher';
import { Test, TestingModule } from '@nestjs/testing';
import { MESSAGE_BROKER_PUBLISHER } from '@src/app.constants';
const mockMessageBrokerPublisher = {
publish: jest.fn().mockImplementation(),
};
describe('Messager', () => {
let messagePublisher: MessagePublisher;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [],
providers: [
MessagePublisher,
{
provide: MESSAGE_BROKER_PUBLISHER,
useValue: mockMessageBrokerPublisher,
},
],
}).compile();
messagePublisher = module.get<MessagePublisher>(MessagePublisher);
});
it('should be defined', () => {
expect(messagePublisher).toBeDefined();
});
it('should publish a message', async () => {
jest.spyOn(mockMessageBrokerPublisher, 'publish');
messagePublisher.publish('health.info', 'my-test');
expect(mockMessageBrokerPublisher.publish).toHaveBeenCalledTimes(1);
});
});

View File

@@ -1,14 +1,13 @@
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';
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
import { MessagePublisher } from './infrastructure/message-publisher';
import { MESSAGE_PUBLISHER } from 'src/app.constants';
import { RepositoriesHealthIndicatorUseCase } from './core/application/usecases/repositories.health-indicator.usecase';
import { AdRepository } from '../ad/infrastructure/ad.repository';
import { AD_REPOSITORY } from './health.di-tokens';
import { HealthGrpcController } from './interface/grpc-controllers/health.grpc.controller';
import { AdModule } from '@modules/ad/ad.module';
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
const grpcControllers = [HealthGrpcController];
@@ -24,13 +23,9 @@ const repositories: Provider[] = [
];
const messageBrokers: Provider[] = [
{
provide: MESSAGE_BROKER_PUBLISHER,
useClass: MessageBrokerPublisher,
},
{
provide: MESSAGE_PUBLISHER,
useClass: MessagePublisher,
useClass: MessageBrokerPublisher,
},
];

View File

@@ -1,16 +0,0 @@
import { Inject, Injectable } from '@nestjs/common';
import { MESSAGE_BROKER_PUBLISHER } from '../../../app.constants';
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
import { MessagePublisherPort } from '@mobicoop/ddd-library';
@Injectable()
export class MessagePublisher implements MessagePublisherPort {
constructor(
@Inject(MESSAGE_BROKER_PUBLISHER)
private readonly messageBrokerPublisher: MessageBrokerPublisher,
) {}
publish = (routingKey: string, message: string): void => {
this.messageBrokerPublisher.publish(routingKey, message);
};
}

View File

@@ -1,36 +0,0 @@
import { MessagePublisher } from '@modules/health/infrastructure/message-publisher';
import { Test, TestingModule } from '@nestjs/testing';
import { MESSAGE_BROKER_PUBLISHER } from '@src/app.constants';
const mockMessageBrokerPublisher = {
publish: jest.fn().mockImplementation(),
};
describe('Messager', () => {
let messagePublisher: MessagePublisher;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [],
providers: [
MessagePublisher,
{
provide: MESSAGE_BROKER_PUBLISHER,
useValue: mockMessageBrokerPublisher,
},
],
}).compile();
messagePublisher = module.get<MessagePublisher>(MessagePublisher);
});
it('should be defined', () => {
expect(messagePublisher).toBeDefined();
});
it('should publish a message', async () => {
jest.spyOn(mockMessageBrokerPublisher, 'publish');
messagePublisher.publish('health.info', 'my-test');
expect(mockMessageBrokerPublisher.publish).toHaveBeenCalledTimes(1);
});
});