add get group by id api

This commit is contained in:
faris Aljohari
2024-03-12 15:03:06 +03:00
parent b5eab2c858
commit db7802776a
4 changed files with 61 additions and 32 deletions

View File

@ -27,18 +27,24 @@ export class GroupController {
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':homeId')
async userList(
@Param('homeId') homeId: string,
@Query() getGroupsDto: GetGroupDto,
) {
@Get()
async getGroupsByHomeId(@Query() getGroupsDto: GetGroupDto) {
try {
return await this.groupService.getGroupsByHomeId(homeId, getGroupsDto);
return await this.groupService.getGroupsByHomeId(getGroupsDto);
} catch (err) {
throw new Error(err);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':groupId')
async getGroupsByGroupId(@Param('groupId') groupId: number) {
try {
return await this.groupService.getGroupsByGroupId(groupId);
} catch (err) {
throw new Error(err);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Post()

View File

@ -2,6 +2,14 @@ import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsNumberString } from 'class-validator';
export class GetGroupDto {
@ApiProperty({
description: 'homeId',
required: true,
})
@IsNumberString()
@IsNotEmpty()
public homeId: string;
@ApiProperty({
description: 'pageSize',
required: true,

View File

@ -1,4 +1,4 @@
export class GetRoomDetailsInterface {
export class GetGroupDetailsInterface {
result: {
id: string;
name: string;

View File

@ -3,8 +3,8 @@ import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { ConfigService } from '@nestjs/config';
import { AddGroupDto } from '../dtos/add.group.dto';
import {
GetGroupDetailsInterface,
GetGroupsInterface,
GetRoomDetailsInterface,
addGroupInterface,
controlGroupInterface,
} from '../interfaces/get.group.interface';
@ -26,9 +26,9 @@ export class GroupService {
});
}
async getGroupsByHomeId(homeId: string, getGroupDto: GetGroupDto) {
async getGroupsByHomeId(getGroupDto: GetGroupDto) {
try {
const response = await this.getGroupsTuya(homeId, getGroupDto);
const response = await this.getGroupsTuya(getGroupDto);
const groups = response.result.data_list.map((group: any) => ({
groupId: group.id,
@ -47,17 +47,14 @@ export class GroupService {
}
}
async getGroupsTuya(
homeId: string,
getGroupDto: GetGroupDto,
): Promise<GetGroupsInterface> {
async getGroupsTuya(getGroupDto: GetGroupDto): Promise<GetGroupsInterface> {
try {
const path = `/v2.0/cloud/thing/group`;
const response = await this.tuya.request({
method: 'GET',
path,
query: {
space_id: homeId,
space_id: getGroupDto.homeId,
page_size: getGroupDto.pageSize,
page_no: getGroupDto.pageNo,
},
@ -70,23 +67,7 @@ export class GroupService {
);
}
}
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,
});
return response as GetRoomDetailsInterface; // Cast response to RoomData
} catch (error) {
throw new HttpException(
'Error fetching rooms details',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async addGroup(addGroupDto: AddGroupDto) {
const response = await this.addGroupTuya(addGroupDto);
@ -227,4 +208,38 @@ export class GroupService {
);
}
}
async getGroupsByGroupId(groupId: number) {
try {
const response = await this.getGroupsByGroupIdTuya(groupId);
return {
groupId: response.result.id,
groupName: response.result.name,
};
} catch (error) {
throw new HttpException(
'Error fetching group',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getGroupsByGroupIdTuya(
groupId: number,
): Promise<GetGroupDetailsInterface> {
try {
const path = `/v2.0/cloud/thing/group/${groupId}`;
const response = await this.tuya.request({
method: 'GET',
path,
});
return response as GetGroupDetailsInterface;
} catch (error) {
throw new HttpException(
'Error fetching group ',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}