From a486b24ef8684cc7898f8d9c025607dba2b8d6f2 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Tue, 29 Oct 2024 18:13:13 +0400 Subject: [PATCH] removed get spaces from community --- libs/common/src/constants/controller-route.ts | 10 ++- src/community/community.module.ts | 5 +- .../community-spaces.controller.ts | 36 --------- src/community/controllers/index.ts | 1 - .../services/community-space.service.ts | 79 ------------------- src/community/services/index.ts | 1 - src/space/controllers/space.controller.ts | 17 ++-- src/space/services/space.service.ts | 40 +++++----- 8 files changed, 38 insertions(+), 151 deletions(-) delete mode 100644 src/community/controllers/community-spaces.controller.ts delete mode 100644 src/community/services/community-space.service.ts diff --git a/libs/common/src/constants/controller-route.ts b/libs/common/src/constants/controller-route.ts index cee2273..a3c6ffb 100644 --- a/libs/common/src/constants/controller-route.ts +++ b/libs/common/src/constants/controller-route.ts @@ -10,7 +10,7 @@ export class ControllerRoute { }; static COMMUNITY = class { - public static readonly ROUTE = 'community'; + public static readonly ROUTE = 'communities'; static ACTIONS = class { public static readonly GET_COMMUNITY_BY_ID_SUMMARY = 'Get community by community community uuid'; @@ -40,7 +40,7 @@ export class ControllerRoute { }; static COMMUNITY_SPACE = class { - public static readonly ROUTE = 'community/:communityUuid/space'; + public static readonly ROUTE = 'communities/:communityUuid/space'; static ACTIONS = class { public static readonly GET_COMMUNITY_SPACES_HIERARCHY_SUMMARY = 'Fetch hierarchical structure of spaces within a community.'; @@ -111,6 +111,12 @@ export class ControllerRoute { 'Generate a new invitation code for a specific space'; public static readonly CREATE_INVITATION_CODE_SPACE_DESCRIPTION = 'This endpoint generates a new 6-character invitation code for a space identified by its UUID and stores it in the space entity'; + + public static readonly GET_COMMUNITY_SPACES_HIERARCHY_SUMMARY = + 'Fetch hierarchical structure of spaces within a community.'; + + public static readonly GET_COMMUNITY_SPACES_HIERARCHY_DESCRIPTION = + 'retrieves all the spaces associated with a given community, organized into a hierarchical structure.'; }; }; diff --git a/src/community/community.module.ts b/src/community/community.module.ts index c01a9a2..cd89657 100644 --- a/src/community/community.module.ts +++ b/src/community/community.module.ts @@ -8,17 +8,14 @@ import { SpaceTypeRepository } from '@app/common/modules/space/repositories'; import { UserSpaceRepository } from '@app/common/modules/user/repositories'; import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module'; import { SpacePermissionService } from '@app/common/helper/services'; -import { CommunitySpaceService } from './services'; -import { CommunitySpaceController } from './controllers'; import { CommunityRepository } from '@app/common/modules/community/repositories'; import { TuyaService } from '@app/common/integrations/tuya/tuya.service'; @Module({ imports: [ConfigModule, SpaceRepositoryModule, UserRepositoryModule], - controllers: [CommunityController, CommunitySpaceController], + controllers: [CommunityController], providers: [ CommunityService, - CommunitySpaceService, SpaceRepository, SpaceTypeRepository, UserSpaceRepository, diff --git a/src/community/controllers/community-spaces.controller.ts b/src/community/controllers/community-spaces.controller.ts deleted file mode 100644 index 1897d21..0000000 --- a/src/community/controllers/community-spaces.controller.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Controller, Get, Param, UseGuards } from '@nestjs/common'; -import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; -import { GetCommunityParams } from '../dtos/get.community.dto'; -// import { CheckUserCommunityGuard } from 'src/guards/user.community.guard'; -import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard'; -import { ControllerRoute } from '@app/common/constants/controller-route'; -import { BaseResponseDto } from '@app/common/dto/base.response.dto'; -import { CommunitySpaceService } from '../services'; - -@ApiTags('Community Module') -@Controller({ - version: '1', - path: ControllerRoute.COMMUNITY_SPACE.ROUTE, -}) -export class CommunitySpaceController { - constructor(private readonly communitySpaceService: CommunitySpaceService) {} - - @ApiBearerAuth() - @UseGuards(JwtAuthGuard) - @ApiOperation({ - summary: - ControllerRoute.COMMUNITY_SPACE.ACTIONS - .GET_COMMUNITY_SPACES_HIERARCHY_SUMMARY, - description: - ControllerRoute.COMMUNITY_SPACE.ACTIONS - .GET_COMMUNITY_SPACES_HIERARCHY_DESCRIPTION, - }) - @Get() - async getCommunityByUuid( - @Param() params: GetCommunityParams, - ): Promise { - return await this.communitySpaceService.getSpacesHierarchyForCommunity( - params.communityUuid, - ); - } -} diff --git a/src/community/controllers/index.ts b/src/community/controllers/index.ts index fd6fd56..b78bbea 100644 --- a/src/community/controllers/index.ts +++ b/src/community/controllers/index.ts @@ -1,2 +1 @@ export * from './community.controller'; -export * from './community-spaces.controller'; diff --git a/src/community/services/community-space.service.ts b/src/community/services/community-space.service.ts deleted file mode 100644 index 54fb4f3..0000000 --- a/src/community/services/community-space.service.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; -import { SpaceRepository } from '@app/common/modules/space/repositories'; -import { BaseResponseDto } from '@app/common/dto/base.response.dto'; -import { CommunityRepository } from '@app/common/modules/community/repositories'; -import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; -import { SpaceEntity } from '@app/common/modules/space/entities'; - -@Injectable() -export class CommunitySpaceService { - constructor( - private readonly spaceRepository: SpaceRepository, - private readonly communityRepository: CommunityRepository, - ) {} - - async getSpacesHierarchyForCommunity( - communityId: string, - ): Promise { - const community = await this.communityRepository.findOne({ - where: { uuid: communityId }, - }); - - // If the community doesn't exist, throw a 404 error - if (!community) { - throw new HttpException( - `Community with ID ${communityId} not found`, - HttpStatus.NOT_FOUND, - ); - } - try { - // Get all spaces related to the community, including the parent-child relations - const spaces = await this.spaceRepository.find({ - where: { community: { uuid: communityId } }, - relations: ['parent', 'children'], // Include parent and children relations - }); - - // Organize spaces into a hierarchical structure - const spaceHierarchy = this.buildSpaceHierarchy(spaces); - - return new SuccessResponseDto({ - message: `Spaces in community ${communityId} successfully fetched in hierarchy`, - data: spaceHierarchy, - statusCode: HttpStatus.OK, - }); - } catch (error) { - throw new HttpException( - 'An error occurred while fetching the spaces', - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } - } - - private buildSpaceHierarchy(spaces: SpaceEntity[]): SpaceEntity[] { - const map = new Map(); - - // Step 1: Create a map of spaces by UUID - spaces.forEach((space) => { - map.set( - space.uuid, - this.spaceRepository.create({ - ...space, - children: [], // Add children if needed - }), - ); - }); - - // Step 2: Organize the hierarchy - const rootSpaces: SpaceEntity[] = []; - spaces.forEach((space) => { - if (space.parent && space.parent.uuid) { - const parent = map.get(space.parent.uuid); - parent?.children?.push(map.get(space.uuid)); - } else { - rootSpaces.push(map.get(space.uuid)); - } - }); - - return rootSpaces; // Return the root spaces with children nested within them - } -} diff --git a/src/community/services/index.ts b/src/community/services/index.ts index 257481b..2408c7c 100644 --- a/src/community/services/index.ts +++ b/src/community/services/index.ts @@ -1,2 +1 @@ export * from './community.service'; -export * from './community-space.service'; diff --git a/src/space/controllers/space.controller.ts b/src/space/controllers/space.controller.ts index 2d70156..cb8cbb8 100644 --- a/src/space/controllers/space.controller.ts +++ b/src/space/controllers/space.controller.ts @@ -9,13 +9,11 @@ import { Param, Post, Put, - Query, UseGuards, } from '@nestjs/common'; import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard'; import { AddSpaceDto, CommunitySpaceParam } from '../dtos'; import { BaseResponseDto } from '@app/common/dto/base.response.dto'; -import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto'; import { GetSpaceParam } from '../dtos/get.space.param'; @ApiTags('Space Module') @@ -46,15 +44,18 @@ export class SpaceController { @ApiBearerAuth() @UseGuards(JwtAuthGuard) @ApiOperation({ - summary: ControllerRoute.SPACE.ACTIONS.LIST_SPACE_SUMMARY, - description: ControllerRoute.SPACE.ACTIONS.LIST_SPACE_DESCRIPTION, + summary: + ControllerRoute.SPACE.ACTIONS.GET_COMMUNITY_SPACES_HIERARCHY_SUMMARY, + description: + ControllerRoute.SPACE.ACTIONS.GET_COMMUNITY_SPACES_HIERARCHY_DESCRIPTION, }) @Get() - async list( - @Param() communitySpaceParam: CommunitySpaceParam, - @Query() query: PaginationRequestGetListDto, + async getHierarchy( + @Param() param: CommunitySpaceParam, ): Promise { - return this.spaceService.list(communitySpaceParam.communityUuid, query); + return this.spaceService.getSpacesHierarchyForCommunity( + param.communityUuid, + ); } @ApiBearerAuth() diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index 6bb6b12..52267d8 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -10,12 +10,6 @@ import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; import { BaseResponseDto } from '@app/common/dto/base.response.dto'; import { CommunityRepository } from '@app/common/modules/community/repositories'; import { SpaceEntity } from '@app/common/modules/space/entities'; -import { - TypeORMCustomModel, - TypeORMCustomModelFindAllQuery, -} from '@app/common/models/typeOrmCustom.model'; -import { SpaceDto } from '@app/common/modules/space/dtos'; -import { PageResponse } from '@app/common/dto/pagination.response.dto'; import { generateRandomString } from '@app/common/helper/randomString'; @Injectable() @@ -76,34 +70,40 @@ export class SpaceService { } } - async list( - communityId: string, - pageable: Partial, + async getSpacesHierarchyForCommunity( + communityUuid: string, ): Promise { const community = await this.communityRepository.findOne({ - where: { uuid: communityId }, + where: { uuid: communityUuid }, }); // If the community doesn't exist, throw a 404 error if (!community) { throw new HttpException( - `Community with ID ${communityId} not found`, + `Community with ID ${communityUuid} not found`, HttpStatus.NOT_FOUND, ); } try { - pageable.modelName = 'space'; - pageable.where = { - community: { uuid: communityId }, - }; + // Get all spaces related to the community, including the parent-child relations + const spaces = await this.spaceRepository.find({ + where: { community: { uuid: communityUuid } }, + relations: ['parent', 'children'], // Include parent and children relations + }); - const customModel = TypeORMCustomModel(this.spaceRepository); + // Organize spaces into a hierarchical structure + const spaceHierarchy = this.buildSpaceHierarchy(spaces); - const { baseResponseDto, paginationResponseDto } = - await customModel.findAll(pageable); - return new PageResponse(baseResponseDto, paginationResponseDto); + return new SuccessResponseDto({ + message: `Spaces in community ${communityUuid} successfully fetched in hierarchy`, + data: spaceHierarchy, + statusCode: HttpStatus.OK, + }); } catch (error) { - throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR); + throw new HttpException( + 'An error occurred while fetching the spaces', + HttpStatus.INTERNAL_SERVER_ERROR, + ); } }