user update

This commit is contained in:
sbriat
2023-07-21 14:22:13 +02:00
parent d5c2bb396d
commit 52fd0b952b
40 changed files with 328 additions and 98 deletions

View File

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

View File

@@ -0,0 +1,11 @@
import { IsInt, IsOptional } from 'class-validator';
export class FindAllUsersRequest {
@IsInt()
@IsOptional()
page?: number;
@IsInt()
@IsOptional()
perPage?: number;
}

View File

@@ -0,0 +1,7 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class FindUserByUuidRequest {
@IsString()
@IsNotEmpty()
uuid: string;
}

View File

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

View File

@@ -0,0 +1,18 @@
import { AutoMap } from '@automapper/classes';
export class User {
@AutoMap()
uuid: string;
@AutoMap()
firstName?: string;
@AutoMap()
lastName?: string;
@AutoMap()
email?: string;
@AutoMap()
phone?: string;
}

View File

@@ -0,0 +1,51 @@
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
import { CommandHandler } from '@nestjs/cqrs';
import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { CreateUserCommand } from '../../commands/create-user.command';
import { CreateUserRequest } from '../dtos/create-user.request';
import { User } from '../entities/user';
import { Inject } from '@nestjs/common';
import { MESSAGE_PUBLISHER } from '../../../../app.constants';
import { IPublishMessage } from '../../../../interfaces/message-publisher';
@CommandHandler(CreateUserCommand)
export class CreateUserUseCase {
constructor(
private readonly repository: UsersRepository,
@Inject(MESSAGE_PUBLISHER)
private readonly messagePublisher: IPublishMessage,
@InjectMapper() private readonly mapper: Mapper,
) {}
execute = async (command: CreateUserCommand): Promise<User> => {
const entity = this.mapper.map(
command.createUserRequest,
CreateUserRequest,
User,
);
try {
const user = await this.repository.create(entity);
this.messagePublisher.publish('user.create', JSON.stringify(user));
this.messagePublisher.publish(
'logging.user.create.info',
JSON.stringify(user),
);
return user;
} catch (error) {
let key = 'logging.user.create.crit';
if (error.message.includes('Already exists')) {
key = 'logging.user.create.warning';
}
this.messagePublisher.publish(
key,
JSON.stringify({
command,
error,
}),
);
throw error;
}
};
}

View File

@@ -0,0 +1,40 @@
import { CommandHandler } from '@nestjs/cqrs';
import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { DeleteUserCommand } from '../../commands/delete-user.command';
import { User } from '../entities/user';
import { IPublishMessage } from '../../../../interfaces/message-publisher';
import { Inject } from '@nestjs/common';
import { MESSAGE_PUBLISHER } from '../../../../app.constants';
@CommandHandler(DeleteUserCommand)
export class DeleteUserUseCase {
constructor(
private readonly repository: UsersRepository,
@Inject(MESSAGE_PUBLISHER)
private readonly messagePublisher: IPublishMessage,
) {}
execute = async (command: DeleteUserCommand): Promise<User> => {
try {
const user = await this.repository.delete(command.uuid);
this.messagePublisher.publish(
'user.delete',
JSON.stringify({ uuid: user.uuid }),
);
this.messagePublisher.publish(
'logging.user.delete.info',
JSON.stringify({ uuid: user.uuid }),
);
return user;
} catch (error) {
this.messagePublisher.publish(
'logging.user.delete.crit',
JSON.stringify({
command,
error,
}),
);
throw error;
}
};
}

View File

@@ -0,0 +1,15 @@
import { QueryHandler } from '@nestjs/cqrs';
import { ICollection } from 'src/modules/database/interfaces/collection.interface';
import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { FindAllUsersQuery } from '../../queries/find-all-users.query';
import { User } from '../entities/user';
@QueryHandler(FindAllUsersQuery)
export class FindAllUsersUseCase {
constructor(private readonly repository: UsersRepository) {}
execute = async (
findAllUsersQuery: FindAllUsersQuery,
): Promise<ICollection<User>> =>
this.repository.findAll(findAllUsersQuery.page, findAllUsersQuery.perPage);
}

View File

@@ -0,0 +1,33 @@
import { Inject, NotFoundException } from '@nestjs/common';
import { QueryHandler } from '@nestjs/cqrs';
import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query';
import { User } from '../entities/user';
import { MESSAGE_PUBLISHER } from '../../../../app.constants';
import { IPublishMessage } from '../../../../interfaces/message-publisher';
@QueryHandler(FindUserByUuidQuery)
export class FindUserByUuidUseCase {
constructor(
private readonly repository: UsersRepository,
@Inject(MESSAGE_PUBLISHER)
private readonly messagePublisher: IPublishMessage,
) {}
execute = async (findUserByUuid: FindUserByUuidQuery): Promise<User> => {
try {
const user = await this.repository.findOneByUuid(findUserByUuid.uuid);
if (!user) throw new NotFoundException();
return user;
} catch (error) {
this.messagePublisher.publish(
'logging.user.read.warning',
JSON.stringify({
query: findUserByUuid,
error,
}),
);
throw error;
}
};
}

View File

@@ -0,0 +1,53 @@
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
import { CommandHandler } from '@nestjs/cqrs';
import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { UpdateUserCommand } from '../../commands/update-user.command';
import { UpdateUserRequest } from '../dtos/update-user.request';
import { User } from '../entities/user';
import { MESSAGE_PUBLISHER } from '../../../../app.constants';
import { Inject } from '@nestjs/common';
import { IPublishMessage } from '../../../../interfaces/message-publisher';
@CommandHandler(UpdateUserCommand)
export class UpdateUserUseCase {
constructor(
private readonly repository: UsersRepository,
@Inject(MESSAGE_PUBLISHER)
private readonly messagePublisher: IPublishMessage,
@InjectMapper() private readonly mapper: Mapper,
) {}
execute = async (command: UpdateUserCommand): Promise<User> => {
const entity = this.mapper.map(
command.updateUserRequest,
UpdateUserRequest,
User,
);
try {
const user = await this.repository.update(
command.updateUserRequest.uuid,
entity,
);
this.messagePublisher.publish(
'user.update',
JSON.stringify(command.updateUserRequest),
);
this.messagePublisher.publish(
'logging.user.update.info',
JSON.stringify(command.updateUserRequest),
);
return user;
} catch (error) {
this.messagePublisher.publish(
'logging.user.update.crit',
JSON.stringify({
command,
error,
}),
);
throw error;
}
};
}