diff --git a/src/community/services/community.service.ts b/src/community/services/community.service.ts index 5de34fa..3b213f9 100644 --- a/src/community/services/community.service.ts +++ b/src/community/services/community.service.ts @@ -207,7 +207,8 @@ export class CommunityService { if (search) { qb.andWhere( - `c.name ILIKE '%${search}%' ${includeSpaces ? "OR space.space_name ILIKE '%" + search + "%'" : ''}`, + `c.name ILIKE :search ${includeSpaces ? 'OR space.space_name ILIKE :search' : ''}`, + { search: `%${search}%` }, ); } @@ -215,12 +216,22 @@ export class CommunityService { const { baseResponseDto, paginationResponseDto } = await customModel.findAll({ ...pageable, modelName: 'community' }, qb); + + if (includeSpaces) { + baseResponseDto.data = baseResponseDto.data.map((community) => ({ + ...community, + spaces: this.spaceService.buildSpaceHierarchy(community.spaces || []), + })); + } return new PageResponse( baseResponseDto, paginationResponseDto, ); } catch (error) { // Generic error handling + if (error instanceof HttpException) { + throw error; + } throw new HttpException( error.message || 'An error occurred while fetching communities.', HttpStatus.INTERNAL_SERVER_ERROR, diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index fb08b59..cbbe953 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -681,7 +681,7 @@ export class SpaceService { } } - private buildSpaceHierarchy(spaces: SpaceEntity[]): SpaceEntity[] { + buildSpaceHierarchy(spaces: SpaceEntity[]): SpaceEntity[] { const map = new Map(); // Step 1: Create a map of spaces by UUID