diff --git a/src/community/services/community.service.ts b/src/community/services/community.service.ts index 53bb74a..9f855aa 100644 --- a/src/community/services/community.service.ts +++ b/src/community/services/community.service.ts @@ -13,8 +13,9 @@ 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 { ILike, Not } from 'typeorm'; +import { ILike, In, Not } from 'typeorm'; import { SpaceService } from 'src/space/services'; +import { SpaceRepository } from '@app/common/modules/space'; @Injectable() export class CommunityService { @@ -23,6 +24,7 @@ export class CommunityService { private readonly projectRepository: ProjectRepository, private readonly spaceService: SpaceService, private readonly tuyaService: TuyaService, + private readonly spaceRepository: SpaceRepository, ) {} async createCommunity( @@ -96,7 +98,36 @@ export class CommunityService { }; if (pageable.search) { - pageable.where = { name: ILike(`%${pageable.search}%`) }; + const matchingCommunities = await this.communityRepository.find({ + where: { + project: { uuid: param.projectUuid }, + name: ILike(`%${pageable.search}%`), + }, + }); + + const matchingSpaces = await this.spaceRepository.find({ + where: { + spaceName: ILike(`%${pageable.search}%`), + community: { project: { uuid: param.projectUuid } }, + }, + relations: ['community'], + }); + + const spaceCommunityUuids = [ + ...new Set(matchingSpaces.map((space) => space.community.uuid)), + ]; + + const allMatchedCommunityUuids = [ + ...new Set([ + ...matchingCommunities.map((c) => c.uuid), + ...spaceCommunityUuids, + ]), + ]; + + pageable.where = { + ...pageable.where, + uuid: In(allMatchedCommunityUuids), + }; } const customModel = TypeORMCustomModel(this.communityRepository); @@ -114,7 +145,6 @@ export class CommunityService { }, { onlyWithDevices: false, - search: pageable.search, }, );