Refactor buildHierarchy method for improved performance

This commit is contained in:
faris Aljohari
2024-04-03 15:07:11 +03:00
parent 249dd67591
commit 9b837bd15a
+26 -18
View File
@@ -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;