mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
Merge pull request #225 from SyncrowIOT/SP-1145-be-get-space-by-community-have-devices
Add query parameter to filter spaces with devices
This commit is contained in:
@ -9,6 +9,7 @@ import {
|
|||||||
Param,
|
Param,
|
||||||
Post,
|
Post,
|
||||||
Put,
|
Put,
|
||||||
|
Query,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { AddSpaceDto, CommunitySpaceParam, UpdateSpaceDto } from '../dtos';
|
import { AddSpaceDto, CommunitySpaceParam, UpdateSpaceDto } from '../dtos';
|
||||||
@ -16,6 +17,7 @@ import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
|||||||
import { GetSpaceParam } from '../dtos/get.space.param';
|
import { GetSpaceParam } from '../dtos/get.space.param';
|
||||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||||
|
import { GetSpaceDto } from '../dtos/get.space.dto';
|
||||||
|
|
||||||
@ApiTags('Space Module')
|
@ApiTags('Space Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -55,8 +57,12 @@ export class SpaceController {
|
|||||||
@Get()
|
@Get()
|
||||||
async getHierarchy(
|
async getHierarchy(
|
||||||
@Param() params: CommunitySpaceParam,
|
@Param() params: CommunitySpaceParam,
|
||||||
|
@Query() getSpaceDto: GetSpaceDto,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
return this.spaceService.getSpacesHierarchyForCommunity(params);
|
return this.spaceService.getSpacesHierarchyForCommunity(
|
||||||
|
params,
|
||||||
|
getSpaceDto,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
|
19
src/space/dtos/get.space.dto.ts
Normal file
19
src/space/dtos/get.space.dto.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { BooleanValues } from '@app/common/constants/boolean-values.enum';
|
||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { Transform } from 'class-transformer';
|
||||||
|
import { IsBoolean, IsOptional } from 'class-validator';
|
||||||
|
|
||||||
|
export class GetSpaceDto {
|
||||||
|
@ApiProperty({
|
||||||
|
example: true,
|
||||||
|
description: 'Only return spaces with devices',
|
||||||
|
required: false,
|
||||||
|
default: false,
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@IsBoolean()
|
||||||
|
@Transform((value) => {
|
||||||
|
return value.obj.onlyWithDevices === BooleanValues.TRUE;
|
||||||
|
})
|
||||||
|
public onlyWithDevices?: boolean = false;
|
||||||
|
}
|
@ -32,6 +32,7 @@ import { CommandBus } from '@nestjs/cqrs';
|
|||||||
import { TagService } from './tag';
|
import { TagService } from './tag';
|
||||||
import { SpaceModelService } from 'src/space-model/services';
|
import { SpaceModelService } from 'src/space-model/services';
|
||||||
import { DisableSpaceCommand } from '../commands';
|
import { DisableSpaceCommand } from '../commands';
|
||||||
|
import { GetSpaceDto } from '../dtos/get.space.dto';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceService {
|
export class SpaceService {
|
||||||
constructor(
|
constructor(
|
||||||
@ -167,15 +168,18 @@ export class SpaceService {
|
|||||||
|
|
||||||
async getSpacesHierarchyForCommunity(
|
async getSpacesHierarchyForCommunity(
|
||||||
params: CommunitySpaceParam,
|
params: CommunitySpaceParam,
|
||||||
|
getSpaceDto?: GetSpaceDto,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
const { communityUuid, projectUuid } = params;
|
const { communityUuid, projectUuid } = params;
|
||||||
|
const { onlyWithDevices } = getSpaceDto;
|
||||||
|
console.log('onlyWithDevices', onlyWithDevices);
|
||||||
|
|
||||||
await this.validationService.validateCommunityAndProject(
|
await this.validationService.validateCommunityAndProject(
|
||||||
communityUuid,
|
communityUuid,
|
||||||
projectUuid,
|
projectUuid,
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
// Get all spaces related to the community, including the parent-child relations
|
const queryBuilder = this.spaceRepository
|
||||||
const spaces = await this.spaceRepository
|
|
||||||
.createQueryBuilder('space')
|
.createQueryBuilder('space')
|
||||||
.leftJoinAndSelect('space.parent', 'parent')
|
.leftJoinAndSelect('space.parent', 'parent')
|
||||||
.leftJoinAndSelect(
|
.leftJoinAndSelect(
|
||||||
@ -214,14 +218,19 @@ export class SpaceService {
|
|||||||
.andWhere('space.spaceName != :orphanSpaceName', {
|
.andWhere('space.spaceName != :orphanSpaceName', {
|
||||||
orphanSpaceName: ORPHAN_SPACE_NAME,
|
orphanSpaceName: ORPHAN_SPACE_NAME,
|
||||||
})
|
})
|
||||||
.andWhere('space.disabled = :disabled', { disabled: false })
|
.andWhere('space.disabled = :disabled', { disabled: false });
|
||||||
.getMany();
|
|
||||||
|
if (onlyWithDevices) {
|
||||||
|
queryBuilder.innerJoin('space.devices', 'devices');
|
||||||
|
}
|
||||||
|
|
||||||
|
const spaces = await queryBuilder.getMany();
|
||||||
|
|
||||||
const spaceHierarchy = this.buildSpaceHierarchy(spaces);
|
const spaceHierarchy = this.buildSpaceHierarchy(spaces);
|
||||||
|
|
||||||
return new SuccessResponseDto({
|
return new SuccessResponseDto({
|
||||||
message: `Spaces in community ${communityUuid} successfully fetched in hierarchy`,
|
message: `Spaces in community ${communityUuid} successfully fetched in hierarchy`,
|
||||||
data: spaceHierarchy,
|
data: onlyWithDevices ? spaces : spaceHierarchy,
|
||||||
statusCode: HttpStatus.OK,
|
statusCode: HttpStatus.OK,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
Reference in New Issue
Block a user