mirror of
https://gitlab.com/mobicoop/v3/service/user.git
synced 2026-03-26 22:25:50 +00:00
user update
This commit is contained in:
29
.old/domain/dtos/create-user.request.ts
Normal file
29
.old/domain/dtos/create-user.request.ts
Normal 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;
|
||||
}
|
||||
11
.old/domain/dtos/find-all-users.request.ts
Normal file
11
.old/domain/dtos/find-all-users.request.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { IsInt, IsOptional } from 'class-validator';
|
||||
|
||||
export class FindAllUsersRequest {
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
page?: number;
|
||||
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
perPage?: number;
|
||||
}
|
||||
7
.old/domain/dtos/find-user-by-uuid.request.ts
Normal file
7
.old/domain/dtos/find-user-by-uuid.request.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class FindUserByUuidRequest {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
uuid: string;
|
||||
}
|
||||
35
.old/domain/dtos/update-user.request.ts
Normal file
35
.old/domain/dtos/update-user.request.ts
Normal 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;
|
||||
}
|
||||
18
.old/domain/entities/user.ts
Normal file
18
.old/domain/entities/user.ts
Normal 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;
|
||||
}
|
||||
51
.old/domain/usecases/create-user.usecase.ts
Normal file
51
.old/domain/usecases/create-user.usecase.ts
Normal 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
40
.old/domain/usecases/delete-user.usecase.ts
Normal file
40
.old/domain/usecases/delete-user.usecase.ts
Normal 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
15
.old/domain/usecases/find-all-users.usecase.ts
Normal file
15
.old/domain/usecases/find-all-users.usecase.ts
Normal 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);
|
||||
}
|
||||
33
.old/domain/usecases/find-user-by-uuid.usecase.ts
Normal file
33
.old/domain/usecases/find-user-by-uuid.usecase.ts
Normal 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
53
.old/domain/usecases/update-user.usecase.ts
Normal file
53
.old/domain/usecases/update-user.usecase.ts
Normal 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user