mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +00:00
finished return the last three parents
This commit is contained in:
7
src/space/dtos/space.parents.dto.ts
Normal file
7
src/space/dtos/space.parents.dto.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { Expose } from 'class-transformer';
|
||||
|
||||
export class SpaceWithParentsDto extends SpaceEntity {
|
||||
@Expose()
|
||||
lastThreeParents: string;
|
||||
}
|
@ -36,6 +36,8 @@ import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { ProcessTagDto } from 'src/tags/dtos';
|
||||
import { SpaceProductAllocationService } from './space-product-allocation.service';
|
||||
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import { SpaceWithParentsDto } from '../dtos/space.parents.dto';
|
||||
@Injectable()
|
||||
export class SpaceService {
|
||||
constructor(
|
||||
@ -47,6 +49,7 @@ export class SpaceService {
|
||||
private readonly validationService: ValidationService,
|
||||
private readonly newTagService: NewTagService,
|
||||
private readonly spaceModelService: SpaceModelService,
|
||||
private readonly deviceService: DeviceService,
|
||||
private commandBus: CommandBus,
|
||||
private readonly spaceProductAllocationService: SpaceProductAllocationService,
|
||||
) {}
|
||||
@ -235,7 +238,25 @@ export class SpaceService {
|
||||
queryBuilder.innerJoin('space.devices', 'devices');
|
||||
}
|
||||
|
||||
const spaces = await queryBuilder.getMany();
|
||||
let spaces = await queryBuilder.getMany();
|
||||
|
||||
if (onlyWithDevices) {
|
||||
spaces = await Promise.all(
|
||||
spaces.map(async (space) => {
|
||||
const spaceHierarchy =
|
||||
await this.deviceService.getParentHierarchy(space);
|
||||
const parentHierarchy = spaceHierarchy
|
||||
.slice(0, 3)
|
||||
.map((space) => space.spaceName)
|
||||
.join(' - ');
|
||||
|
||||
return {
|
||||
...space,
|
||||
lastThreeParents: parentHierarchy,
|
||||
} as SpaceWithParentsDto;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const transformedSpaces = spaces.map(this.transformSpace);
|
||||
const spaceHierarchy = this.buildSpaceHierarchy(transformedSpaces);
|
||||
|
Reference in New Issue
Block a user