diff --git a/src/community/controllers/community.controller.ts b/src/community/controllers/community.controller.ts index a6b269f..a2b0ef2 100644 --- a/src/community/controllers/community.controller.ts +++ b/src/community/controllers/community.controller.ts @@ -1,4 +1,3 @@ -import { CommunityService } from '../services/community.service'; import { Body, Controller, @@ -10,17 +9,18 @@ import { Query, UseGuards, } from '@nestjs/common'; -import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; +import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'; import { AddCommunityDto } from '../dtos/add.community.dto'; import { GetCommunityParams } from '../dtos/get.community.dto'; import { UpdateCommunityNameDto } from '../dtos/update.community.dto'; +import { CommunityService } from '../services/community.service'; // import { CheckUserCommunityGuard } from 'src/guards/user.community.guard'; import { ControllerRoute } from '@app/common/constants/controller-route'; import { BaseResponseDto } from '@app/common/dto/base.response.dto'; -import { ProjectParam } from '../dtos'; -import { PermissionsGuard } from 'src/guards/permissions.guard'; -import { Permissions } from 'src/decorators/permissions.decorator'; import { PaginationRequestWithSearchGetListDto } from '@app/common/dto/pagination-with-search.request.dto'; +import { Permissions } from 'src/decorators/permissions.decorator'; +import { PermissionsGuard } from 'src/guards/permissions.guard'; +import { ProjectParam } from '../dtos'; @ApiTags('Community Module') @Controller({ @@ -45,6 +45,21 @@ export class CommunityController { return await this.communityService.createCommunity(param, addCommunityDto); } + @ApiBearerAuth() + @UseGuards(PermissionsGuard) + @Permissions('COMMUNITY_VIEW') + @ApiOperation({ + summary: ControllerRoute.COMMUNITY.ACTIONS.LIST_COMMUNITY_SUMMARY, + description: ControllerRoute.COMMUNITY.ACTIONS.LIST_COMMUNITY_DESCRIPTION, + }) + @Get('v2') + async getCommunitiesV2( + @Param() param: ProjectParam, + @Query() query: PaginationRequestWithSearchGetListDto, + ): Promise { + return this.communityService.getCommunitiesV2(param, query); + } + @ApiBearerAuth() @UseGuards(PermissionsGuard) @Permissions('COMMUNITY_VIEW') diff --git a/src/community/services/community.service.ts b/src/community/services/community.service.ts index fc33011..4b9720d 100644 --- a/src/community/services/community.service.ts +++ b/src/community/services/community.service.ts @@ -1,4 +1,7 @@ -import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant'; +import { + ORPHAN_COMMUNITY_NAME, + ORPHAN_SPACE_NAME, +} from '@app/common/constants/orphan-constant'; import { BaseResponseDto } from '@app/common/dto/base.response.dto'; import { PageResponse } from '@app/common/dto/pagination.response.dto'; import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; @@ -161,6 +164,64 @@ export class CommunityService { } } + async getCommunitiesV2( + { projectUuid }: ProjectParam, + { + search, + includeSpaces, + ...pageable + }: Partial, + ) { + try { + const project = await this.validateProject(projectUuid); + + let qb: undefined | SelectQueryBuilder = undefined; + + qb = this.communityRepository + .createQueryBuilder('c') + .where('c.project = :projectUuid', { projectUuid }) + .andWhere(`c.name != '${ORPHAN_COMMUNITY_NAME}-${project.name}'`) + .distinct(true); + + if (includeSpaces) { + qb.leftJoinAndSelect('c.spaces', 'space', 'space.disabled = false') + .leftJoinAndSelect('space.parent', 'parent') + .leftJoinAndSelect( + 'space.children', + 'children', + 'children.disabled = :disabled', + { disabled: false }, + ) + // .leftJoinAndSelect('space.spaceModel', 'spaceModel') + .andWhere('space.spaceName != :orphanSpaceName', { + orphanSpaceName: ORPHAN_SPACE_NAME, + }) + .andWhere('space.disabled = :disabled', { disabled: false }); + } + + if (search) { + qb.andWhere( + `c.name ILIKE '%${search}%' ${includeSpaces ? "OR space.space_name ILIKE '%" + search + "%'" : ''}`, + ); + } + + const customModel = TypeORMCustomModel(this.communityRepository); + + const { baseResponseDto, paginationResponseDto } = + await customModel.findAll({ ...pageable, modelName: 'community' }, qb); + return new PageResponse( + baseResponseDto, + paginationResponseDto, + ); + } catch (error) { + // Generic error handling + throw new HttpException( + error.message || 'An error occurred while fetching communities.', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + async updateCommunity( params: GetCommunityParams, updateCommunityDto: UpdateCommunityNameDto,