From 9b837bd15a6e20c91329bd8569b88b2079b21540 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Wed, 3 Apr 2024 15:07:11 +0300 Subject: [PATCH] Refactor buildHierarchy method for improved performance --- src/community/services/community.service.ts | 44 ++++++++++++--------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/community/services/community.service.ts b/src/community/services/community.service.ts index 2861fe2..a917d94 100644 --- a/src/community/services/community.service.ts +++ b/src/community/services/community.service.ts @@ -77,17 +77,21 @@ export class CommunityService { if (space.spaceType.type !== 'community') { throw new HttpException('Community not found', HttpStatus.NOT_FOUND); } - + const totalCount = await this.spaceRepository.count({ + where: { parent: { uuid: space.uuid } }, + }); + const children = await this.buildHierarchy( + space, + includeSubSpaces, + page, + pageSize, + ); return { uuid: space.uuid, name: space.spaceName, type: space.spaceType.type, - children: await this.buildHierarchy( - space, - includeSubSpaces, - page, - pageSize, - ), + totalCount, + children, }; } @@ -105,20 +109,24 @@ export class CommunityService { }); if (!children || children.length === 0 || !includeSubSpaces) { - return children.map((child) => ({ - uuid: child.uuid, - name: child.spaceName, - type: child.spaceType.type, - })); + return children + .filter((child) => child.spaceType.type !== 'community') // Filter remaining community type + .map((child) => ({ + uuid: child.uuid, + name: child.spaceName, + type: child.spaceType.type, + })); } const childHierarchies = await Promise.all( - children.map(async (child) => ({ - uuid: child.uuid, - name: child.spaceName, - type: child.spaceType.type, - children: await this.buildHierarchy(child, true, 1, pageSize), - })), + children + .filter((child) => child.spaceType.type !== 'community') // Filter remaining community type + .map(async (child) => ({ + uuid: child.uuid, + name: child.spaceName, + type: child.spaceType.type, + children: await this.buildHierarchy(child, true, 1, pageSize), + })), ); return childHierarchies;