send logging messages

This commit is contained in:
Gsk54 2022-12-23 15:14:51 +01:00
parent 4e2ee28219
commit 6eb9b40e14
17 changed files with 151 additions and 66 deletions

2
.env
View File

@ -7,8 +7,6 @@ SERVICE_PORT=5001
DATABASE_URL="postgresql://user:user@v3-user-db:5432/user?schema=public" DATABASE_URL="postgresql://user:user@v3-user-db:5432/user?schema=public"
# RABBIT MQ # RABBIT MQ
RMQ_EXCHANGE_NAME=user
RMQ_EXCHANGE_TYPE=topic
RMQ_URI=amqp://v3-gateway-broker:5672 RMQ_URI=amqp://v3-gateway-broker:5672
# POSTGRES # POSTGRES

View File

@ -7,8 +7,6 @@ SERVICE_PORT=5001
DATABASE_URL="postgresql://user:user@v3-user-db:5432/user?schema=public" DATABASE_URL="postgresql://user:user@v3-user-db:5432/user?schema=public"
# RABBIT MQ # RABBIT MQ
RMQ_EXCHANGES=user
RMQ_EXCHANGE_TYPE=topic
RMQ_URI=amqp://localhost:5672 RMQ_URI=amqp://localhost:5672
# POSTGRES # POSTGRES

View File

