mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-24 12:37:55 +00:00
removed get spaces from community
This commit is contained in:
@@ -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<BaseResponseDto> {
|
||||
return this.spaceService.list(communitySpaceParam.communityUuid, query);
|
||||
return this.spaceService.getSpacesHierarchyForCommunity(
|
||||
param.communityUuid,
|
||||
);
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
|
||||
@@ -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<TypeORMCustomModelFindAllQuery>,
|
||||
async getSpacesHierarchyForCommunity(
|
||||
communityUuid: string,
|
||||
): Promise<BaseResponseDto> {
|
||||
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<SpaceDto>(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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user