send logging messages
This commit is contained in:
parent
4e2ee28219
commit
6eb9b40e14
2
.env
2
.env
|
@ -7,8 +7,6 @@ SERVICE_PORT=5001
|
|||
DATABASE_URL="postgresql://user:user@v3-user-db:5432/user?schema=public"
|
||||
|
||||
# RABBIT MQ
|
||||
RMQ_EXCHANGE_NAME=user
|
||||
RMQ_EXCHANGE_TYPE=topic
|
||||
RMQ_URI=amqp://v3-gateway-broker:5672
|
||||
|
||||
# POSTGRES
|
||||
|
|
|
@ -7,8 +7,6 @@ SERVICE_PORT=5001
|
|||
DATABASE_URL="postgresql://user:user@v3-user-db:5432/user?schema=public"
|
||||
|
||||
# RABBIT MQ
|
||||
RMQ_EXCHANGES=user
|
||||
RMQ_EXCHANGE_TYPE=topic
|
||||
RMQ_URI=amqp://localhost:5672
|
||||
|
||||
# POSTGRES
|
||||
|
|
|
@ -149,7 +149,6 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
|||
const entity = await this._prisma[this._model].delete({
|
||||
where: { uuid },
|
||||
});
|
||||
|
||||
return entity;
|
||||
} catch (e) {
|
||||
if (e instanceof PrismaClientKnownRequestError) {
|
||||
|
|
|
@ -1,11 +1,6 @@
|
|||
import { Mapper } from '@automapper/core';
|
||||
import { InjectMapper } from '@automapper/nestjs';
|
||||
import {
|
||||
Controller,
|
||||
UsePipes,
|
||||
ValidationError,
|
||||
ValidationPipe,
|
||||
} from '@nestjs/common';
|
||||
import { Controller, UsePipes } from '@nestjs/common';
|
||||
import { CommandBus, QueryBus } from '@nestjs/cqrs';
|
||||
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
||||
import { DatabaseException } from 'src/modules/database/src/exceptions/DatabaseException';
|
||||
|
@ -46,17 +41,18 @@ export class UsersController {
|
|||
|
||||
@GrpcMethod('UsersService', 'FindOneByUuid')
|
||||
async findOneByUuid(data: FindUserByUuidRequest): Promise<UserPresenter> {
|
||||
try {
|
||||
const user = await this._queryBus.execute(
|
||||
new FindUserByUuidQuery(data.uuid),
|
||||
);
|
||||
if (user) {
|
||||
return this._mapper.map(user, User, UserPresenter);
|
||||
}
|
||||
} catch (error) {
|
||||
throw new RpcException({
|
||||
code: 5,
|
||||
message: 'User not found',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@GrpcMethod('UsersService', 'Create')
|
||||
async createUser(data: CreateUserRequest): Promise<UserPresenter> {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -1,15 +1,11 @@
|
|||
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { IMessageUser } from '../../domain/interfaces/user-messager';
|
||||
import { IMessageBroker } from '../../domain/interfaces/user-messager';
|
||||
|
||||
@Injectable()
|
||||
export class UserMessager extends IMessageUser {
|
||||
constructor(
|
||||
private readonly _configService: ConfigService,
|
||||
private readonly _amqpConnection: AmqpConnection,
|
||||
) {
|
||||
super(_configService.get<string>('RMQ_EXCHANGE_NAME'));
|
||||
export class UserMessager extends IMessageBroker {
|
||||
constructor(private readonly _amqpConnection: AmqpConnection) {
|
||||
super('user');
|
||||
}
|
||||
|
||||
publish(routingKey: string, message: string): void {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export abstract class IMessageUser {
|
||||
export abstract class IMessageBroker {
|
||||
exchange: string;
|
||||
|
||||
constructor(exchange: string) {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { Mapper } from '@automapper/core';
|
||||
import { InjectMapper } from '@automapper/nestjs';
|
||||
import { CommandHandler } from '@nestjs/cqrs';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { UserMessager } from '../../adapters/secondaries/user.messager';
|
||||
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
||||
import { CreateUserCommand } from '../../commands/create-user.command';
|
||||
|
@ -11,7 +12,8 @@ import { User } from '../entities/user';
|
|||
export class CreateUserUseCase {
|
||||
constructor(
|
||||
private readonly _repository: UsersRepository,
|
||||
private readonly _messager: UserMessager,
|
||||
private readonly _userMessager: UserMessager,
|
||||
private readonly _loggingMessager: LoggingMessager,
|
||||
@InjectMapper() private readonly _mapper: Mapper,
|
||||
) {}
|
||||
|
||||
|
@ -22,12 +24,20 @@ export class CreateUserUseCase {
|
|||
User,
|
||||
);
|
||||
|
||||
try {
|
||||
const user = await this._repository.create(entity);
|
||||
|
||||
if (user) {
|
||||
this._messager.publish('user.create', JSON.stringify(user));
|
||||
}
|
||||
|
||||
this._userMessager.publish('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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { CommandHandler } from '@nestjs/cqrs';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { UserMessager } from '../../adapters/secondaries/user.messager';
|
||||
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
||||
import { DeleteUserCommand } from '../../commands/delete-user.command';
|
||||
|
@ -8,19 +9,28 @@ import { User } from '../entities/user';
|
|||
export class DeleteUserUseCase {
|
||||
constructor(
|
||||
private readonly _repository: UsersRepository,
|
||||
private readonly _messager: UserMessager,
|
||||
private readonly _userMessager: UserMessager,
|
||||
private readonly _loggingMessager: LoggingMessager,
|
||||
) {}
|
||||
|
||||
async execute(command: DeleteUserCommand): Promise<User> {
|
||||
try {
|
||||
const user = await this._repository.delete(command.uuid);
|
||||
|
||||
if (user) {
|
||||
this._messager.publish(
|
||||
'user.delete',
|
||||
this._userMessager.publish('delete', JSON.stringify({ uuid: user.uuid }));
|
||||
this._loggingMessager.publish(
|
||||
'delete',
|
||||
JSON.stringify({ uuid: user.uuid }),
|
||||
);
|
||||
}
|
||||
|
||||
return user;
|
||||
} catch (error) {
|
||||
this._loggingMessager.publish(
|
||||
'user.delete.critical',
|
||||
JSON.stringify({
|
||||
command,
|
||||
error,
|
||||
}),
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,12 +6,12 @@ import { User } from '../entities/user';
|
|||
|
||||
@QueryHandler(FindAllUsersQuery)
|
||||
export class FindAllUsersUseCase {
|
||||
constructor(private readonly _usersRepository: UsersRepository) {}
|
||||
constructor(private readonly _repository: UsersRepository) {}
|
||||
|
||||
async execute(
|
||||
findAllUsersQuery: FindAllUsersQuery,
|
||||
): Promise<ICollection<User>> {
|
||||
return this._usersRepository.findAll(
|
||||
return this._repository.findAll(
|
||||
findAllUsersQuery.page,
|
||||
findAllUsersQuery.perPage,
|
||||
);
|
||||
|
|
|
@ -1,13 +1,31 @@
|
|||
import { NotFoundException } from '@nestjs/common';
|
||||
import { QueryHandler } from '@nestjs/cqrs';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
||||
import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query';
|
||||
import { User } from '../entities/user';
|
||||
|
||||
@QueryHandler(FindUserByUuidQuery)
|
||||
export class FindUserByUuidUseCase {
|
||||
constructor(private readonly _usersRepository: UsersRepository) {}
|
||||
constructor(
|
||||
private readonly _repository: UsersRepository,
|
||||
private readonly _loggingMessager: LoggingMessager,
|
||||
) {}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { Mapper } from '@automapper/core';
|
||||
import { InjectMapper } from '@automapper/nestjs';
|
||||
import { CommandHandler } from '@nestjs/cqrs';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { UserMessager } from '../../adapters/secondaries/user.messager';
|
||||
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
||||
import { UpdateUserCommand } from '../../commands/update-user.command';
|
||||
|
@ -11,7 +12,8 @@ import { User } from '../entities/user';
|
|||
export class UpdateUserUseCase {
|
||||
constructor(
|
||||
private readonly _repository: UsersRepository,
|
||||
private readonly _messager: UserMessager,
|
||||
private readonly _userMessager: UserMessager,
|
||||
private readonly _loggingMessager: LoggingMessager,
|
||||
@InjectMapper() private readonly _mapper: Mapper,
|
||||
) {}
|
||||
|
||||
|
@ -22,17 +24,29 @@ export class UpdateUserUseCase {
|
|||
User,
|
||||
);
|
||||
|
||||
try {
|
||||
const user = await this._repository.update(
|
||||
command.updateUserRequest.uuid,
|
||||
entity,
|
||||
);
|
||||
|
||||
if (user) {
|
||||
this._messager.publish(
|
||||
'user.update',
|
||||
this._userMessager.publish(
|
||||
'update',
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { classes } from '@automapper/classes';
|
||||
import { AutomapperModule } from '@automapper/nestjs';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { UserMessager } from '../../adapters/secondaries/user.messager';
|
||||
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
||||
import { CreateUserCommand } from '../../commands/create-user.command';
|
||||
|
@ -24,7 +25,7 @@ const mockUsersRepository = {
|
|||
}),
|
||||
};
|
||||
|
||||
const mockUserMessager = {
|
||||
const mockMessager = {
|
||||
publish: jest.fn().mockImplementation(),
|
||||
};
|
||||
|
||||
|
@ -43,7 +44,11 @@ describe('CreateUserUseCase', () => {
|
|||
UserProfile,
|
||||
{
|
||||
provide: UserMessager,
|
||||
useValue: mockUserMessager,
|
||||
useValue: mockMessager,
|
||||
},
|
||||
{
|
||||
provide: LoggingMessager,
|
||||
useValue: mockMessager,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { UserMessager } from '../../adapters/secondaries/user.messager';
|
||||
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
||||
import { DeleteUserCommand } from '../../commands/delete-user.command';
|
||||
|
@ -30,16 +31,18 @@ const mockUsers = [
|
|||
|
||||
const mockUsersRepository = {
|
||||
delete: jest.fn().mockImplementation((uuid: string) => {
|
||||
let savedUser = {};
|
||||
mockUsers.forEach((user, index) => {
|
||||
if (user.uuid === uuid) {
|
||||
savedUser = { ...user };
|
||||
mockUsers.splice(index, 1);
|
||||
return Promise.resolve();
|
||||
}
|
||||
});
|
||||
return savedUser;
|
||||
}),
|
||||
};
|
||||
|
||||
const mockUserMessager = {
|
||||
const mockMessager = {
|
||||
publish: jest.fn().mockImplementation(),
|
||||
};
|
||||
|
||||
|
@ -56,7 +59,11 @@ describe('DeleteUserUseCase', () => {
|
|||
DeleteUserUseCase,
|
||||
{
|
||||
provide: UserMessager,
|
||||
useValue: mockUserMessager,
|
||||
useValue: mockMessager,
|
||||
},
|
||||
{
|
||||
provide: LoggingMessager,
|
||||
useValue: mockMessager,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
||||
import { FindUserByUuidUseCase } from '../../domain/usecases/find-user-by-uuid.usecase';
|
||||
import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query';
|
||||
|
@ -18,6 +19,10 @@ const mockUserRepository = {
|
|||
}),
|
||||
};
|
||||
|
||||
const mockMessager = {
|
||||
publish: jest.fn().mockImplementation(),
|
||||
};
|
||||
|
||||
describe('FindUserByUuidUseCase', () => {
|
||||
let findUserByUuidUseCase: FindUserByUuidUseCase;
|
||||
|
||||
|
@ -29,6 +34,10 @@ describe('FindUserByUuidUseCase', () => {
|
|||
provide: UsersRepository,
|
||||
useValue: mockUserRepository,
|
||||
},
|
||||
{
|
||||
provide: LoggingMessager,
|
||||
useValue: mockMessager,
|
||||
},
|
||||
FindUserByUuidUseCase,
|
||||
],
|
||||
}).compile();
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { classes } from '@automapper/classes';
|
||||
import { AutomapperModule } from '@automapper/nestjs';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
|
||||
import { UserMessager } from '../../adapters/secondaries/user.messager';
|
||||
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
||||
import { UpdateUserCommand } from '../../commands/update-user.command';
|
||||
|
@ -30,7 +31,7 @@ const mockUsersRepository = {
|
|||
}),
|
||||
};
|
||||
|
||||
const mockUserMessager = {
|
||||
const mockMessager = {
|
||||
publish: jest.fn().mockImplementation(),
|
||||
};
|
||||
|
||||
|
@ -50,7 +51,11 @@ describe('UpdateUserUseCase', () => {
|
|||
UserProfile,
|
||||
{
|
||||
provide: UserMessager,
|
||||
useValue: mockUserMessager,
|
||||
useValue: mockMessager,
|
||||
},
|
||||
{
|
||||
provide: LoggingMessager,
|
||||
useValue: mockMessager,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
|
|
@ -4,6 +4,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
|
|||
import { CqrsModule } from '@nestjs/cqrs';
|
||||
import { DatabaseModule } from '../database/database.module';
|
||||
import { UsersController } from './adapters/primaries/users.controller';
|
||||
import { LoggingMessager } from './adapters/secondaries/logging.messager';
|
||||
import { UserMessager } from './adapters/secondaries/user.messager';
|
||||
import { UsersRepository } from './adapters/secondaries/users.repository';
|
||||
import { CreateUserUseCase } from './domain/usecases/create-user.usecase';
|
||||
|
@ -22,8 +23,12 @@ import { UserProfile } from './mappers/user.profile';
|
|||
useFactory: async (configService: ConfigService) => ({
|
||||
exchanges: [
|
||||
{
|
||||
name: configService.get<string>('RMQ_EXCHANGE_NAME'),
|
||||
type: configService.get<string>('RMQ_EXCHANGE_TYPE'),
|
||||
name: 'user',
|
||||
type: 'topic',
|
||||
},
|
||||
{
|
||||
name: 'logging',
|
||||
type: 'topic',
|
||||
},
|
||||
],
|
||||
uri: configService.get<string>('RMQ_URI'),
|
||||
|
@ -36,6 +41,7 @@ import { UserProfile } from './mappers/user.profile';
|
|||
UserProfile,
|
||||
UsersRepository,
|
||||
UserMessager,
|
||||
LoggingMessager,
|
||||
FindAllUsersUseCase,
|
||||
FindUserByUuidUseCase,
|
||||
CreateUserUseCase,
|
||||
|
|
Loading…
Reference in New Issue