@ -149,7 +149,6 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
const entity = await this._prisma[this._model].delete({ const entity = await this._prisma[this._model].delete({
where: { uuid }, where: { uuid },
}); });
return entity; return entity;
} catch (e) { } catch (e) {
if (e instanceof PrismaClientKnownRequestError) { if (e instanceof PrismaClientKnownRequestError) {

View File

@ -1,11 +1,6 @@
import { Mapper } from '@automapper/core'; import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs'; import { InjectMapper } from '@automapper/nestjs';
import { import { Controller, UsePipes } from '@nestjs/common';
Controller,
UsePipes,
ValidationError,
ValidationPipe,
} from '@nestjs/common';
import { CommandBus, QueryBus } from '@nestjs/cqrs'; import { CommandBus, QueryBus } from '@nestjs/cqrs';
import { GrpcMethod, RpcException } from '@nestjs/microservices'; import { GrpcMethod, RpcException } from '@nestjs/microservices';
import { DatabaseException } from 'src/modules/database/src/exceptions/DatabaseException'; import { DatabaseException } from 'src/modules/database/src/exceptions/DatabaseException';
@ -46,17 +41,18 @@ export class UsersController {
@GrpcMethod('UsersService', 'FindOneByUuid') @GrpcMethod('UsersService', 'FindOneByUuid')
async findOneByUuid(data: FindUserByUuidRequest): Promise<UserPresenter> { async findOneByUuid(data: FindUserByUuidRequest): Promise<UserPresenter> {
try {
const user = await this._queryBus.execute( const user = await this._queryBus.execute(
new FindUserByUuidQuery(data.uuid), new FindUserByUuidQuery(data.uuid),
); );
if (user) {
return this._mapper.map(user, User, UserPresenter); return this._mapper.map(user, User, UserPresenter);
} } catch (error) {
throw new RpcException({ throw new RpcException({
code: 5, code: 5,
message: 'User not found', message: 'User not found',
}); });
} }
}
@GrpcMethod('UsersService', 'Create') @GrpcMethod('UsersService', 'Create')
async createUser(data: CreateUserRequest): Promise<UserPresenter> { async createUser(data: CreateUserRequest): Promise<UserPresenter> {

View File

@ -0,0 +1,14 @@
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
import { Injectable } from '@nestjs/common';
import { IMessageBroker } from '../../domain/interfaces/user-messager';
@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);
}
}

View File

@ -1,15 +1,11 @@
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq'; import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config'; import { IMessageBroker } from '../../domain/interfaces/user-messager';
import { IMessageUser } from '../../domain/interfaces/user-messager';
@Injectable() @Injectable()
export class UserMessager extends IMessageUser { export class UserMessager extends IMessageBroker {
constructor( constructor(private readonly _amqpConnection: AmqpConnection) {
private readonly _configService: ConfigService, super('user');
private readonly _amqpConnection: AmqpConnection,
) {
super(_configService.get<string>('RMQ_EXCHANGE_NAME'));
} }
publish(routingKey: string, message: string): void { publish(routingKey: string, message: string): void {

View File

@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
@Injectable() @Injectable()
export abstract class IMessageUser { export abstract class IMessageBroker {
exchange: string; exchange: string;
constructor(exchange: string) { constructor(exchange: string) {

View File

@ -1,6 +1,7 @@
import { Mapper } from '@automapper/core'; import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs'; import { InjectMapper } from '@automapper/nestjs';
import { CommandHandler } from '@nestjs/cqrs'; import { CommandHandler } from '@nestjs/cqrs';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { UserMessager } from '../../adapters/secondaries/user.messager'; import { UserMessager } from '../../adapters/secondaries/user.messager';
import { UsersRepository } from '../../adapters/secondaries/users.repository'; import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { CreateUserCommand } from '../../commands/create-user.command'; import { CreateUserCommand } from '../../commands/create-user.command';
@ -11,7 +12,8 @@ import { User } from '../entities/user';
export class CreateUserUseCase { export class CreateUserUseCase {
constructor( constructor(
private readonly _repository: UsersRepository, private readonly _repository: UsersRepository,
private readonly _messager: UserMessager, private readonly _userMessager: UserMessager,
private readonly _loggingMessager: LoggingMessager,
@InjectMapper() private readonly _mapper: Mapper, @InjectMapper() private readonly _mapper: Mapper,
) {} ) {}
@ -22,12 +24,20 @@ export class CreateUserUseCase {
User, User,
); );
try {
const user = await this._repository.create(entity); const user = await this._repository.create(entity);
this._userMessager.publish('create', JSON.stringify(user));
if (user) { this._loggingMessager.publish('user.create.info', JSON.stringify(user));
this._messager.publish('user.create', JSON.stringify(user));
}
return user; return user;
} catch (error) {
this._loggingMessager.publish(
'user.create.critical',
JSON.stringify({
command,
error,
}),
);
throw error;
}
} }
} }

View File

@ -1,4 +1,5 @@
import { CommandHandler } from '@nestjs/cqrs'; import { CommandHandler } from '@nestjs/cqrs';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { UserMessager } from '../../adapters/secondaries/user.messager'; import { UserMessager } from '../../adapters/secondaries/user.messager';
import { UsersRepository } from '../../adapters/secondaries/users.repository'; import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { DeleteUserCommand } from '../../commands/delete-user.command'; import { DeleteUserCommand } from '../../commands/delete-user.command';
@ -8,19 +9,28 @@ import { User } from '../entities/user';
export class DeleteUserUseCase { export class DeleteUserUseCase {
constructor( constructor(
private readonly _repository: UsersRepository, private readonly _repository: UsersRepository,
private readonly _messager: UserMessager, private readonly _userMessager: UserMessager,
private readonly _loggingMessager: LoggingMessager,
) {} ) {}
async execute(command: DeleteUserCommand): Promise<User> { async execute(command: DeleteUserCommand): Promise<User> {
try {
const user = await this._repository.delete(command.uuid); const user = await this._repository.delete(command.uuid);
this._userMessager.publish('delete', JSON.stringify({ uuid: user.uuid }));
if (user) { this._loggingMessager.publish(
this._messager.publish( 'delete',
'user.delete',
JSON.stringify({ uuid: user.uuid }), JSON.stringify({ uuid: user.uuid }),
); );
}
return user; return user;
} catch (error) {
this._loggingMessager.publish(
'user.delete.critical',
JSON.stringify({
command,
error,
}),
);
throw error;
}
} }
} }

View File

@ -6,12 +6,12 @@ import { User } from '../entities/user';
@QueryHandler(FindAllUsersQuery) @QueryHandler(FindAllUsersQuery)
export class FindAllUsersUseCase { export class FindAllUsersUseCase {
constructor(private readonly _usersRepository: UsersRepository) {} constructor(private readonly _repository: UsersRepository) {}
async execute( async execute(
findAllUsersQuery: FindAllUsersQuery, findAllUsersQuery: FindAllUsersQuery,
): Promise<ICollection<User>> { ): Promise<ICollection<User>> {
return this._usersRepository.findAll( return this._repository.findAll(
findAllUsersQuery.page, findAllUsersQuery.page,
findAllUsersQuery.perPage, findAllUsersQuery.perPage,
); );

View File

@ -1,13 +1,31 @@
import { NotFoundException } from '@nestjs/common';
import { QueryHandler } from '@nestjs/cqrs'; import { QueryHandler } from '@nestjs/cqrs';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { UsersRepository } from '../../adapters/secondaries/users.repository'; import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query'; import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query';
import { User } from '../entities/user'; import { User } from '../entities/user';
@QueryHandler(FindUserByUuidQuery) @QueryHandler(FindUserByUuidQuery)
export class FindUserByUuidUseCase { export class FindUserByUuidUseCase {
constructor(private readonly _usersRepository: UsersRepository) {} constructor(
private readonly _repository: UsersRepository,
private readonly _loggingMessager: LoggingMessager,
) {}
async execute(findUserByUuid: FindUserByUuidQuery): Promise<User> { async execute(findUserByUuid: FindUserByUuidQuery): Promise<User> {
return this._usersRepository.findOneByUuid(findUserByUuid.uuid); try {
const user = await this._repository.findOneByUuid(findUserByUuid.uuid);
if (!user) throw new NotFoundException();
return user;
} catch (error) {
this._loggingMessager.publish(
'user.read.warning',
JSON.stringify({
query: findUserByUuid,
error,
}),
);
throw error;
}
} }
} }

View File

@ -1,6 +1,7 @@
import { Mapper } from '@automapper/core'; import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs'; import { InjectMapper } from '@automapper/nestjs';
import { CommandHandler } from '@nestjs/cqrs'; import { CommandHandler } from '@nestjs/cqrs';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { UserMessager } from '../../adapters/secondaries/user.messager'; import { UserMessager } from '../../adapters/secondaries/user.messager';
import { UsersRepository } from '../../adapters/secondaries/users.repository'; import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { UpdateUserCommand } from '../../commands/update-user.command'; import { UpdateUserCommand } from '../../commands/update-user.command';
@ -11,7 +12,8 @@ import { User } from '../entities/user';
export class UpdateUserUseCase { export class UpdateUserUseCase {
constructor( constructor(
private readonly _repository: UsersRepository, private readonly _repository: UsersRepository,
private readonly _messager: UserMessager, private readonly _userMessager: UserMessager,
private readonly _loggingMessager: LoggingMessager,
@InjectMapper() private readonly _mapper: Mapper, @InjectMapper() private readonly _mapper: Mapper,
) {} ) {}
@ -22,17 +24,29 @@ export class UpdateUserUseCase {
User, User,
); );
try {
const user = await this._repository.update( const user = await this._repository.update(
command.updateUserRequest.uuid, command.updateUserRequest.uuid,
entity, entity,
); );
this._userMessager.publish(
if (user) { 'update',
this._messager.publish( JSON.stringify(command.updateUserRequest),
'user.update', );
this._loggingMessager.publish(
'user.update.info',
JSON.stringify(command.updateUserRequest), JSON.stringify(command.updateUserRequest),
); );
}
return user; return user;
} catch (error) {
this._loggingMessager.publish(
'user.update.critical',
JSON.stringify({
command,
error,
}),
);
throw error;
}
} }
} }

View File

@ -1,6 +1,7 @@
import { classes } from '@automapper/classes'; import { classes } from '@automapper/classes';
import { AutomapperModule } from '@automapper/nestjs'; import { AutomapperModule } from '@automapper/nestjs';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { UserMessager } from '../../adapters/secondaries/user.messager'; import { UserMessager } from '../../adapters/secondaries/user.messager';
import { UsersRepository } from '../../adapters/secondaries/users.repository'; import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { CreateUserCommand } from '../../commands/create-user.command'; import { CreateUserCommand } from '../../commands/create-user.command';
@ -24,7 +25,7 @@ const mockUsersRepository = {
}), }),
}; };
const mockUserMessager = { const mockMessager = {
publish: jest.fn().mockImplementation(), publish: jest.fn().mockImplementation(),
}; };
@ -43,7 +44,11 @@ describe('CreateUserUseCase', () => {
UserProfile, UserProfile,
{ {
provide: UserMessager, provide: UserMessager,
useValue: mockUserMessager, useValue: mockMessager,
},
{
provide: LoggingMessager,
useValue: mockMessager,
}, },
], ],
}).compile(); }).compile();

