diff --git a/src/device/controllers/device.controller.ts b/src/device/controllers/device.controller.ts index cf977d2..564b9fc 100644 --- a/src/device/controllers/device.controller.ts +++ b/src/device/controllers/device.controller.ts @@ -231,7 +231,7 @@ export class DeviceController { } @ApiBearerAuth() @UseGuards(JwtAuthGuard) - @Get('four-scene/:deviceUuid') + @Get('four-scene/switch/:deviceUuid') async getSceneFourSceneDevice( @Param('deviceUuid') deviceUuid: string, @Query() getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto, @@ -241,4 +241,10 @@ export class DeviceController { getSceneFourSceneDeviceDto, ); } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get('four-scene/:deviceUuid') + async getScenesFourSceneDevice(@Param('deviceUuid') deviceUuid: string) { + return await this.deviceService.getScenesFourSceneDevice(deviceUuid); + } } diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index 15276d9..950da44 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -1350,4 +1350,51 @@ export class DeviceService { throw new HttpException('Scene device not found', HttpStatus.NOT_FOUND); } } + async getScenesFourSceneDevice(deviceUuid: string): Promise { + try { + const sceneDevices = await this.sceneDeviceRepository.find({ + where: { device: { uuid: deviceUuid } }, + relations: ['device', 'scene'], + }); + + if (!sceneDevices.length) { + throw new HttpException( + 'No scenes found for the device', + HttpStatus.NOT_FOUND, + ); + } + + const results = await Promise.all( + sceneDevices.map(async (sceneDevice) => { + if (!sceneDevice.scene?.uuid) return null; + + try { + const sceneDetails = await this.sceneService.getSceneByUuid( + sceneDevice.scene.uuid, + ); + + return { + switchName: sceneDevice.switchName, + createdAt: sceneDevice.createdAt, + updatedAt: sceneDevice.updatedAt, + deviceUuid: sceneDevice.device.uuid, + scene: sceneDetails.data, + }; + } catch (error) { + throw new HttpException( + 'Failed to fetch scene details', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + }), + ); + + return results.filter(Boolean); + } catch (error) { + throw new HttpException( + error.message || 'Failed to fetch scenes for device', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } }