mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
finished get rooms api
This commit is contained in:
@ -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 {
|
||||
|
11
src/room/interfaces/get.room.interface.ts
Normal file
11
src/room/interfaces/get.room.interface.ts
Normal file
@ -0,0 +1,11 @@
|
||||
export class GetRoomDetailsInterface {
|
||||
result: {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
}
|
||||
export class GetRoomsIdsInterface {
|
||||
result: {
|
||||
data: [];
|
||||
};
|
||||
}
|
@ -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<GetRoomsIdsInterface> {
|
||||
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<GetRoomDetailsInterface> {
|
||||
// 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`;
|
||||
|
Reference in New Issue
Block a user