Skip to content

Commit

Permalink
Merge pull request #148 from wandersonDeve/main
Browse files Browse the repository at this point in the history
feat: set catch in db calls
  • Loading branch information
wandersonDeve authored Oct 1, 2023
2 parents 4414ef2 + 0da75b7 commit 454fc30
Show file tree
Hide file tree
Showing 55 changed files with 676 additions and 697 deletions.
3 changes: 1 addition & 2 deletions src/modules/applications/applications.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { CurriculumEntity } from '../../database/entities/curriculum.entity';
import { JobsEntity } from '../../database/entities/jobs.entity';
import { UsersEntity } from '../../database/entities/users.entity';
import GetEntity from '../../shared/pipes/pipe-entity.pipe';
import { LoggedAdmin } from '../auth/decorator/logged-admin.decorator';
import { ApplicationsService } from './applications.service';
import { LoggedUser } from '../auth/decorator/logged-user.decorator';
import { ApplicationsService } from './applications.service';

@ApiTags('Applications')
@ApiBearerAuth()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { EntityRepository, Repository } from 'typeorm';
import { ApplicationEntity } from '../../../database/entities/applications.entity';
import { handleError } from '../../../shared/utils/handle-error.util';

@EntityRepository(ApplicationEntity)
export class ApplicationsRepository extends Repository<ApplicationEntity> {
async saveApplication(data: any): Promise<ApplicationEntity> {
return this.save(data);
return this.save(data).catch(handleError);
}
}
14 changes: 2 additions & 12 deletions src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import {
Body,
Controller,
Get,
Post,
Res,
UseGuards
} from '@nestjs/common';
import { Body, Controller, Get, Post, Res, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import {
ApiBearerAuth,
ApiTags
} from '@nestjs/swagger';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { Response } from 'express';
import { LoginSwagger } from 'src/shared/Swagger/decorators/auth/login.swagger';
import { UserLoggedSwagger } from 'src/shared/Swagger/decorators/auth/user-logged.swagger';
Expand Down
7 changes: 2 additions & 5 deletions src/modules/company/company.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ import {
Res,
UploadedFile,
UseGuards,
UseInterceptors
UseInterceptors,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { FileInterceptor } from '@nestjs/platform-express';
import {
ApiBearerAuth,
ApiTags
} from '@nestjs/swagger';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { Response } from 'express';
import { CompaniesEntity } from 'src/database/entities/companies.entity';
import { ActivateCompanySwagger } from 'src/shared/Swagger/decorators/company/activate-company.swagger';
Expand Down
2 changes: 1 addition & 1 deletion src/modules/company/dtos/decorators/match.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ export class MatchConstraint implements ValidatorConstraintInterface {
const relatedValue = (args.object as any)[relatedPropertyName];
return value === relatedValue;
}
}
}
6 changes: 4 additions & 2 deletions src/modules/company/dtos/update-my-password.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export class UpdateMyPasswordDto {
@IsNotEmpty({ message: "O campo 'password' não pode ficar vazio" })
@Length(8, 50)
@Matches(/((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/, {
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.',
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: 'Senha de Login',
Expand All @@ -23,7 +24,8 @@ export class UpdateMyPasswordDto {
@IsNotEmpty({ message: "O campo 'confirmPassword' não pode ficar vazio" })
@Length(8, 50)
@Matches(/((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/, {
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.',
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: 'Senha de Login',
Expand Down
4 changes: 2 additions & 2 deletions src/modules/company/repository/company-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { UpdateMyPasswordDto } from '../dtos/update-my-password.dto';
export class CompanyRepository extends Repository<CompaniesEntity> {
async createCompany(data: CreateCompanyDto): Promise<CompaniesEntity> {
delete data.passwordConfirmation;
return this.save(data);
return this.save(data).catch(handleError);
}

async findAllCompany(
Expand Down Expand Up @@ -89,7 +89,7 @@ export class CompanyRepository extends Repository<CompaniesEntity> {

company.recoverPasswordToken = recoverPasswordToken;

await this.save(company);
await this.save(company).catch(handleError);

return company;
}
Expand Down
8 changes: 2 additions & 6 deletions src/modules/jobs/jobs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ import {
Post,
Put,
Query,
UseGuards
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import {
ApiBearerAuth,
ApiOperation,
ApiTags
} from '@nestjs/swagger';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { ArchiveJobSwagger } from 'src/shared/Swagger/decorators/jobs/archive-job.swagger';
import { CreateNewJobSwagger } from 'src/shared/Swagger/decorators/jobs/create-new-job.swagger';
import { GetOneJobSwagger } from 'src/shared/Swagger/decorators/jobs/get-one-job.swagger';
Expand Down
2 changes: 1 addition & 1 deletion src/modules/jobs/repository/job.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class JobRepository extends Repository<JobsEntity> {
])
.where('jobs.id = :id', { id });

return queryBuilder.getOne();
return queryBuilder.getOne().catch(handleError);
}

async updateJob(id: string, data: UpdateJobDto) {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/mails/mail.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { MailService } from './mail.service';
pass: config.get('MAIL_PASSWORD'),
},
tls: {
rejectUnauthorized: false
}
rejectUnauthorized: false,
},
},
defaults: {
from: `no-reply <[email protected]>`,
Expand Down
52 changes: 26 additions & 26 deletions src/modules/user/decorators/match.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,28 +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,
});
};
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;
}

@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;
}
}
}
6 changes: 4 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,17 @@ export class CreatePasswordHashDto {
message: 'Senha muito fraca',
})
@ApiProperty({
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.',
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: 'A senha precisa ter no mínimo 8 caracteres, máximo de 50, uma letra maiúscula, um número e um símbolo.',
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/repository/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class UserRepository extends Repository<UsersEntity> {

user.mailConfirm = true;

await this.update(id, { mailConfirm: true });
await this.update(id, { mailConfirm: true }).catch(handleError);

return this.findOne(id);
}
Expand Down
3 changes: 1 addition & 2 deletions src/modules/user/services/find-one-user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ export class FindOneUserService {
constructor(public userRepository: UserRepository) {}

async execute(id: string) {

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

delete userExists.password;
delete userExists.type;
Expand Down
40 changes: 20 additions & 20 deletions src/shared/Swagger/decorators/auth/login.swagger.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { HttpStatus, applyDecorators } from "@nestjs/common";
import { ApiOperation, ApiResponse } from "@nestjs/swagger";
import { UserLoginResponseDto } from "src/modules/auth/dtos/user-login-response.dto";
import { BadRequestSwagger } from "../../bad-request.swagger";
import { HttpStatus, applyDecorators } from '@nestjs/common';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
import { UserLoginResponseDto } from 'src/modules/auth/dtos/user-login-response.dto';
import { BadRequestSwagger } from '../../bad-request.swagger';

export function LoginSwagger() {
return applyDecorators(
ApiResponse({
status: HttpStatus.OK,
description: 'Exemplo do retorno de sucesso da rota',
type: UserLoginResponseDto,
}),
ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: 'Modelo de erro',
type: BadRequestSwagger,
}),
ApiOperation({
summary: 'Rota para fazer login na plataforma',
})
)
}
return applyDecorators(
ApiResponse({
status: HttpStatus.OK,
description: 'Exemplo do retorno de sucesso da rota',
type: UserLoginResponseDto,
}),
ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: 'Modelo de erro',
type: BadRequestSwagger,
}),
ApiOperation({
summary: 'Rota para fazer login na plataforma',
}),
);
}
42 changes: 21 additions & 21 deletions src/shared/Swagger/decorators/auth/user-logged.swagger.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { HttpStatus, applyDecorators } from "@nestjs/common";
import { ApiOperation, ApiResponse } from "@nestjs/swagger";
import { LoggerUserType } from "src/modules/auth/types/logged-user.types";
import { UnauthorizedSwagger } from "../../unauthorized.swagger";
import { HttpStatus, applyDecorators } from '@nestjs/common';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
import { LoggerUserType } from 'src/modules/auth/types/logged-user.types';
import { UnauthorizedSwagger } from '../../unauthorized.swagger';

export function UserLoggedSwagger(){
return applyDecorators(
ApiResponse({
status: HttpStatus.OK,
description: 'Exemplo do retorno de sucesso da rota',
type: LoggerUserType,
}),
ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: 'Modelo de erro',
type: UnauthorizedSwagger,
}),
ApiOperation({
summary: 'Retorna usuário logado',
})
)
}
export function UserLoggedSwagger() {
return applyDecorators(
ApiResponse({
status: HttpStatus.OK,
description: 'Exemplo do retorno de sucesso da rota',
type: LoggerUserType,
}),
ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: 'Modelo de erro',
type: UnauthorizedSwagger,
}),
ApiOperation({
summary: 'Retorna usuário logado',
}),
);
}
16 changes: 8 additions & 8 deletions src/shared/Swagger/decorators/comment/create-comment.swagger.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { applyDecorators } from "@nestjs/common";
import { ApiOperation } from "@nestjs/swagger";
import { applyDecorators } from '@nestjs/common';
import { ApiOperation } from '@nestjs/swagger';

export function CreateCommentSwagger() {
return applyDecorators(
ApiOperation({
summary: 'Cadastrar um comentário.',
})
)
}
return applyDecorators(
ApiOperation({
summary: 'Cadastrar um comentário.',
}),
);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { applyDecorators } from "@nestjs/common";
import { ApiOperation } from "@nestjs/swagger";
import { applyDecorators } from '@nestjs/common';
import { ApiOperation } from '@nestjs/swagger';

export function DeleteCommentarySwagger() {
return applyDecorators(
ApiOperation({
summary: 'Excluir um comentário por id.',
})
)
}
return applyDecorators(
ApiOperation({
summary: 'Excluir um comentário por id.',
}),
);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { applyDecorators } from "@nestjs/common";
import { ApiOperation } from "@nestjs/swagger";
import { applyDecorators } from '@nestjs/common';
import { ApiOperation } from '@nestjs/swagger';

export function GetAllCommentariesSwagger() {
return applyDecorators(
ApiOperation({
summary: 'Encontrar todos os comentários.',
})
)
}
return applyDecorators(
ApiOperation({
summary: 'Encontrar todos os comentários.',
}),
);
}
Loading

0 comments on commit 454fc30

Please sign in to comment.