From 229909f9594fbb9d23b65ae221ebd37057650231 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Wed, 1 May 2024 10:21:55 +0300 Subject: [PATCH 1/2] Import ProductRepository in DeviceService --- src/device/services/device.service.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index 112deb5..39bc0ce 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -1,3 +1,4 @@ +import { ProductRepository } from './../../../libs/common/src/modules/product/repositories/product.repository'; import { Injectable, HttpException, @@ -33,6 +34,7 @@ export class DeviceService { private readonly configService: ConfigService, private readonly deviceRepository: DeviceRepository, private readonly groupDeviceRepository: GroupDeviceRepository, + private readonly productRepository: ProductRepository, ) { const accessKey = this.configService.get('auth-config.ACCESS_KEY'); const secretKey = this.configService.get('auth-config.SECRET_KEY'); @@ -247,18 +249,17 @@ export class DeviceService { // Convert keys to camel case const camelCaseResponse = convertKeysToCamelCase(response); - const deviceDetails = await this.deviceRepository.findOne({ + const product = await this.productRepository.findOne({ where: { - deviceTuyaUuid: deviceId, + prodId: camelCaseResponse.result.productId, }, - relations: ['productDevice'], }); // eslint-disable-next-line @typescript-eslint/no-unused-vars const { productName, productId, ...rest } = camelCaseResponse.result; return { ...rest, - productUuid: deviceDetails.productDevice.uuid, + productUuid: product.uuid, } as GetDeviceDetailsInterface; } catch (error) { throw new HttpException( From d146cce1bbbdb5d047fa587c94d3bc739838dc4e Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Wed, 1 May 2024 10:34:07 +0300 Subject: [PATCH 2/2] Refactor controllers to return UUID of added entities --- src/building/controllers/building.controller.ts | 4 ++-- src/building/services/building.service.ts | 3 ++- src/community/controllers/community.controller.ts | 5 +++-- src/community/services/community.service.ts | 3 ++- src/floor/controllers/floor.controller.ts | 4 ++-- src/floor/services/floor.service.ts | 3 ++- src/room/controllers/room.controller.ts | 4 ++-- src/room/services/room.service.ts | 3 ++- src/unit/controllers/unit.controller.ts | 4 ++-- src/unit/services/unit.service.ts | 3 ++- 10 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/building/controllers/building.controller.ts b/src/building/controllers/building.controller.ts index 8517ee1..dc4dbd7 100644 --- a/src/building/controllers/building.controller.ts +++ b/src/building/controllers/building.controller.ts @@ -32,8 +32,8 @@ export class BuildingController { @Post() async addBuilding(@Body() addBuildingDto: AddBuildingDto) { try { - await this.buildingService.addBuilding(addBuildingDto); - return { message: 'Building added successfully' }; + const building = await this.buildingService.addBuilding(addBuildingDto); + return { message: 'Building added successfully', uuid: building.uuid }; } catch (error) { throw new HttpException( error.message || 'Internal server error', diff --git a/src/building/services/building.service.ts b/src/building/services/building.service.ts index fcb803b..dfc2089 100644 --- a/src/building/services/building.service.ts +++ b/src/building/services/building.service.ts @@ -38,11 +38,12 @@ export class BuildingService { if (!spaceType) { throw new BadRequestException('Invalid building UUID'); } - await this.spaceRepository.save({ + const building = await this.spaceRepository.save({ spaceName: addBuildingDto.buildingName, parent: { uuid: addBuildingDto.communityUuid }, spaceType: { uuid: spaceType.uuid }, }); + return building; } catch (err) { if (err instanceof BadRequestException) { throw err; // Re-throw BadRequestException diff --git a/src/community/controllers/community.controller.ts b/src/community/controllers/community.controller.ts index f20aa3d..3fd2233 100644 --- a/src/community/controllers/community.controller.ts +++ b/src/community/controllers/community.controller.ts @@ -34,8 +34,9 @@ export class CommunityController { @Post() async addCommunity(@Body() addCommunityDto: AddCommunityDto) { try { - await this.communityService.addCommunity(addCommunityDto); - return { message: 'Community added successfully' }; + const community = + await this.communityService.addCommunity(addCommunityDto); + return { message: 'Community added successfully', uuid: community.uuid }; } catch (error) { throw new HttpException( error.message || 'Internal server error', diff --git a/src/community/services/community.service.ts b/src/community/services/community.service.ts index e88290f..66c7037 100644 --- a/src/community/services/community.service.ts +++ b/src/community/services/community.service.ts @@ -34,10 +34,11 @@ export class CommunityService { }, }); - await this.spaceRepository.save({ + const community = await this.spaceRepository.save({ spaceName: addCommunityDto.communityName, spaceType: { uuid: spaceType.uuid }, }); + return community; } catch (err) { throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR); } diff --git a/src/floor/controllers/floor.controller.ts b/src/floor/controllers/floor.controller.ts index 2033918..a42db7b 100644 --- a/src/floor/controllers/floor.controller.ts +++ b/src/floor/controllers/floor.controller.ts @@ -32,8 +32,8 @@ export class FloorController { @Post() async addFloor(@Body() addFloorDto: AddFloorDto) { try { - await this.floorService.addFloor(addFloorDto); - return { message: 'Floor added successfully' }; + const floor = await this.floorService.addFloor(addFloorDto); + return { message: 'Floor added successfully', uuid: floor.uuid }; } catch (error) { throw new HttpException( error.message || 'Internal server error', diff --git a/src/floor/services/floor.service.ts b/src/floor/services/floor.service.ts index 91de93b..af4b26f 100644 --- a/src/floor/services/floor.service.ts +++ b/src/floor/services/floor.service.ts @@ -35,11 +35,12 @@ export class FloorService { }, }); - await this.spaceRepository.save({ + const floor = await this.spaceRepository.save({ spaceName: addFloorDto.floorName, parent: { uuid: addFloorDto.buildingUuid }, spaceType: { uuid: spaceType.uuid }, }); + return floor; } catch (err) { throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR); } diff --git a/src/room/controllers/room.controller.ts b/src/room/controllers/room.controller.ts index 054bedf..221fb39 100644 --- a/src/room/controllers/room.controller.ts +++ b/src/room/controllers/room.controller.ts @@ -30,8 +30,8 @@ export class RoomController { @Post() async addRoom(@Body() addRoomDto: AddRoomDto) { try { - await this.roomService.addRoom(addRoomDto); - return { message: 'Room added successfully' }; + const room = await this.roomService.addRoom(addRoomDto); + return { message: 'Room added successfully', uuid: room.uuid }; } catch (error) { throw new HttpException( error.message || 'Internal server error', diff --git a/src/room/services/room.service.ts b/src/room/services/room.service.ts index 4771c25..b9ea30e 100644 --- a/src/room/services/room.service.ts +++ b/src/room/services/room.service.ts @@ -32,11 +32,12 @@ export class RoomService { }, }); - await this.spaceRepository.save({ + const room = await this.spaceRepository.save({ spaceName: addRoomDto.roomName, parent: { uuid: addRoomDto.unitUuid }, spaceType: { uuid: spaceType.uuid }, }); + return room; } catch (err) { throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR); } diff --git a/src/unit/controllers/unit.controller.ts b/src/unit/controllers/unit.controller.ts index a3e59e9..f0f3775 100644 --- a/src/unit/controllers/unit.controller.ts +++ b/src/unit/controllers/unit.controller.ts @@ -32,8 +32,8 @@ export class UnitController { @Post() async addUnit(@Body() addUnitDto: AddUnitDto) { try { - await this.unitService.addUnit(addUnitDto); - return { message: 'Unit added successfully' }; + const unit = await this.unitService.addUnit(addUnitDto); + return { message: 'Unit added successfully', uuid: unit.uuid }; } catch (error) { throw new HttpException( error.message || 'Internal server error', diff --git a/src/unit/services/unit.service.ts b/src/unit/services/unit.service.ts index 1daa297..11d654f 100644 --- a/src/unit/services/unit.service.ts +++ b/src/unit/services/unit.service.ts @@ -35,11 +35,12 @@ export class UnitService { }, }); - await this.spaceRepository.save({ + const unit = await this.spaceRepository.save({ spaceName: addUnitDto.unitName, parent: { uuid: addUnitDto.floorUuid }, spaceType: { uuid: spaceType.uuid }, }); + return unit; } catch (err) { throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR); }