From 1cf345d28db3121b6dfa2ed942037910d3177a21 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 12 Mar 2024 12:56:14 +0300 Subject: [PATCH 1/8] finished groups endpoint --- src/app.module.ts | 2 + src/group/controllers/group.controller.ts | 48 ++++++++ src/group/controllers/index.ts | 1 + src/group/dtos/add.group.dto.ts | 36 ++++++ src/group/dtos/get.group.dto.ts | 20 ++++ src/group/dtos/index.ts | 1 + src/group/group.module.ts | 11 ++ src/group/interfaces/get.group.interface.ts | 20 ++++ src/group/services/group.service.ts | 124 ++++++++++++++++++++ src/group/services/index.ts | 1 + 10 files changed, 264 insertions(+) create mode 100644 src/group/controllers/group.controller.ts create mode 100644 src/group/controllers/index.ts create mode 100644 src/group/dtos/add.group.dto.ts create mode 100644 src/group/dtos/get.group.dto.ts create mode 100644 src/group/dtos/index.ts create mode 100644 src/group/group.module.ts create mode 100644 src/group/interfaces/get.group.interface.ts create mode 100644 src/group/services/group.service.ts create mode 100644 src/group/services/index.ts diff --git a/src/app.module.ts b/src/app.module.ts index b890f1a..1da5880 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -6,6 +6,7 @@ import { AuthenticationController } from './auth/controllers/authentication.cont import { UserModule } from './users/user.module'; import { HomeModule } from './home/home.module'; import { RoomModule } from './room/room.module'; +import { GroupModule } from './group/group.module'; @Module({ imports: [ ConfigModule.forRoot({ @@ -15,6 +16,7 @@ import { RoomModule } from './room/room.module'; UserModule, HomeModule, RoomModule, + GroupModule, ], controllers: [AuthenticationController], }) diff --git a/src/group/controllers/group.controller.ts b/src/group/controllers/group.controller.ts new file mode 100644 index 0000000..4cdb617 --- /dev/null +++ b/src/group/controllers/group.controller.ts @@ -0,0 +1,48 @@ +import { GroupService } from '../services/group.service'; +import { + Body, + Controller, + Get, + Post, + UseGuards, + Query, + Param, +} 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'; + +@ApiTags('Group Module') +@Controller({ + version: '1', + path: 'group', +}) +export class GroupController { + constructor(private readonly groupService: GroupService) {} + + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get(':homeId') + async userList( + @Param('homeId') homeId: string, + @Query() getGroupsDto: GetGroupDto, + ) { + try { + return await this.groupService.getGroupsByHomeId(homeId, getGroupsDto); + } catch (err) { + throw new Error(err); + } + } + + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Post() + async addGroup(@Body() addGroupDto: AddGroupDto) { + try { + return await this.groupService.addGroup(addGroupDto); + } catch (err) { + throw new Error(err); + } + } +} diff --git a/src/group/controllers/index.ts b/src/group/controllers/index.ts new file mode 100644 index 0000000..daf2953 --- /dev/null +++ b/src/group/controllers/index.ts @@ -0,0 +1 @@ +export * from './group.controller'; diff --git a/src/group/dtos/add.group.dto.ts b/src/group/dtos/add.group.dto.ts new file mode 100644 index 0000000..b91f793 --- /dev/null +++ b/src/group/dtos/add.group.dto.ts @@ -0,0 +1,36 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString, IsNumberString } from 'class-validator'; + +export class AddGroupDto { + @ApiProperty({ + description: 'groupName', + required: true, + }) + @IsString() + @IsNotEmpty() + public groupName: string; + + @ApiProperty({ + description: 'homeId', + required: true, + }) + @IsNumberString() + @IsNotEmpty() + public homeId: string; + + @ApiProperty({ + description: 'productId', + required: true, + }) + @IsString() + @IsNotEmpty() + public productId: string; + + @ApiProperty({ + description: 'The list of up to 20 device IDs, separated with commas (,)', + required: true, + }) + @IsString() + @IsNotEmpty() + public deviceIds: string; +} diff --git a/src/group/dtos/get.group.dto.ts b/src/group/dtos/get.group.dto.ts new file mode 100644 index 0000000..16f8236 --- /dev/null +++ b/src/group/dtos/get.group.dto.ts @@ -0,0 +1,20 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsNumberString } from 'class-validator'; + +export class GetGroupDto { + @ApiProperty({ + description: 'pageSize', + required: true, + }) + @IsNumberString() + @IsNotEmpty() + public pageSize: number; + + @ApiProperty({ + description: 'pageNo', + required: true, + }) + @IsNumberString() + @IsNotEmpty() + public pageNo: number; +} diff --git a/src/group/dtos/index.ts b/src/group/dtos/index.ts new file mode 100644 index 0000000..61cffa2 --- /dev/null +++ b/src/group/dtos/index.ts @@ -0,0 +1 @@ +export * from './add.group.dto'; diff --git a/src/group/group.module.ts b/src/group/group.module.ts new file mode 100644 index 0000000..3969d39 --- /dev/null +++ b/src/group/group.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { GroupService } from './services/group.service'; +import { GroupController } from './controllers/group.controller'; +import { ConfigModule } from '@nestjs/config'; +@Module({ + imports: [ConfigModule], + controllers: [GroupController], + providers: [GroupService], + exports: [GroupService], +}) +export class GroupModule {} diff --git a/src/group/interfaces/get.group.interface.ts b/src/group/interfaces/get.group.interface.ts new file mode 100644 index 0000000..3e93b95 --- /dev/null +++ b/src/group/interfaces/get.group.interface.ts @@ -0,0 +1,20 @@ +export class GetRoomDetailsInterface { + result: { + id: string; + name: string; + }; +} +export class GetGroupsInterface { + result: { + count: number; + data_list: []; + }; +} + +export class addGroupInterface { + success: boolean; + msg: string; + result: { + id: string; + }; +} diff --git a/src/group/services/group.service.ts b/src/group/services/group.service.ts new file mode 100644 index 0000000..f05dc10 --- /dev/null +++ b/src/group/services/group.service.ts @@ -0,0 +1,124 @@ +import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; +import { TuyaContext } from '@tuya/tuya-connector-nodejs'; +import { ConfigService } from '@nestjs/config'; +import { AddGroupDto } from '../dtos'; +import { + GetGroupsInterface, + GetRoomDetailsInterface, + addGroupInterface, +} from '../interfaces/get.group.interface'; +import { GetGroupDto } from '../dtos/get.group.dto'; + +@Injectable() +export class GroupService { + private tuya: TuyaContext; + constructor(private readonly configService: ConfigService) { + const accessKey = this.configService.get('auth-config.ACCESS_KEY'); + const secretKey = this.configService.get('auth-config.SECRET_KEY'); + // const clientId = this.configService.get('auth-config.CLIENT_ID'); + this.tuya = new TuyaContext({ + baseUrl: 'https://openapi.tuyaeu.com', + accessKey, + secretKey, + }); + } + + async getGroupsByHomeId(homeId: string, getGroupDto: GetGroupDto) { + try { + const response = await this.getGroupsTuya(homeId, getGroupDto); + + const groups = response.result.data_list.map((group: any) => ({ + groupId: group.id, + groupName: group.name, + })); + + return { + count: response.result.count, + groups: groups, + }; + } catch (error) { + throw new HttpException( + 'Error fetching groups', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + + async getGroupsTuya( + homeId: string, + getGroupDto: GetGroupDto, + ): Promise { + try { + const path = `/v2.0/cloud/thing/group`; + const response = await this.tuya.request({ + method: 'GET', + path, + query: { + space_id: homeId, + page_size: getGroupDto.pageSize, + page_no: getGroupDto.pageNo, + }, + }); + return response as unknown as GetGroupsInterface; + } catch (error) { + throw new HttpException( + 'Error fetching groups ', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + 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); + + if (response.success) { + return { + success: true, + groupId: response.result.id, + }; + } else { + throw new HttpException( + response.msg || 'Unknown error', + HttpStatus.BAD_REQUEST, + ); + } + } + async addGroupTuya(addGroupDto: AddGroupDto): Promise { + try { + const path = `/v2.0/cloud/thing/group`; + const response = await this.tuya.request({ + method: 'POST', + path, + body: { + space_id: addGroupDto.homeId, + name: addGroupDto.groupName, + product_id: addGroupDto.productId, + device_ids: addGroupDto.deviceIds, + }, + }); + + return response as addGroupInterface; + } catch (error) { + throw new HttpException( + 'Error adding group', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } +} diff --git a/src/group/services/index.ts b/src/group/services/index.ts new file mode 100644 index 0000000..ee9dd3f --- /dev/null +++ b/src/group/services/index.ts @@ -0,0 +1 @@ +export * from './group.service'; From 70020171c5fe00f3a3953e159cb23d392d7c8b5e Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 12 Mar 2024 13:36:11 +0300 Subject: [PATCH 2/8] finished control group api --- src/group/controllers/group.controller.ts | 12 +++++++ src/group/dtos/control.group.dto.ts | 20 +++++++++++ src/group/interfaces/get.group.interface.ts | 6 ++++ src/group/services/group.service.ts | 39 ++++++++++++++++++++- 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/group/dtos/control.group.dto.ts diff --git a/src/group/controllers/group.controller.ts b/src/group/controllers/group.controller.ts index 4cdb617..8430fe4 100644 --- a/src/group/controllers/group.controller.ts +++ b/src/group/controllers/group.controller.ts @@ -12,6 +12,7 @@ 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'; @ApiTags('Group Module') @Controller({ @@ -45,4 +46,15 @@ export class GroupController { throw new Error(err); } } + + // @ApiBearerAuth() + // @UseGuards(JwtAuthGuard) + @Post('control') + async controlGroup(@Body() controlGroupDto: ControlGroupDto) { + try { + return await this.groupService.controlGroup(controlGroupDto); + } catch (err) { + throw new Error(err); + } + } } diff --git a/src/group/dtos/control.group.dto.ts b/src/group/dtos/control.group.dto.ts new file mode 100644 index 0000000..36040b8 --- /dev/null +++ b/src/group/dtos/control.group.dto.ts @@ -0,0 +1,20 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString, IsObject } from 'class-validator'; + +export class ControlGroupDto { + @ApiProperty({ + description: 'groupId', + required: true, + }) + @IsString() + @IsNotEmpty() + public groupId: string; + + @ApiProperty({ + description: 'example {"switch_1":true,"add_ele":300}', + required: true, + }) + @IsObject() + @IsNotEmpty() + public properties: object; +} diff --git a/src/group/interfaces/get.group.interface.ts b/src/group/interfaces/get.group.interface.ts index 3e93b95..94dec62 100644 --- a/src/group/interfaces/get.group.interface.ts +++ b/src/group/interfaces/get.group.interface.ts @@ -18,3 +18,9 @@ export class addGroupInterface { id: string; }; } + +export class controlGroupInterface { + success: boolean; + result: boolean; + msg: string; +} diff --git a/src/group/services/group.service.ts b/src/group/services/group.service.ts index f05dc10..286e21f 100644 --- a/src/group/services/group.service.ts +++ b/src/group/services/group.service.ts @@ -1,13 +1,15 @@ import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { TuyaContext } from '@tuya/tuya-connector-nodejs'; import { ConfigService } from '@nestjs/config'; -import { AddGroupDto } from '../dtos'; +import { AddGroupDto } from '../dtos/add.group.dto'; import { GetGroupsInterface, GetRoomDetailsInterface, addGroupInterface, + controlGroupInterface, } from '../interfaces/get.group.interface'; import { GetGroupDto } from '../dtos/get.group.dto'; +import { ControlGroupDto } from '../dtos/control.group.dto'; @Injectable() export class GroupService { @@ -121,4 +123,39 @@ export class GroupService { ); } } + + async controlGroup(controlGroupDto: ControlGroupDto) { + const response = await this.controlGroupTuya(controlGroupDto); + + if (response.success) { + return response; + } else { + throw new HttpException( + response.msg || 'Unknown error', + HttpStatus.BAD_REQUEST, + ); + } + } + async controlGroupTuya( + controlGroupDto: ControlGroupDto, + ): Promise { + try { + const path = `/v2.0/cloud/thing/group/properties`; + const response = await this.tuya.request({ + method: 'POST', + path, + body: { + group_id: controlGroupDto.groupId, + properties: controlGroupDto.properties, + }, + }); + + return response as controlGroupInterface; + } catch (error) { + throw new HttpException( + 'Error control group', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } } From db22ceba17b5440b9387a0f5ee3c18a1aac6f9b4 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 12 Mar 2024 13:36:38 +0300 Subject: [PATCH 3/8] remove comments --- src/group/controllers/group.controller.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/group/controllers/group.controller.ts b/src/group/controllers/group.controller.ts index 8430fe4..a37abc4 100644 --- a/src/group/controllers/group.controller.ts +++ b/src/group/controllers/group.controller.ts @@ -47,8 +47,8 @@ export class GroupController { } } - // @ApiBearerAuth() - // @UseGuards(JwtAuthGuard) + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) @Post('control') async controlGroup(@Body() controlGroupDto: ControlGroupDto) { try { From 1f5789db1b88b590e6f92f94ed67ad11d1392347 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 12 Mar 2024 13:49:41 +0300 Subject: [PATCH 4/8] finished rename group api --- src/group/controllers/group.controller.ts | 13 ++++++++ src/group/dtos/control.group.dto.ts | 4 +-- src/group/dtos/rename.group.dto copy.ts | 20 +++++++++++++ src/group/interfaces/get.group.interface.ts | 6 ++++ src/group/services/group.service.ts | 33 +++++++++++++++++++++ 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/group/dtos/rename.group.dto copy.ts 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, + ); + } + } } From 4cdba753673f3e0e7178f29005d886d614b257fe Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 12 Mar 2024 14:11:23 +0300 Subject: [PATCH 5/8] finished delete group api --- src/group/controllers/group.controller.ts | 12 ++++++ src/group/interfaces/get.group.interface.ts | 6 --- src/group/services/group.service.ts | 46 ++++++++++++++++++--- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/group/controllers/group.controller.ts b/src/group/controllers/group.controller.ts index 1db7a11..7b5b12d 100644 --- a/src/group/controllers/group.controller.ts +++ b/src/group/controllers/group.controller.ts @@ -8,6 +8,7 @@ import { Query, Param, Put, + Delete, } from '@nestjs/common'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard'; @@ -70,4 +71,15 @@ export class GroupController { throw new Error(err); } } + + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Delete(':groupId') + async deleteGroup(@Param('groupId') groupId: number) { + try { + return await this.groupService.deleteGroup(groupId); + } catch (err) { + throw new Error(err); + } + } } diff --git a/src/group/interfaces/get.group.interface.ts b/src/group/interfaces/get.group.interface.ts index 69c5050..94dec62 100644 --- a/src/group/interfaces/get.group.interface.ts +++ b/src/group/interfaces/get.group.interface.ts @@ -24,9 +24,3 @@ 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 c534d39..48cadeb 100644 --- a/src/group/services/group.service.ts +++ b/src/group/services/group.service.ts @@ -7,7 +7,6 @@ import { GetRoomDetailsInterface, addGroupInterface, controlGroupInterface, - renameGroupInterface, } from '../interfaces/get.group.interface'; import { GetGroupDto } from '../dtos/get.group.dto'; import { ControlGroupDto } from '../dtos/control.group.dto'; @@ -165,7 +164,11 @@ export class GroupService { const response = await this.renameGroupTuya(renameGroupDto); if (response.success) { - return response; + return { + success: response.success, + result: response.result, + msg: response.msg, + }; } else { throw new HttpException( response.msg || 'Unknown error', @@ -175,7 +178,7 @@ export class GroupService { } async renameGroupTuya( renameGroupDto: RenameGroupDto, - ): Promise { + ): Promise { try { const path = `/v2.0/cloud/thing/group/${renameGroupDto.groupId}/${renameGroupDto.groupName}`; const response = await this.tuya.request({ @@ -183,10 +186,43 @@ export class GroupService { path, }); - return response as renameGroupInterface; + return response as controlGroupInterface; } catch (error) { throw new HttpException( - 'Error control group', + 'Error rename group', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + + async deleteGroup(groupId: number) { + const response = await this.deleteGroupTuya(groupId); + + if (response.success) { + return { + success: response.success, + result: response.result, + msg: response.msg, + }; + } else { + throw new HttpException( + response.msg || 'Unknown error', + HttpStatus.BAD_REQUEST, + ); + } + } + async deleteGroupTuya(groupId: number): Promise { + try { + const path = `/v2.0/cloud/thing/group/${groupId}`; + const response = await this.tuya.request({ + method: 'DELETE', + path, + }); + + return response as controlGroupInterface; + } catch (error) { + throw new HttpException( + 'Error delete group', HttpStatus.INTERNAL_SERVER_ERROR, ); } From db7802776a47d3ad734a6c5eb0d8f2a5605f6f7f Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 12 Mar 2024 15:03:06 +0300 Subject: [PATCH 6/8] add get group by id api --- src/group/controllers/group.controller.ts | 20 ++++--- src/group/dtos/get.group.dto.ts | 8 +++ src/group/interfaces/get.group.interface.ts | 2 +- src/group/services/group.service.ts | 63 +++++++++++++-------- 4 files changed, 61 insertions(+), 32 deletions(-) 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, + ); + } + } } From dc51cbb5ef11ede7631aafc6f44278d1db6b1ded Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 12 Mar 2024 15:29:25 +0300 Subject: [PATCH 7/8] finshed get room by id api --- src/room/controllers/room.controller.ts | 25 +++++++++++++++++++---- src/room/interfaces/get.room.interface.ts | 1 + src/room/services/room.service.ts | 16 +++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/room/controllers/room.controller.ts b/src/room/controllers/room.controller.ts index 2545254..b90210c 100644 --- a/src/room/controllers/room.controller.ts +++ b/src/room/controllers/room.controller.ts @@ -1,5 +1,13 @@ import { RoomService } from '../services/room.service'; -import { Body, Controller, Get, Post, Param, UseGuards } from '@nestjs/common'; +import { + Body, + Controller, + Get, + Post, + UseGuards, + Query, + Param, +} from '@nestjs/common'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard'; import { AddRoomDto } from '../dtos/add.room.dto'; @@ -14,15 +22,24 @@ export class RoomController { @ApiBearerAuth() @UseGuards(JwtAuthGuard) - @Get(':homeId') - async userList(@Param('homeId') homeId: string) { + @Get() + async getRoomsByHomeId(@Query('homeId') homeId: string) { try { return await this.roomService.getRoomsByHomeId(homeId); } catch (err) { throw new Error(err); } } - + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get(':roomId') + async getRoomsByRoomId(@Param('roomId') roomId: string) { + try { + return await this.roomService.getRoomsByRoomId(roomId); + } catch (err) { + throw new Error(err); + } + } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Post() diff --git a/src/room/interfaces/get.room.interface.ts b/src/room/interfaces/get.room.interface.ts index 2d000a5..56c0d49 100644 --- a/src/room/interfaces/get.room.interface.ts +++ b/src/room/interfaces/get.room.interface.ts @@ -2,6 +2,7 @@ export class GetRoomDetailsInterface { result: { id: string; name: string; + root_id: string; }; } export class GetRoomsIdsInterface { diff --git a/src/room/services/room.service.ts b/src/room/services/room.service.ts index 0bc5b18..095e8df 100644 --- a/src/room/services/room.service.ts +++ b/src/room/services/room.service.ts @@ -96,4 +96,20 @@ export class RoomService { ); } } + async getRoomsByRoomId(roomId: string) { + try { + const response = await this.getRoomDetails(roomId); + + return { + homeId: response.result.root_id, + roomId: response.result.id, + roomName: response.result.name, + }; + } catch (error) { + throw new HttpException( + 'Error fetching rooms', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } } From e011949bdab6b0c939d0a1d275c18cbe92e47ae9 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 12 Mar 2024 15:40:16 +0300 Subject: [PATCH 8/8] finshed get home by id api --- src/home/controllers/home.controller.ts | 24 ++++++++++++++--- src/home/interfaces/get.home.interface.ts | 6 +++++ src/home/services/home.service.ts | 32 +++++++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/home/interfaces/get.home.interface.ts diff --git a/src/home/controllers/home.controller.ts b/src/home/controllers/home.controller.ts index 6b4eaa4..c8aeb4b 100644 --- a/src/home/controllers/home.controller.ts +++ b/src/home/controllers/home.controller.ts @@ -1,5 +1,13 @@ import { HomeService } from './../services/home.service'; -import { Body, Controller, Get, Post, Param, UseGuards } from '@nestjs/common'; +import { + Body, + Controller, + Get, + Post, + Param, + UseGuards, + Query, +} from '@nestjs/common'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard'; import { AddHomeDto } from '../dtos/add.home.dto'; @@ -14,14 +22,24 @@ export class HomeController { @ApiBearerAuth() @UseGuards(JwtAuthGuard) - @Get(':userUuid') - async userList(@Param('userUuid') userUuid: string) { + @Get() + async getHomesByUserId(@Query('userUuid') userUuid: string) { try { return await this.homeService.getHomesByUserId(userUuid); } catch (err) { throw new Error(err); } } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get(':homeId') + async getHomesByHomeId(@Param('homeId') homeId: string) { + try { + return await this.homeService.getHomeByHomeId(homeId); + } catch (err) { + throw new Error(err); + } + } @ApiBearerAuth() @UseGuards(JwtAuthGuard) diff --git a/src/home/interfaces/get.home.interface.ts b/src/home/interfaces/get.home.interface.ts new file mode 100644 index 0000000..c7015f8 --- /dev/null +++ b/src/home/interfaces/get.home.interface.ts @@ -0,0 +1,6 @@ +export class GetHomeDetailsInterface { + result: { + id: string; + name: string; + }; +} diff --git a/src/home/services/home.service.ts b/src/home/services/home.service.ts index 74da59b..f1ad1f4 100644 --- a/src/home/services/home.service.ts +++ b/src/home/services/home.service.ts @@ -4,6 +4,7 @@ import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { TuyaContext } from '@tuya/tuya-connector-nodejs'; import { ConfigService } from '@nestjs/config'; import { AddHomeDto } from '../dtos'; +import { GetHomeDetailsInterface } from '../interfaces/get.home.interface'; @Injectable() export class HomeService { @@ -78,4 +79,35 @@ export class HomeService { ); } } + async getHomeDetails(homeId: string): Promise { + try { + const path = `/v2.0/cloud/space/${homeId}`; + const response = await this.tuya.request({ + method: 'GET', + path, + }); + + return response as GetHomeDetailsInterface; + } catch (error) { + throw new HttpException( + 'Error fetching home details', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + async getHomeByHomeId(homeId: string) { + try { + const response = await this.getHomeDetails(homeId); + + return { + homeId: response.result.id, + homeName: response.result.name, + }; + } catch (error) { + throw new HttpException( + 'Error fetching home', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } }