finished delete group api

This commit is contained in:
faris Aljohari
2024-03-12 14:11:23 +03:00
parent 1f5789db1b
commit 4cdba75367
3 changed files with 53 additions and 11 deletions

View File

@ -8,6 +8,7 @@ import {
Query,
Param,
Put,
Delete,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
@ -70,4 +71,15 @@ export class GroupController {
throw new Error(err);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Delete(':groupId')
async deleteGroup(@Param('groupId') groupId: number) {
try {
return await this.groupService.deleteGroup(groupId);
} catch (err) {
throw new Error(err);
}
}
}

View File

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

View File

@ -7,7 +7,6 @@ import {
GetRoomDetailsInterface,
addGroupInterface,
controlGroupInterface,
renameGroupInterface,
} from '../interfaces/get.group.interface';
import { GetGroupDto } from '../dtos/get.group.dto';
import { ControlGroupDto } from '../dtos/control.group.dto';
@ -165,7 +164,11 @@ export class GroupService {
const response = await this.renameGroupTuya(renameGroupDto);
if (response.success) {
return response;
return {
success: response.success,
result: response.result,
msg: response.msg,
};
} else {
throw new HttpException(
response.msg || 'Unknown error',
@ -175,7 +178,7 @@ export class GroupService {
}
async renameGroupTuya(
renameGroupDto: RenameGroupDto,
): Promise<renameGroupInterface> {
): Promise<controlGroupInterface> {
try {
const path = `/v2.0/cloud/thing/group/${renameGroupDto.groupId}/${renameGroupDto.groupName}`;
const response = await this.tuya.request({
@ -183,10 +186,43 @@ export class GroupService {
path,
});
return response as renameGroupInterface;
return response as controlGroupInterface;
} catch (error) {
throw new HttpException(
'Error control group',
'Error rename group',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async deleteGroup(groupId: number) {
const response = await this.deleteGroupTuya(groupId);
if (response.success) {
return {
success: response.success,
result: response.result,
msg: response.msg,
};
} else {
throw new HttpException(
response.msg || 'Unknown error',
HttpStatus.BAD_REQUEST,
);
}
}
async deleteGroupTuya(groupId: number): Promise<controlGroupInterface> {
try {
const path = `/v2.0/cloud/thing/group/${groupId}`;
const response = await this.tuya.request({
method: 'DELETE',
path,
});
return response as controlGroupInterface;
} catch (error) {
throw new HttpException(
'Error delete group',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}