mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:54:54 +00:00
Add query parameter to filter spaces with devices
This commit is contained in:
@ -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