mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 10:46:17 +00:00
finished delete switch scene device
This commit is contained in:
@ -1,8 +1,9 @@
|
|||||||
import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
|
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
|
||||||
import { SceneDto, SceneIconDto } from '../dtos';
|
import { SceneDto, SceneIconDto } from '../dtos';
|
||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
import { SceneIconType } from '@app/common/constants/secne-icon-type.enum';
|
import { SceneIconType } from '@app/common/constants/secne-icon-type.enum';
|
||||||
import { SceneDeviceEntity } from '../../scene-device/entities';
|
import { SceneDeviceEntity } from '../../scene-device/entities';
|
||||||
|
import { SpaceEntity } from '../../space/entities';
|
||||||
|
|
||||||
// Define SceneIconEntity before SceneEntity
|
// Define SceneIconEntity before SceneEntity
|
||||||
@Entity({ name: 'scene-icon' })
|
@Entity({ name: 'scene-icon' })
|
||||||
@ -45,16 +46,19 @@ export class SceneEntity extends AbstractEntity<SceneDto> {
|
|||||||
nullable: false,
|
nullable: false,
|
||||||
})
|
})
|
||||||
sceneTuyaUuid: string;
|
sceneTuyaUuid: string;
|
||||||
@Column({
|
|
||||||
nullable: false,
|
|
||||||
})
|
|
||||||
spaceUuid: string;
|
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
})
|
})
|
||||||
showInHomePage: boolean;
|
showInHomePage: boolean;
|
||||||
|
|
||||||
|
@ManyToOne(() => SpaceEntity, (space) => space.scenes, {
|
||||||
|
nullable: false,
|
||||||
|
onDelete: 'CASCADE',
|
||||||
|
})
|
||||||
|
@JoinColumn({ name: 'space_uuid' })
|
||||||
|
space: SpaceEntity;
|
||||||
|
|
||||||
@ManyToOne(() => SceneIconEntity, (icon) => icon.scenesIconEntity, {
|
@ManyToOne(() => SceneIconEntity, (icon) => icon.scenesIconEntity, {
|
||||||
nullable: false,
|
nullable: false,
|
||||||
})
|
})
|
||||||
|
@ -14,6 +14,7 @@ import { CommunityEntity } from '../../community/entities';
|
|||||||
import { SubspaceEntity } from './subspace.entity';
|
import { SubspaceEntity } from './subspace.entity';
|
||||||
import { SpaceLinkEntity } from './space-link.entity';
|
import { SpaceLinkEntity } from './space-link.entity';
|
||||||
import { SpaceProductEntity } from './space-product.entity';
|
import { SpaceProductEntity } from './space-product.entity';
|
||||||
|
import { SceneEntity } from '../../scene/entities';
|
||||||
|
|
||||||
@Entity({ name: 'space' })
|
@Entity({ name: 'space' })
|
||||||
@Unique(['invitationCode'])
|
@Unique(['invitationCode'])
|
||||||
@ -94,6 +95,9 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
|||||||
@OneToMany(() => SpaceProductEntity, (spaceProduct) => spaceProduct.space)
|
@OneToMany(() => SpaceProductEntity, (spaceProduct) => spaceProduct.space)
|
||||||
spaceProducts: SpaceProductEntity[];
|
spaceProducts: SpaceProductEntity[];
|
||||||
|
|
||||||
|
@OneToMany(() => SceneEntity, (scene) => scene.space)
|
||||||
|
scenes: SceneEntity[];
|
||||||
|
|
||||||
constructor(partial: Partial<SpaceEntity>) {
|
constructor(partial: Partial<SpaceEntity>) {
|
||||||
super();
|
super();
|
||||||
Object.assign(this, partial);
|
Object.assign(this, partial);
|
||||||
|
@ -10,6 +10,7 @@ import {
|
|||||||
UseGuards,
|
UseGuards,
|
||||||
Req,
|
Req,
|
||||||
Put,
|
Put,
|
||||||
|
Delete,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import {
|
import {
|
||||||
@ -32,6 +33,8 @@ import { CheckDeviceGuard } from 'src/guards/device.guard';
|
|||||||
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
|
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
|
||||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||||
import { CheckFourAndSixSceneDeviceTypeGuard } from 'src/guards/scene.device.type.guard';
|
import { CheckFourAndSixSceneDeviceTypeGuard } from 'src/guards/scene.device.type.guard';
|
||||||
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
|
import { DeviceSceneParamDto } from '../dtos/device.param.dto';
|
||||||
|
|
||||||
@ApiTags('Device Module')
|
@ApiTags('Device Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -242,4 +245,12 @@ export class DeviceController {
|
|||||||
getSceneFourSceneDeviceDto,
|
getSceneFourSceneDeviceDto,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Delete(':switchSceneUuid/scenes')
|
||||||
|
async deleteSceneToSceneDevice(
|
||||||
|
@Param() param: DeviceSceneParamDto,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
return await this.deviceService.deleteSceneToSceneDevice(param);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
11
src/device/dtos/device.param.dto.ts
Normal file
11
src/device/dtos/device.param.dto.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsUUID } from 'class-validator';
|
||||||
|
|
||||||
|
export class DeviceSceneParamDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'UUID of the Switch Scene',
|
||||||
|
example: 'b3421478-3bec-4634-9805-a53950260ecb',
|
||||||
|
})
|
||||||
|
@IsUUID()
|
||||||
|
switchSceneUuid: string;
|
||||||
|
}
|
@ -55,6 +55,9 @@ import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service
|
|||||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||||
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||||
import { AUTOMATION_CONFIG } from '@app/common/constants/automation.enum';
|
import { AUTOMATION_CONFIG } from '@app/common/constants/automation.enum';
|
||||||
|
import { DeviceSceneParamDto } from '../dtos/device.param.dto';
|
||||||
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DeviceService {
|
export class DeviceService {
|
||||||
@ -1347,6 +1350,7 @@ export class DeviceService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
switchSceneUuid: sceneDevice.uuid,
|
||||||
switchName: sceneDevice.switchName,
|
switchName: sceneDevice.switchName,
|
||||||
createdAt: sceneDevice.createdAt,
|
createdAt: sceneDevice.createdAt,
|
||||||
updatedAt: sceneDevice.updatedAt,
|
updatedAt: sceneDevice.updatedAt,
|
||||||
@ -1375,6 +1379,7 @@ export class DeviceService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
switchSceneUuid: sceneDevice.uuid,
|
||||||
switchName: sceneDevice.switchName,
|
switchName: sceneDevice.switchName,
|
||||||
createdAt: sceneDevice.createdAt,
|
createdAt: sceneDevice.createdAt,
|
||||||
updatedAt: sceneDevice.updatedAt,
|
updatedAt: sceneDevice.updatedAt,
|
||||||
@ -1392,4 +1397,59 @@ export class DeviceService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async deleteSceneToSceneDevice(
|
||||||
|
params: DeviceSceneParamDto,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
const { switchSceneUuid } = params;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const existingSceneDevice = await this.sceneDeviceRepository.findOne({
|
||||||
|
where: { uuid: switchSceneUuid },
|
||||||
|
relations: ['scene.space.community'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!existingSceneDevice) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Switch Scene not found for ID ${switchSceneUuid}`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteResult = await this.sceneDeviceRepository.delete({
|
||||||
|
uuid: switchSceneUuid,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (deleteResult.affected === 0) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Failed to delete Switch Scene with ID ${switchSceneUuid}`,
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const tuyaAutomationResult = await this.tuyaService.deleteAutomation(
|
||||||
|
existingSceneDevice.scene.space.community.externalId,
|
||||||
|
existingSceneDevice.automationTuyaUuid,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!tuyaAutomationResult.success) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Failed to delete Tuya automation for Switch Scene with ID ${switchSceneUuid}`,
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
message: `Switch Scene with ID ${switchSceneUuid} deleted successfully`,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof HttpException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || `An unexpected error occurred`,
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user