finished rename group api

This commit is contained in:
faris Aljohari
2024-03-12 13:49:41 +03:00
parent db22ceba17
commit 1f5789db1b
5 changed files with 74 additions and 2 deletions

View File

@ -7,12 +7,14 @@ import {
UseGuards,
Query,
Param,
Put,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
import { AddGroupDto } from '../dtos/add.group.dto';
import { GetGroupDto } from '../dtos/get.group.dto';
import { ControlGroupDto } from '../dtos/control.group.dto';
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
@ApiTags('Group Module')
@Controller({
@ -57,4 +59,15 @@ export class GroupController {
throw new Error(err);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Put('rename')
async renameGroup(@Body() renameGroupDto: RenameGroupDto) {
try {
return await this.groupService.renameGroup(renameGroupDto);
} catch (err) {
throw new Error(err);
}
}
}

View File

@ -1,12 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, IsObject } from 'class-validator';
import { IsNotEmpty, IsObject, IsNumberString } from 'class-validator';
export class ControlGroupDto {
@ApiProperty({
description: 'groupId',
required: true,
})
@IsString()
@IsNumberString()
@IsNotEmpty()
public groupId: string;

View File

@ -0,0 +1,20 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, IsNumberString } from 'class-validator';
export class RenameGroupDto {
@ApiProperty({
description: 'groupId',
required: true,
})
@IsNumberString()
@IsNotEmpty()
public groupId: string;
@ApiProperty({
description: 'groupName',
required: true,
})
@IsString()
@IsNotEmpty()
public groupName: string;
}

View File

@ -24,3 +24,9 @@ export class controlGroupInterface {
result: boolean;
msg: string;
}
export class renameGroupInterface {
success: boolean;
result: boolean;
msg: string;
}

View File

@ -7,9 +7,11 @@ import {
GetRoomDetailsInterface,
addGroupInterface,
controlGroupInterface,
renameGroupInterface,
} from '../interfaces/get.group.interface';
import { GetGroupDto } from '../dtos/get.group.dto';
import { ControlGroupDto } from '../dtos/control.group.dto';
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
@Injectable()
export class GroupService {
@ -158,4 +160,35 @@ export class GroupService {
);
}
}
async renameGroup(renameGroupDto: RenameGroupDto) {
const response = await this.renameGroupTuya(renameGroupDto);
if (response.success) {
return response;
} else {
throw new HttpException(
response.msg || 'Unknown error',
HttpStatus.BAD_REQUEST,
);
}
}
async renameGroupTuya(
renameGroupDto: RenameGroupDto,
): Promise<renameGroupInterface> {
try {
const path = `/v2.0/cloud/thing/group/${renameGroupDto.groupId}/${renameGroupDto.groupName}`;
const response = await this.tuya.request({
method: 'PUT',
path,
});
return response as renameGroupInterface;
} catch (error) {
throw new HttpException(
'Error control group',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}