added community space

This commit is contained in:
hannathkadher
2024-10-15 11:38:49 +04:00
parent 2292c01220
commit d14d3b5ba2
16 changed files with 253 additions and 11 deletions

View File

@ -9,15 +9,22 @@ import { UserSpaceRepository } from '@app/common/modules/user/repositories';
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
import { UserRepository } from '@app/common/modules/user/repositories';
import { SpacePermissionService } from '@app/common/helper/services';
import { CommunitySpaceService } from './services';
import { CommunitySpaceController } from './controllers';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { RegionRepository } from '@app/common/modules/region/repositories';
@Module({
imports: [ConfigModule, SpaceRepositoryModule, UserRepositoryModule],
controllers: [CommunityController],
controllers: [CommunityController, CommunitySpaceController],
providers: [
CommunityService,
CommunitySpaceService,
SpaceRepository,
SpaceTypeRepository,
UserSpaceRepository,
RegionRepository,
CommunityRepository,
UserRepository,
SpacePermissionService,
],

View File

@ -0,0 +1,36 @@
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.COMMUNITYSPACE.ROUTE,
})
export class CommunitySpaceController {
constructor(private readonly communitySpaceService: CommunitySpaceService) {}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOperation({
summary:
ControllerRoute.COMMUNITYSPACE.ACTIONS
.GET_COMMUNITY_SPACES_HIERARCHY_SUMMARY,
description:
ControllerRoute.COMMUNITYSPACE.ACTIONS
.GET_COMMUNITY_SPACES_HIERARCHY_DESCRIPTION,
})
@Get('/:communityId')
async getCommunityByUuid(
@Param() params: GetCommunityParams,
): Promise<BaseResponseDto> {
return await this.communitySpaceService.getSpacesHierarchyForCommunity(
params.communityId,
);
}
}

View File

@ -25,7 +25,6 @@ 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 { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto';
// import { CommunityPermissionGuard } from 'src/guards/community.permission.guard';
@ApiTags('Community Module')
@Controller({

View File

@ -1 +1,2 @@
export * from './community.controller';
export * from './community-spaces.controller';

View File

@ -0,0 +1,81 @@
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<BaseResponseDto> {
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<string, SpaceEntity>();
// 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) {
// If the space has a parent, add it to the parent's children array
const parent = map.get(space.parent.uuid);
parent?.children?.push(map.get(space.uuid));
} else {
// If the space doesn't have a parent, it's a root space
rootSpaces.push(map.get(space.uuid));
}
});
return rootSpaces; // Return the root spaces with children nested within them
}
}

View File

@ -189,7 +189,7 @@ export class CommunityService {
}
const spaces = communities.map((community) => ({
uuid: community.space.uuid,
name: community.space.name,
name: community.space.spaceName,
type: community.space.spaceType.type,
}));

View File

@ -1 +1,2 @@
export * from './community.service';
export * from './community-space.service';