mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
Add endpoint to delete scenes by device UUID and switch name
This commit is contained in:
@ -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.';
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
12
src/device/dtos/delete.device.dto.ts
Normal file
12
src/device/dtos/delete.device.dto.ts
Normal 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;
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Reference in New Issue
Block a user