From c677be400c46974d061dfab0d690fde4de4a05c1 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Thu, 17 Apr 2025 10:41:39 +0400 Subject: [PATCH] added community info and tag --- .../modules/space/entities/space.entity.ts | 4 - src/project/services/project.service.ts | 133 +++++++++++------- 2 files changed, 79 insertions(+), 58 deletions(-) diff --git a/libs/common/src/modules/space/entities/space.entity.ts b/libs/common/src/modules/space/entities/space.entity.ts index 815302f..9087d80 100644 --- a/libs/common/src/modules/space/entities/space.entity.ts +++ b/libs/common/src/modules/space/entities/space.entity.ts @@ -8,7 +8,6 @@ import { SpaceLinkEntity } from './space-link.entity'; import { SceneEntity } from '../../scene/entities'; import { SpaceModelEntity } from '../../space-model'; import { InviteUserSpaceEntity } from '../../Invite-user/entities'; -import { TagEntity } from './tag.entity'; import { SpaceProductAllocationEntity } from './space-product-allocation.entity'; import { SubspaceEntity } from './subspace/subspace.entity'; @@ -103,9 +102,6 @@ export class SpaceEntity extends AbstractEntity { ) invitedUsers: InviteUserSpaceEntity[]; - @OneToMany(() => TagEntity, (tag) => tag.space) - tags: TagEntity[]; - @OneToMany( () => SpaceProductAllocationEntity, (allocation) => allocation.space, diff --git a/src/project/services/project.service.ts b/src/project/services/project.service.ts index c4a7fc3..50de089 100644 --- a/src/project/services/project.service.ts +++ b/src/project/services/project.service.ts @@ -23,6 +23,7 @@ import { format } from '@fast-csv/format'; import { PassThrough } from 'stream'; import { SpaceEntity } from '@app/common/modules/space/entities/space.entity'; import { SpaceService } from 'src/space/services'; +import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant'; @Injectable() export class ProjectService { @@ -229,7 +230,18 @@ export class ProjectService { async exportToCsv(param: GetProjectParam): Promise { const project = await this.projectRepository.findOne({ where: { uuid: param.projectUuid }, - relations: ['communities', 'communities.spaces'], + relations: [ + 'communities', + 'communities.spaces', + 'communities.spaces.parent', + 'communities.spaces.productAllocations', + 'communities.spaces.productAllocations.product', + 'communities.spaces.productAllocations.tags', + 'communities.spaces.subspaces', + 'communities.spaces.subspaces.productAllocations', + 'communities.spaces.subspaces.productAllocations.product', + 'communities.spaces.subspaces.productAllocations.tags', + ], }); if (!project) { @@ -239,25 +251,13 @@ export class ProjectService { ); } - const allCommunitySpaces: SpaceEntity[] = []; - - // Step 1: Load the entire space hierarchy for each community - for (const community of project.communities || []) { - const { data: spaceHierarchy } = - await this.spaceService.getSpacesHierarchyForCommunity( - { communityUuid: community.uuid, projectUuid: project.uuid }, - { search: '', onlyWithDevices: false }, - ); - - allCommunitySpaces.push(...(spaceHierarchy as SpaceEntity[])); - } - - // Step 2: Setup CSV const stream = new PassThrough(); const csvStream = format({ headers: [ 'Project Name', 'Project UUID', + 'Community Name', + 'Community UUID', 'Space Location', 'Space Name', 'Space UUID', @@ -267,53 +267,78 @@ export class ProjectService { 'Tag Product Name', ], }); + csvStream.pipe(stream); - // Step 3: Recursive flattening - const writeSpaceData = (space: any, path: string[] = []) => { - const location = path.join(' > '); + const allSpaces: SpaceEntity[] = []; + for (const community of project.communities) { + if (community.name === `${ORPHAN_COMMUNITY_NAME}-${project.name}`) + continue; + for (const space of community.spaces) { + if (!space.disabled) { + space.community = community; + allSpaces.push(space); + } + } + } + + const spaceMap = new Map(); + for (const space of allSpaces) { + spaceMap.set(space.uuid, space); + } + + function buildSpaceLocation(space: SpaceEntity): string { + const path: string[] = []; + let current = space.parent; + while (current) { + path.unshift(current.spaceName); + current = current.parent ?? spaceMap.get(current.uuid)?.parent ?? null; + } + return path.join(' > '); + } + + for (const space of allSpaces) { + const spaceLocation = buildSpaceLocation(space); - // 1. Subspaces and their tags for (const subspace of space.subspaces || []) { - for (const tag of subspace.tags || []) { - csvStream.write({ - 'Project Name': project.name, - 'Project UUID': project.uuid, - 'Space Location': location, - 'Space Name': space.spaceName, - 'Space UUID': space.uuid, - 'Subspace Name': subspace.subspaceName || '', - 'Subspace UUID': subspace.uuid, - Tag: tag.tag, - 'Tag Product Name': tag.product?.productName || '', - }); + if (subspace.disabled) continue; + + for (const productAllocation of subspace.productAllocations || []) { + for (const tag of productAllocation.tags || []) { + csvStream.write({ + 'Project Name': project.name, + 'Project UUID': project.uuid, + 'Community Name': space.community?.name || '', + 'Community UUID': space.community?.uuid || '', + 'Space Location': spaceLocation, + 'Space Name': space.spaceName, + 'Space UUID': space.uuid, + 'Subspace Name': subspace.subspaceName || '', + 'Subspace UUID': subspace.uuid, + Tag: tag.name, + 'Tag Product Name': productAllocation.product.name || '', + }); + } } } - // 2. Tags directly on the space - for (const tag of space.tags || []) { - csvStream.write({ - 'Project Name': project.name, - 'Project UUID': project.uuid, - 'Space Location': location, - 'Space Name': space.spaceName, - 'Space UUID': space.uuid, - 'Subspace Name': '', - 'Subspace UUID': '', - Tag: tag.tag, - 'Tag Product Name': tag.product?.productName || '', - }); + for (const productAllocation of space.productAllocations || []) { + for (const tag of productAllocation.tags || []) { + csvStream.write({ + 'Project Name': project.name, + 'Project UUID': project.uuid, + 'Community Name': space.community?.name || '', + 'Community UUID': space.community?.uuid || '', + 'Space Location': spaceLocation, + 'Space Name': space.spaceName, + 'Space UUID': space.uuid, + 'Subspace Name': '', + 'Subspace UUID': '', + Tag: tag.name, + 'Tag Product Name': productAllocation.product.name || '', + }); + } } - - // 3. Recursively process children - for (const child of space.children || []) { - writeSpaceData(child, [...path, space.spaceName]); - } - }; - - // Step 4: Flatten the hierarchy starting from roots - for (const rootSpace of allCommunitySpaces) { - writeSpaceData(rootSpace, []); } csvStream.end();