Merge pull request #147 from SyncrowIOT/implemnt-get-all-switches-four-scene-api

finished get all switches
This commit is contained in:
faris Aljohari
2024-11-18 00:24:38 -06:00
committed by GitHub
2 changed files with 54 additions and 1 deletions

View File

@ -231,7 +231,7 @@ export class DeviceController {
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('four-scene/:deviceUuid') @Get('four-scene/switch/:deviceUuid')
async getSceneFourSceneDevice( async getSceneFourSceneDevice(
@Param('deviceUuid') deviceUuid: string, @Param('deviceUuid') deviceUuid: string,
@Query() getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto, @Query() getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
@ -241,4 +241,10 @@ export class DeviceController {
getSceneFourSceneDeviceDto, getSceneFourSceneDeviceDto,
); );
} }
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get('four-scene/:deviceUuid')
async getScenesFourSceneDevice(@Param('deviceUuid') deviceUuid: string) {
return await this.deviceService.getScenesFourSceneDevice(deviceUuid);
}
} }

View File

@ -1350,4 +1350,51 @@ export class DeviceService {
throw new HttpException('Scene device not found', HttpStatus.NOT_FOUND); throw new HttpException('Scene device not found', HttpStatus.NOT_FOUND);
} }
} }
async getScenesFourSceneDevice(deviceUuid: string): Promise<any[]> {
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,
);
}
}
} }