Merge branch 'refactorMessager' into 'main'
refactor message broker See merge request v3/service/auth!22
This commit is contained in:
commit
87e142fcfd
|
@ -7,6 +7,7 @@ DATABASE_URL="postgresql://auth:auth@v3-auth-db:5432/auth?schema=public"
|
|||
|
||||
# RABBIT MQ
|
||||
RMQ_URI=amqp://v3-broker:5672
|
||||
RMQ_EXCHANGE=mobicoop
|
||||
|
||||
# POSTGRES
|
||||
POSTGRES_IMAGE=postgres:15.0
|
||||
|
|
|
@ -5,9 +5,6 @@ SERVICE_PORT=5002
|
|||
# PRISMA
|
||||
DATABASE_URL="postgresql://auth:auth@localhost:5602/auth?schema=public"
|
||||
|
||||
# RABBIT MQ
|
||||
RMQ_URI=amqp://v3-broker:5672
|
||||
|
||||
# POSTGRES
|
||||
POSTGRES_IMAGE=postgres:15.0
|
||||
|
||||
|
|
|
@ -12,9 +12,7 @@ export class AuthenticationMessagerController {
|
|||
constructor(private readonly _commandBus: CommandBus) {}
|
||||
|
||||
@RabbitSubscribe({
|
||||
exchange: 'user',
|
||||
routingKey: 'update',
|
||||
queue: 'authentication-user-update',
|
||||
name: 'user-update',
|
||||
})
|
||||
public async userUpdatedHandler(message: string) {
|
||||
const updatedUser = JSON.parse(message);
|
||||
|
@ -40,9 +38,7 @@ export class AuthenticationMessagerController {
|
|||
}
|
||||
|
||||
@RabbitSubscribe({
|
||||
exchange: 'user',
|
||||
routingKey: 'delete',
|
||||
queue: 'authentication-user-delete',
|
||||
name: 'user-delete',
|
||||
})
|
||||
public async userDeletedHandler(message: string) {
|
||||
const deletedUser = JSON.parse(message);
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { IMessageBroker } from '../../domain/interfaces/message-broker';
|
||||
|
||||
@Injectable()
|
||||
export class LoggingMessager extends IMessageBroker {
|
||||
constructor(private readonly _amqpConnection: AmqpConnection) {
|
||||
super('logging');
|
||||
}
|
||||
|
||||
publish(routingKey: string, message: string): void {
|
||||
this._amqpConnection.publish(this.exchange, routingKey, message);
|
||||
}
|
||||
}
|
|
@ -1,11 +1,15 @@
|
|||
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { IMessageBroker } from '../../domain/interfaces/message-broker';
|
||||
|
||||
@Injectable()
|
||||
export class AuthenticationMessager extends IMessageBroker {
|
||||
constructor(private readonly _amqpConnection: AmqpConnection) {
|
||||
super('auth');
|
||||
export class Messager extends IMessageBroker {
|
||||
constructor(
|
||||
private readonly _amqpConnection: AmqpConnection,
|
||||
configService: ConfigService,
|
||||
) {
|
||||
super(configService.get<string>('RMQ_EXCHANGE'));
|
||||
}
|
||||
|
||||
publish(routingKey: string, message: string): void {
|
|
@ -15,7 +15,7 @@ import { DeleteAuthenticationUseCase } from './domain/usecases/delete-authentica
|
|||
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { AuthenticationMessagerController } from './adapters/primaries/authentication-messager.controller';
|
||||
import { LoggingMessager } from './adapters/secondaries/logging.messager';
|
||||
import { Messager } from './adapters/secondaries/messager';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
@ -27,14 +27,20 @@ import { LoggingMessager } from './adapters/secondaries/logging.messager';
|
|||
useFactory: async (configService: ConfigService) => ({
|
||||
exchanges: [
|
||||
{
|
||||
name: 'user',
|
||||
type: 'topic',
|
||||
},
|
||||
{
|
||||
name: 'logging',
|
||||
name: configService.get<string>('RMQ_EXCHANGE'),
|
||||
type: 'topic',
|
||||
},
|
||||
],
|
||||
handlers: {
|
||||
userUpdate: {
|
||||
exchange: configService.get<string>('RMQ_EXCHANGE'),
|
||||
routingKey: 'user.update',
|
||||
},
|
||||
userDelete: {
|
||||
exchange: configService.get<string>('RMQ_EXCHANGE'),
|
||||
routingKey: 'user.delete',
|
||||
},
|
||||
},
|
||||
uri: configService.get<string>('RMQ_URI'),
|
||||
connectionInitOptions: { wait: false },
|
||||
enableControllerDiscovery: true,
|
||||
|
@ -46,7 +52,7 @@ import { LoggingMessager } from './adapters/secondaries/logging.messager';
|
|||
AuthenticationProfile,
|
||||
UsernameProfile,
|
||||
AuthenticationRepository,
|
||||
LoggingMessager,
|
||||
Messager,
|
||||
ValidateAuthenticationUseCase,
|
||||
CreateAuthenticationUseCase,
|
||||
AddUsernameUseCase,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { CommandHandler } from '@nestjs/cqrs';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
import { UsernameRepository } from '../../adapters/secondaries/username.repository';
|
||||
import { AddUsernameCommand } from '../../commands/add-username.command';
|
||||
import { Username } from '../entities/username';
|
||||
|
@ -8,7 +8,7 @@ import { Username } from '../entities/username';
|
|||
export class AddUsernameUseCase {
|
||||
constructor(
|
||||
private readonly _usernameRepository: UsernameRepository,
|
||||
private readonly _loggingMessager: LoggingMessager,
|
||||
private readonly _messager: Messager,
|
||||
) {}
|
||||
|
||||
async execute(command: AddUsernameCommand): Promise<Username> {
|
||||
|
@ -20,8 +20,8 @@ export class AddUsernameUseCase {
|
|||
username,
|
||||
});
|
||||
} catch (error) {
|
||||
this._loggingMessager.publish(
|
||||
'auth.username.add.warning',
|
||||
this._messager.publish(
|
||||
'logging.auth.username.add.warning',
|
||||
JSON.stringify({
|
||||
command,
|
||||
error,
|
||||
|
|
|
@ -4,14 +4,14 @@ import { CreateAuthenticationCommand } from '../../commands/create-authenticatio
|
|||
import { Authentication } from '../entities/authentication';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { UsernameRepository } from '../../adapters/secondaries/username.repository';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
|
||||
@CommandHandler(CreateAuthenticationCommand)
|
||||
export class CreateAuthenticationUseCase {
|
||||
constructor(
|
||||
private readonly _authenticationRepository: AuthenticationRepository,
|
||||
private readonly _usernameRepository: UsernameRepository,
|
||||
private readonly _loggingMessager: LoggingMessager,
|
||||
private readonly _messager: Messager,
|
||||
) {}
|
||||
|
||||
async execute(command: CreateAuthenticationCommand): Promise<Authentication> {
|
||||
|
@ -31,8 +31,8 @@ export class CreateAuthenticationUseCase {
|
|||
|
||||
return auth;
|
||||
} catch (error) {
|
||||
this._loggingMessager.publish(
|
||||
'auth.create.crit',
|
||||
this._messager.publish(
|
||||
'logging.auth.create.crit',
|
||||
JSON.stringify({
|
||||
command,
|
||||
error,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { CommandHandler } from '@nestjs/cqrs';
|
||||
import { AuthenticationRepository } from '../../adapters/secondaries/authentication.repository';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
import { UsernameRepository } from '../../adapters/secondaries/username.repository';
|
||||
import { DeleteAuthenticationCommand } from '../../commands/delete-authentication.command';
|
||||
|
||||
|
@ -9,7 +9,7 @@ export class DeleteAuthenticationUseCase {
|
|||
constructor(
|
||||
private readonly _authenticationRepository: AuthenticationRepository,
|
||||
private readonly _usernameRepository: UsernameRepository,
|
||||
private readonly _loggingMessager: LoggingMessager,
|
||||
private readonly _messager: Messager,
|
||||
) {}
|
||||
|
||||
async execute(command: DeleteAuthenticationCommand) {
|
||||
|
@ -21,8 +21,8 @@ export class DeleteAuthenticationUseCase {
|
|||
command.deleteAuthenticationRequest.uuid,
|
||||
);
|
||||
} catch (error) {
|
||||
this._loggingMessager.publish(
|
||||
'auth.delete.crit',
|
||||
this._messager.publish(
|
||||
'logging.auth.delete.crit',
|
||||
JSON.stringify({
|
||||
command,
|
||||
error,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { UnauthorizedException } from '@nestjs/common';
|
||||
import { CommandHandler } from '@nestjs/cqrs';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
import { UsernameRepository } from '../../adapters/secondaries/username.repository';
|
||||
import { DeleteUsernameCommand } from '../../commands/delete-username.command';
|
||||
|
||||
|
@ -8,7 +8,7 @@ import { DeleteUsernameCommand } from '../../commands/delete-username.command';
|
|||
export class DeleteUsernameUseCase {
|
||||
constructor(
|
||||
private readonly _usernameRepository: UsernameRepository,
|
||||
private readonly _loggingMessager: LoggingMessager,
|
||||
private readonly _messager: Messager,
|
||||
) {}
|
||||
|
||||
async execute(command: DeleteUsernameCommand) {
|
||||
|
@ -25,8 +25,8 @@ export class DeleteUsernameUseCase {
|
|||
}
|
||||
throw new UnauthorizedException();
|
||||
} catch (error) {
|
||||
this._loggingMessager.publish(
|
||||
'auth.username.delete.warning',
|
||||
this._messager.publish(
|
||||
'logging.auth.username.delete.warning',
|
||||
JSON.stringify({
|
||||
command,
|
||||
error,
|
||||
|
|
|
@ -3,13 +3,13 @@ import { AuthenticationRepository } from '../../adapters/secondaries/authenticat
|
|||
import { Authentication } from '../entities/authentication';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { UpdatePasswordCommand } from '../../commands/update-password.command';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
|
||||
@CommandHandler(UpdatePasswordCommand)
|
||||
export class UpdatePasswordUseCase {
|
||||
constructor(
|
||||
private readonly _authenticationRepository: AuthenticationRepository,
|
||||
private readonly _loggingMessager: LoggingMessager,
|
||||
private readonly _messager: Messager,
|
||||
) {}
|
||||
|
||||
async execute(command: UpdatePasswordCommand): Promise<Authentication> {
|
||||
|
@ -21,8 +21,8 @@ export class UpdatePasswordUseCase {
|
|||
password: hash,
|
||||
});
|
||||
} catch (error) {
|
||||
this._loggingMessager.publish(
|
||||
'auth.password.update.warning',
|
||||
this._messager.publish(
|
||||
'logging.auth.password.update.warning',
|
||||
JSON.stringify({
|
||||
command,
|
||||
error,
|
||||
|
|
|
@ -2,7 +2,7 @@ import { Mapper } from '@automapper/core';
|
|||
import { InjectMapper } from '@automapper/nestjs';
|
||||
import { BadRequestException } from '@nestjs/common';
|
||||
import { CommandBus, CommandHandler } from '@nestjs/cqrs';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
import { UsernameRepository } from '../../adapters/secondaries/username.repository';
|
||||
import { AddUsernameCommand } from '../../commands/add-username.command';
|
||||
import { UpdateUsernameCommand } from '../../commands/update-username.command';
|
||||
|
@ -16,7 +16,7 @@ export class UpdateUsernameUseCase {
|
|||
private readonly _usernameRepository: UsernameRepository,
|
||||
private readonly _commandBus: CommandBus,
|
||||
@InjectMapper() private readonly _mapper: Mapper,
|
||||
private readonly _loggingMessager: LoggingMessager,
|
||||
private readonly _messager: Messager,
|
||||
) {}
|
||||
|
||||
async execute(command: UpdateUsernameCommand): Promise<Username> {
|
||||
|
@ -41,8 +41,8 @@ export class UpdateUsernameUseCase {
|
|||
},
|
||||
);
|
||||
} catch (error) {
|
||||
this._loggingMessager.publish(
|
||||
'auth.username.update.warning',
|
||||
this._messager.publish(
|
||||
'logging.auth.username.update.warning',
|
||||
JSON.stringify({
|
||||
command,
|
||||
error,
|
||||
|
|
|
@ -8,7 +8,7 @@ import { Type } from '../../domain/dtos/type.enum';
|
|||
import { AddUsernameRequest } from '../../domain/dtos/add-username.request';
|
||||
import { AddUsernameCommand } from '../../commands/add-username.command';
|
||||
import { AddUsernameUseCase } from '../../domain/usecases/add-username.usecase';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
|
||||
const addUsernameRequest: AddUsernameRequest = {
|
||||
uuid: 'bb281075-1b98-4456-89d6-c643d3044a91',
|
||||
|
@ -46,7 +46,7 @@ describe('AddUsernameUseCase', () => {
|
|||
useValue: mockUsernameRepository,
|
||||
},
|
||||
{
|
||||
provide: LoggingMessager,
|
||||
provide: Messager,
|
||||
useValue: mockMessager,
|
||||
},
|
||||
AddUsernameUseCase,
|
||||
|
|
|
@ -9,7 +9,7 @@ import { CreateAuthenticationUseCase } from '../../domain/usecases/create-authen
|
|||
import * as bcrypt from 'bcrypt';
|
||||
import { UsernameRepository } from '../../adapters/secondaries/username.repository';
|
||||
import { Type } from '../../domain/dtos/type.enum';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
|
||||
const newAuthenticationRequest: CreateAuthenticationRequest =
|
||||
new CreateAuthenticationRequest();
|
||||
|
@ -62,7 +62,7 @@ describe('CreateAuthenticationUseCase', () => {
|
|||
useValue: mockUsernameRepository,
|
||||
},
|
||||
{
|
||||
provide: LoggingMessager,
|
||||
provide: Messager,
|
||||
useValue: mockMessager,
|
||||
},
|
||||
CreateAuthenticationUseCase,
|
||||
|
|
|
@ -2,7 +2,7 @@ import { classes } from '@automapper/classes';
|
|||
import { AutomapperModule } from '@automapper/nestjs';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AuthenticationRepository } from '../../adapters/secondaries/authentication.repository';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
import { UsernameRepository } from '../../adapters/secondaries/username.repository';
|
||||
import { DeleteAuthenticationCommand } from '../../commands/delete-authentication.command';
|
||||
import { DeleteAuthenticationRequest } from '../../domain/dtos/delete-authentication.request';
|
||||
|
@ -69,7 +69,7 @@ describe('DeleteAuthenticationUseCase', () => {
|
|||
useValue: mockUsernameRepository,
|
||||
},
|
||||
{
|
||||
provide: LoggingMessager,
|
||||
provide: Messager,
|
||||
useValue: mockMessager,
|
||||
},
|
||||
DeleteAuthenticationUseCase,
|
||||
|
|
|
@ -2,7 +2,7 @@ import { classes } from '@automapper/classes';
|
|||
import { AutomapperModule } from '@automapper/nestjs';
|
||||
import { UnauthorizedException } from '@nestjs/common';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
import { UsernameRepository } from '../../adapters/secondaries/username.repository';
|
||||
import { DeleteUsernameCommand } from '../../commands/delete-username.command';
|
||||
import { DeleteUsernameRequest } from '../../domain/dtos/delete-username.request';
|
||||
|
@ -82,7 +82,7 @@ describe('DeleteUsernameUseCase', () => {
|
|||
useValue: mockUsernameRepository,
|
||||
},
|
||||
{
|
||||
provide: LoggingMessager,
|
||||
provide: Messager,
|
||||
useValue: mockMessager,
|
||||
},
|
||||
DeleteUsernameUseCase,
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
|
||||
const mockAmqpConnection = {
|
||||
publish: jest.fn().mockImplementation(),
|
||||
};
|
||||
|
||||
describe('LoggingMessager', () => {
|
||||
let loggingMessager: LoggingMessager;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
imports: [],
|
||||
providers: [
|
||||
LoggingMessager,
|
||||
{
|
||||
provide: AmqpConnection,
|
||||
useValue: mockAmqpConnection,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
loggingMessager = module.get<LoggingMessager>(LoggingMessager);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(LoggingMessager).toBeDefined();
|
||||
});
|
||||
|
||||
it('should publish a message', async () => {
|
||||
jest.spyOn(mockAmqpConnection, 'publish');
|
||||
await loggingMessager.publish('authentication.create.info', 'my-test');
|
||||
expect(mockAmqpConnection.publish).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
|
@ -1,41 +1,47 @@
|
|||
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AuthenticationMessager } from '../../adapters/secondaries/authentication.messager';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
|
||||
const mockAmqpConnection = {
|
||||
publish: jest.fn().mockImplementation(),
|
||||
};
|
||||
|
||||
describe('AuthenticationMessager', () => {
|
||||
let authenticationMessager: AuthenticationMessager;
|
||||
const mockConfigService = {
|
||||
get: jest.fn().mockResolvedValue({
|
||||
RMQ_EXCHANGE: 'mobicoop',
|
||||
}),
|
||||
};
|
||||
|
||||
describe('Messager', () => {
|
||||
let messager: Messager;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
imports: [],
|
||||
providers: [
|
||||
AuthenticationMessager,
|
||||
Messager,
|
||||
{
|
||||
provide: AmqpConnection,
|
||||
useValue: mockAmqpConnection,
|
||||
},
|
||||
{
|
||||
provide: ConfigService,
|
||||
useValue: mockConfigService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
authenticationMessager = module.get<AuthenticationMessager>(
|
||||
AuthenticationMessager,
|
||||
);
|
||||
messager = module.get<Messager>(Messager);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(authenticationMessager).toBeDefined();
|
||||
expect(messager).toBeDefined();
|
||||
});
|
||||
|
||||
it('should publish a message', async () => {
|
||||
jest.spyOn(mockAmqpConnection, 'publish');
|
||||
await authenticationMessager.publish(
|
||||
'authentication.create.info',
|
||||
'my-test',
|
||||
);
|
||||
messager.publish('authentication.create.info', 'my-test');
|
||||
expect(mockAmqpConnection.publish).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
|
@ -7,7 +7,7 @@ import * as bcrypt from 'bcrypt';
|
|||
import { UpdatePasswordRequest } from '../../domain/dtos/update-password.request';
|
||||
import { UpdatePasswordCommand } from '../../commands/update-password.command';
|
||||
import { UpdatePasswordUseCase } from '../../domain/usecases/update-password.usecase';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
|
||||
const updatePasswordRequest: UpdatePasswordRequest =
|
||||
new UpdatePasswordRequest();
|
||||
|
@ -46,7 +46,7 @@ describe('UpdatePasswordUseCase', () => {
|
|||
useValue: mockAuthenticationRepository,
|
||||
},
|
||||
{
|
||||
provide: LoggingMessager,
|
||||
provide: Messager,
|
||||
useValue: mockMessager,
|
||||
},
|
||||
UpdatePasswordUseCase,
|
||||
|
|
|
@ -10,7 +10,7 @@ import { UpdateUsernameUseCase } from '../../domain/usecases/update-username.use
|
|||
import { CommandBus } from '@nestjs/cqrs';
|
||||
import { UsernameProfile } from '../../mappers/username.profile';
|
||||
import { BadRequestException } from '@nestjs/common';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
|
||||
const existingUsername = {
|
||||
uuid: 'bb281075-1b98-4456-89d6-c643d3044a91',
|
||||
|
@ -100,7 +100,7 @@ describe('UpdateUsernameUseCase', () => {
|
|||
useValue: mockAddUsernameCommand,
|
||||
},
|
||||
{
|
||||
provide: LoggingMessager,
|
||||
provide: Messager,
|
||||
useValue: mockMessager,
|
||||
},
|
||||
UpdateUsernameUseCase,
|
||||
|
|
Loading…
Reference in New Issue