View File

@ -1,4 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { UserMessager } from '../../adapters/secondaries/user.messager'; import { UserMessager } from '../../adapters/secondaries/user.messager';
import { UsersRepository } from '../../adapters/secondaries/users.repository'; import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { DeleteUserCommand } from '../../commands/delete-user.command'; import { DeleteUserCommand } from '../../commands/delete-user.command';
@ -30,16 +31,18 @@ const mockUsers = [
const mockUsersRepository = { const mockUsersRepository = {
delete: jest.fn().mockImplementation((uuid: string) => { delete: jest.fn().mockImplementation((uuid: string) => {
let savedUser = {};
mockUsers.forEach((user, index) => { mockUsers.forEach((user, index) => {
if (user.uuid === uuid) { if (user.uuid === uuid) {
savedUser = { ...user };
mockUsers.splice(index, 1); mockUsers.splice(index, 1);
return Promise.resolve();
} }
}); });
return savedUser;
}), }),
}; };
const mockUserMessager = { const mockMessager = {
publish: jest.fn().mockImplementation(), publish: jest.fn().mockImplementation(),
}; };
@ -56,7 +59,11 @@ describe('DeleteUserUseCase', () => {
DeleteUserUseCase, DeleteUserUseCase,
{ {
provide: UserMessager, provide: UserMessager,
useValue: mockUserMessager, useValue: mockMessager,
},
{
provide: LoggingMessager,
useValue: mockMessager,
}, },
], ],
}).compile(); }).compile();

