From b2e835c8050c3de05be47231f8d0f8379c009dc7 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Mon, 11 Mar 2024 10:31:47 +0300 Subject: [PATCH 1/2] finished add room api --- src/app.module.ts | 2 + src/room/controllers/index.ts | 1 + src/room/controllers/room.controller.ts | 36 ++++++++++++++ src/room/dtos/add.room.dto.ts | 20 ++++++++ src/room/dtos/index.ts | 1 + src/room/room.module.ts | 11 +++++ src/room/services/index.ts | 1 + src/room/services/room.service.ts | 66 +++++++++++++++++++++++++ 8 files changed, 138 insertions(+) create mode 100644 src/room/controllers/index.ts create mode 100644 src/room/controllers/room.controller.ts create mode 100644 src/room/dtos/add.room.dto.ts create mode 100644 src/room/dtos/index.ts create mode 100644 src/room/room.module.ts create mode 100644 src/room/services/index.ts create mode 100644 src/room/services/room.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index 18a0a42..b890f1a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -5,6 +5,7 @@ import { AuthenticationModule } from './auth/auth.module'; import { AuthenticationController } from './auth/controllers/authentication.controller'; import { UserModule } from './users/user.module'; import { HomeModule } from './home/home.module'; +import { RoomModule } from './room/room.module'; @Module({ imports: [ ConfigModule.forRoot({ @@ -13,6 +14,7 @@ import { HomeModule } from './home/home.module'; AuthenticationModule, UserModule, HomeModule, + RoomModule, ], controllers: [AuthenticationController], }) diff --git a/src/room/controllers/index.ts b/src/room/controllers/index.ts new file mode 100644 index 0000000..4225d61 --- /dev/null +++ b/src/room/controllers/index.ts @@ -0,0 +1 @@ +export * from './room.controller'; diff --git a/src/room/controllers/room.controller.ts b/src/room/controllers/room.controller.ts new file mode 100644 index 0000000..e7c2b68 --- /dev/null +++ b/src/room/controllers/room.controller.ts @@ -0,0 +1,36 @@ +import { RoomService } from '../services/room.service'; +import { Body, Controller, Get, Post, Param, UseGuards } 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'; + +@ApiTags('Room Module') +@Controller({ + version: '1', + path: 'room', +}) +export class RoomController { + constructor(private readonly roomService: RoomService) {} + + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get(':userUuid') + async userList(@Param('userUuid') userUuid: string) { + try { + return await this.roomService.getHomesByUserId(userUuid); + } catch (err) { + throw new Error(err); + } + } + + // @ApiBearerAuth() + // @UseGuards(JwtAuthGuard) + @Post() + async addRoom(@Body() addRoomDto: AddRoomDto) { + try { + return await this.roomService.addRoom(addRoomDto); + } catch (err) { + throw new Error(err); + } + } +} diff --git a/src/room/dtos/add.room.dto.ts b/src/room/dtos/add.room.dto.ts new file mode 100644 index 0000000..3d39559 --- /dev/null +++ b/src/room/dtos/add.room.dto.ts @@ -0,0 +1,20 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString, IsNumberString } from 'class-validator'; + +export class AddRoomDto { + @ApiProperty({ + description: 'roomName', + required: true, + }) + @IsString() + @IsNotEmpty() + public roomName: string; + + @ApiProperty({ + description: 'homeId', + required: true, + }) + @IsNumberString() + @IsNotEmpty() + public homeId: string; +} diff --git a/src/room/dtos/index.ts b/src/room/dtos/index.ts new file mode 100644 index 0000000..a510b75 --- /dev/null +++ b/src/room/dtos/index.ts @@ -0,0 +1 @@ +export * from './add.room.dto'; diff --git a/src/room/room.module.ts b/src/room/room.module.ts new file mode 100644 index 0000000..cd520c6 --- /dev/null +++ b/src/room/room.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { RoomService } from './services/room.service'; +import { RoomController } from './controllers/room.controller'; +import { ConfigModule } from '@nestjs/config'; +@Module({ + imports: [ConfigModule], + controllers: [RoomController], + providers: [RoomService], + exports: [RoomService], +}) +export class RoomModule {} diff --git a/src/room/services/index.ts b/src/room/services/index.ts new file mode 100644 index 0000000..4f45e9a --- /dev/null +++ b/src/room/services/index.ts @@ -0,0 +1 @@ +export * from './room.service'; diff --git a/src/room/services/room.service.ts b/src/room/services/room.service.ts new file mode 100644 index 0000000..14e765c --- /dev/null +++ b/src/room/services/room.service.ts @@ -0,0 +1,66 @@ +import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; +import { TuyaContext } from '@tuya/tuya-connector-nodejs'; +import { ConfigService } from '@nestjs/config'; +import { AddRoomDto } from '../dtos'; + +@Injectable() +export class RoomService { + 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 getHomesByUserId(userUuid: string) { + // const homesData = await this.findHomes(userUuid); + + // const homesMapper = homesData.map((home) => ({ + // homeId: home.homeId, + // homeName: home.homeName, + // })); + + // return homesMapper; + console.log(userUuid); + } + + // async findHomes(userUuid: string) { + // try { + // return await this.homeRepository.find({ + // where: { + // userUuid: userUuid, + // }, + // }); + // } catch (error) { + // throw new HttpException( + // 'Error get homes', + // HttpStatus.INTERNAL_SERVER_ERROR, + // ); + // } + // } + async addRoom(addRoomDto: AddRoomDto) { + try { + const path = `/v2.0/cloud/space/creation`; + const data = await this.tuya.request({ + method: 'POST', + path, + body: { name: addRoomDto.roomName, parent_id: addRoomDto.homeId }, + }); + + return { + success: data.success, + roomId: data.result, + }; + } catch (error) { + throw new HttpException( + 'Error adding room', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } +} From e522a3a207eab1b38e00ff9603421da99ce3fd47 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:40:24 +0300 Subject: [PATCH 2/2] finished get rooms api --- src/room/controllers/room.controller.ts | 10 +-- src/room/interfaces/get.room.interface.ts | 11 ++++ src/room/services/room.service.ts | 77 ++++++++++++++++------- 3 files changed, 71 insertions(+), 27 deletions(-) create mode 100644 src/room/interfaces/get.room.interface.ts diff --git a/src/room/controllers/room.controller.ts b/src/room/controllers/room.controller.ts index e7c2b68..2545254 100644 --- a/src/room/controllers/room.controller.ts +++ b/src/room/controllers/room.controller.ts @@ -14,17 +14,17 @@ export class RoomController { @ApiBearerAuth() @UseGuards(JwtAuthGuard) - @Get(':userUuid') - async userList(@Param('userUuid') userUuid: string) { + @Get(':homeId') + async userList(@Param('homeId') homeId: string) { try { - return await this.roomService.getHomesByUserId(userUuid); + return await this.roomService.getRoomsByHomeId(homeId); } catch (err) { throw new Error(err); } } - // @ApiBearerAuth() - // @UseGuards(JwtAuthGuard) + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) @Post() async addRoom(@Body() addRoomDto: AddRoomDto) { try { diff --git a/src/room/interfaces/get.room.interface.ts b/src/room/interfaces/get.room.interface.ts new file mode 100644 index 0000000..2d000a5 --- /dev/null +++ b/src/room/interfaces/get.room.interface.ts @@ -0,0 +1,11 @@ +export class GetRoomDetailsInterface { + result: { + id: string; + name: string; + }; +} +export class GetRoomsIdsInterface { + result: { + data: []; + }; +} diff --git a/src/room/services/room.service.ts b/src/room/services/room.service.ts index 14e765c..0bc5b18 100644 --- a/src/room/services/room.service.ts +++ b/src/room/services/room.service.ts @@ -2,6 +2,10 @@ import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { TuyaContext } from '@tuya/tuya-connector-nodejs'; import { ConfigService } from '@nestjs/config'; import { AddRoomDto } from '../dtos'; +import { + GetRoomDetailsInterface, + GetRoomsIdsInterface, +} from '../interfaces/get.room.interface'; @Injectable() export class RoomService { @@ -17,32 +21,61 @@ export class RoomService { }); } - async getHomesByUserId(userUuid: string) { - // const homesData = await this.findHomes(userUuid); + async getRoomsByHomeId(homeId: string) { + try { + const roomsIds = await this.getRoomsIds(homeId); - // const homesMapper = homesData.map((home) => ({ - // homeId: home.homeId, - // homeName: home.homeName, - // })); + const roomsDetails = await Promise.all( + roomsIds.result.data.map(async (roomId) => { + const roomData = await this.getRoomDetails(roomId); + return { + roomId: roomData?.result?.id, + roomName: roomData ? roomData.result.name : null, + }; + }), + ); - // return homesMapper; - console.log(userUuid); + return roomsDetails; + } catch (error) { + throw new HttpException( + 'Error fetching rooms', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } } + async getRoomsIds(homeId: string): Promise { + try { + const path = `/v2.0/cloud/space/child`; + const response = await this.tuya.request({ + method: 'GET', + path, + query: { space_id: homeId }, + }); + return response as GetRoomsIdsInterface; + } catch (error) { + throw new HttpException( + 'Error fetching rooms ids', + 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, + }); - // async findHomes(userUuid: string) { - // try { - // return await this.homeRepository.find({ - // where: { - // userUuid: userUuid, - // }, - // }); - // } catch (error) { - // throw new HttpException( - // 'Error get homes', - // HttpStatus.INTERNAL_SERVER_ERROR, - // ); - // } - // } + return response as GetRoomDetailsInterface; // Cast response to RoomData + } catch (error) { + throw new HttpException( + 'Error fetching rooms details', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } async addRoom(addRoomDto: AddRoomDto) { try { const path = `/v2.0/cloud/space/creation`;