Add endpoint to delete scenes by device UUID and switch name

This commit is contained in:
faris Aljohari
2024-11-27 03:17:56 -06:00
parent f869f9a511
commit 4d2257a3e3
5 changed files with 45 additions and 14 deletions

View File

@ -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.';
};
};

View File

@ -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<BaseResponseDto> {
return await this.deviceService.deleteSceneToSceneDevice(param);
return await this.deviceService.deleteSceneFromSceneDevice(param, query);
}
}

View File

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

View File

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

View File

@ -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<BaseResponseDto> {
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) {