mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
Merge branch 'dev'
This commit is contained in:
@ -1021,10 +1021,13 @@ export class DeviceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const spaceHierarchy = await this.getFullSpaceHierarchy(
|
const spaceHierarchy = await this.getParentHierarchy(
|
||||||
device?.spaceDevice,
|
device?.spaceDevice,
|
||||||
);
|
);
|
||||||
const orderedHierarchy = spaceHierarchy.reverse();
|
const orderedHierarchy = [
|
||||||
|
device?.spaceDevice,
|
||||||
|
...spaceHierarchy.reverse(),
|
||||||
|
];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
spaces: orderedHierarchy.map((space) => ({
|
spaces: orderedHierarchy.map((space) => ({
|
||||||
@ -1515,4 +1518,32 @@ export class DeviceService {
|
|||||||
}
|
}
|
||||||
return project;
|
return project;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getParentHierarchy(
|
||||||
|
space: SpaceEntity,
|
||||||
|
): Promise<{ uuid: string; spaceName: string }[]> {
|
||||||
|
try {
|
||||||
|
const targetSpace = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: space.uuid },
|
||||||
|
relations: ['parent'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!targetSpace) {
|
||||||
|
throw new HttpException('Space not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
const ancestors = await this.fetchAncestors(targetSpace);
|
||||||
|
|
||||||
|
return ancestors.map((parentSpace) => ({
|
||||||
|
uuid: parentSpace.uuid,
|
||||||
|
spaceName: parentSpace.spaceName,
|
||||||
|
}));
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching parent hierarchy:', error.message);
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching parent hierarchy',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,10 +32,10 @@ export class SpaceDeviceService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch space hierarchy **once** and reverse it to get an ordered hierarchy
|
|
||||||
const spaceHierarchy =
|
const spaceHierarchy =
|
||||||
await this.validationService.getFullSpaceHierarchy(space);
|
await this.validationService.getParentHierarchy(space);
|
||||||
const orderedHierarchy = spaceHierarchy.reverse();
|
|
||||||
|
const orderedHierarchy = [space, ...spaceHierarchy.reverse()];
|
||||||
|
|
||||||
// Fetch Tuya details for each device in parallel using Promise.allSettled
|
// Fetch Tuya details for each device in parallel using Promise.allSettled
|
||||||
const deviceDetailsPromises = space.devices.map((device) =>
|
const deviceDetailsPromises = space.devices.map((device) =>
|
||||||
|
@ -222,4 +222,32 @@ export class ValidationService {
|
|||||||
|
|
||||||
return descendants;
|
return descendants;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getParentHierarchy(
|
||||||
|
space: SpaceEntity,
|
||||||
|
): Promise<{ uuid: string; spaceName: string }[]> {
|
||||||
|
try {
|
||||||
|
const targetSpace = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: space.uuid },
|
||||||
|
relations: ['parent'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!targetSpace) {
|
||||||
|
throw new HttpException('Space not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
const ancestors = await this.fetchAncestors(targetSpace);
|
||||||
|
|
||||||
|
return ancestors.map((parentSpace) => ({
|
||||||
|
uuid: parentSpace.uuid,
|
||||||
|
spaceName: parentSpace.spaceName,
|
||||||
|
}));
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching parent hierarchy:', error.message);
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching parent hierarchy',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user