mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
Merge pull request #45 from SyncrowIOT/SP-217-device-grouping
Sp 217 device grouping
This commit is contained in:
@ -11,8 +11,6 @@ import { PermissionTypeEntity } from '../modules/permission/entities';
|
||||
import { SpaceEntity } from '../modules/space/entities';
|
||||
import { SpaceTypeEntity } from '../modules/space-type/entities';
|
||||
import { UserSpaceEntity } from '../modules/user-space/entities';
|
||||
import { GroupEntity } from '../modules/group/entities';
|
||||
import { GroupDeviceEntity } from '../modules/group-device/entities';
|
||||
import { DeviceUserPermissionEntity } from '../modules/device-user-permission/entities';
|
||||
import { UserRoleEntity } from '../modules/user-role/entities';
|
||||
import { RoleTypeEntity } from '../modules/role-type/entities';
|
||||
@ -43,8 +41,6 @@ import { DeviceNotificationEntity } from '../modules/device-notification/entitie
|
||||
SpaceEntity,
|
||||
SpaceTypeEntity,
|
||||
UserSpaceEntity,
|
||||
GroupEntity,
|
||||
GroupDeviceEntity,
|
||||
DeviceUserPermissionEntity,
|
||||
UserRoleEntity,
|
||||
RoleTypeEntity,
|
||||
|
@ -1,7 +1,6 @@
|
||||
import { Column, Entity, ManyToOne, OneToMany, Unique } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { DeviceDto } from '../dtos/device.dto';
|
||||
import { GroupDeviceEntity } from '../../group-device/entities';
|
||||
import { SpaceEntity } from '../../space/entities';
|
||||
import { ProductEntity } from '../../product/entities';
|
||||
import { DeviceUserPermissionEntity } from '../../device-user-permission/entities';
|
||||
@ -41,11 +40,6 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
||||
},
|
||||
)
|
||||
deviceUserNotification: DeviceNotificationEntity[];
|
||||
@OneToMany(
|
||||
() => GroupDeviceEntity,
|
||||
(userGroupDevices) => userGroupDevices.device,
|
||||
)
|
||||
userGroupDevices: GroupDeviceEntity[];
|
||||
|
||||
@ManyToOne(() => SpaceEntity, (space) => space.devicesSpaceEntity, {
|
||||
nullable: true,
|
||||
|
@ -1,15 +0,0 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class GroupDeviceDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public deviceUuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public groupUuid: string;
|
||||
}
|
@ -1 +0,0 @@
|
||||
export * from './group.device.dto';
|
@ -1,48 +0,0 @@
|
||||
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
|
||||
import { GroupDeviceDto } from '../dtos';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { DeviceEntity } from '../../device/entities';
|
||||
import { GroupEntity } from '../../group/entities';
|
||||
|
||||
@Entity({ name: 'group-device' })
|
||||
@Unique(['device', 'group'])
|
||||
export class GroupDeviceEntity extends AbstractEntity<GroupDeviceDto> {
|
||||
@Column({
|
||||
type: 'uuid',
|
||||
default: () => 'gen_random_uuid()', // Use gen_random_uuid() for default value
|
||||
nullable: false,
|
||||
})
|
||||
public uuid: string;
|
||||
|
||||
@Column({
|
||||
type: 'string',
|
||||
nullable: false,
|
||||
})
|
||||
deviceUuid: string;
|
||||
|
||||
@Column({
|
||||
type: 'string',
|
||||
nullable: false,
|
||||
})
|
||||
groupUuid: string;
|
||||
|
||||
@ManyToOne(() => DeviceEntity, (device) => device.userGroupDevices, {
|
||||
nullable: false,
|
||||
})
|
||||
device: DeviceEntity;
|
||||
|
||||
@ManyToOne(() => GroupEntity, (group) => group.groupDevices, {
|
||||
nullable: false,
|
||||
})
|
||||
group: GroupEntity;
|
||||
|
||||
@Column({
|
||||
nullable: true,
|
||||
default: true,
|
||||
})
|
||||
public isActive: boolean;
|
||||
constructor(partial: Partial<GroupDeviceEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
export * from './group.device.entity';
|
@ -1,11 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { GroupDeviceEntity } from './entities/group.device.entity';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [TypeOrmModule.forFeature([GroupDeviceEntity])],
|
||||
})
|
||||
export class GroupDeviceRepositoryModule {}
|
@ -1,10 +0,0 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { GroupDeviceEntity } from '../entities/group.device.entity';
|
||||
|
||||
@Injectable()
|
||||
export class GroupDeviceRepository extends Repository<GroupDeviceEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(GroupDeviceEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
export * from './group.device.repository';
|
@ -1,11 +0,0 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class GroupDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public groupName: string;
|
||||
}
|
@ -1 +0,0 @@
|
||||
export * from './group.dto';
|
@ -1,34 +0,0 @@
|
||||
import { Column, Entity, OneToMany } from 'typeorm';
|
||||
import { GroupDto } from '../dtos';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { GroupDeviceEntity } from '../../group-device/entities';
|
||||
|
||||
@Entity({ name: 'group' })
|
||||
export class GroupEntity extends AbstractEntity<GroupDto> {
|
||||
@Column({
|
||||
type: 'uuid',
|
||||
default: () => 'gen_random_uuid()', // Use gen_random_uuid() for default value
|
||||
nullable: false,
|
||||
})
|
||||
public uuid: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
public groupName: string;
|
||||
|
||||
@OneToMany(() => GroupDeviceEntity, (groupDevice) => groupDevice.group, {
|
||||
cascade: true,
|
||||
})
|
||||
groupDevices: GroupDeviceEntity[];
|
||||
|
||||
@Column({
|
||||
nullable: true,
|
||||
default: true,
|
||||
})
|
||||
public isActive: boolean;
|
||||
constructor(partial: Partial<GroupEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
export * from './group.entity';
|
@ -1,11 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { GroupEntity } from './entities/group.entity';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [TypeOrmModule.forFeature([GroupEntity])],
|
||||
})
|
||||
export class GroupRepositoryModule {}
|
@ -1,10 +0,0 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { GroupEntity } from '../entities/group.entity';
|
||||
|
||||
@Injectable()
|
||||
export class GroupRepository extends Repository<GroupEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(GroupEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
export * from './group.repository';
|
@ -13,18 +13,10 @@ import {
|
||||
Put,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import {
|
||||
AddDeviceDto,
|
||||
AddDeviceInGroupDto,
|
||||
UpdateDeviceInRoomDto,
|
||||
} from '../dtos/add.device.dto';
|
||||
import {
|
||||
GetDeviceByGroupIdDto,
|
||||
GetDeviceByRoomUuidDto,
|
||||
} from '../dtos/get.device.dto';
|
||||
import { AddDeviceDto, UpdateDeviceInRoomDto } from '../dtos/add.device.dto';
|
||||
import { GetDeviceByRoomUuidDto } from '../dtos/get.device.dto';
|
||||
import { ControlDeviceDto } from '../dtos/control.device.dto';
|
||||
import { CheckRoomGuard } from 'src/guards/room.guard';
|
||||
import { CheckGroupGuard } from 'src/guards/group.guard';
|
||||
import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard';
|
||||
import { CheckUserHaveControllablePermission } from 'src/guards/user.device.controllable.permission.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()
|
||||
@UseGuards(JwtAuthGuard, CheckUserHavePermission)
|
||||
@Get(':deviceUuid')
|
||||
|
@ -8,18 +8,10 @@ import { DeviceRepositoryModule } from '@app/common/modules/device';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { PermissionTypeRepository } from '@app/common/modules/permission/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 { UserRepository } from '@app/common/modules/user/repositories';
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule,
|
||||
ProductRepositoryModule,
|
||||
DeviceRepositoryModule,
|
||||
GroupRepositoryModule,
|
||||
],
|
||||
imports: [ConfigModule, ProductRepositoryModule, DeviceRepositoryModule],
|
||||
controllers: [DeviceController],
|
||||
providers: [
|
||||
DeviceService,
|
||||
@ -28,8 +20,6 @@ import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
PermissionTypeRepository,
|
||||
SpaceRepository,
|
||||
DeviceRepository,
|
||||
GroupDeviceRepository,
|
||||
GroupRepository,
|
||||
UserRepository,
|
||||
],
|
||||
exports: [DeviceService],
|
||||
|
@ -35,20 +35,3 @@ export class UpdateDeviceInRoomDto {
|
||||
@IsNotEmpty()
|
||||
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()
|
||||
public roomUuid: string;
|
||||
}
|
||||
export class GetDeviceByGroupIdDto {
|
||||
@ApiProperty({
|
||||
description: 'groupUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public groupUuid: string;
|
||||
}
|
||||
|
@ -7,11 +7,7 @@ import {
|
||||
} from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import {
|
||||
AddDeviceDto,
|
||||
AddDeviceInGroupDto,
|
||||
UpdateDeviceInRoomDto,
|
||||
} from '../dtos/add.device.dto';
|
||||
import { AddDeviceDto, UpdateDeviceInRoomDto } from '../dtos/add.device.dto';
|
||||
import {
|
||||
DeviceInstructionResponse,
|
||||
GetDeviceDetailsFunctionsInterface,
|
||||
@ -19,14 +15,10 @@ import {
|
||||
GetDeviceDetailsInterface,
|
||||
controlDeviceInterface,
|
||||
} from '../interfaces/get.device.interface';
|
||||
import {
|
||||
GetDeviceByGroupIdDto,
|
||||
GetDeviceByRoomUuidDto,
|
||||
} from '../dtos/get.device.dto';
|
||||
import { GetDeviceByRoomUuidDto } from '../dtos/get.device.dto';
|
||||
import { ControlDeviceDto } from '../dtos/control.device.dto';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
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 { In } from 'typeorm';
|
||||
|
||||
@ -36,7 +28,6 @@ export class DeviceService {
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
private readonly groupDeviceRepository: GroupDeviceRepository,
|
||||
private readonly productRepository: ProductRepository,
|
||||
) {
|
||||
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) {
|
||||
try {
|
||||
|
@ -1,22 +1,16 @@
|
||||
import { GroupService } from '../services/group.service';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
UseGuards,
|
||||
Param,
|
||||
Put,
|
||||
Delete,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Req,
|
||||
} from '@nestjs/common';
|
||||
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 { UnitPermissionGuard } from 'src/guards/unit.permission.guard';
|
||||
|
||||
@ApiTags('Group Module')
|
||||
@Controller({
|
||||
@ -27,11 +21,11 @@ export class GroupController {
|
||||
constructor(private readonly groupService: GroupService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('space/:spaceUuid')
|
||||
async getGroupsBySpaceUuid(@Param('spaceUuid') spaceUuid: string) {
|
||||
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
||||
@Get(':unitUuid')
|
||||
async getGroupsBySpaceUuid(@Param('unitUuid') unitUuid: string) {
|
||||
try {
|
||||
return await this.groupService.getGroupsBySpaceUuid(spaceUuid);
|
||||
return await this.groupService.getGroupsByUnitUuid(unitUuid);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
@ -40,72 +34,21 @@ export class GroupController {
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':groupUuid')
|
||||
async getGroupsByGroupId(@Param('groupUuid') groupUuid: string) {
|
||||
try {
|
||||
return await this.groupService.getGroupsByGroupUuid(groupUuid);
|
||||
} catch (error) {
|
||||
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,
|
||||
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
||||
@Get(':unitUuid/devices/:groupName')
|
||||
async getUnitDevicesByGroupName(
|
||||
@Param('unitUuid') unitUuid: string,
|
||||
@Param('groupName') groupName: string,
|
||||
@Req() req: any,
|
||||
) {
|
||||
try {
|
||||
return await this.groupService.renameGroupByUuid(
|
||||
groupUuid,
|
||||
renameGroupDto,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
const userUuid = req.user.uuid;
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Delete(':groupUuid')
|
||||
async deleteGroup(@Param('groupUuid') groupUuid: string) {
|
||||
try {
|
||||
return await this.groupService.deleteGroup(groupUuid);
|
||||
return await this.groupService.getUnitDevicesByGroupName(
|
||||
unitUuid,
|
||||
groupName,
|
||||
userUuid,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
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 { GroupService } from './services/group.service';
|
||||
import { GroupController } from './controllers/group.controller';
|
||||
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 { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule,
|
||||
GroupRepositoryModule,
|
||||
GroupDeviceRepositoryModule,
|
||||
DeviceRepositoryModule,
|
||||
],
|
||||
imports: [ConfigModule, DeviceRepositoryModule],
|
||||
controllers: [GroupController],
|
||||
providers: [
|
||||
GroupService,
|
||||
GroupRepository,
|
||||
GroupDeviceRepository,
|
||||
DeviceRepository,
|
||||
SpaceRepository,
|
||||
DeviceRepository,
|
||||
ProductRepository,
|
||||
],
|
||||
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 {
|
||||
Injectable,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AddGroupDto } from '../dtos/add.group.dto';
|
||||
import {
|
||||
GetGroupDetailsInterface,
|
||||
GetGroupsBySpaceUuidInterface,
|
||||
} 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 { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { GetDeviceDetailsInterface } from 'src/device/interfaces/get.device.interface';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
||||
import { In } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class GroupService {
|
||||
private tuya: TuyaContext;
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly groupRepository: GroupRepository,
|
||||
private readonly groupDeviceRepository: GroupDeviceRepository,
|
||||
private readonly productRepository: ProductRepository,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
) {
|
||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_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({
|
||||
baseUrl: 'https://openapi.tuyaeu.com',
|
||||
accessKey,
|
||||
@ -35,230 +25,126 @@ export class GroupService {
|
||||
});
|
||||
}
|
||||
|
||||
async getGroupsBySpaceUuid(
|
||||
spaceUuid: string,
|
||||
): Promise<GetGroupsBySpaceUuidInterface[]> {
|
||||
async getGroupsByUnitUuid(unitUuid: string) {
|
||||
try {
|
||||
const groupDevices = await this.groupDeviceRepository.find({
|
||||
relations: ['group', 'device'],
|
||||
const spaces = await this.spaceRepository.find({
|
||||
where: {
|
||||
device: { spaceDevice: { uuid: spaceUuid } },
|
||||
isActive: true,
|
||||
},
|
||||
});
|
||||
|
||||
// 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,
|
||||
parent: {
|
||||
uuid: unitUuid,
|
||||
},
|
||||
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) {
|
||||
return response;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
response.msg || 'Unknown error',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
const groupNames = spaces.flatMap((space) => {
|
||||
return space.devicesSpaceEntity.map(
|
||||
(device) => device.productDevice.prodType,
|
||||
);
|
||||
}
|
||||
} 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) {
|
||||
throw new HttpException(
|
||||
'Error control device from Tuya',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
'This unit does not have any groups',
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
}
|
||||
async controlGroup(controlGroupDto: ControlGroupDto) {
|
||||
const devices = await this.getDevicesByGroupUuid(controlGroupDto.groupUuid);
|
||||
|
||||
async getUnitDevicesByGroupName(
|
||||
unitUuid: string,
|
||||
groupName: string,
|
||||
userUuid: string,
|
||||
) {
|
||||
try {
|
||||
await Promise.all(
|
||||
devices.map(async (device) => {
|
||||
return this.controlDevice(
|
||||
device.device.deviceTuyaUuid,
|
||||
controlGroupDto.code,
|
||||
controlGroupDto.value,
|
||||
const spaces = await this.spaceRepository.find({
|
||||
where: {
|
||||
parent: {
|
||||
uuid: unitUuid,
|
||||
},
|
||||
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) {
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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