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`;