mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
Refactor group and device modules
This commit is contained in:
@ -13,18 +13,10 @@ import {
|
|||||||
Put,
|
Put,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import {
|
import { AddDeviceDto, UpdateDeviceInRoomDto } from '../dtos/add.device.dto';
|
||||||
AddDeviceDto,
|
import { GetDeviceByRoomUuidDto } from '../dtos/get.device.dto';
|
||||||
AddDeviceInGroupDto,
|
|
||||||
UpdateDeviceInRoomDto,
|
|
||||||
} from '../dtos/add.device.dto';
|
|
||||||
import {
|
|
||||||
GetDeviceByGroupIdDto,
|
|
||||||
GetDeviceByRoomUuidDto,
|
|
||||||
} from '../dtos/get.device.dto';
|
|
||||||
import { ControlDeviceDto } from '../dtos/control.device.dto';
|
import { ControlDeviceDto } from '../dtos/control.device.dto';
|
||||||
import { CheckRoomGuard } from 'src/guards/room.guard';
|
import { CheckRoomGuard } from 'src/guards/room.guard';
|
||||||
import { CheckGroupGuard } from 'src/guards/group.guard';
|
|
||||||
import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard';
|
import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard';
|
||||||
import { CheckUserHaveControllablePermission } from 'src/guards/user.device.controllable.permission.guard';
|
import { CheckUserHaveControllablePermission } from 'src/guards/user.device.controllable.permission.guard';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
@ -116,39 +108,7 @@ export class DeviceController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
|
||||||
@UseGuards(JwtAuthGuard, CheckGroupGuard)
|
|
||||||
@Get('group')
|
|
||||||
async getDevicesByGroupId(
|
|
||||||
@Query() getDeviceByGroupIdDto: GetDeviceByGroupIdDto,
|
|
||||||
@Req() req: any,
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
const userUuid = req.user.uuid;
|
|
||||||
return await this.deviceService.getDevicesByGroupId(
|
|
||||||
getDeviceByGroupIdDto,
|
|
||||||
userUuid,
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.message || 'Internal server error',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ApiBearerAuth()
|
|
||||||
@UseGuards(JwtAuthGuard, CheckGroupGuard)
|
|
||||||
@Post('group')
|
|
||||||
async addDeviceInGroup(@Body() addDeviceInGroupDto: AddDeviceInGroupDto) {
|
|
||||||
try {
|
|
||||||
return await this.deviceService.addDeviceInGroup(addDeviceInGroupDto);
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.message || 'Internal server error',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard, CheckUserHavePermission)
|
@UseGuards(JwtAuthGuard, CheckUserHavePermission)
|
||||||
@Get(':deviceUuid')
|
@Get(':deviceUuid')
|
||||||
|
@ -8,18 +8,10 @@ import { DeviceRepositoryModule } from '@app/common/modules/device';
|
|||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
|
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
|
|
||||||
import { GroupRepository } from '@app/common/modules/group/repositories';
|
|
||||||
import { GroupRepositoryModule } from '@app/common/modules/group/group.repository.module';
|
|
||||||
import { DeviceUserPermissionRepository } from '@app/common/modules/device-user-permission/repositories';
|
import { DeviceUserPermissionRepository } from '@app/common/modules/device-user-permission/repositories';
|
||||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [ConfigModule, ProductRepositoryModule, DeviceRepositoryModule],
|
||||||
ConfigModule,
|
|
||||||
ProductRepositoryModule,
|
|
||||||
DeviceRepositoryModule,
|
|
||||||
GroupRepositoryModule,
|
|
||||||
],
|
|
||||||
controllers: [DeviceController],
|
controllers: [DeviceController],
|
||||||
providers: [
|
providers: [
|
||||||
DeviceService,
|
DeviceService,
|
||||||
@ -28,8 +20,6 @@ import { UserRepository } from '@app/common/modules/user/repositories';
|
|||||||
PermissionTypeRepository,
|
PermissionTypeRepository,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
DeviceRepository,
|
DeviceRepository,
|
||||||
GroupDeviceRepository,
|
|
||||||
GroupRepository,
|
|
||||||
UserRepository,
|
UserRepository,
|
||||||
],
|
],
|
||||||
exports: [DeviceService],
|
exports: [DeviceService],
|
||||||
|
@ -35,20 +35,3 @@ export class UpdateDeviceInRoomDto {
|
|||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public roomUuid: string;
|
public roomUuid: string;
|
||||||
}
|
}
|
||||||
export class AddDeviceInGroupDto {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'deviceUuid',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public deviceUuid: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'groupUuid',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public groupUuid: string;
|
|
||||||
}
|
|
||||||
|
@ -10,12 +10,3 @@ export class GetDeviceByRoomUuidDto {
|
|||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public roomUuid: string;
|
public roomUuid: string;
|
||||||
}
|
}
|
||||||
export class GetDeviceByGroupIdDto {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'groupUuid',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public groupUuid: string;
|
|
||||||
}
|
|
||||||
|
@ -7,11 +7,7 @@ import {
|
|||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import {
|
import { AddDeviceDto, UpdateDeviceInRoomDto } from '../dtos/add.device.dto';
|
||||||
AddDeviceDto,
|
|
||||||
AddDeviceInGroupDto,
|
|
||||||
UpdateDeviceInRoomDto,
|
|
||||||
} from '../dtos/add.device.dto';
|
|
||||||
import {
|
import {
|
||||||
DeviceInstructionResponse,
|
DeviceInstructionResponse,
|
||||||
GetDeviceDetailsFunctionsInterface,
|
GetDeviceDetailsFunctionsInterface,
|
||||||
@ -19,14 +15,10 @@ import {
|
|||||||
GetDeviceDetailsInterface,
|
GetDeviceDetailsInterface,
|
||||||
controlDeviceInterface,
|
controlDeviceInterface,
|
||||||
} from '../interfaces/get.device.interface';
|
} from '../interfaces/get.device.interface';
|
||||||
import {
|
import { GetDeviceByRoomUuidDto } from '../dtos/get.device.dto';
|
||||||
GetDeviceByGroupIdDto,
|
|
||||||
GetDeviceByRoomUuidDto,
|
|
||||||
} from '../dtos/get.device.dto';
|
|
||||||
import { ControlDeviceDto } from '../dtos/control.device.dto';
|
import { ControlDeviceDto } from '../dtos/control.device.dto';
|
||||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
|
|
||||||
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
||||||
import { In } from 'typeorm';
|
import { In } from 'typeorm';
|
||||||
|
|
||||||
@ -36,7 +28,6 @@ export class DeviceService {
|
|||||||
constructor(
|
constructor(
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly deviceRepository: DeviceRepository,
|
private readonly deviceRepository: DeviceRepository,
|
||||||
private readonly groupDeviceRepository: GroupDeviceRepository,
|
|
||||||
private readonly productRepository: ProductRepository,
|
private readonly productRepository: ProductRepository,
|
||||||
) {
|
) {
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
@ -195,73 +186,6 @@ export class DeviceService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async getDevicesByGroupId(
|
|
||||||
getDeviceByGroupIdDto: GetDeviceByGroupIdDto,
|
|
||||||
userUuid: string,
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
const groupDevices = await this.groupDeviceRepository.find({
|
|
||||||
where: {
|
|
||||||
group: { uuid: getDeviceByGroupIdDto.groupUuid },
|
|
||||||
device: {
|
|
||||||
permission: {
|
|
||||||
userUuid,
|
|
||||||
permissionType: {
|
|
||||||
type: PermissionType.READ || PermissionType.CONTROLLABLE,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
relations: [
|
|
||||||
'device',
|
|
||||||
'device.productDevice',
|
|
||||||
'device.permission',
|
|
||||||
'device.permission.permissionType',
|
|
||||||
],
|
|
||||||
});
|
|
||||||
const devicesData = await Promise.all(
|
|
||||||
groupDevices.map(async (device) => {
|
|
||||||
return {
|
|
||||||
...(await this.getDeviceDetailsByDeviceIdTuya(
|
|
||||||
device.device.deviceTuyaUuid,
|
|
||||||
)),
|
|
||||||
uuid: device.device.uuid,
|
|
||||||
productUuid: device.device.productDevice.uuid,
|
|
||||||
productType: device.device.productDevice.prodType,
|
|
||||||
} as GetDeviceDetailsInterface;
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
return devicesData;
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
'Error fetching devices by group',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async addDeviceInGroup(addDeviceInGroupDto: AddDeviceInGroupDto) {
|
|
||||||
try {
|
|
||||||
await this.groupDeviceRepository.save({
|
|
||||||
device: { uuid: addDeviceInGroupDto.deviceUuid },
|
|
||||||
group: { uuid: addDeviceInGroupDto.groupUuid },
|
|
||||||
});
|
|
||||||
return { message: 'device added in group successfully' };
|
|
||||||
} catch (error) {
|
|
||||||
if (error.code === '23505') {
|
|
||||||
throw new HttpException(
|
|
||||||
'Device already exists in the group',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
throw new HttpException(
|
|
||||||
'Failed to add device in group',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async controlDevice(controlDeviceDto: ControlDeviceDto, deviceUuid: string) {
|
async controlDevice(controlDeviceDto: ControlDeviceDto, deviceUuid: string) {
|
||||||
try {
|
try {
|
||||||
|
@ -1,22 +1,16 @@
|
|||||||
import { GroupService } from '../services/group.service';
|
import { GroupService } from '../services/group.service';
|
||||||
import {
|
import {
|
||||||
Body,
|
|
||||||
Controller,
|
Controller,
|
||||||
Get,
|
Get,
|
||||||
Post,
|
|
||||||
UseGuards,
|
UseGuards,
|
||||||
Param,
|
Param,
|
||||||
Put,
|
|
||||||
Delete,
|
|
||||||
HttpException,
|
HttpException,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
|
Req,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import { AddGroupDto } from '../dtos/add.group.dto';
|
|
||||||
import { ControlGroupDto } from '../dtos/control.group.dto';
|
|
||||||
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
|
|
||||||
import { CheckProductUuidForAllDevicesGuard } from 'src/guards/device.product.guard';
|
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
|
import { UnitPermissionGuard } from 'src/guards/unit.permission.guard';
|
||||||
|
|
||||||
@ApiTags('Group Module')
|
@ApiTags('Group Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -27,11 +21,11 @@ export class GroupController {
|
|||||||
constructor(private readonly groupService: GroupService) {}
|
constructor(private readonly groupService: GroupService) {}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
||||||
@Get('space/:spaceUuid')
|
@Get(':unitUuid')
|
||||||
async getGroupsBySpaceUuid(@Param('spaceUuid') spaceUuid: string) {
|
async getGroupsBySpaceUuid(@Param('unitUuid') unitUuid: string) {
|
||||||
try {
|
try {
|
||||||
return await this.groupService.getGroupsBySpaceUuid(spaceUuid);
|
return await this.groupService.getGroupsByUnitUuid(unitUuid);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
@ -40,72 +34,21 @@ export class GroupController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
||||||
@Get(':groupUuid')
|
@Get(':unitUuid/devices/:groupName')
|
||||||
async getGroupsByGroupId(@Param('groupUuid') groupUuid: string) {
|
async getUnitDevicesByGroupName(
|
||||||
try {
|
@Param('unitUuid') unitUuid: string,
|
||||||
return await this.groupService.getGroupsByGroupUuid(groupUuid);
|
@Param('groupName') groupName: string,
|
||||||
} catch (error) {
|
@Req() req: any,
|
||||||
throw new HttpException(
|
|
||||||
error.message || 'Internal server error',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ApiBearerAuth()
|
|
||||||
@UseGuards(JwtAuthGuard, CheckProductUuidForAllDevicesGuard)
|
|
||||||
@Post()
|
|
||||||
async addGroup(@Body() addGroupDto: AddGroupDto) {
|
|
||||||
try {
|
|
||||||
return await this.groupService.addGroup(addGroupDto);
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.message || 'Internal server error',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ApiBearerAuth()
|
|
||||||
@UseGuards(JwtAuthGuard)
|
|
||||||
@Post('control')
|
|
||||||
async controlGroup(@Body() controlGroupDto: ControlGroupDto) {
|
|
||||||
try {
|
|
||||||
return await this.groupService.controlGroup(controlGroupDto);
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.message || 'Internal server error',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ApiBearerAuth()
|
|
||||||
@UseGuards(JwtAuthGuard)
|
|
||||||
@Put('rename/:groupUuid')
|
|
||||||
async renameGroupByUuid(
|
|
||||||
@Param('groupUuid') groupUuid: string,
|
|
||||||
@Body() renameGroupDto: RenameGroupDto,
|
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
return await this.groupService.renameGroupByUuid(
|
const userUuid = req.user.uuid;
|
||||||
groupUuid,
|
|
||||||
renameGroupDto,
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.message || 'Internal server error',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ApiBearerAuth()
|
return await this.groupService.getUnitDevicesByGroupName(
|
||||||
@UseGuards(JwtAuthGuard)
|
unitUuid,
|
||||||
@Delete(':groupUuid')
|
groupName,
|
||||||
async deleteGroup(@Param('groupUuid') groupUuid: string) {
|
userUuid,
|
||||||
try {
|
);
|
||||||
return await this.groupService.deleteGroup(groupUuid);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
|
||||||
import { IsNotEmpty, IsString, IsArray } from 'class-validator';
|
|
||||||
|
|
||||||
export class AddGroupDto {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'groupName',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public groupName: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'deviceUuids',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsArray()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public deviceUuids: [string];
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
|
||||||
import { IsNotEmpty, IsString } from 'class-validator';
|
|
||||||
|
|
||||||
export class ControlGroupDto {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'groupUuid',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public groupUuid: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'code',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public code: string;
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'value',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsNotEmpty()
|
|
||||||
public value: any;
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
export * from './add.group.dto';
|
|
@ -1,12 +0,0 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
|
||||||
import { IsNotEmpty, IsString } from 'class-validator';
|
|
||||||
|
|
||||||
export class RenameGroupDto {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'groupName',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public groupName: string;
|
|
||||||
}
|
|
@ -1,26 +1,20 @@
|
|||||||
|
import { DeviceRepository } from './../../libs/common/src/modules/device/repositories/device.repository';
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { GroupService } from './services/group.service';
|
import { GroupService } from './services/group.service';
|
||||||
import { GroupController } from './controllers/group.controller';
|
import { GroupController } from './controllers/group.controller';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
import { GroupRepositoryModule } from '@app/common/modules/group/group.repository.module';
|
|
||||||
import { GroupRepository } from '@app/common/modules/group/repositories';
|
|
||||||
import { GroupDeviceRepositoryModule } from '@app/common/modules/group-device/group.device.repository.module';
|
|
||||||
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
|
|
||||||
import { DeviceRepositoryModule } from '@app/common/modules/device';
|
import { DeviceRepositoryModule } from '@app/common/modules/device';
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
|
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [ConfigModule, DeviceRepositoryModule],
|
||||||
ConfigModule,
|
|
||||||
GroupRepositoryModule,
|
|
||||||
GroupDeviceRepositoryModule,
|
|
||||||
DeviceRepositoryModule,
|
|
||||||
],
|
|
||||||
controllers: [GroupController],
|
controllers: [GroupController],
|
||||||
providers: [
|
providers: [
|
||||||
GroupService,
|
GroupService,
|
||||||
GroupRepository,
|
|
||||||
GroupDeviceRepository,
|
|
||||||
DeviceRepository,
|
DeviceRepository,
|
||||||
|
SpaceRepository,
|
||||||
|
DeviceRepository,
|
||||||
|
ProductRepository,
|
||||||
],
|
],
|
||||||
exports: [GroupService],
|
exports: [GroupService],
|
||||||
})
|
})
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
export interface GetGroupDetailsInterface {
|
|
||||||
groupUuid: string;
|
|
||||||
groupName: string;
|
|
||||||
createdAt: Date;
|
|
||||||
updatedAt: Date;
|
|
||||||
}
|
|
||||||
export interface GetGroupsBySpaceUuidInterface {
|
|
||||||
groupUuid: string;
|
|
||||||
groupName: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface controlGroupInterface {
|
|
||||||
success: boolean;
|
|
||||||
result: boolean;
|
|
||||||
msg: string;
|
|
||||||
}
|
|
@ -1,33 +1,23 @@
|
|||||||
import {
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
Injectable,
|
|
||||||
HttpException,
|
|
||||||
HttpStatus,
|
|
||||||
BadRequestException,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
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 { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import {
|
import { GetDeviceDetailsInterface } from 'src/device/interfaces/get.device.interface';
|
||||||
GetGroupDetailsInterface,
|
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||||
GetGroupsBySpaceUuidInterface,
|
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||||
} from '../interfaces/get.group.interface';
|
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
||||||
import { ControlGroupDto } from '../dtos/control.group.dto';
|
import { In } from 'typeorm';
|
||||||
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';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GroupService {
|
export class GroupService {
|
||||||
private tuya: TuyaContext;
|
private tuya: TuyaContext;
|
||||||
constructor(
|
constructor(
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly groupRepository: GroupRepository,
|
private readonly productRepository: ProductRepository,
|
||||||
private readonly groupDeviceRepository: GroupDeviceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
) {
|
) {
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||||
// const clientId = this.configService.get<string>('auth-config.CLIENT_ID');
|
|
||||||
this.tuya = new TuyaContext({
|
this.tuya = new TuyaContext({
|
||||||
baseUrl: 'https://openapi.tuyaeu.com',
|
baseUrl: 'https://openapi.tuyaeu.com',
|
||||||
accessKey,
|
accessKey,
|
||||||
@ -35,230 +25,126 @@ export class GroupService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getGroupsBySpaceUuid(
|
async getGroupsByUnitUuid(unitUuid: string) {
|
||||||
spaceUuid: string,
|
|
||||||
): Promise<GetGroupsBySpaceUuidInterface[]> {
|
|
||||||
try {
|
try {
|
||||||
const groupDevices = await this.groupDeviceRepository.find({
|
const spaces = await this.spaceRepository.find({
|
||||||
relations: ['group', 'device'],
|
|
||||||
where: {
|
where: {
|
||||||
device: { spaceDevice: { uuid: spaceUuid } },
|
parent: {
|
||||||
isActive: true,
|
uuid: unitUuid,
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Extract and return only the group entities
|
|
||||||
const groups = groupDevices.map((groupDevice) => {
|
|
||||||
return {
|
|
||||||
groupUuid: groupDevice.uuid,
|
|
||||||
groupName: groupDevice.group.groupName,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
if (groups.length > 0) {
|
|
||||||
return groups;
|
|
||||||
} else {
|
|
||||||
throw new HttpException(
|
|
||||||
'this space has no groups',
|
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.message || 'Error fetching groups',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async addGroup(addGroupDto: AddGroupDto) {
|
|
||||||
try {
|
|
||||||
const group = await this.groupRepository.save({
|
|
||||||
groupName: addGroupDto.groupName,
|
|
||||||
});
|
|
||||||
|
|
||||||
const groupDevicePromises = addGroupDto.deviceUuids.map(
|
|
||||||
async (deviceUuid) => {
|
|
||||||
await this.saveGroupDevice(group.uuid, deviceUuid);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
await Promise.all(groupDevicePromises);
|
|
||||||
return { message: 'Group added successfully' };
|
|
||||||
} catch (err) {
|
|
||||||
if (err.code === '23505') {
|
|
||||||
throw new HttpException(
|
|
||||||
'User already belongs to this group',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
throw new HttpException(
|
|
||||||
err.message || 'Internal Server Error',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async saveGroupDevice(groupUuid: string, deviceUuid: string) {
|
|
||||||
try {
|
|
||||||
await this.groupDeviceRepository.save({
|
|
||||||
group: {
|
|
||||||
uuid: groupUuid,
|
|
||||||
},
|
|
||||||
device: {
|
|
||||||
uuid: deviceUuid,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async getDevicesByGroupUuid(groupUuid: string) {
|
|
||||||
try {
|
|
||||||
const devices = await this.groupDeviceRepository.find({
|
|
||||||
relations: ['device'],
|
|
||||||
where: {
|
|
||||||
group: {
|
|
||||||
uuid: groupUuid,
|
|
||||||
},
|
},
|
||||||
isActive: true,
|
|
||||||
},
|
},
|
||||||
|
relations: ['devicesSpaceEntity', 'devicesSpaceEntity.productDevice'],
|
||||||
});
|
});
|
||||||
return devices;
|
|
||||||
} catch (error) {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async controlDevice(deviceUuid: string, code: string, value: any) {
|
|
||||||
try {
|
|
||||||
const response = await this.controlDeviceTuya(deviceUuid, code, value);
|
|
||||||
|
|
||||||
if (response.success) {
|
const groupNames = spaces.flatMap((space) => {
|
||||||
return response;
|
return space.devicesSpaceEntity.map(
|
||||||
} else {
|
(device) => device.productDevice.prodType,
|
||||||
throw new HttpException(
|
|
||||||
response.msg || 'Unknown error',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
);
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async controlDeviceTuya(
|
|
||||||
deviceUuid: string,
|
|
||||||
code: string,
|
|
||||||
value: any,
|
|
||||||
): Promise<controlDeviceInterface> {
|
|
||||||
try {
|
|
||||||
const path = `/v1.0/iot-03/devices/${deviceUuid}/commands`;
|
|
||||||
const response = await this.tuya.request({
|
|
||||||
method: 'POST',
|
|
||||||
path,
|
|
||||||
body: {
|
|
||||||
commands: [{ code, value: value }],
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return response as controlDeviceInterface;
|
const uniqueGroupNames = [...new Set(groupNames)];
|
||||||
|
|
||||||
|
return uniqueGroupNames.map((groupName) => ({ groupName }));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error control device from Tuya',
|
'This unit does not have any groups',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.NOT_FOUND,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async controlGroup(controlGroupDto: ControlGroupDto) {
|
|
||||||
const devices = await this.getDevicesByGroupUuid(controlGroupDto.groupUuid);
|
|
||||||
|
|
||||||
|
async getUnitDevicesByGroupName(
|
||||||
|
unitUuid: string,
|
||||||
|
groupName: string,
|
||||||
|
userUuid: string,
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
await Promise.all(
|
const spaces = await this.spaceRepository.find({
|
||||||
devices.map(async (device) => {
|
where: {
|
||||||
return this.controlDevice(
|
parent: {
|
||||||
device.device.deviceTuyaUuid,
|
uuid: unitUuid,
|
||||||
controlGroupDto.code,
|
},
|
||||||
controlGroupDto.value,
|
devicesSpaceEntity: {
|
||||||
|
productDevice: {
|
||||||
|
prodType: groupName,
|
||||||
|
},
|
||||||
|
permission: {
|
||||||
|
userUuid,
|
||||||
|
permissionType: {
|
||||||
|
type: In([PermissionType.READ, PermissionType.CONTROLLABLE]),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
relations: [
|
||||||
|
'devicesSpaceEntity',
|
||||||
|
'devicesSpaceEntity.productDevice',
|
||||||
|
'devicesSpaceEntity.spaceDevice',
|
||||||
|
'devicesSpaceEntity.permission',
|
||||||
|
'devicesSpaceEntity.permission.permissionType',
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const devices = await Promise.all(
|
||||||
|
spaces.flatMap(async (space) => {
|
||||||
|
return await Promise.all(
|
||||||
|
space.devicesSpaceEntity.map(async (device) => {
|
||||||
|
const deviceDetails = await this.getDeviceDetailsByDeviceIdTuya(
|
||||||
|
device.deviceTuyaUuid,
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
haveRoom: !!device.spaceDevice,
|
||||||
|
productUuid: device.productDevice.uuid,
|
||||||
|
productType: device.productDevice.prodType,
|
||||||
|
permissionType: device.permission[0]?.permissionType?.type,
|
||||||
|
...deviceDetails,
|
||||||
|
uuid: device.uuid,
|
||||||
|
};
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
return { message: 'Group controlled successfully', success: true };
|
if (devices.length === 0)
|
||||||
|
throw new HttpException('No devices found', HttpStatus.NOT_FOUND);
|
||||||
|
return devices.flat(); // Flatten the array since flatMap was used
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error controlling devices',
|
'This unit does not have any devices for the specified group name',
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getDeviceDetailsByDeviceIdTuya(
|
||||||
|
deviceId: string,
|
||||||
|
): Promise<GetDeviceDetailsInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v1.1/iot-03/devices/${deviceId}`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'GET',
|
||||||
|
path,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Convert keys to camel case
|
||||||
|
const camelCaseResponse = convertKeysToCamelCase(response);
|
||||||
|
const product = await this.productRepository.findOne({
|
||||||
|
where: {
|
||||||
|
prodId: camelCaseResponse.result.productId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
const { productName, productId, ...rest } = camelCaseResponse.result;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...rest,
|
||||||
|
productUuid: product.uuid,
|
||||||
|
} as GetDeviceDetailsInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching device details from Tuya',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async renameGroupByUuid(
|
|
||||||
groupUuid: string,
|
|
||||||
renameGroupDto: RenameGroupDto,
|
|
||||||
): Promise<GetGroupsBySpaceUuidInterface> {
|
|
||||||
try {
|
|
||||||
await this.groupRepository.update(
|
|
||||||
{ uuid: groupUuid },
|
|
||||||
{ groupName: renameGroupDto.groupName },
|
|
||||||
);
|
|
||||||
|
|
||||||
// Fetch the updated floor
|
|
||||||
const updatedGroup = await this.groupRepository.findOneOrFail({
|
|
||||||
where: { uuid: groupUuid },
|
|
||||||
});
|
|
||||||
return {
|
|
||||||
groupUuid: updatedGroup.uuid,
|
|
||||||
groupName: updatedGroup.groupName,
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException('Group not found', HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async deleteGroup(groupUuid: string) {
|
|
||||||
try {
|
|
||||||
const group = await this.getGroupsByGroupUuid(groupUuid);
|
|
||||||
|
|
||||||
if (!group) {
|
|
||||||
throw new HttpException('Group not found', HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.groupRepository.update(
|
|
||||||
{ uuid: groupUuid },
|
|
||||||
{ isActive: false },
|
|
||||||
);
|
|
||||||
|
|
||||||
return { message: 'Group deleted successfully' };
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.message || 'Error deleting group',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async getGroupsByGroupUuid(
|
|
||||||
groupUuid: string,
|
|
||||||
): Promise<GetGroupDetailsInterface> {
|
|
||||||
try {
|
|
||||||
const group = await this.groupRepository.findOne({
|
|
||||||
where: {
|
|
||||||
uuid: groupUuid,
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
if (!group) {
|
|
||||||
throw new BadRequestException('Invalid group UUID');
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
groupUuid: group.uuid,
|
|
||||||
groupName: group.groupName,
|
|
||||||
createdAt: group.createdAt,
|
|
||||||
updatedAt: group.updatedAt,
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
if (err instanceof BadRequestException) {
|
|
||||||
throw err; // Re-throw BadRequestException
|
|
||||||
} else {
|
|
||||||
throw new HttpException('Group not found', HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,81 +0,0 @@
|
|||||||
import {
|
|
||||||
CanActivate,
|
|
||||||
ExecutionContext,
|
|
||||||
Injectable,
|
|
||||||
HttpStatus,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
|
|
||||||
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|
||||||
import { GroupRepository } from '@app/common/modules/group/repositories';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class CheckGroupGuard implements CanActivate {
|
|
||||||
constructor(
|
|
||||||
private readonly groupRepository: GroupRepository,
|
|
||||||
private readonly deviceRepository: DeviceRepository,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
||||||
const req = context.switchToHttp().getRequest();
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (req.query && req.query.groupUuid) {
|
|
||||||
const { groupUuid } = req.query;
|
|
||||||
await this.checkGroupIsFound(groupUuid);
|
|
||||||
} else if (req.body && req.body.groupUuid && req.body.deviceUuid) {
|
|
||||||
const { groupUuid, deviceUuid } = req.body;
|
|
||||||
await this.checkGroupIsFound(groupUuid);
|
|
||||||
await this.checkDeviceIsFound(deviceUuid);
|
|
||||||
} else {
|
|
||||||
throw new BadRequestException('Invalid request parameters');
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
} catch (error) {
|
|
||||||
this.handleGuardError(error, context);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async checkGroupIsFound(groupUuid: string) {
|
|
||||||
const group = await this.groupRepository.findOne({
|
|
||||||
where: {
|
|
||||||
uuid: groupUuid,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!group) {
|
|
||||||
throw new NotFoundException('Group not found');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async checkDeviceIsFound(deviceUuid: string) {
|
|
||||||
const device = await this.deviceRepository.findOne({
|
|
||||||
where: {
|
|
||||||
uuid: deviceUuid,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!device) {
|
|
||||||
throw new NotFoundException('Device not found');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private handleGuardError(error: Error, context: ExecutionContext) {
|
|
||||||
const response = context.switchToHttp().getResponse();
|
|
||||||
if (error instanceof NotFoundException) {
|
|
||||||
response
|
|
||||||
.status(HttpStatus.NOT_FOUND)
|
|
||||||
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
|
|
||||||
} else if (error instanceof BadRequestException) {
|
|
||||||
response
|
|
||||||
.status(HttpStatus.BAD_REQUEST)
|
|
||||||
.json({ statusCode: HttpStatus.BAD_REQUEST, message: error.message });
|
|
||||||
} else {
|
|
||||||
response.status(HttpStatus.BAD_REQUEST).json({
|
|
||||||
statusCode: HttpStatus.BAD_REQUEST,
|
|
||||||
message: error.message || 'Invalid UUID',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user