diff --git a/src/group/controllers/group.controller.ts b/src/group/controllers/group.controller.ts index 7b5b12d..be0c7a3 100644 --- a/src/group/controllers/group.controller.ts +++ b/src/group/controllers/group.controller.ts @@ -27,18 +27,24 @@ export class GroupController { @ApiBearerAuth() @UseGuards(JwtAuthGuard) - @Get(':homeId') - async userList( - @Param('homeId') homeId: string, - @Query() getGroupsDto: GetGroupDto, - ) { + @Get() + async getGroupsByHomeId(@Query() getGroupsDto: GetGroupDto) { try { - return await this.groupService.getGroupsByHomeId(homeId, getGroupsDto); + return await this.groupService.getGroupsByHomeId(getGroupsDto); + } catch (err) { + throw new Error(err); + } + } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get(':groupId') + async getGroupsByGroupId(@Param('groupId') groupId: number) { + try { + return await this.groupService.getGroupsByGroupId(groupId); } catch (err) { throw new Error(err); } } - @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Post() diff --git a/src/group/dtos/get.group.dto.ts b/src/group/dtos/get.group.dto.ts index 16f8236..aad234b 100644 --- a/src/group/dtos/get.group.dto.ts +++ b/src/group/dtos/get.group.dto.ts @@ -2,6 +2,14 @@ import { ApiProperty } from '@nestjs/swagger'; import { IsNotEmpty, IsNumberString } from 'class-validator'; export class GetGroupDto { + @ApiProperty({ + description: 'homeId', + required: true, + }) + @IsNumberString() + @IsNotEmpty() + public homeId: string; + @ApiProperty({ description: 'pageSize', required: true, diff --git a/src/group/interfaces/get.group.interface.ts b/src/group/interfaces/get.group.interface.ts index 94dec62..970c343 100644 --- a/src/group/interfaces/get.group.interface.ts +++ b/src/group/interfaces/get.group.interface.ts @@ -1,4 +1,4 @@ -export class GetRoomDetailsInterface { +export class GetGroupDetailsInterface { result: { id: string; name: string; diff --git a/src/group/services/group.service.ts b/src/group/services/group.service.ts index 48cadeb..07fbacf 100644 --- a/src/group/services/group.service.ts +++ b/src/group/services/group.service.ts @@ -3,8 +3,8 @@ import { TuyaContext } from '@tuya/tuya-connector-nodejs'; import { ConfigService } from '@nestjs/config'; import { AddGroupDto } from '../dtos/add.group.dto'; import { + GetGroupDetailsInterface, GetGroupsInterface, - GetRoomDetailsInterface, addGroupInterface, controlGroupInterface, } from '../interfaces/get.group.interface'; @@ -26,9 +26,9 @@ export class GroupService { }); } - async getGroupsByHomeId(homeId: string, getGroupDto: GetGroupDto) { + async getGroupsByHomeId(getGroupDto: GetGroupDto) { try { - const response = await this.getGroupsTuya(homeId, getGroupDto); + const response = await this.getGroupsTuya(getGroupDto); const groups = response.result.data_list.map((group: any) => ({ groupId: group.id, @@ -47,17 +47,14 @@ export class GroupService { } } - async getGroupsTuya( - homeId: string, - getGroupDto: GetGroupDto, - ): Promise { + async getGroupsTuya(getGroupDto: GetGroupDto): Promise { try { const path = `/v2.0/cloud/thing/group`; const response = await this.tuya.request({ method: 'GET', path, query: { - space_id: homeId, + space_id: getGroupDto.homeId, page_size: getGroupDto.pageSize, page_no: getGroupDto.pageNo, }, @@ -70,23 +67,7 @@ export class GroupService { ); } } - async getRoomDetails(roomId: string): Promise { - // Added return type - try { - const path = `/v2.0/cloud/space/${roomId}`; - const response = await this.tuya.request({ - method: 'GET', - path, - }); - return response as GetRoomDetailsInterface; // Cast response to RoomData - } catch (error) { - throw new HttpException( - 'Error fetching rooms details', - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } - } async addGroup(addGroupDto: AddGroupDto) { const response = await this.addGroupTuya(addGroupDto); @@ -227,4 +208,38 @@ export class GroupService { ); } } + + async getGroupsByGroupId(groupId: number) { + try { + const response = await this.getGroupsByGroupIdTuya(groupId); + + return { + groupId: response.result.id, + groupName: response.result.name, + }; + } catch (error) { + throw new HttpException( + 'Error fetching group', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + + async getGroupsByGroupIdTuya( + groupId: number, + ): Promise { + try { + const path = `/v2.0/cloud/thing/group/${groupId}`; + const response = await this.tuya.request({ + method: 'GET', + path, + }); + return response as GetGroupDetailsInterface; + } catch (error) { + throw new HttpException( + 'Error fetching group ', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } }