Refactor group controller methods and add device product guard

This commit is contained in:
faris Aljohari
2024-04-21 16:27:53 +03:00
parent 18e7e35d35
commit 7abfe29746
8 changed files with 284 additions and 260 deletions
+56 -34
View File
@@ -5,17 +5,18 @@ import {
Get,
Post,
UseGuards,
Query,
Param,
Put,
Delete,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
import { AddGroupDto } from '../dtos/add.group.dto';
import { GetGroupDto } from '../dtos/get.group.dto';
import { ControlGroupDto } from '../dtos/control.group.dto';
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
import { CheckProductUuidForAllDevicesGuard } from 'src/guards/device.product.guard';
@ApiTags('Group Module')
@Controller({
@@ -25,34 +26,43 @@ import { RenameGroupDto } from '../dtos/rename.group.dto copy';
export class GroupController {
constructor(private readonly groupService: GroupService) {}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get()
async getGroupsByHomeId(@Query() getGroupsDto: GetGroupDto) {
// @ApiBearerAuth()
// @UseGuards(JwtAuthGuard)
@Get('space/:spaceUuid')
async getGroupsBySpaceUuid(@Param('spaceUuid') spaceUuid: string) {
try {
return await this.groupService.getGroupsByHomeId(getGroupsDto);
} catch (err) {
throw new Error(err);
return await this.groupService.getGroupsBySpaceUuid(spaceUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':groupId')
async getGroupsByGroupId(@Param('groupId') groupId: number) {
// @ApiBearerAuth()
// @UseGuards(JwtAuthGuard)
@Get(':groupUuid')
async getGroupsByGroupId(@Param('groupUuid') groupUuid: string) {
try {
return await this.groupService.getGroupsByGroupId(groupId);
} catch (err) {
throw new Error(err);
return await this.groupService.getGroupsByGroupUuid(groupUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
// @ApiBearerAuth()
@UseGuards(CheckProductUuidForAllDevicesGuard)
@Post()
async addGroup(@Body() addGroupDto: AddGroupDto) {
try {
return await this.groupService.addGroup(addGroupDto);
} catch (err) {
throw new Error(err);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@@ -67,25 +77,37 @@ export class GroupController {
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Put('rename')
async renameGroup(@Body() renameGroupDto: RenameGroupDto) {
// @ApiBearerAuth()
// @UseGuards(JwtAuthGuard)
@Put('rename/:groupUuid')
async renameGroupByUuid(
@Param('groupUuid') groupUuid: string,
@Body() renameGroupDto: RenameGroupDto,
) {
try {
return await this.groupService.renameGroup(renameGroupDto);
} catch (err) {
throw new Error(err);
return await this.groupService.renameGroupByUuid(
groupUuid,
renameGroupDto,
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Delete(':groupId')
async deleteGroup(@Param('groupId') groupId: number) {
// @ApiBearerAuth()
// @UseGuards(JwtAuthGuard)
@Delete(':groupUuid')
async deleteGroup(@Param('groupUuid') groupUuid: string) {
try {
return await this.groupService.deleteGroup(groupId);
} catch (err) {
throw new Error(err);
return await this.groupService.deleteGroup(groupUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}