Add query parameter to filter spaces with devices

This commit is contained in:
faris Aljohari
2025-01-26 11:59:15 -06:00
parent c3c30828ad
commit 65e585c8f2
3 changed files with 40 additions and 6 deletions

View File

@ -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) {