diff --git a/libs/common/src/modules/space/entities/tag.entity.ts b/libs/common/src/modules/space/entities/tag.entity.ts index 6ba1289..7340caf 100644 --- a/libs/common/src/modules/space/entities/tag.entity.ts +++ b/libs/common/src/modules/space/entities/tag.entity.ts @@ -1,11 +1,4 @@ -import { - Entity, - Column, - ManyToOne, - JoinColumn, - Unique, - OneToOne, -} from 'typeorm'; +import { Entity, Column, ManyToOne, JoinColumn, OneToOne } from 'typeorm'; import { AbstractEntity } from '../../abstract/entities/abstract.entity'; import { ProductEntity } from '../../product/entities'; import { TagDto } from '../dtos'; @@ -15,7 +8,6 @@ import { SubspaceEntity } from './subspace'; import { DeviceEntity } from '../../device/entities'; @Entity({ name: 'tag' }) -@Unique(['tag', 'product', 'space', 'subspace']) export class TagEntity extends AbstractEntity { @Column({ type: 'varchar', length: 255 }) tag: string; diff --git a/src/space/services/space-scene.service.ts b/src/space/services/space-scene.service.ts index 6c6b528..f8d2c42 100644 --- a/src/space/services/space-scene.service.ts +++ b/src/space/services/space-scene.service.ts @@ -23,7 +23,7 @@ export class SpaceSceneService { try { const { spaceUuid, communityUuid, projectUuid } = params; - await this.validationService.validateSpaceWithinCommunityAndProject( + await this.validationService.checkCommunityAndProjectSpaceExistence( communityUuid, projectUuid, spaceUuid, diff --git a/src/space/services/space-user.service.ts b/src/space/services/space-user.service.ts index 0912132..fe94983 100644 --- a/src/space/services/space-user.service.ts +++ b/src/space/services/space-user.service.ts @@ -72,7 +72,7 @@ export class SpaceUserService { } // Find the space by ID - await this.validationService.validateSpaceWithinCommunityAndProject( + await this.validationService.checkCommunityAndProjectSpaceExistence( communityUuid, projectUuid, spaceUuid, diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index fa9c30c..43266e5 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -492,7 +492,7 @@ export class SpaceService { params: GetSpaceParam, ): Promise { const { spaceUuid, communityUuid, projectUuid } = params; - await this.validationService.validateSpaceWithinCommunityAndProject( + await this.validationService.checkCommunityAndProjectSpaceExistence( communityUuid, projectUuid, spaceUuid, diff --git a/src/space/services/subspace/subspace-device.service.ts b/src/space/services/subspace/subspace-device.service.ts index 9ee9b9b..cdd80db 100644 --- a/src/space/services/subspace/subspace-device.service.ts +++ b/src/space/services/subspace/subspace-device.service.ts @@ -29,22 +29,14 @@ export class SubspaceDeviceService { ): Promise { const { subSpaceUuid, spaceUuid, communityUuid, projectUuid } = params; - console.time('Total Execution Time'); // ⏳ Start total execution time - console.time('Validation Time'); - await this.validationService.checkCommunityAndProjectSpaceExistence( communityUuid, projectUuid, spaceUuid, ); - console.timeEnd('Validation Time'); // ⏳ Log validation time - - console.time('Subspace Query Time'); const subspace = await this.findSubspaceWithDevices(subSpaceUuid); - console.timeEnd('Subspace Query Time'); // ⏳ Log subspace fetching time - console.time('Fetching Tuya Details Time'); const safeFetch = async (device: any) => { try { const tuyaDetails = await this.getDeviceDetailsByDeviceIdTuya( @@ -62,17 +54,11 @@ export class SubspaceDeviceService { ...tuyaDetails, }; } catch (error) { - console.warn( - `⚠️ Skipping device with deviceTuyaUuid: ${device.deviceTuyaUuid} due to error.`, - ); return null; } }; const detailedDevices = await Promise.all(subspace.devices.map(safeFetch)); - console.timeEnd('Fetching Tuya Details Time'); // ⏳ Log total Tuya fetching time - - console.timeEnd('Total Execution Time'); // ⏳ End total execution time return new SuccessResponseDto({ data: detailedDevices.filter(Boolean), // Remove nulls @@ -119,14 +105,14 @@ export class SubspaceDeviceService { return new SuccessResponseDto({ data: newDevice, - message: 'Successfully associated device to subspace', + message: `Successfully associated device to subspace`, }); } catch (error) { if (error instanceof HttpException) { throw error; } else { throw new HttpException( - 'Failed to associate device to subspace', + `Failed to associate device to subspace with error = ${error}`, HttpStatus.INTERNAL_SERVER_ERROR, ); } @@ -145,7 +131,7 @@ export class SubspaceDeviceService { spaceUuid, ); const subspace = await this.findSubspace(subSpaceUuid); - const device = await this.findDevice(deviceUuid); + const device = await this.findDeviceWithSubspaceAndTag(deviceUuid); if (!device.subspace || device.subspace.uuid !== subspace.uuid) { throw new HttpException( @@ -157,7 +143,7 @@ export class SubspaceDeviceService { if (device.tag?.subspace !== null) { await this.tagRepository.update( { uuid: device.tag.uuid }, - { subspace: null, space: device.subspace }, + { subspace: null, space: device.spaceDevice }, ); } @@ -186,7 +172,7 @@ export class SubspaceDeviceService { throw error; } else { throw new HttpException( - 'Failed to dissociate device from subspace', + `Failed to dissociate device from subspace error = ${error}`, HttpStatus.INTERNAL_SERVER_ERROR, ); } @@ -218,7 +204,13 @@ export class SubspaceDeviceService { private async findDevice(deviceUuid: string) { const device = await this.deviceRepository.findOne({ where: { uuid: deviceUuid }, - relations: ['subspace', , 'tag', 'tag.space', 'tag.subspace'], + relations: [ + 'subspace', + 'tag', + 'tag.space', + 'tag.subspace', + 'spaceDevice', + ], }); if (!device) { this.throwNotFound('Device', deviceUuid); @@ -300,4 +292,12 @@ export class SubspaceDeviceService { : 1; return nextTagNumber; } + + private async findDeviceWithSubspaceAndTag(deviceUuid: string) { + return await this.deviceRepository.findOne({ + where: { uuid: deviceUuid }, + relations: ['subspace', 'tag', 'spaceDevice'], + select: ['uuid', 'subspace', 'spaceDevice', 'productDevice', 'tag'], + }); + } } diff --git a/src/space/services/subspace/subspace.service.ts b/src/space/services/subspace/subspace.service.ts index 3a27aee..78f9445 100644 --- a/src/space/services/subspace/subspace.service.ts +++ b/src/space/services/subspace/subspace.service.ts @@ -178,7 +178,7 @@ export class SubSpaceService { pageable: Partial, ): Promise { const { communityUuid, spaceUuid, projectUuid } = params; - await this.validationService.validateSpaceWithinCommunityAndProject( + await this.validationService.checkCommunityAndProjectSpaceExistence( communityUuid, projectUuid, spaceUuid, @@ -205,7 +205,7 @@ export class SubSpaceService { updateSubSpaceDto: AddSubspaceDto, ): Promise { const { spaceUuid, communityUuid, subSpaceUuid, projectUuid } = params; - await this.validationService.validateSpaceWithinCommunityAndProject( + await this.validationService.checkCommunityAndProjectSpaceExistence( communityUuid, projectUuid, spaceUuid, @@ -244,7 +244,7 @@ export class SubSpaceService { async delete(params: GetSubSpaceParam): Promise { const { spaceUuid, communityUuid, subSpaceUuid, projectUuid } = params; - await this.validationService.validateSpaceWithinCommunityAndProject( + await this.validationService.checkCommunityAndProjectSpaceExistence( communityUuid, projectUuid, spaceUuid, @@ -367,7 +367,7 @@ export class SubSpaceService { } async getOne(params: GetSubSpaceParam): Promise { - await this.validationService.validateSpaceWithinCommunityAndProject( + await this.validationService.checkCommunityAndProjectSpaceExistence( params.communityUuid, params.projectUuid, params.spaceUuid,