From 30badcde67595659f0a506cac2e2f98589e285df Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Tue, 18 Mar 2025 21:20:27 +0400 Subject: [PATCH 1/2] add where for community name --- .../src/dto/pagination-with-search.request.dto.ts | 13 +++++++++++++ libs/common/src/models/typeOrmCustom.model.ts | 6 ++++++ src/community/controllers/community.controller.ts | 4 ++-- src/community/services/community.service.ts | 9 ++++++--- 4 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 libs/common/src/dto/pagination-with-search.request.dto.ts diff --git a/libs/common/src/dto/pagination-with-search.request.dto.ts b/libs/common/src/dto/pagination-with-search.request.dto.ts new file mode 100644 index 0000000..9cf5f4c --- /dev/null +++ b/libs/common/src/dto/pagination-with-search.request.dto.ts @@ -0,0 +1,13 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsOptional } from 'class-validator'; +import { PaginationRequestGetListDto } from './pagination.request.dto'; + +export class PaginationRequestWithSearchGetListDto extends PaginationRequestGetListDto { + @IsOptional() + @ApiProperty({ + name: 'search', + required: false, + description: 'Search for community or space name', + }) + search?: string; +} diff --git a/libs/common/src/models/typeOrmCustom.model.ts b/libs/common/src/models/typeOrmCustom.model.ts index a7b97b0..4d20cf1 100644 --- a/libs/common/src/models/typeOrmCustom.model.ts +++ b/libs/common/src/models/typeOrmCustom.model.ts @@ -18,6 +18,12 @@ export interface TypeORMCustomModelFindAllQuery { includeDisable?: boolean | string; includeSpaces?: boolean; } + +export interface ExtendedTypeORMCustomModelFindAllQuery + extends TypeORMCustomModelFindAllQuery { + search?: string; +} + interface CustomFindAllQuery { page?: number; size?: number; diff --git a/src/community/controllers/community.controller.ts b/src/community/controllers/community.controller.ts index e823989..a6b269f 100644 --- a/src/community/controllers/community.controller.ts +++ b/src/community/controllers/community.controller.ts @@ -17,10 +17,10 @@ import { UpdateCommunityNameDto } from '../dtos/update.community.dto'; // 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 { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.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'; @ApiTags('Community Module') @Controller({ @@ -70,7 +70,7 @@ export class CommunityController { @Get() async getCommunities( @Param() param: ProjectParam, - @Query() query: PaginationRequestGetListDto, + @Query() query: PaginationRequestWithSearchGetListDto, ): Promise { return this.communityService.getCommunities(param, query); } diff --git a/src/community/services/community.service.ts b/src/community/services/community.service.ts index a627673..522e7a6 100644 --- a/src/community/services/community.service.ts +++ b/src/community/services/community.service.ts @@ -3,8 +3,8 @@ import { AddCommunityDto, GetCommunityParams, ProjectParam } from '../dtos'; import { UpdateCommunityNameDto } from '../dtos/update.community.dto'; import { BaseResponseDto } from '@app/common/dto/base.response.dto'; import { + ExtendedTypeORMCustomModelFindAllQuery, TypeORMCustomModel, - TypeORMCustomModelFindAllQuery, } from '@app/common/models/typeOrmCustom.model'; import { PageResponse } from '@app/common/dto/pagination.response.dto'; import { CommunityRepository } from '@app/common/modules/community/repositories'; @@ -13,7 +13,7 @@ import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service'; import { ProjectRepository } from '@app/common/modules/project/repositiories'; import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant'; -import { Not } from 'typeorm'; +import { ILike, Not } from 'typeorm'; import { SpaceService } from 'src/space/services'; @Injectable() @@ -84,7 +84,7 @@ export class CommunityService { async getCommunities( param: ProjectParam, - pageable: Partial, + pageable: Partial, ): Promise { try { const project = await this.validateProject(param.projectUuid); @@ -95,6 +95,9 @@ export class CommunityService { name: Not(`${ORPHAN_COMMUNITY_NAME}-${project.name}`), }; + if (pageable.search) { + pageable.where = { name: ILike(`%${pageable.search}%`) }; + } const customModel = TypeORMCustomModel(this.communityRepository); const { baseResponseDto, paginationResponseDto } = From b0a403b0cf743e16e2e9c91e5a51bf22a05689c2 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Wed, 19 Mar 2025 10:51:38 +0400 Subject: [PATCH 2/2] added search for spaces --- src/community/services/community.service.ts | 1 + src/space/services/space.service.ts | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/community/services/community.service.ts b/src/community/services/community.service.ts index 522e7a6..53bb74a 100644 --- a/src/community/services/community.service.ts +++ b/src/community/services/community.service.ts @@ -114,6 +114,7 @@ export class CommunityService { }, { onlyWithDevices: false, + search: pageable.search, }, ); diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index 682292c..3cdd61e 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -185,10 +185,10 @@ export class SpaceService { async getSpacesHierarchyForCommunity( params: CommunitySpaceParam, - getSpaceDto?: GetSpaceDto, + getSpaceDto?: GetSpaceDto & { search?: string }, ): Promise { const { communityUuid, projectUuid } = params; - const { onlyWithDevices } = getSpaceDto; + const { onlyWithDevices, search } = getSpaceDto; await this.validationService.validateCommunityAndProject( communityUuid, projectUuid, @@ -231,6 +231,13 @@ export class SpaceService { }) .andWhere('space.disabled = :disabled', { disabled: false }); + if (search) { + queryBuilder.andWhere( + '(space.spaceName ILIKE :search OR parent.spaceName ILIKE :search)', + { search: `%${search}%` }, + ); + } + if (onlyWithDevices) { queryBuilder.innerJoin('space.devices', 'devices'); }