mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
finished config with 6 scene device
This commit is contained in:
@ -1,6 +0,0 @@
|
||||
export enum FourSceneSwitchesEnum {
|
||||
Scene_1 = 'scene_1',
|
||||
Scene_2 = 'scene_2',
|
||||
Scene_3 = 'scene_3',
|
||||
Scene_4 = 'scene_4',
|
||||
}
|
@ -16,4 +16,6 @@ export enum ProductType {
|
||||
GD = 'GD',
|
||||
CUR = 'CUR',
|
||||
PC = 'PC',
|
||||
FOUR_S = '4S',
|
||||
SIX_S = '6S',
|
||||
}
|
||||
|
8
libs/common/src/constants/scene-switch-type.enum.ts
Normal file
8
libs/common/src/constants/scene-switch-type.enum.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export enum SceneSwitchesTypeEnum {
|
||||
SCENE_1 = 'scene_1',
|
||||
SCENE_2 = 'scene_2',
|
||||
SCENE_3 = 'scene_3',
|
||||
SCENE_4 = 'scene_4',
|
||||
SCENE_5 = 'scene_5',
|
||||
SCENE_6 = 'scene_6',
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';
|
||||
import { SceneDeviceDto } from '../dtos';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { FourSceneSwitchesEnum } from '@app/common/constants/four-scene.enum';
|
||||
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||
import { DeviceEntity } from '../../device/entities';
|
||||
import { SceneEntity } from '../../scene/entities';
|
||||
|
||||
@ -28,9 +28,9 @@ export class SceneDeviceEntity extends AbstractEntity<SceneDeviceDto> {
|
||||
@Column({
|
||||
nullable: false,
|
||||
type: 'enum',
|
||||
enum: FourSceneSwitchesEnum,
|
||||
enum: SceneSwitchesTypeEnum,
|
||||
})
|
||||
switchName: FourSceneSwitchesEnum;
|
||||
switchName: SceneSwitchesTypeEnum;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
|
@ -31,6 +31,7 @@ import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { CheckDeviceGuard } from 'src/guards/device.guard';
|
||||
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import { CheckFourAndSixSceneDeviceTypeGuard } from 'src/guards/scene.device.type.guard';
|
||||
|
||||
@ApiTags('Device Module')
|
||||
@Controller({
|
||||
@ -211,13 +212,13 @@ export class DeviceController {
|
||||
);
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('four-scene/:deviceUuid')
|
||||
async addSceneToFourSceneDevice(
|
||||
@UseGuards(JwtAuthGuard, CheckFourAndSixSceneDeviceTypeGuard)
|
||||
@Post('scene-switch/:deviceUuid')
|
||||
async addSceneToSceneDevice(
|
||||
@Param('deviceUuid') deviceUuid: string,
|
||||
@Body() addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
||||
) {
|
||||
const device = await this.deviceService.addSceneToFourSceneDevice(
|
||||
const device = await this.deviceService.addSceneToSceneDevice(
|
||||
deviceUuid,
|
||||
addSceneToFourSceneDeviceDto,
|
||||
);
|
||||
@ -230,21 +231,21 @@ export class DeviceController {
|
||||
};
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('four-scene/switch/:deviceUuid')
|
||||
async getSceneFourSceneDevice(
|
||||
@UseGuards(JwtAuthGuard, CheckFourAndSixSceneDeviceTypeGuard)
|
||||
@Get('scene-switch/switch/:deviceUuid')
|
||||
async getSceneBySceneDevice(
|
||||
@Param('deviceUuid') deviceUuid: string,
|
||||
@Query() getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
|
||||
) {
|
||||
return await this.deviceService.getSceneFourSceneDevice(
|
||||
return await this.deviceService.getSceneBySceneDevice(
|
||||
deviceUuid,
|
||||
getSceneFourSceneDeviceDto,
|
||||
);
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('four-scene/:deviceUuid')
|
||||
async getScenesFourSceneDevice(@Param('deviceUuid') deviceUuid: string) {
|
||||
return await this.deviceService.getScenesFourSceneDevice(deviceUuid);
|
||||
@UseGuards(JwtAuthGuard, CheckFourAndSixSceneDeviceTypeGuard)
|
||||
@Get('scene-switch/:deviceUuid')
|
||||
async getScenesBySceneDevice(@Param('deviceUuid') deviceUuid: string) {
|
||||
return await this.deviceService.getScenesBySceneDevice(deviceUuid);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { FourSceneSwitchesEnum } from '@app/common/constants/four-scene.enum';
|
||||
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
@ -41,9 +41,9 @@ export class AddSceneToFourSceneDeviceDto {
|
||||
description: 'switchName',
|
||||
required: true,
|
||||
})
|
||||
@IsEnum(FourSceneSwitchesEnum)
|
||||
@IsEnum(SceneSwitchesTypeEnum)
|
||||
@IsNotEmpty()
|
||||
switchName: FourSceneSwitchesEnum;
|
||||
switchName: SceneSwitchesTypeEnum;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'sceneUuid',
|
||||
|
@ -53,7 +53,7 @@ import { SceneService } from 'src/scene/services';
|
||||
import { AddAutomationDto } from 'src/automation/dtos';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import { FourSceneSwitchesEnum } from '@app/common/constants/four-scene.enum';
|
||||
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||
|
||||
@Injectable()
|
||||
export class DeviceService {
|
||||
@ -1220,7 +1220,7 @@ export class DeviceService {
|
||||
|
||||
return descendants;
|
||||
}
|
||||
async addSceneToFourSceneDevice(
|
||||
async addSceneToSceneDevice(
|
||||
deviceUuid: string,
|
||||
addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
||||
) {
|
||||
@ -1319,7 +1319,8 @@ export class DeviceService {
|
||||
throw new HttpException(errorMessage, errorStatus);
|
||||
}
|
||||
}
|
||||
async getSceneFourSceneDevice(
|
||||
|
||||
async getSceneBySceneDevice(
|
||||
deviceUuid: string,
|
||||
getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
|
||||
) {
|
||||
@ -1328,7 +1329,7 @@ export class DeviceService {
|
||||
where: {
|
||||
device: { uuid: deviceUuid },
|
||||
switchName:
|
||||
getSceneFourSceneDeviceDto.switchName as FourSceneSwitchesEnum, // Cast the string to the enum
|
||||
getSceneFourSceneDeviceDto.switchName as SceneSwitchesTypeEnum, // Cast the string to the enum
|
||||
},
|
||||
relations: ['device', 'scene'],
|
||||
});
|
||||
@ -1347,10 +1348,13 @@ export class DeviceService {
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
throw new HttpException('Scene device not found', HttpStatus.NOT_FOUND);
|
||||
throw new HttpException(
|
||||
error.message || 'Scene device not found',
|
||||
error.status || HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getScenesFourSceneDevice(deviceUuid: string): Promise<any[]> {
|
||||
async getScenesBySceneDevice(deviceUuid: string): Promise<any[]> {
|
||||
try {
|
||||
const sceneDevices = await this.sceneDeviceRepository.find({
|
||||
where: { device: { uuid: deviceUuid } },
|
||||
|
42
src/guards/scene.device.type.guard.ts
Normal file
42
src/guards/scene.device.type.guard.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||
import {
|
||||
Injectable,
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
BadRequestException,
|
||||
HttpException,
|
||||
} from '@nestjs/common';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
|
||||
@Injectable()
|
||||
export class CheckFourAndSixSceneDeviceTypeGuard implements CanActivate {
|
||||
constructor(private readonly deviceService: DeviceService) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const deviceUuid = request.params.deviceUuid;
|
||||
|
||||
if (!deviceUuid) {
|
||||
throw new BadRequestException('Device UUID is required');
|
||||
}
|
||||
|
||||
try {
|
||||
const deviceDetails =
|
||||
await this.deviceService.getDeviceByDeviceUuid(deviceUuid);
|
||||
|
||||
if (
|
||||
deviceDetails.productDevice.prodType !== ProductType.FOUR_S &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.SIX_S
|
||||
) {
|
||||
throw new BadRequestException('The device type is not supported');
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'An error occurred',
|
||||
error.status || 500,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user