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

View File

@ -10,6 +10,7 @@ import {
UseGuards,
Req,
Put,
Delete,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import {
@ -32,6 +33,8 @@ 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';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { DeviceSceneParamDto } from '../dtos/device.param.dto';
@ApiTags('Device Module')
@Controller({
@ -242,4 +245,12 @@ export class DeviceController {
getSceneFourSceneDeviceDto,
);
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Delete(':switchSceneUuid/scenes')
async deleteSceneToSceneDevice(
@Param() param: DeviceSceneParamDto,
): Promise<BaseResponseDto> {
return await this.deviceService.deleteSceneToSceneDevice(param);
}
}

View 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;
}

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,
);
}
}
}