finished return the last three parents

This commit is contained in:
faris Aljohari
2025-03-19 00:50:24 +03:00
parent ec851eda9d
commit 1a313c103d
4 changed files with 65 additions and 1 deletions

View 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;
}

View File

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