Refactor group controller and service for better code organization

This commit is contained in:
faris Aljohari
2024-04-22 10:05:54 +03:00
parent 7abfe29746
commit fc7907d204
3 changed files with 77 additions and 31 deletions

View File

@ -26,8 +26,8 @@ import { CheckProductUuidForAllDevicesGuard } from 'src/guards/device.product.gu
export class GroupController {
constructor(private readonly groupService: GroupService) {}
// @ApiBearerAuth()
// @UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get('space/:spaceUuid')
async getGroupsBySpaceUuid(@Param('spaceUuid') spaceUuid: string) {
try {
@ -39,8 +39,8 @@ export class GroupController {
);
}
}
// @ApiBearerAuth()
// @UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':groupUuid')
async getGroupsByGroupId(@Param('groupUuid') groupUuid: string) {
try {
@ -52,8 +52,8 @@ export class GroupController {
);
}
}
// @ApiBearerAuth()
@UseGuards(CheckProductUuidForAllDevicesGuard)
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckProductUuidForAllDevicesGuard)
@Post()
async addGroup(@Body() addGroupDto: AddGroupDto) {
try {
@ -72,13 +72,16 @@ export class GroupController {
async controlGroup(@Body() controlGroupDto: ControlGroupDto) {
try {
return await this.groupService.controlGroup(controlGroupDto);
} catch (err) {
throw new Error(err);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
// @ApiBearerAuth()
// @UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Put('rename/:groupUuid')
async renameGroupByUuid(
@Param('groupUuid') groupUuid: string,
@ -97,8 +100,8 @@ export class GroupController {
}
}
// @ApiBearerAuth()
// @UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Delete(':groupUuid')
async deleteGroup(@Param('groupUuid') groupUuid: string) {
try {

View File

@ -1,20 +1,26 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsObject, IsNumberString } from 'class-validator';
import { IsNotEmpty, IsString } from 'class-validator';
export class ControlGroupDto {
@ApiProperty({
description: 'groupId',
description: 'groupUuid',
required: true,
})
@IsNumberString()
@IsString()
@IsNotEmpty()
public groupId: string;
public groupUuid: string;
@ApiProperty({
description: 'example {"switch_1":true,"add_ele":300}',
description: 'code',
required: true,
})
@IsObject()
@IsString()
@IsNotEmpty()
public properties: object;
public code: string;
@ApiProperty({
description: 'value',
required: true,
})
@IsNotEmpty()
public value: any;
}

View File

@ -10,12 +10,13 @@ import { AddGroupDto } from '../dtos/add.group.dto';
import {
GetGroupDetailsInterface,
GetGroupsBySpaceUuidInterface,
controlGroupInterface,
} from '../interfaces/get.group.interface';
import { ControlGroupDto } from '../dtos/control.group.dto';
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
import { GroupRepository } from '@app/common/modules/group/repositories';
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
import { controlDeviceInterface } from 'src/device/interfaces/get.device.interface';
import { ControlDeviceDto } from 'src/device/dtos';
@Injectable()
export class GroupService {
@ -111,9 +112,24 @@ export class GroupService {
throw error;
}
}
async controlGroup(controlGroupDto: ControlGroupDto) {
const response = await this.controlGroupTuya(controlGroupDto);
async getDevicesByGroupUuid(groupUuid: string) {
try {
const devices = await this.groupDeviceRepository.find({
relations: ['device'],
where: {
group: {
uuid: groupUuid,
},
isActive: true,
},
});
return devices;
} catch (error) {
throw error;
}
}
async controlDevice(controlDeviceDto: ControlDeviceDto) {
const response = await this.controlDeviceTuya(controlDeviceDto);
if (response.success) {
return response;
@ -124,24 +140,45 @@ export class GroupService {
);
}
}
async controlGroupTuya(
controlGroupDto: ControlGroupDto,
): Promise<controlGroupInterface> {
async controlDeviceTuya(
controlDeviceDto: ControlDeviceDto,
): Promise<controlDeviceInterface> {
try {
const path = `/v2.0/cloud/thing/group/properties`;
const path = `/v1.0/iot-03/devices/${controlDeviceDto.deviceId}/commands`;
const response = await this.tuya.request({
method: 'POST',
path,
body: {
group_id: controlGroupDto.groupId,
properties: controlGroupDto.properties,
commands: [
{ code: controlDeviceDto.code, value: controlDeviceDto.value },
],
},
});
return response as controlGroupInterface;
return response as controlDeviceInterface;
} catch (error) {
throw new HttpException(
'Error control group',
'Error control device from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async controlGroup(controlGroupDto: ControlGroupDto) {
const devices = await this.getDevicesByGroupUuid(controlGroupDto.groupUuid);
try {
await Promise.all(
devices.map(async (device) => {
return this.controlDevice({
deviceId: device.device.deviceTuyaUuid,
code: controlGroupDto.code,
value: controlGroupDto.value,
});
}),
);
} catch (error) {
throw new HttpException(
'Error controlling devices',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}