diff --git a/src/space/controllers/space.controller.ts b/src/space/controllers/space.controller.ts index bb18516..ca47488 100644 --- a/src/space/controllers/space.controller.ts +++ b/src/space/controllers/space.controller.ts @@ -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 { - return this.spaceService.getSpacesHierarchyForCommunity(params); + return this.spaceService.getSpacesHierarchyForCommunity( + params, + getSpaceDto, + ); } @ApiBearerAuth() diff --git a/src/space/dtos/get.space.dto.ts b/src/space/dtos/get.space.dto.ts new file mode 100644 index 0000000..d0b6475 --- /dev/null +++ b/src/space/dtos/get.space.dto.ts @@ -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; +} diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index d60abb4..267d1d1 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -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 { 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) {