From 4d2257a3e30ab2aae796f79a723ef1918d91e21a Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Wed, 27 Nov 2024 03:17:56 -0600 Subject: [PATCH] Add endpoint to delete scenes by device UUID and switch name --- libs/common/src/constants/controller-route.ts | 4 ++++ src/device/controllers/device.controller.ts | 14 ++++++++--- src/device/dtos/delete.device.dto.ts | 12 ++++++++++ src/device/dtos/device.param.dto.ts | 6 ++--- src/device/services/device.service.ts | 23 ++++++++++++------- 5 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 src/device/dtos/delete.device.dto.ts diff --git a/libs/common/src/constants/controller-route.ts b/libs/common/src/constants/controller-route.ts index 0f13f5c..eeacd0f 100644 --- a/libs/common/src/constants/controller-route.ts +++ b/libs/common/src/constants/controller-route.ts @@ -440,6 +440,10 @@ export class ControllerRoute { 'Get scenes by device (4 Scene and 6 Scene devices only)'; public static readonly GET_SCENES_BY_DEVICE_DESCRIPTION = 'This endpoint retrieves all scenes associated with a specific switch device.'; + public static readonly DELETE_SCENES_BY_SWITCH_NAME_SUMMARY = + 'Delete scenes by device uuid and switch name (4 Scene and 6 Scene devices only)'; + public static readonly DELETE_SCENES_BY_SWITCH_NAME_DESCRIPTION = + 'This endpoint deletes all scenes associated with a specific switch device.'; }; }; diff --git a/src/device/controllers/device.controller.ts b/src/device/controllers/device.controller.ts index 5db48cb..a49b1f7 100644 --- a/src/device/controllers/device.controller.ts +++ b/src/device/controllers/device.controller.ts @@ -36,6 +36,7 @@ import { CheckFourAndSixSceneDeviceTypeGuard } from 'src/guards/scene.device.typ import { ControllerRoute } from '@app/common/constants/controller-route'; import { BaseResponseDto } from '@app/common/dto/base.response.dto'; import { DeviceSceneParamDto } from '../dtos/device.param.dto'; +import { DeleteSceneFromSceneDeviceDto } from '../dtos/delete.device.dto'; @ApiTags('Device Module') @Controller({ @@ -334,10 +335,17 @@ export class DeviceController { } @ApiBearerAuth() @UseGuards(JwtAuthGuard) - @Delete(':switchSceneUuid/scenes') - async deleteSceneToSceneDevice( + @Delete(':deviceUuid/scenes') + @ApiOperation({ + summary: + ControllerRoute.DEVICE.ACTIONS.DELETE_SCENES_BY_SWITCH_NAME_SUMMARY, + description: + ControllerRoute.DEVICE.ACTIONS.DELETE_SCENES_BY_SWITCH_NAME_DESCRIPTION, + }) + async deleteSceneFromSceneDevice( @Param() param: DeviceSceneParamDto, + @Query() query: DeleteSceneFromSceneDeviceDto, ): Promise { - return await this.deviceService.deleteSceneToSceneDevice(param); + return await this.deviceService.deleteSceneFromSceneDevice(param, query); } } diff --git a/src/device/dtos/delete.device.dto.ts b/src/device/dtos/delete.device.dto.ts new file mode 100644 index 0000000..2e60435 --- /dev/null +++ b/src/device/dtos/delete.device.dto.ts @@ -0,0 +1,12 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString } from 'class-validator'; + +export class DeleteSceneFromSceneDeviceDto { + @ApiProperty({ + description: 'switchName', + required: true, + }) + @IsString() + @IsNotEmpty() + public switchName: string; +} diff --git a/src/device/dtos/device.param.dto.ts b/src/device/dtos/device.param.dto.ts index 264b656..0a0eb6c 100644 --- a/src/device/dtos/device.param.dto.ts +++ b/src/device/dtos/device.param.dto.ts @@ -3,9 +3,9 @@ import { IsUUID } from 'class-validator'; export class DeviceSceneParamDto { @ApiProperty({ - description: 'UUID of the Switch Scene', - example: 'b3421478-3bec-4634-9805-a53950260ecb', + description: 'UUID of the device', + example: 'b3a37332-9c03-4ce2-ac94-bea75382b365', }) @IsUUID() - switchSceneUuid: string; + deviceUuid: string; } diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index 521693b..3ed3327 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -58,6 +58,7 @@ 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'; +import { DeleteSceneFromSceneDeviceDto } from '../dtos/delete.device.dto'; @Injectable() export class DeviceService { @@ -1406,31 +1407,37 @@ export class DeviceService { ); } } - async deleteSceneToSceneDevice( + async deleteSceneFromSceneDevice( params: DeviceSceneParamDto, + query: DeleteSceneFromSceneDeviceDto, ): Promise { - const { switchSceneUuid } = params; + const { deviceUuid } = params; + const { switchName } = query; try { const existingSceneDevice = await this.sceneDeviceRepository.findOne({ - where: { uuid: switchSceneUuid }, + where: { + device: { uuid: deviceUuid }, + switchName: switchName as SceneSwitchesTypeEnum, + }, relations: ['scene.space.community'], }); if (!existingSceneDevice) { throw new HttpException( - `Switch Scene not found for ID ${switchSceneUuid}`, + `No scene found for device with UUID ${deviceUuid} and switch name ${switchName}`, HttpStatus.NOT_FOUND, ); } const deleteResult = await this.sceneDeviceRepository.delete({ - uuid: switchSceneUuid, + device: { uuid: deviceUuid }, + switchName: switchName as SceneSwitchesTypeEnum, }); if (deleteResult.affected === 0) { throw new HttpException( - `Failed to delete Switch Scene with ID ${switchSceneUuid}`, + `Failed to delete Switch Scene with device ID ${deviceUuid} and switch name ${switchName}`, HttpStatus.INTERNAL_SERVER_ERROR, ); } @@ -1442,13 +1449,13 @@ export class DeviceService { if (!tuyaAutomationResult.success) { throw new HttpException( - `Failed to delete Tuya automation for Switch Scene with ID ${switchSceneUuid}`, + `Failed to delete Tuya automation for Switch Scene with ID ${existingSceneDevice.automationTuyaUuid}`, HttpStatus.INTERNAL_SERVER_ERROR, ); } return new SuccessResponseDto({ - message: `Switch Scene with ID ${switchSceneUuid} deleted successfully`, + message: `Switch Scene with device ID ${deviceUuid} and switch name ${switchName} deleted successfully`, }); } catch (error) { if (error instanceof HttpException) {