Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions .env.example

This file was deleted.

47 changes: 47 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/bcrypt": "^5.0.2",
"@types/bcryptjs": "^2.4.6",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/multer": "^1.4.11",
"@types/node": "^20.3.1",
"@types/passport-jwt": "^4.0.1",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class AuthController {
@ApiBadRequestResponse({ description: 'Bad Request' })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
@ApiInternalServerErrorResponse({ description: 'Server Error' })
@Post('login')
@Post('/login')
login(@Body() dto: LoginUserDto) {
return this.authService.login(dto);
}
Expand Down
24 changes: 24 additions & 0 deletions src/auth/guards/guards.new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Injectable, CanActivate, ExecutionContext, ForbiddenException } from "@nestjs/common";
import { Reflector } from "@nestjs/core";
import { User } from "aws-sdk/clients/budgets";

@Injectable()
export class newGuard implements CanActivate {

constructor(private readonly reflector: Reflector){}

canActivate(context: ExecutionContext): boolean {

const id = this.reflector.get<string[]>('id_thomas', context.getHandler());
const req = context.switchToHttp().getRequest()
const user = req.user

if (id == user.id) return true;

throw new ForbiddenException('USER NOT VALIDE')

//setmetadata

}

}
1 change: 1 addition & 0 deletions src/auth/guards/user-role.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ export class UserRoleGuard implements CanActivate {

throw new ForbiddenException(`User need a valid role: ${validRoles}`);
}

}
5 changes: 3 additions & 2 deletions src/books/books.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { Auth } from 'src/auth/decorators/auth.decorator';
import { ValidRoles } from 'src/auth/interfaces/valid-roles';
import { FilesInterceptor } from '@nestjs/platform-express';
import { UploadFilesDto } from './dto/upload-files.dto';
import { interceptor } from './intercept/intercept.service';

@ApiTags('Books')
@Controller('books')
Expand Down Expand Up @@ -69,14 +70,14 @@ export class BooksController {
return this.booksService.upload(id, dto, files, req.user);
}

@Auth()
@ApiOperation({ summary: 'Get all books' })
@ApiOkResponse({ description: 'Success' })
@ApiNotFoundResponse({ description: 'Not Found' })
@ApiBadRequestResponse({ description: 'Bad Request' })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
@ApiInternalServerErrorResponse({ description: 'Server Error' })
@Get()
@UseInterceptors(interceptor)
@Get('/get')
findAll(@Query() dto: PaginationDto) {
return this.booksService.findAll(dto);
}
Expand Down
7 changes: 6 additions & 1 deletion src/books/books.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import { Book } from './entities/book.entity';
import { BookFile } from './entities/book-file.entity';
import { User } from 'src/users/entities/user.entity';
import { AuthModule } from 'src/auth/auth.module';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { interceptor } from './intercept/intercept.service';

@Module({
imports: [TypeOrmModule.forFeature([Book, BookFile, User]), AuthModule],
controllers: [BooksController],
providers: [BooksService],
providers: [BooksService, {
provide: APP_INTERCEPTOR,
useClass: interceptor
}],
})
export class BooksModule {}
3 changes: 3 additions & 0 deletions src/books/books.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
BadRequestException,
CallHandler,
ExecutionContext,
Injectable,
InternalServerErrorException,
NotFoundException,
Expand All @@ -14,6 +16,7 @@ import { UploadFilesDto } from './dto/upload-files.dto';
import { Book } from './entities/book.entity';
import { User } from 'src/users/entities/user.entity';
import { BookFile } from './entities/book-file.entity';
import { Observable } from 'rxjs';

@Injectable()
export class BooksService {
Expand Down
24 changes: 24 additions & 0 deletions src/books/intercept/intercept.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CallHandler, ExecutionContext, HttpException, HttpStatus, NestInterceptor } from "@nestjs/common";
import { Observable, throwError } from "rxjs";
import { TimeoutError } from "rxjs";
import { catchError, tap, timeout} from "rxjs/operators";

export class interceptor implements NestInterceptor {

intercept(context: ExecutionContext, next: CallHandler): Observable<any>{
console.log('before.......')

const now = Date.now()
const timeoutDuration = 30000

return next.handle().pipe(timeout(timeoutDuration),
tap(() => console.log(`After... ${Date.now() - now}/ms`)),
catchError(err => {
if(err instanceof TimeoutError){
return throwError(() => {new HttpException('Request time out', HttpStatus.REQUEST_TIMEOUT)})
}
return throwError(() => {})
})
)
}
}
26 changes: 26 additions & 0 deletions src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
Query,
Request,
ParseUUIDPipe,
UseGuards,
SetMetadata,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
Expand All @@ -26,6 +28,8 @@ import {
import { PaginationDto } from 'src/common/dto/pagination.dto';
import { Auth } from 'src/auth/decorators/auth.decorator';
import { ValidRoles } from 'src/auth/interfaces/valid-roles';
import { newGuard } from 'src/auth/guards/guards.new';
import { AuthGuard } from '@nestjs/passport';

@ApiTags('Users')
@Controller('users')
Expand Down Expand Up @@ -72,4 +76,26 @@ export class UsersController {
) {
return this.usersService.updateRole(id, role, req.user);
}

@Auth()
@ApiOperation({ summary: 'Update user role' })
@ApiOkResponse({ description: 'Success' })
@ApiNotFoundResponse({ description: 'Not Found' })
@ApiBadRequestResponse({ description: 'Bad Request' })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
@ApiInternalServerErrorResponse({ description: 'Server Error' })
@UseGuards(newGuard)
@Get('/coder/:id')
async GetUser(@Param('id') idString: string) {
return await this.usersService.findById(idString)
}


@SetMetadata('id_thomas', "496a03b9-0488-4065-bffe-ad219b2d848a" )
@UseGuards(AuthGuard(), newGuard)
@Get('/id/496a03b9-0488-4065-bffe-ad219b2d848a')
async getUser(){
return this.usersService.giveData()
}

}
15 changes: 15 additions & 0 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ export class UsersService {
}
}

async findById(id: string){
const userFind = await this.userRepository.findOne({where:{id: id}})
if(!userFind){
throw new NotFoundException('USER NOT FOUND')
}
return userFind
}

async giveData(){
return {
"phrase":"Mi moto alpina derrapante",
"author":"Angel"
}
}

async findAll(dto: PaginationDto) {
const { limit = +process.env.LIMIT, page = 1 } = dto;

Expand Down