From d1cdc1375a0e99f9f86c8c9c95218112f4b168f2 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 17 Sep 2024 09:18:40 +0300 Subject: [PATCH] finished get status for batch devices endpoint --- src/device/controllers/device.controller.ts | 16 ++++++++++++++ src/device/dtos/control.device.dto.ts | 7 +++++++ src/device/services/device.service.ts | 23 +++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/device/controllers/device.controller.ts b/src/device/controllers/device.controller.ts index 8e7e6d6..6ded947 100644 --- a/src/device/controllers/device.controller.ts +++ b/src/device/controllers/device.controller.ts @@ -21,6 +21,7 @@ import { import { ControlDeviceDto, BatchControlDevicesDto, + BatchStatusDevicesDto, } from '../dtos/control.device.dto'; import { CheckRoomGuard } from 'src/guards/room.guard'; import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard'; @@ -275,4 +276,19 @@ export class DeviceController { ); } } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get('status/batch') + async batchStatusDevices( + @Query() batchStatusDevicesDto: BatchStatusDevicesDto, + ) { + try { + return await this.deviceService.batchStatusDevices(batchStatusDevicesDto); + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } } diff --git a/src/device/dtos/control.device.dto.ts b/src/device/dtos/control.device.dto.ts index 9bb1d40..7634fd0 100644 --- a/src/device/dtos/control.device.dto.ts +++ b/src/device/dtos/control.device.dto.ts @@ -38,3 +38,10 @@ export class BatchControlDevicesDto { @IsNotEmpty() public value: any; } +export class BatchStatusDevicesDto { + @ApiProperty({ + example: 'uuid1,uuid2,uuid3', + description: 'Comma-separated list of device UUIDs', + }) + devicesUuid: string; +} diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index 93433a3..c856143 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -24,6 +24,7 @@ import { } from '../dtos/get.device.dto'; import { BatchControlDevicesDto, + BatchStatusDevicesDto, ControlDeviceDto, } from '../dtos/control.device.dto'; import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter'; @@ -362,7 +363,29 @@ export class DeviceService { ); } } + async batchStatusDevices(batchStatusDevicesDto: BatchStatusDevicesDto) { + const { devicesUuid } = batchStatusDevicesDto; + const devicesUuidArray = devicesUuid.split(','); + try { + await this.checkAllDevicesHaveSameProductUuid(devicesUuidArray); + const statuses = await Promise.all( + devicesUuidArray.map(async (deviceUuid) => { + const result = await this.getDevicesInstructionStatus(deviceUuid); + return { deviceUuid, result }; + }), + ); + return { + status: statuses[0].result, + devices: statuses, + }; + } catch (error) { + throw new HttpException( + error.message || 'Device Not Found', + error.status || HttpStatus.NOT_FOUND, + ); + } + } async checkAllDevicesHaveSameProductUuid(deviceUuids: string[]) { const firstDevice = await this.deviceRepository.findOne({ where: { uuid: deviceUuids[0] },