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, + ); + } + } }