Skip to content

Commit

Permalink
Merge pull request #10 from fga-eps-mds/feat#95/conf-swagger
Browse files Browse the repository at this point in the history
[FEAT] Adiciona Swagger à API (fga-eps-mds/2024.2-ARANDU-DOC#95)
  • Loading branch information
GabrielCostaDeOliveira authored Dec 8, 2024
2 parents 5441d32 + 1b85bae commit e831d28
Show file tree
Hide file tree
Showing 31 changed files with 235 additions and 133 deletions.
3 changes: 2 additions & 1 deletion .env.dev.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ MICROSOFT_CLIENT_ID=
MICROSOFT_CLIENT_SECRET=
MICROSOFT_TENANT_ID=
MICROSOFT_CALLBACK_URL=
FRONTEND_URL=
FRONTEND_URL=
NODE_ENV=development
3 changes: 2 additions & 1 deletion .env.prod.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ MICROSOFT_CLIENT_ID=
MICROSOFT_CLIENT_SECRET=
MICROSOFT_TENANT_ID=
MICROSOFT_CALLBACK_URL=
FRONTEND_URL=
FRONTEND_URL=
NODE_ENV=production
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@nestjs/microservices": "^10.3.10",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^8.1.0",
"amqp-connection-manager": "^4.1.14",
"amqplib": "^0.10.4",
"express": "^4.19.2",
Expand Down
26 changes: 13 additions & 13 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import {
Controller,
Post,
Body,
UnauthorizedException,
Controller,
Get,
UseGuards,
Req,
Res,
Logger,
Post,
Put,
Req,
Res,
UnauthorizedException,
UseGuards,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { LoginDto } from 'src/users/dtos/login.dto';
import { AuthGuard } from '@nestjs/passport';
import { Response, Request } from 'express';
import { RefreshTokenDto } from 'src/users/dtos/refresh-tokens.dto';
import { ChangePasswordDto } from 'src/users/dtos/change-password.dto';
import { Request, Response } from 'express';
import { ChangePasswordDto } from 'src/dtos/change-password.dto';
import { ForgotPasswordDto } from 'src/dtos/forgot-password.dto';
import { LoginDto } from 'src/dtos/login.dto';
import { RefreshTokenDto } from 'src/dtos/refresh-tokens.dto';
import { ResetPasswordDto } from 'src/dtos/reset-password.dto';
import { AuthService } from './auth.service';
import { JwtAuthGuard } from './guards/auth.guard';
import { ForgotPasswordDto } from 'src/users/dtos/forgot-password.dto';
import { ResetPasswordDto } from 'src/users/dtos/reset-password.dto';

@Controller('auth')
export class AuthController {
Expand Down
24 changes: 12 additions & 12 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {
Injectable,
Logger,
NotFoundException,
UnauthorizedException,
Injectable,
Logger,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { UsersService } from '../users/users.service';
import { InjectModel } from '@nestjs/mongoose';
import * as bcrypt from 'bcryptjs';
import { v4 as uuidv4 } from 'uuid';
import { RefreshToken } from 'src/users/interface/refresh-token.schema';
import { Response } from 'express';
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { User } from 'src/users/interface/user.interface';
import { nanoid } from 'nanoid';
import { ResetPasswordDto } from 'src/dtos/reset-password.dto';
import { EmailService } from 'src/users/email.service';
import { RefreshToken } from 'src/users/interface/refresh-token.schema';
import { ResetToken } from 'src/users/interface/reset-token.schema';
import { Response } from 'express';
import { ConfigService } from '@nestjs/config';
import { ResetPasswordDto } from 'src/users/dtos/reset-password.dto';
import { User } from 'src/users/interface/user.interface';
import { v4 as uuidv4 } from 'uuid';
import { UsersService } from '../users/users.service';

@Injectable()
export class AuthService {
Expand Down
2 changes: 1 addition & 1 deletion src/auth/guards/roles.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SetMetadata } from '@nestjs/common';
import { UserRole } from 'src/users/dtos/user-role.enum';
import { UserRole } from 'src/dtos/user-role.enum';

export const Roles = (...roles: UserRole[]) => SetMetadata('roles', roles);
4 changes: 2 additions & 2 deletions src/auth/guards/roles.guard.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable prettier/prettier */
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { JwtService } from '@nestjs/jwt';
import { UserRole } from 'src/users/dtos/user-role.enum';
import { IncomingHttpHeaders } from 'http';
import { UserRole } from 'src/dtos/user-role.enum';

@Injectable()
export class RolesGuard implements CanActivate {
Expand Down
20 changes: 20 additions & 0 deletions src/dtos/change-password.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString, MinLength } from 'class-validator';

export class ChangePasswordDto {

@ApiProperty({
example: 'SenhaAntiga123',
required: true
})
@IsString()
oldPassword: string;

@ApiProperty({
example: 'SenhaNova123',
required: true
})
@IsString()
@MinLength(6)
newPassword: string;
}
33 changes: 33 additions & 0 deletions src/dtos/create-user-federated.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsNotEmpty } from 'class-validator';

export class CreateUserDtoFederated {

@ApiProperty({
example: 'Nome',
required: true
})
@IsNotEmpty()
name: string;

@ApiProperty({
example: '[email protected]',
required: true
})
@IsEmail()
@IsNotEmpty()
email: string;

@ApiProperty({
example: 'Username',
required: true
})
@IsNotEmpty()
username: string;

@ApiProperty({
example: 'Senha123',
required: false
})
password?: string;
}
42 changes: 42 additions & 0 deletions src/dtos/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsEnum, IsNotEmpty, IsOptional } from 'class-validator';
import { UserRole } from './user-role.enum';

export class CreateUserDto {
@ApiProperty({
example: 'Nome',
required: true
})
@IsNotEmpty()
name: string;

@ApiProperty({
example: '[email protected]',
required: true
})
@IsEmail()
@IsNotEmpty()
email: string;

@ApiProperty({
example: 'Username',
required: true
})
@IsNotEmpty()
username: string;

@ApiProperty({
example: 'Senha123',
required: true
})
@IsNotEmpty()
password: string;

@ApiProperty({
example: 'aluno',
required: false
})
@IsOptional()
@IsEnum(UserRole)
role?: UserRole;
}
11 changes: 11 additions & 0 deletions src/dtos/forgot-password.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail } from 'class-validator';

export class ForgotPasswordDto {
@ApiProperty({
example: '[email protected]',
required: true
})
@IsEmail()
email: string;
}
15 changes: 15 additions & 0 deletions src/dtos/login.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ApiProperty } from "@nestjs/swagger";

