Skip to content

Commit

Permalink
cleaning - user controller cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickOtero committed Sep 7, 2023
1 parent 32e583d commit c8c095c
Show file tree
Hide file tree
Showing 17 changed files with 341 additions and 217 deletions.
3 changes: 3 additions & 0 deletions src/modules/mails/mail.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { MailService } from './mail.service';
user: config.get('MAIL_USER'),
pass: config.get('MAIL_PASSWORD'),
},
tls: {
rejectUnauthorized: false
}
},
defaults: {
from: `no-reply <[email protected]>`,
Expand Down
28 changes: 28 additions & 0 deletions src/modules/user/decorators/match.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
registerDecorator,
ValidationArguments,
ValidationOptions,
ValidatorConstraint,
ValidatorConstraintInterface,
} from 'class-validator';

export function Match(property: string, validationOptions?: ValidationOptions) {
return (object: any, propertyName: string) => {
registerDecorator({
target: object.constructor,
propertyName,
options: validationOptions,
constraints: [property],
validator: MatchConstraint,
});
};
}

@ValidatorConstraint({ name: 'Match' })
export class MatchConstraint implements ValidatorConstraintInterface {
validate(value: any, args: ValidationArguments) {
const [relatedPropertyName] = args.constraints;
const relatedValue = (args.object as any)[relatedPropertyName];
return value === relatedValue;
}
}
7 changes: 3 additions & 4 deletions src/modules/user/dtos/create-user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Validate,
} from 'class-validator';
import { UserRole } from '../../../shared/utils/userRole/userRole';
import { Match } from '../decorators/match.decorator';

export class CreateUserDto {
@IsNotEmpty()
Expand All @@ -33,7 +34,6 @@ export class CreateUserDto {

@IsNotEmpty()
@IsString()
@Length(8, 20)
@Matches(
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()\-_=+{};:,<.>])[a-zA-Z\d!@#$%^&*()\-_=+{};:,<.>.]{8,}$/,
{
Expand All @@ -49,13 +49,12 @@ export class CreateUserDto {

@IsNotEmpty()
@IsString()
@Length(8, 20)
@ApiProperty({
description: 'Confirmação de senha',
example: 'Abcd@1234',
})
@Validate((value, { object }) => value === object.password, {
message: 'Senhas precisam ser idênticas',
@Match('password', {
message: 'The password does not match with the password confirmation',
})
confirmPassword: string;

Expand Down
4 changes: 2 additions & 2 deletions src/modules/user/dtos/update-my-password.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export class CreatePasswordHashDto {
message: 'Senha muito fraca',
})
@ApiProperty({
description: 'Inserir senha com os critérios informados',
description: 'A senha precisa ter no mínimo 8 caracteres, máximo de 50, uma letra maiúscula, um número e um símbolo.',
example: 'Abcd@1234',
})
password: string;

@IsString()
@Length(8, 50)
@Matches(/((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/, {
message: 'Inserir senha com os critérios informados',
message: 'A senha precisa ter no mínimo 8 caracteres, máximo de 50, uma letra maiúscula, um número e um símbolo.',
})
@ApiProperty({
description: 'Confirmação de senha de Login',
Expand Down
2 changes: 1 addition & 1 deletion src/modules/user/services/create-user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class CreateUserService {

if (userAlreadyExists) {
return {
status: 404,
status: 400,
data: {
message: 'E-mail já cadastrado.',
},
Expand Down
5 changes: 2 additions & 3 deletions src/modules/user/services/delete-user.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Injectable } from '@nestjs/common';
import { UsersEntity } from '../../../database/entities/users.entity';
import { UserRepository } from '../repository/user.repository';

@Injectable()
export class DeleteUserService {
constructor(private userRepository: UserRepository) {}

async execute(user: UsersEntity) {
await this.userRepository.deleteUserById(user.id);
async execute(id: string) {
await this.userRepository.deleteUserById(id);

return { message: 'User deleted successfully' };
}
Expand Down
16 changes: 9 additions & 7 deletions src/modules/user/services/find-one-user.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Injectable } from '@nestjs/common';
import { UsersEntity } from '../../../database/entities/users.entity';
import { UserRepository } from '../repository/user.repository';

@Injectable()
export class FindOneUserService {
constructor(public userRepository: UserRepository) {}

async execute(user: UsersEntity) {
delete user.password;
delete user.type;
delete user.ip;
delete user.recoverPasswordToken;
async execute(id: string) {

return user;
const userExists = await this.userRepository.findOneById(id)

delete userExists.password;
delete userExists.type;
delete userExists.ip;
delete userExists.recoverPasswordToken;

return userExists;
}
}
Loading

0 comments on commit c8c095c

Please sign in to comment.