mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 02:15:21 +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,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
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 { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||
import { GetSpaceDto } from '../dtos/get.space.dto';
|
||||
|
||||
@ApiTags('Space Module')
|
||||
@Controller({
|
||||
@ -55,8 +57,12 @@ export class SpaceController {
|
||||
@Get()
|
||||
async getHierarchy(
|
||||
@Param() params: CommunitySpaceParam,
|
||||
@Query() getSpaceDto: GetSpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return this.spaceService.getSpacesHierarchyForCommunity(params);
|
||||
return this.spaceService.getSpacesHierarchyForCommunity(
|
||||
params,
|
||||
getSpaceDto,
|
||||
);
|
||||
}
|
||||
|
||||
@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 { SpaceModelService } from 'src/space-model/services';
|
||||
import { DisableSpaceCommand } from '../commands';
|
||||
import { GetSpaceDto } from '../dtos/get.space.dto';
|
||||
@Injectable()
|
||||
export class SpaceService {
|
||||
constructor(
|
||||
@ -167,15 +168,18 @@ export class SpaceService {
|
||||
|
||||
async getSpacesHierarchyForCommunity(
|
||||
params: CommunitySpaceParam,
|
||||
getSpaceDto?: GetSpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { communityUuid, projectUuid } = params;
|
||||
const { onlyWithDevices } = getSpaceDto;
|
||||
console.log('onlyWithDevices', onlyWithDevices);
|
||||
|
||||
await this.validationService.validateCommunityAndProject(
|
||||
communityUuid,
|
||||
projectUuid,
|
||||
);
|
||||
try {
|
||||
// Get all spaces related to the community, including the parent-child relations
|
||||
const spaces = await this.spaceRepository
|
||||
const queryBuilder = this.spaceRepository
|
||||
.createQueryBuilder('space')
|
||||
.leftJoinAndSelect('space.parent', 'parent')
|
||||
.leftJoinAndSelect(
|
||||
@ -214,14 +218,19 @@ export class SpaceService {
|
||||
.andWhere('space.spaceName != :orphanSpaceName', {
|
||||
orphanSpaceName: ORPHAN_SPACE_NAME,
|
||||
})
|
||||
.andWhere('space.disabled = :disabled', { disabled: false })
|
||||
.getMany();
|
||||
.andWhere('space.disabled = :disabled', { disabled: false });
|
||||
|
||||
if (onlyWithDevices) {
|
||||
queryBuilder.innerJoin('space.devices', 'devices');
|
||||
}
|
||||
|
||||
const spaces = await queryBuilder.getMany();
|
||||
|
||||
const spaceHierarchy = this.buildSpaceHierarchy(spaces);
|
||||
|
||||
return new SuccessResponseDto({
|
||||
message: `Spaces in community ${communityUuid} successfully fetched in hierarchy`,
|
||||
data: spaceHierarchy,
|
||||
data: onlyWithDevices ? spaces : spaceHierarchy,
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
} catch (error) {
|
||||
|
Reference in New Issue
Block a user