View File

@ -1,4 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { UsersRepository } from '../../adapters/secondaries/users.repository'; import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { FindUserByUuidUseCase } from '../../domain/usecases/find-user-by-uuid.usecase'; import { FindUserByUuidUseCase } from '../../domain/usecases/find-user-by-uuid.usecase';
import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query'; import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query';
@ -18,6 +19,10 @@ const mockUserRepository = {
}), }),
}; };
const mockMessager = {
publish: jest.fn().mockImplementation(),
};
describe('FindUserByUuidUseCase', () => { describe('FindUserByUuidUseCase', () => {
let findUserByUuidUseCase: FindUserByUuidUseCase; let findUserByUuidUseCase: FindUserByUuidUseCase;
@ -29,6 +34,10 @@ describe('FindUserByUuidUseCase', () => {
provide: UsersRepository, provide: UsersRepository,
useValue: mockUserRepository, useValue: mockUserRepository,
}, },
{
provide: LoggingMessager,
useValue: mockMessager,
},
FindUserByUuidUseCase, FindUserByUuidUseCase,
], ],
}).compile(); }).compile();

View File

@ -1,6 +1,7 @@
import { classes } from '@automapper/classes'; import { classes } from '@automapper/classes';
import { AutomapperModule } from '@automapper/nestjs'; import { AutomapperModule } from '@automapper/nestjs';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { UserMessager } from '../../adapters/secondaries/user.messager'; import { UserMessager } from '../../adapters/secondaries/user.messager';
import { UsersRepository } from '../../adapters/secondaries/users.repository'; import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { UpdateUserCommand } from '../../commands/update-user.command'; import { UpdateUserCommand } from '../../commands/update-user.command';
@ -30,7 +31,7 @@ const mockUsersRepository = {
}), }),
}; };
const mockUserMessager = { const mockMessager = {
publish: jest.fn().mockImplementation(), publish: jest.fn().mockImplementation(),
}; };
@ -50,7 +51,11 @@ describe('UpdateUserUseCase', () => {
UserProfile, UserProfile,
{ {
provide: UserMessager, provide: UserMessager,
useValue: mockUserMessager, useValue: mockMessager,
},
{
provide: LoggingMessager,
useValue: mockMessager,
}, },
], ],
}).compile(); }).compile();

View File

@ -4,6 +4,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
import { CqrsModule } from '@nestjs/cqrs'; import { CqrsModule } from '@nestjs/cqrs';
import { DatabaseModule } from '../database/database.module'; import { DatabaseModule } from '../database/database.module';
import { UsersController } from './adapters/primaries/users.controller'; import { UsersController } from './adapters/primaries/users.controller';
import { LoggingMessager } from './adapters/secondaries/logging.messager';
import { UserMessager } from './adapters/secondaries/user.messager'; import { UserMessager } from './adapters/secondaries/user.messager';
import { UsersRepository } from './adapters/secondaries/users.repository'; import { UsersRepository } from './adapters/secondaries/users.repository';
import { CreateUserUseCase } from './domain/usecases/create-user.usecase'; import { CreateUserUseCase } from './domain/usecases/create-user.usecase';
@ -22,8 +23,12 @@ import { UserProfile } from './mappers/user.profile';
useFactory: async (configService: ConfigService) => ({ useFactory: async (configService: ConfigService) => ({
exchanges: [ exchanges: [
{ {
name: configService.get<string>('RMQ_EXCHANGE_NAME'), name: 'user',
type: configService.get<string>('RMQ_EXCHANGE_TYPE'), type: 'topic',
},
{
name: 'logging',
type: 'topic',
}, },
], ],
uri: configService.get<string>('RMQ_URI'), uri: configService.get<string>('RMQ_URI'),
@ -36,6 +41,7 @@ import { UserProfile } from './mappers/user.profile';
UserProfile, UserProfile,
UsersRepository, UsersRepository,
UserMessager, UserMessager,
LoggingMessager,
FindAllUsersUseCase, FindAllUsersUseCase,
FindUserByUuidUseCase, FindUserByUuidUseCase,
CreateUserUseCase, CreateUserUseCase,