Refactor group and device modules

This commit is contained in:
faris Aljohari
2024-06-02 21:12:37 +03:00
parent aad3b31cf0
commit 4d1861d137
14 changed files with 137 additions and 622 deletions
+18 -75
View File
@@ -1,22 +1,16 @@
import { GroupService } from '../services/group.service';
import {
Body,
Controller,
Get,
Post,
UseGuards,
Param,
Put,
Delete,
HttpException,
HttpStatus,
Req,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AddGroupDto } from '../dtos/add.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';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
import { UnitPermissionGuard } from 'src/guards/unit.permission.guard';
@ApiTags('Group Module')
@Controller({
@@ -27,11 +21,11 @@ export class GroupController {
constructor(private readonly groupService: GroupService) {}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get('space/:spaceUuid')
async getGroupsBySpaceUuid(@Param('spaceUuid') spaceUuid: string) {
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
@Get(':unitUuid')
async getGroupsBySpaceUuid(@Param('unitUuid') unitUuid: string) {
try {
return await this.groupService.getGroupsBySpaceUuid(spaceUuid);
return await this.groupService.getGroupsByUnitUuid(unitUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
@@ -40,72 +34,21 @@ export class GroupController {
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':groupUuid')
async getGroupsByGroupId(@Param('groupUuid') groupUuid: string) {
try {
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, CheckProductUuidForAllDevicesGuard)
@Post()
async addGroup(@Body() addGroupDto: AddGroupDto) {
try {
return await this.groupService.addGroup(addGroupDto);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Post('control')
async controlGroup(@Body() controlGroupDto: ControlGroupDto) {
try {
return await this.groupService.controlGroup(controlGroupDto);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Put('rename/:groupUuid')
async renameGroupByUuid(
@Param('groupUuid') groupUuid: string,
@Body() renameGroupDto: RenameGroupDto,
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
@Get(':unitUuid/devices/:groupName')
async getUnitDevicesByGroupName(
@Param('unitUuid') unitUuid: string,
@Param('groupName') groupName: string,
@Req() req: any,
) {
try {
return await this.groupService.renameGroupByUuid(
groupUuid,
renameGroupDto,
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
const userUuid = req.user.uuid;
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Delete(':groupUuid')
async deleteGroup(@Param('groupUuid') groupUuid: string) {
try {
return await this.groupService.deleteGroup(groupUuid);
return await this.groupService.getUnitDevicesByGroupName(
unitUuid,
groupName,
userUuid,
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',