updated the search for commuities

This commit is contained in:
hannathkadher
2025-04-06 21:14:16 +04:00
parent 5e2e95b098
commit e04f68a7ac

View File

@ -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,
},
);