diff --git a/src/community/services/community.service.ts b/src/community/services/community.service.ts index ec964a9..81aab77 100644 --- a/src/community/services/community.service.ts +++ b/src/community/services/community.service.ts @@ -1,8 +1,13 @@ +import { GetCommunityChildDto } from './../dtos/get.community.dto'; import { SpaceTypeRepository } from './../../../libs/common/src/modules/space-type/repositories/space.type.repository'; import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { SpaceRepository } from '@app/common/modules/space/repositories'; import { AddCommunityDto } from '../dtos'; -import { GetCommunityByUuidInterface } from '../interface/community.interface'; +import { + CommunityChildInterface, + GetCommunityByUuidInterface, +} from '../interface/community.interface'; +import { SpaceEntity } from '@app/common/modules/space/entities'; @Injectable() export class CommunityService { @@ -21,7 +26,6 @@ export class CommunityService { await this.spaceRepository.save({ spaceName: addCommunityDto.spaceName, - parent: { uuid: addCommunityDto.parentUuid }, spaceType: { uuid: spaceType.uuid }, }); } catch (err) { @@ -33,20 +37,25 @@ export class CommunityService { communityUuid: string, ): Promise { try { - const community: GetCommunityByUuidInterface = - await this.spaceRepository.findOne({ - where: { - uuid: communityUuid, - spaceType: { - type: 'community', - }, + const community = await this.spaceRepository.findOne({ + where: { + uuid: communityUuid, + spaceType: { + type: 'community', }, - relations: ['spaceType'], - }); + }, + relations: ['spaceType'], + }); if (!community) { throw new HttpException('Community not found', HttpStatus.NOT_FOUND); } - return community; + return { + uuid: community.uuid, + createdAt: community.createdAt, + updatedAt: community.updatedAt, + name: community.spaceName, + type: community.spaceType.type, + }; } catch (err) { throw new HttpException( err.message, @@ -54,4 +63,64 @@ export class CommunityService { ); } } + async getCommunityChildByUuid( + communityUuid: string, + getCommunityChildDto: GetCommunityChildDto, + ): Promise { + const { includeSubSpaces, page, pageSize } = getCommunityChildDto; + + const space = await this.spaceRepository.findOneOrFail({ + where: { uuid: communityUuid }, + relations: ['children', 'spaceType'], + }); + + if (space.spaceType.type !== 'community') { + throw new HttpException('Community not found', HttpStatus.NOT_FOUND); + } + + return { + uuid: space.uuid, + name: space.spaceName, + type: space.spaceType.type, + children: await this.buildHierarchy( + space, + includeSubSpaces, + page, + pageSize, + ), + }; + } + + private async buildHierarchy( + space: SpaceEntity, + includeSubSpaces: boolean, + page: number, + pageSize: number, + ): Promise { + const children = await this.spaceRepository.find({ + where: { parent: { uuid: space.uuid } }, + relations: ['spaceType'], + skip: (page - 1) * pageSize, + take: pageSize, + }); + + if (!children || children.length === 0 || !includeSubSpaces) { + return children.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), + })), + ); + + return childHierarchies; + } }