diff --git a/src/device/controllers/device.controller.ts b/src/device/controllers/device.controller.ts index a54b5a9..6e834eb 100644 --- a/src/device/controllers/device.controller.ts +++ b/src/device/controllers/device.controller.ts @@ -223,4 +223,17 @@ export class DeviceController { ); } } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get() + async getAllDevices() { + try { + return await this.deviceService.getAllDevices(); + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } } diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index 2e109f7..ff8bfed 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -620,4 +620,46 @@ export class DeviceService { ); } } + async getAllDevices(): Promise { + try { + const devices = await this.deviceRepository.find({ + relations: [ + 'spaceDevice.parent', + 'productDevice', + 'permission', + 'permission.permissionType', + ], + }); + const devicesData = await Promise.all( + devices.map(async (device) => { + const spaceDevice = device?.spaceDevice; + const parentDevice = spaceDevice?.parent; + return { + room: { + uuid: spaceDevice?.uuid, + name: spaceDevice?.spaceName, + }, + unit: { + uuid: parentDevice?.uuid, + name: parentDevice?.spaceName, + }, + productUuid: device.productDevice.uuid, + productType: device.productDevice.prodType, + permissionType: device.permission[0].permissionType.type, + ...(await this.getDeviceDetailsByDeviceIdTuya( + device.deviceTuyaUuid, + )), + uuid: device.uuid, + } as GetDeviceDetailsInterface; + }), + ); + + return devicesData; + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } }