From a8d33bbc526f2f5230776802a470b3e0e994360b Mon Sep 17 00:00:00 2001 From: Mhd Zayd Skaff Date: Wed, 9 Jul 2025 16:05:31 +0300 Subject: [PATCH] fix: commission device API --- src/device/services/device.service.ts | 42 ++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index 02210b7..9b3897c 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -323,7 +323,7 @@ export class DeviceService { async addNewDevice(addDeviceDto: AddDeviceDto, projectUuid: string) { try { - const device = await this.getDeviceDetailsByDeviceIdTuya( + const device = await this.getNewDeviceDetailsFromTuya( addDeviceDto.deviceTuyaUuid, ); @@ -349,6 +349,7 @@ export class DeviceService { spaceDevice: { uuid: addDeviceDto.spaceUuid }, tag: { uuid: addDeviceDto.tagUuid }, name: addDeviceDto.deviceName, + deviceTuyaConstUuid: device.uuid, }); if (deviceSaved.uuid) { const deviceStatus: BaseResponseDto = @@ -752,6 +753,45 @@ export class DeviceService { ); } } + + async getNewDeviceDetailsFromTuya( + deviceId: string, + ): Promise { + console.log('fetching device details from Tuya for deviceId:', deviceId); + try { + const result = await this.tuyaService.getDeviceDetails(deviceId); + + if (!result) { + throw new NotFoundException('Device not found'); + } + // Convert keys to camel case + const camelCaseResponse = convertKeysToCamelCase(result); + + const product = await this.productRepository.findOne({ + where: { prodId: camelCaseResponse.productId }, + }); + + if (!product) { + throw new NotFoundException('Product Type is not supported'); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { productId, id, productName, ...rest } = camelCaseResponse; + + return { + ...rest, + productUuid: product.uuid, + productName: product.name, + } as GetDeviceDetailsInterface; + } catch (error) { + console.log('error', error); + + throw new HttpException( + error.message || 'Error fetching device details from Tuya', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } async getDeviceInstructionByDeviceId( deviceUuid: string, projectUuid: string,