diff --git a/libs/common/src/modules/space/entities/tag.entity.ts b/libs/common/src/modules/space/entities/tag.entity.ts index e7f8599..6ba1289 100644 --- a/libs/common/src/modules/space/entities/tag.entity.ts +++ b/libs/common/src/modules/space/entities/tag.entity.ts @@ -48,5 +48,6 @@ export class TagEntity extends AbstractEntity { @OneToOne(() => DeviceEntity, (device) => device.tag, { nullable: true, }) + @JoinColumn({ name: 'device_id' }) device: DeviceEntity; } diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index 2d581e9..d0394c6 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -19,7 +19,7 @@ import { SpaceEntity } from '@app/common/modules/space/entities'; import { generateRandomString } from '@app/common/helper/randomString'; import { SpaceLinkService } from './space-link'; import { SubSpaceService } from './subspace'; -import { DataSource, Not, QueryRunner } from 'typeorm'; +import { DataSource, QueryRunner } from 'typeorm'; import { ValidationService } from './space-validation.service'; import { ORPHAN_COMMUNITY_NAME, @@ -171,25 +171,28 @@ export class SpaceService { ); try { // Get all spaces related to the community, including the parent-child relations - const spaces = await this.spaceRepository.find({ - where: { - community: { uuid: communityUuid }, - spaceName: Not(`${ORPHAN_SPACE_NAME}`), - disabled: false, - }, - relations: [ - 'parent', + const spaces = await this.spaceRepository + .createQueryBuilder('space') + .leftJoinAndSelect('space.parent', 'parent') + .leftJoinAndSelect( + 'space.children', 'children', - 'incomingConnections', - 'tags', - 'tags.product', - 'subspaces', - 'subspaces.tags', - 'subspaces.tags.product', - ], - }); + 'children.disabled = :disabled', + { disabled: false }, + ) + .leftJoinAndSelect('space.incomingConnections', 'incomingConnections') + .leftJoinAndSelect('space.tags', 'tags') + .leftJoinAndSelect('tags.product', 'tagProduct') + .leftJoinAndSelect('space.subspaces', 'subspaces') + .leftJoinAndSelect('subspaces.tags', 'subspaceTags') + .leftJoinAndSelect('subspaceTags.product', 'subspaceTagProduct') + .where('space.community_id = :communityUuid', { communityUuid }) + .andWhere('space.spaceName != :orphanSpaceName', { + orphanSpaceName: ORPHAN_SPACE_NAME, + }) + .andWhere('space.disabled = :disabled', { disabled: false }) + .getMany(); - // Organize spaces into a hierarchical structure const spaceHierarchy = this.buildSpaceHierarchy(spaces); return new SuccessResponseDto({ diff --git a/src/space/services/tag/tag.service.ts b/src/space/services/tag/tag.service.ts index 4c3bb8f..be32a2e 100644 --- a/src/space/services/tag/tag.service.ts +++ b/src/space/services/tag/tag.service.ts @@ -138,7 +138,7 @@ export class TagService { ); return { message: 'Tags deleted successfully', tagUuids }; } catch (error) { - throw this.handleUnexpectedError('Failed to update tags', error); + throw this.handleUnexpectedError('Failed to delete tags', error); } } diff --git a/src/users/services/user-space.service.ts b/src/users/services/user-space.service.ts index 0ee6b03..71611bc 100644 --- a/src/users/services/user-space.service.ts +++ b/src/users/services/user-space.service.ts @@ -152,9 +152,10 @@ export class UserSpaceService { async deleteUserSpace(spaceUuid: string) { try { await this.userSpaceRepository - .createQueryBuilder() + .createQueryBuilder('userSpace') + .leftJoin('userSpace.space', 'space') .delete() - .where('spaceUuid = :spaceUuid', { spaceUuid }) + .where('space.uuid = :spaceUuid', { spaceUuid }) .execute(); } catch (error) { console.error(`Error deleting user-space associations: ${error.message}`);