export class LoginDto {
@ApiProperty({
example: '[email protected]',
required: true
})
readonly email: string;

@ApiProperty({
example: 'Senha123',
required: true
})
readonly password: string;
}
12 changes: 12 additions & 0 deletions src/dtos/refresh-tokens.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';

export class RefreshTokenDto {

@ApiProperty({
example: 'jwt_refresh_token',
required: true
})
@IsString()
refreshToken: string;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString, Matches, MinLength } from 'class-validator';

export class ResetPasswordDto {
@ApiProperty({
example: 'jwt_reset_token',
required: true
})
@IsString()
resetToken: string;

@ApiProperty({
example: 'SenhaNova123',
required: true
})
@IsString()
@MinLength(6)
@Matches(/^(?=.*[0-9])/, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsEnum } from 'class-validator';
import { UserRole } from './user-role.enum';

export class UpdateRoleDto {
@ApiProperty({
example: 'professor',
required: true
})
@IsEnum(UserRole)
role: UserRole;
}
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { Logger, ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';

const configService = new ConfigService();
const logger = new Logger('Main');

async function bootstrap() {
const app = await NestFactory.create(AppModule);

logger.log(`Application runnning at ${configService.get('NODE_ENV')}`);
if ( configService.get('NODE_ENV') !== 'production' ) {
const config = new DocumentBuilder()
.setTitle('ARANDU')
.setDescription('Endpoints do UserService ARANDU')
.setVersion('1.0')
.addTag('UserService')
.build();
const documentFactory = () => SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, documentFactory);
}

app.useGlobalPipes(new ValidationPipe());
app.enableCors();
await app.listen(configService.get('PORT'), '0.0.0.0', () => {
Expand Down
10 changes: 0 additions & 10 deletions src/users/dtos/change-password.dto.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/users/dtos/create-user-federated.dto.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/users/dtos/create-user.dto.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/users/dtos/forgot-password.dto.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/users/dtos/login.dto.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/users/dtos/refresh-tokens.dto.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/users/interface/user.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mongoose, { Document } from 'mongoose';
import { UserRole } from '../dtos/user-role.enum';
import { UserRole } from '../../dtos/user-role.enum';

export interface User extends Document {
name: string;
Expand Down
4 changes: 2 additions & 2 deletions src/users/interface/user.schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as mongoose from 'mongoose';
import * as bcrypt from 'bcryptjs';
import * as mongoose from 'mongoose';
import { UserRole } from '../../dtos/user-role.enum';
import { User } from './user.interface';
import { UserRole } from '../dtos/user-role.enum';

export const UserSchema = new mongoose.Schema(
{
Expand Down
Loading

0 comments on commit e831d28

Please sign in to comment.