Merge branch 'logging' into 'main'

Logging

See merge request mobicoop/lab/v3/services/user!7
This commit is contained in:
Gsk54 2022-12-23 14:50:51 +00:00
commit 59f486df9b
20 changed files with 194 additions and 67 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

@ -0,0 +1,14 @@
import { Injectable, ValidationPipe } from '@nestjs/common';
import { RpcException } from '@nestjs/microservices';
@Injectable()
export class RpcValidationPipe extends ValidationPipe {
createExceptionFactory() {
return (validationErrors = []) => {
return new RpcException({
code: 3,
message: this.flattenValidationErrors(validationErrors),
});
};
}
}

View File

@ -1,6 +1,6 @@
import { Mapper } from '@automapper/core'; import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs'; import { InjectMapper } from '@automapper/nestjs';
import { Controller } from '@nestjs/common'; import { Controller, UsePipes } 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';
@ -16,7 +16,14 @@ import { FindAllUsersQuery } from '../../queries/find-all-users.query';
import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query'; import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query';
import { UserPresenter } from './user.presenter'; import { UserPresenter } from './user.presenter';
import { ICollection } from '../../../database/src/interfaces/collection.interface'; import { ICollection } from '../../../database/src/interfaces/collection.interface';
import { RpcValidationPipe } from './rpc.validation-pipe';
@UsePipes(
new RpcValidationPipe({
whitelist: true,
forbidUnknownValues: false,
}),
)
@Controller() @Controller()
export class UsersController { export class UsersController {
constructor( constructor(
@ -40,16 +47,17 @@ export class UsersController {
@GrpcMethod('UsersService', 'FindOneByUuid') @GrpcMethod('UsersService', 'FindOneByUuid')
async findOneByUuid(data: FindUserByUuidRequest): Promise<UserPresenter> { async findOneByUuid(data: FindUserByUuidRequest): Promise<UserPresenter> {
const user = await this._queryBus.execute( try {
new FindUserByUuidQuery(data.uuid), const user = await this._queryBus.execute(
); 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({
code: 5,
message: 'User not found',
});
} }
throw new RpcException({
code: 5,
message: 'User not found',
});
} }
@GrpcMethod('UsersService', 'Create') @GrpcMethod('UsersService', 'Create')

View File

@ -0,0 +1,14 @@
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);
}
}

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/message-broker';
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,24 +1,29 @@
import { AutoMap } from '@automapper/classes'; import { AutoMap } from '@automapper/classes';
import { IsString } from 'class-validator'; import { IsEmail, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
export class CreateUserRequest { export class CreateUserRequest {
@IsString() @IsString()
@IsOptional()
@AutoMap() @AutoMap()
uuid?: string; uuid?: string;
@IsString() @IsString()
@IsOptional()
@AutoMap() @AutoMap()
firstName?: string; firstName?: string;
@IsString() @IsString()
@IsOptional()
@AutoMap() @AutoMap()
lastName?: string; lastName?: string;
@IsString() @IsEmail()
@IsOptional()
@AutoMap() @AutoMap()
email?: string; email?: string;
@IsString() @IsPhoneNumber()
@IsOptional()
@AutoMap() @AutoMap()
phone?: string; phone?: string;
} }

View File

@ -1,24 +1,35 @@
import { AutoMap } from '@automapper/classes'; import { AutoMap } from '@automapper/classes';
import { IsString } from 'class-validator'; import {
IsEmail,
IsNotEmpty,
IsOptional,
IsPhoneNumber,
IsString,
} from 'class-validator';
export class UpdateUserRequest { export class UpdateUserRequest {
@IsString() @IsString()
@IsNotEmpty()
@AutoMap() @AutoMap()
uuid: string; uuid: string;
@IsString() @IsString()
@IsOptional()
@AutoMap() @AutoMap()
firstName?: string; firstName?: string;
@IsString() @IsString()
@IsOptional()
@AutoMap() @AutoMap()
lastName?: string; lastName?: string;
@IsString() @IsEmail()
@IsOptional()
@AutoMap() @AutoMap()
email?: string; email?: string;
@IsString() @IsPhoneNumber()
@IsOptional()
@AutoMap() @AutoMap()
phone?: string; phone?: string;
} }

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,
); );
const user = await this._repository.create(entity); try {
const user = await this._repository.create(entity);
if (user) { this._userMessager.publish('create', JSON.stringify(user));
this._messager.publish('user.create', JSON.stringify(user)); this._loggingMessager.publish('user.create.info', JSON.stringify(user));
return user;
} catch (error) {
this._loggingMessager.publish(
'user.create.critical',
JSON.stringify({
command,
error,
}),
);
throw error;
} }
return user;
} }
} }

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> {
const user = await this._repository.delete(command.uuid); try {
const user = await this._repository.delete(command.uuid);
if (user) { this._userMessager.publish('delete', JSON.stringify({ uuid: user.uuid }));
this._messager.publish( this._loggingMessager.publish(
'user.delete', 'delete',
JSON.stringify({ uuid: user.uuid }), JSON.stringify({ uuid: user.uuid }),
); );
return user;
} catch (error) {
this._loggingMessager.publish(
'user.delete.critical',
JSON.stringify({
command,
error,
}),
);
throw error;
} }
return user;
} }
} }

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,
); );
const user = await this._repository.update( try {
command.updateUserRequest.uuid, const user = await this._repository.update(
entity, command.updateUserRequest.uuid,
); entity,
);
if (user) { this._userMessager.publish(
this._messager.publish( 'update',
'user.update',
JSON.stringify(command.updateUserRequest), JSON.stringify(command.updateUserRequest),
); );
this._loggingMessager.publish(
'user.update.info',
JSON.stringify(command.updateUserRequest),
);
return user;
} catch (error) {
this._loggingMessager.publish(
'user.update.critical',
JSON.stringify({
command,
error,
}),
);
throw error;
} }
return user;
} }
} }

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,