diff --git a/src/group/controllers/group.controller.ts b/src/group/controllers/group.controller.ts index a37abc4..1db7a11 100644 --- a/src/group/controllers/group.controller.ts +++ b/src/group/controllers/group.controller.ts @@ -7,12 +7,14 @@ import { UseGuards, Query, Param, + Put, } 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'; @ApiTags('Group Module') @Controller({ @@ -57,4 +59,15 @@ export class GroupController { throw new Error(err); } } + + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Put('rename') + async renameGroup(@Body() renameGroupDto: RenameGroupDto) { + try { + return await this.groupService.renameGroup(renameGroupDto); + } catch (err) { + throw new Error(err); + } + } } diff --git a/src/group/dtos/control.group.dto.ts b/src/group/dtos/control.group.dto.ts index 36040b8..33a6870 100644 --- a/src/group/dtos/control.group.dto.ts +++ b/src/group/dtos/control.group.dto.ts @@ -1,12 +1,12 @@ import { ApiProperty } from '@nestjs/swagger'; -import { IsNotEmpty, IsString, IsObject } from 'class-validator'; +import { IsNotEmpty, IsObject, IsNumberString } from 'class-validator'; export class ControlGroupDto { @ApiProperty({ description: 'groupId', required: true, }) - @IsString() + @IsNumberString() @IsNotEmpty() public groupId: string; diff --git a/src/group/dtos/rename.group.dto copy.ts b/src/group/dtos/rename.group.dto copy.ts new file mode 100644 index 0000000..a85f41b --- /dev/null +++ b/src/group/dtos/rename.group.dto copy.ts @@ -0,0 +1,20 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString, IsNumberString } from 'class-validator'; + +export class RenameGroupDto { + @ApiProperty({ + description: 'groupId', + required: true, + }) + @IsNumberString() + @IsNotEmpty() + public groupId: string; + + @ApiProperty({ + description: 'groupName', + required: true, + }) + @IsString() + @IsNotEmpty() + public groupName: string; +} diff --git a/src/group/interfaces/get.group.interface.ts b/src/group/interfaces/get.group.interface.ts index 94dec62..69c5050 100644 --- a/src/group/interfaces/get.group.interface.ts +++ b/src/group/interfaces/get.group.interface.ts @@ -24,3 +24,9 @@ export class controlGroupInterface { result: boolean; msg: string; } + +export class renameGroupInterface { + success: boolean; + result: boolean; + msg: string; +} diff --git a/src/group/services/group.service.ts b/src/group/services/group.service.ts index 286e21f..c534d39 100644 --- a/src/group/services/group.service.ts +++ b/src/group/services/group.service.ts @@ -7,9 +7,11 @@ import { GetRoomDetailsInterface, addGroupInterface, controlGroupInterface, + renameGroupInterface, } from '../interfaces/get.group.interface'; import { GetGroupDto } from '../dtos/get.group.dto'; import { ControlGroupDto } from '../dtos/control.group.dto'; +import { RenameGroupDto } from '../dtos/rename.group.dto copy'; @Injectable() export class GroupService { @@ -158,4 +160,35 @@ export class GroupService { ); } } + + async renameGroup(renameGroupDto: RenameGroupDto) { + const response = await this.renameGroupTuya(renameGroupDto); + + if (response.success) { + return response; + } else { + throw new HttpException( + response.msg || 'Unknown error', + HttpStatus.BAD_REQUEST, + ); + } + } + async renameGroupTuya( + renameGroupDto: RenameGroupDto, + ): Promise { + try { + const path = `/v2.0/cloud/thing/group/${renameGroupDto.groupId}/${renameGroupDto.groupName}`; + const response = await this.tuya.request({ + method: 'PUT', + path, + }); + + return response as renameGroupInterface; + } catch (error) { + throw new HttpException( + 'Error control group', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } }