From a553481c9aeae93685eb83e653129fbad21a9f93 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Sun, 10 Mar 2024 21:07:13 +0300 Subject: [PATCH 1/6] create home entity --- libs/common/src/database/database.module.ts | 3 +- libs/common/src/modules/home/dtos/home.dto.ts | 15 ++++++++++ libs/common/src/modules/home/dtos/index.ts | 1 + .../src/modules/home/entities/home.entity.ts | 29 +++++++++++++++++++ .../common/src/modules/home/entities/index.ts | 1 + .../modules/home/home.repository.module.ts | 11 +++++++ .../home/repositories/home.repository.ts | 10 +++++++ .../src/modules/home/repositories/index.ts | 1 + 8 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 libs/common/src/modules/home/dtos/home.dto.ts create mode 100644 libs/common/src/modules/home/dtos/index.ts create mode 100644 libs/common/src/modules/home/entities/home.entity.ts create mode 100644 libs/common/src/modules/home/entities/index.ts create mode 100644 libs/common/src/modules/home/home.repository.module.ts create mode 100644 libs/common/src/modules/home/repositories/home.repository.ts create mode 100644 libs/common/src/modules/home/repositories/index.ts diff --git a/libs/common/src/database/database.module.ts b/libs/common/src/database/database.module.ts index c3aafad..e263571 100644 --- a/libs/common/src/database/database.module.ts +++ b/libs/common/src/database/database.module.ts @@ -5,6 +5,7 @@ import { SnakeNamingStrategy } from './strategies'; import { UserEntity } from '../modules/user/entities/user.entity'; import { UserSessionEntity } from '../modules/session/entities/session.entity'; import { UserOtpEntity } from '../modules/user-otp/entities'; +import { HomeEntity } from '../modules/home/entities'; @Module({ imports: [ @@ -19,7 +20,7 @@ import { UserOtpEntity } from '../modules/user-otp/entities'; username: configService.get('DB_USER'), password: configService.get('DB_PASSWORD'), database: configService.get('DB_NAME'), - entities: [UserEntity, UserSessionEntity, UserOtpEntity], + entities: [UserEntity, UserSessionEntity, UserOtpEntity, HomeEntity], namingStrategy: new SnakeNamingStrategy(), synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))), logging: true, diff --git a/libs/common/src/modules/home/dtos/home.dto.ts b/libs/common/src/modules/home/dtos/home.dto.ts new file mode 100644 index 0000000..9db59f2 --- /dev/null +++ b/libs/common/src/modules/home/dtos/home.dto.ts @@ -0,0 +1,15 @@ +import { IsNotEmpty, IsString } from 'class-validator'; + +export class HomeDto { + @IsString() + @IsNotEmpty() + public uuid: string; + + @IsString() + @IsNotEmpty() + public userUuid: string; + + @IsString() + @IsNotEmpty() + public homeId: string; +} diff --git a/libs/common/src/modules/home/dtos/index.ts b/libs/common/src/modules/home/dtos/index.ts new file mode 100644 index 0000000..217847e --- /dev/null +++ b/libs/common/src/modules/home/dtos/index.ts @@ -0,0 +1 @@ +export * from './home.dto'; diff --git a/libs/common/src/modules/home/entities/home.entity.ts b/libs/common/src/modules/home/entities/home.entity.ts new file mode 100644 index 0000000..7760343 --- /dev/null +++ b/libs/common/src/modules/home/entities/home.entity.ts @@ -0,0 +1,29 @@ +import { Column, Entity } from 'typeorm'; +import { HomeDto } from '../dtos'; +import { AbstractEntity } from '../../abstract/entities/abstract.entity'; + +@Entity({ name: 'home' }) +export class HomeEntity extends AbstractEntity { + @Column({ + type: 'uuid', + default: () => 'gen_random_uuid()', // Use gen_random_uuid() for default value + nullable: false, + }) + public uuid: string; + + @Column({ + nullable: false, + }) + userUuid: string; + + @Column({ + nullable: false, + unique: true, + }) + public homeId: string; + + constructor(partial: Partial) { + super(); + Object.assign(this, partial); + } +} diff --git a/libs/common/src/modules/home/entities/index.ts b/libs/common/src/modules/home/entities/index.ts new file mode 100644 index 0000000..dbf8038 --- /dev/null +++ b/libs/common/src/modules/home/entities/index.ts @@ -0,0 +1 @@ +export * from './home.entity'; diff --git a/libs/common/src/modules/home/home.repository.module.ts b/libs/common/src/modules/home/home.repository.module.ts new file mode 100644 index 0000000..e2527b8 --- /dev/null +++ b/libs/common/src/modules/home/home.repository.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { HomeEntity } from './entities/home.entity'; + +@Module({ + providers: [], + exports: [], + controllers: [], + imports: [TypeOrmModule.forFeature([HomeEntity])], +}) +export class HomeRepositoryModule {} diff --git a/libs/common/src/modules/home/repositories/home.repository.ts b/libs/common/src/modules/home/repositories/home.repository.ts new file mode 100644 index 0000000..3f525e2 --- /dev/null +++ b/libs/common/src/modules/home/repositories/home.repository.ts @@ -0,0 +1,10 @@ +import { DataSource, Repository } from 'typeorm'; +import { Injectable } from '@nestjs/common'; +import { HomeEntity } from '../entities/home.entity'; + +@Injectable() +export class HomeRepository extends Repository { + constructor(private dataSource: DataSource) { + super(HomeEntity, dataSource.createEntityManager()); + } +} diff --git a/libs/common/src/modules/home/repositories/index.ts b/libs/common/src/modules/home/repositories/index.ts new file mode 100644 index 0000000..af66ad7 --- /dev/null +++ b/libs/common/src/modules/home/repositories/index.ts @@ -0,0 +1 @@ +export * from './home.repository'; From 63411cada43a75f9a4aabc6ec8c53db7bfe28e03 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Sun, 10 Mar 2024 22:43:02 +0300 Subject: [PATCH 2/6] create get and post home api --- libs/common/src/modules/home/dtos/home.dto.ts | 4 ++ .../src/modules/home/entities/home.entity.ts | 5 ++ src/app.module.ts | 2 + src/home/controllers/home.controller.ts | 36 ++++++++++ src/home/controllers/index.ts | 1 + src/home/dtos/add.home.dto.ts | 20 ++++++ src/home/dtos/home.list.dto.ts | 32 +++++++++ src/home/dtos/index.ts | 2 + src/home/home.module.ts | 14 ++++ src/home/services/home.service.ts | 66 +++++++++++++++++++ src/home/services/index.ts | 1 + 11 files changed, 183 insertions(+) create mode 100644 src/home/controllers/home.controller.ts create mode 100644 src/home/controllers/index.ts create mode 100644 src/home/dtos/add.home.dto.ts create mode 100644 src/home/dtos/home.list.dto.ts create mode 100644 src/home/dtos/index.ts create mode 100644 src/home/home.module.ts create mode 100644 src/home/services/home.service.ts create mode 100644 src/home/services/index.ts diff --git a/libs/common/src/modules/home/dtos/home.dto.ts b/libs/common/src/modules/home/dtos/home.dto.ts index 9db59f2..eae0630 100644 --- a/libs/common/src/modules/home/dtos/home.dto.ts +++ b/libs/common/src/modules/home/dtos/home.dto.ts @@ -12,4 +12,8 @@ export class HomeDto { @IsString() @IsNotEmpty() public homeId: string; + + @IsString() + @IsNotEmpty() + public homeName: string; } diff --git a/libs/common/src/modules/home/entities/home.entity.ts b/libs/common/src/modules/home/entities/home.entity.ts index 7760343..ac85641 100644 --- a/libs/common/src/modules/home/entities/home.entity.ts +++ b/libs/common/src/modules/home/entities/home.entity.ts @@ -22,6 +22,11 @@ export class HomeEntity extends AbstractEntity { }) public homeId: string; + @Column({ + nullable: false, + }) + public homeName: string; + constructor(partial: Partial) { super(); Object.assign(this, partial); diff --git a/src/app.module.ts b/src/app.module.ts index ff8439b..18a0a42 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -4,6 +4,7 @@ import config from './config'; 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'; @Module({ imports: [ ConfigModule.forRoot({ @@ -11,6 +12,7 @@ import { UserModule } from './users/user.module'; }), AuthenticationModule, UserModule, + HomeModule, ], controllers: [AuthenticationController], }) diff --git a/src/home/controllers/home.controller.ts b/src/home/controllers/home.controller.ts new file mode 100644 index 0000000..6b4eaa4 --- /dev/null +++ b/src/home/controllers/home.controller.ts @@ -0,0 +1,36 @@ +import { HomeService } from './../services/home.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 { AddHomeDto } from '../dtos/add.home.dto'; + +@ApiTags('Home Module') +@Controller({ + version: '1', + path: 'home', +}) +export class HomeController { + constructor(private readonly homeService: HomeService) {} + + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get(':userUuid') + async userList(@Param('userUuid') userUuid: string) { + try { + return await this.homeService.getHomesByUserId(userUuid); + } catch (err) { + throw new Error(err); + } + } + + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Post() + async addHome(@Body() addHomeDto: AddHomeDto) { + try { + return await this.homeService.addHome(addHomeDto); + } catch (err) { + throw new Error(err); + } + } +} diff --git a/src/home/controllers/index.ts b/src/home/controllers/index.ts new file mode 100644 index 0000000..66a5c99 --- /dev/null +++ b/src/home/controllers/index.ts @@ -0,0 +1 @@ +export * from './home.controller'; diff --git a/src/home/dtos/add.home.dto.ts b/src/home/dtos/add.home.dto.ts new file mode 100644 index 0000000..f0d5fbe --- /dev/null +++ b/src/home/dtos/add.home.dto.ts @@ -0,0 +1,20 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString } from 'class-validator'; + +export class AddHomeDto { + @ApiProperty({ + description: 'userUuid', + required: true, + }) + @IsString() + @IsNotEmpty() + public userUuid: string; + + @ApiProperty({ + description: 'homeName', + required: true, + }) + @IsString() + @IsNotEmpty() + public homeName: string; +} diff --git a/src/home/dtos/home.list.dto.ts b/src/home/dtos/home.list.dto.ts new file mode 100644 index 0000000..327a2ba --- /dev/null +++ b/src/home/dtos/home.list.dto.ts @@ -0,0 +1,32 @@ +import { + IsNotEmpty, + IsNumberString, + IsOptional, + IsString, +} from 'class-validator'; + +export class UserListDto { + @IsString() + @IsOptional() + schema: string; + + @IsNumberString() + @IsNotEmpty() + page_no: number; + + @IsNumberString() + @IsNotEmpty() + page_size: number; + + @IsString() + @IsOptional() + username: string; + + @IsNumberString() + @IsOptional() + start_time: number; + + @IsNumberString() + @IsOptional() + end_time: number; +} diff --git a/src/home/dtos/index.ts b/src/home/dtos/index.ts new file mode 100644 index 0000000..12618f9 --- /dev/null +++ b/src/home/dtos/index.ts @@ -0,0 +1,2 @@ +export * from './home.list.dto'; +export * from './add.home.dto'; diff --git a/src/home/home.module.ts b/src/home/home.module.ts new file mode 100644 index 0000000..76af90d --- /dev/null +++ b/src/home/home.module.ts @@ -0,0 +1,14 @@ +import { Module } from '@nestjs/common'; +import { HomeService } from './services/home.service'; +import { HomeController } from './controllers/home.controller'; +import { ConfigModule } from '@nestjs/config'; +import { HomeRepositoryModule } from '@app/common/modules/home/home.repository.module'; +import { HomeRepository } from '@app/common/modules/home/repositories'; + +@Module({ + imports: [ConfigModule, HomeRepositoryModule], + controllers: [HomeController], + providers: [HomeService, HomeRepository], + exports: [HomeService], +}) +export class HomeModule {} diff --git a/src/home/services/home.service.ts b/src/home/services/home.service.ts new file mode 100644 index 0000000..8c6602d --- /dev/null +++ b/src/home/services/home.service.ts @@ -0,0 +1,66 @@ +import { HomeRepository } from './../../../libs/common/src/modules/home/repositories/home.repository'; +import { HomeEntity } from './../../../libs/common/src/modules/home/entities/home.entity'; +import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; +import { TuyaContext } from '@tuya/tuya-connector-nodejs'; +import { ConfigService } from '@nestjs/config'; +import { AddHomeDto } from '../dtos'; + +@Injectable() +export class HomeService { + private tuya: TuyaContext; + constructor( + private readonly configService: ConfigService, + private readonly homeRepository: HomeRepository, + ) { + 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); + + return homesData; + } + + async findHomes(userUuid: string) { + return await this.homeRepository.find({ + where: { + userUuid: userUuid, + }, + }); + } + async addHome(addHomeDto: AddHomeDto) { + try { + const path = `/v2.0/cloud/space/creation`; + const data = await this.tuya.request({ + method: 'POST', + path, + body: { name: addHomeDto.homeName }, + }); + if (data.success) { + const homeEntity = { + userUuid: addHomeDto.userUuid, + homeId: data.result, + homeName: addHomeDto.homeName, + } as HomeEntity; + const savedHome = await this.homeRepository.save(homeEntity); + return savedHome; + } + return { + success: data.success, + homeId: data.result, + }; + } catch (error) { + throw new HttpException( + 'Error adding home', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } +} diff --git a/src/home/services/index.ts b/src/home/services/index.ts new file mode 100644 index 0000000..23d0070 --- /dev/null +++ b/src/home/services/index.ts @@ -0,0 +1 @@ +export * from './home.service'; From 35feab71bbd8eb5daa68c6682c27bbc832d2b1a3 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Mon, 11 Mar 2024 09:26:22 +0300 Subject: [PATCH 3/6] finshed homes endpoint --- src/home/services/home.service.ts | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/home/services/home.service.ts b/src/home/services/home.service.ts index 8c6602d..74da59b 100644 --- a/src/home/services/home.service.ts +++ b/src/home/services/home.service.ts @@ -25,15 +25,27 @@ export class HomeService { async getHomesByUserId(userUuid: string) { const homesData = await this.findHomes(userUuid); - return homesData; + const homesMapper = homesData.map((home) => ({ + homeId: home.homeId, + homeName: home.homeName, + })); + + return homesMapper; } async findHomes(userUuid: string) { - return await this.homeRepository.find({ - where: { - userUuid: userUuid, - }, - }); + try { + return await this.homeRepository.find({ + where: { + userUuid: userUuid, + }, + }); + } catch (error) { + throw new HttpException( + 'Error get homes', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } } async addHome(addHomeDto: AddHomeDto) { try { @@ -50,7 +62,10 @@ export class HomeService { homeName: addHomeDto.homeName, } as HomeEntity; const savedHome = await this.homeRepository.save(homeEntity); - return savedHome; + return { + homeId: savedHome.homeId, + homeName: savedHome.homeName, + }; } return { success: data.success, 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 4/6] 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 65ff07b0e6dcdd51da0dc360dc1c8136f6f555be Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Mon, 11 Mar 2024 10:34:03 +0300 Subject: [PATCH 5/6] fix some issues --- src/home/dtos/home.list.dto.ts | 32 -------------------------------- src/home/dtos/index.ts | 1 - 2 files changed, 33 deletions(-) delete mode 100644 src/home/dtos/home.list.dto.ts diff --git a/src/home/dtos/home.list.dto.ts b/src/home/dtos/home.list.dto.ts deleted file mode 100644 index 327a2ba..0000000 --- a/src/home/dtos/home.list.dto.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { - IsNotEmpty, - IsNumberString, - IsOptional, - IsString, -} from 'class-validator'; - -export class UserListDto { - @IsString() - @IsOptional() - schema: string; - - @IsNumberString() - @IsNotEmpty() - page_no: number; - - @IsNumberString() - @IsNotEmpty() - page_size: number; - - @IsString() - @IsOptional() - username: string; - - @IsNumberString() - @IsOptional() - start_time: number; - - @IsNumberString() - @IsOptional() - end_time: number; -} diff --git a/src/home/dtos/index.ts b/src/home/dtos/index.ts index 12618f9..912a7bd 100644 --- a/src/home/dtos/index.ts +++ b/src/home/dtos/index.ts @@ -1,2 +1 @@ -export * from './home.list.dto'; export * from './add.home.dto'; 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 6/6] 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`;