mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-14 09:57:28 +00:00
Merge pull request #319 from SyncrowIOT/SP-1322
added search for spaces
This commit is contained in:
13
libs/common/src/dto/pagination-with-search.request.dto.ts
Normal file
13
libs/common/src/dto/pagination-with-search.request.dto.ts
Normal file
@ -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;
|
||||
}
|
@ -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;
|
||||
|
@ -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<BaseResponseDto> {
|
||||
return this.communityService.getCommunities(param, query);
|
||||
}
|
||||
|
@ -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<TypeORMCustomModelFindAllQuery>,
|
||||
pageable: Partial<ExtendedTypeORMCustomModelFindAllQuery>,
|
||||
): Promise<BaseResponseDto> {
|
||||
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 } =
|
||||
@ -111,6 +114,7 @@ export class CommunityService {
|
||||
},
|
||||
{
|
||||
onlyWithDevices: false,
|
||||
search: pageable.search,
|
||||
},
|
||||
);
|
||||
|
||||
|
@ -188,10 +188,10 @@ export class SpaceService {
|
||||
|
||||
async getSpacesHierarchyForCommunity(
|
||||
params: CommunitySpaceParam,
|
||||
getSpaceDto?: GetSpaceDto,
|
||||
getSpaceDto?: GetSpaceDto & { search?: string },
|
||||
): Promise<BaseResponseDto> {
|
||||
const { communityUuid, projectUuid } = params;
|
||||
const { onlyWithDevices } = getSpaceDto;
|
||||
const { onlyWithDevices, search } = getSpaceDto;
|
||||
await this.validationService.validateCommunityAndProject(
|
||||
communityUuid,
|
||||
projectUuid,
|
||||
@ -234,6 +234,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');
|
||||
}
|
||||
|
Reference in New Issue
Block a user