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() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get(':homeId') @Get()
async userList( async getGroupsByHomeId(@Query() getGroupsDto: GetGroupDto) {
@Param('homeId') homeId: string,
@Query() getGroupsDto: GetGroupDto,
) {
try { 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) { } catch (err) {
throw new Error(err); throw new Error(err);
} }
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post() @Post()

View File

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

View File

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

View File

@ -3,8 +3,8 @@ import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { AddGroupDto } from '../dtos/add.group.dto'; import { AddGroupDto } from '../dtos/add.group.dto';
import { import {
GetGroupDetailsInterface,
GetGroupsInterface, GetGroupsInterface,
GetRoomDetailsInterface,
addGroupInterface, addGroupInterface,
controlGroupInterface, controlGroupInterface,
} from '../interfaces/get.group.interface'; } from '../interfaces/get.group.interface';
@ -26,9 +26,9 @@ export class GroupService {
}); });
} }
async getGroupsByHomeId(homeId: string, getGroupDto: GetGroupDto) { async getGroupsByHomeId(getGroupDto: GetGroupDto) {
try { try {
const response = await this.getGroupsTuya(homeId, getGroupDto); const response = await this.getGroupsTuya(getGroupDto);
const groups = response.result.data_list.map((group: any) => ({ const groups = response.result.data_list.map((group: any) => ({
groupId: group.id, groupId: group.id,
@ -47,17 +47,14 @@ export class GroupService {
} }
} }
async getGroupsTuya( async getGroupsTuya(getGroupDto: GetGroupDto): Promise<GetGroupsInterface> {
homeId: string,
getGroupDto: GetGroupDto,
): Promise<GetGroupsInterface> {
try { try {
const path = `/v2.0/cloud/thing/group`; const path = `/v2.0/cloud/thing/group`;
const response = await this.tuya.request({ const response = await this.tuya.request({
method: 'GET', method: 'GET',
path, path,
query: { query: {
space_id: homeId, space_id: getGroupDto.homeId,
page_size: getGroupDto.pageSize, page_size: getGroupDto.pageSize,
page_no: getGroupDto.pageNo, 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) { async addGroup(addGroupDto: AddGroupDto) {
const response = await this.addGroupTuya(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,
);
}
}
} }