finished delete switch scene device

This commit is contained in:
faris Aljohari
2024-11-23 20:50:34 -06:00
parent 67ed5cdca1
commit 7b3c929aa6
5 changed files with 95 additions and 5 deletions
+60
View File
@@ -55,6 +55,9 @@ import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.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()
export class DeviceService {
@@ -1347,6 +1350,7 @@ export class DeviceService {
);
return {
switchSceneUuid: sceneDevice.uuid,
switchName: sceneDevice.switchName,
createdAt: sceneDevice.createdAt,
updatedAt: sceneDevice.updatedAt,
@@ -1375,6 +1379,7 @@ export class DeviceService {
);
return {
switchSceneUuid: sceneDevice.uuid,
switchName: sceneDevice.switchName,
createdAt: sceneDevice.createdAt,
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,
);
}
}
}