Skip to content

Commit

Permalink
Update api method and error code
Browse files Browse the repository at this point in the history
  • Loading branch information
thongnguyen5 authored and thongnguyendev committed Apr 13, 2024
1 parent 87012f0 commit 80000b2
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 13 deletions.
3 changes: 2 additions & 1 deletion app/apps/onebox/src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Req,
UseGuards,
} from '@nestjs/common';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Request } from 'express';
import { User } from '../users/schemas/user.schema';
import { AuthService } from './auth.service';
Expand All @@ -19,6 +19,7 @@ import { LocalAuthGuard } from './guards/local-auth.guard';
export class AuthController {
constructor(private authService: AuthService) {}

@ApiOperation({ summary: 'Login' })
@UseGuards(LocalAuthGuard)
@Post('login')
@HttpCode(200)
Expand Down
6 changes: 3 additions & 3 deletions app/apps/onebox/src/modules/monitor/monitor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
Delete,
Get,
Param,
Patch,
Post,
Put,
Query,
Req,
UseGuards,
Expand Down Expand Up @@ -75,7 +75,7 @@ export class MonitorController {
@ApiBearerAuth('JWT')
@UseGuards(JwtAuthGuard)
@Delete('/:id')
@ApiCreatedResponse({ type: DeleteMonitorResponseDto })
@ApiOkResponse({ type: DeleteMonitorResponseDto })
async deleteMonitor(
@Req() req: Request,
@Param('id') monitorId: string,
Expand All @@ -89,7 +89,7 @@ export class MonitorController {
@ApiOperation({ summary: 'Update monitor' })
@ApiBearerAuth('JWT')
@UseGuards(JwtAuthGuard)
@Put('/:id')
@Patch('/:id')
@ApiOkResponse({ type: MonitorResponseDto })
async updateMonitor(
@Req() req: Request,
Expand Down
6 changes: 5 additions & 1 deletion app/apps/onebox/src/modules/monitor/monitor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ export class MonitorService {
user: User,
request: DeleteMonitorDto,
): Promise<DeleteMonitorResponseDto> {
const monitor = await this.findAndAuthMonitor(user, request.monitorId);
const monitor = await this.monitorRepository.findById(request.monitorId);
if (!monitor) {
return Builder<DeleteMonitorResponseDto>().success(true).build();
}
await this.projectService.checkProjectPermission(user, monitor.projectId);
await this.webhookService.deleteWebhook(monitor.webhookId);
await this.monitorRepository.deleteMonitor(monitor.monitorId);
await this.projectRepository.increaseMonitorCount(monitor.projectId, -1);
Expand Down
22 changes: 16 additions & 6 deletions app/apps/onebox/src/modules/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Body,
Controller,
Get,
HttpCode,
Post,
Req,
UseGuards,
Expand All @@ -11,6 +12,7 @@ import {
ApiBearerAuth,
ApiCreatedResponse,
ApiOkResponse,
ApiOperation,
ApiTags,
} from '@nestjs/swagger';
import { Request } from 'express';
Expand All @@ -21,21 +23,22 @@ import {
ChangePasswordResponseDto,
} from './dto/change-password.dto';
import { CreateUserDto } from './dto/create-user.dto';
import { UserProfileDto } from './dto/user-profile.dto';
import { CreateUserValidationPipe } from './users.pipe';
import { UsersService } from './users.service';
import {
ForgotPasswordDto,
ForgotPasswordResponseDto,
ResetPasswordDto,
ResetPasswordResponseDto,
} from './dto/forgot-password.dto';
import { UserProfileDto } from './dto/user-profile.dto';
import { CreateUserValidationPipe } from './users.pipe';
import { UsersService } from './users.service';

@ApiTags('User')
@Controller('user')
export class UsersController {
constructor(private usersService: UsersService) {}

@ApiOperation({ summary: 'Register new account' })
@Post('/register')
@UsePipes(new CreateUserValidationPipe())
@ApiCreatedResponse({ type: UserProfileDto })
Expand All @@ -46,6 +49,7 @@ export class UsersController {
return UserProfileDto.from(result);
}

@ApiOperation({ summary: 'Get user profile' })
@ApiBearerAuth('JWT')
@UseGuards(JwtAuthGuard)
@Get('profile')
Expand All @@ -55,10 +59,12 @@ export class UsersController {
return UserProfileDto.from(req.user as User);
}

@ApiOperation({ summary: 'Change user password' })
@ApiBearerAuth('JWT')
@UseGuards(JwtAuthGuard)
@Post('change-password')
@ApiCreatedResponse({ type: ChangePasswordResponseDto })
@HttpCode(200)
@ApiOkResponse({ type: ChangePasswordResponseDto })
async changePassword(
@Req() req: Request,
@Body() changePasswordDto: ChangePasswordDto,
Expand All @@ -69,16 +75,20 @@ export class UsersController {
);
}

@ApiOperation({ summary: 'Forgot password' })
@Post('forgot-password')
@ApiCreatedResponse({ type: ForgotPasswordResponseDto })
@HttpCode(200)
@ApiOkResponse({ type: ForgotPasswordResponseDto })
async forgotPassword(
@Body() forgotPasswordDto: ForgotPasswordDto,
): Promise<ForgotPasswordResponseDto> {
return this.usersService.forgotPassword(forgotPasswordDto);
}

@ApiOperation({ summary: 'Reset password' })
@Post('reset-password')
@ApiCreatedResponse({ type: ResetPasswordResponseDto })
@HttpCode(200)
@ApiOkResponse({ type: ResetPasswordResponseDto })
async resetPassword(
@Body() resetPasswordDto: ResetPasswordDto,
): Promise<ResetPasswordResponseDto> {
Expand Down
4 changes: 2 additions & 2 deletions app/libs/global/src/global.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import { ErrorCode } from './global.error';
@Controller('global')
export class GlobalController {
@ApiOperation({ summary: 'List all networks' })
@ApiOkResponse()
@ApiOkResponse({ type: [MonitorNetwork] })
@Get('/networks')
async getNetworks(): Promise<MonitorNetwork[]> {
return Object.values(MonitorNetwork);
}

@ApiOperation({ summary: 'List all error codes' })
@ApiOkResponse()
@ApiOkResponse({ type: [ErrorCode] })
@Get('/error-codes')
async getErrorCodes(): Promise<ErrorCode[]> {
return ErrorCode.ALL_ERRORS;
Expand Down
7 changes: 7 additions & 0 deletions app/libs/global/src/global.error.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { ApiResponseProperty } from '@nestjs/swagger';
import { ServiceException } from './global.exception';

export class ErrorCode {
static ALL_ERRORS: ErrorCode[] = [];

@ApiResponseProperty()
code: number;
@ApiResponseProperty()
description: string;
constructor(code: number, description: string) {
this.code = code;
Expand Down Expand Up @@ -42,6 +45,10 @@ export class ErrorCode {
static ACCOUNT_NOT_FOUND = new ErrorCode(404002, 'Account Not Exists');
static PROJECT_NOT_FOUND = new ErrorCode(404003, 'Project Not Found');
static MONITOR_NOT_FOUND = new ErrorCode(404004, 'Monitor Not Found');
static API_NOT_FOUND = new ErrorCode(404005, 'API Not Found');

// Method Not Allowed - Client send request with invalid method
static METHOD_NOT_ALLOWED = new ErrorCode(405001, 'Method Not Allowed');

// Internal Server Error - Something went wrong
static INTERNAL_SERVER_ERROR = new ErrorCode(500001, 'Internal Server Error');
Expand Down
14 changes: 14 additions & 0 deletions app/libs/global/src/global.exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
ExceptionFilter,
HttpException,
Logger,
MethodNotAllowedException,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
Expand All @@ -19,6 +21,18 @@ export function handleException(exception): HttpException {
if (exception instanceof BadRequestException) {
return ErrorCode.BAD_REQUEST.asException(null, exception.getResponse());
}
if (exception instanceof NotFoundException) {
return ErrorCode.API_NOT_FOUND.asException(
exception.message,
exception.getResponse(),
);
}
if (exception instanceof MethodNotAllowedException) {
return ErrorCode.METHOD_NOT_ALLOWED.asException(
exception.message,
exception.getResponse(),
);
}
if (exception instanceof ServiceException) {
return exception;
}
Expand Down

0 comments on commit 80000b2

Please sign in to comment.