diff --git a/libs/common/src/util/device-utils.ts b/libs/common/src/util/device-utils.ts new file mode 100644 index 0000000..9a930ae --- /dev/null +++ b/libs/common/src/util/device-utils.ts @@ -0,0 +1,11 @@ +import { DeviceEntity } from '../modules/device/entities'; + +export function addSpaceUuidToDevices( + devices: DeviceEntity[], + spaceUuid: string, +): DeviceEntity[] { + return devices.map((device) => { + (device as any).spaceUuid = spaceUuid; + return device; + }); +} diff --git a/src/community/services/community.service.ts b/src/community/services/community.service.ts index 542926c..f3b7f28 100644 --- a/src/community/services/community.service.ts +++ b/src/community/services/community.service.ts @@ -23,6 +23,7 @@ import { SpaceService } from 'src/space/services'; import { SpaceRepository } from '@app/common/modules/space'; import { DeviceEntity } from '@app/common/modules/device/entities'; import { SpaceEntity } from '@app/common/modules/space/entities/space.entity'; +import { addSpaceUuidToDevices } from '@app/common/util/device-utils'; @Injectable() export class CommunityService { @@ -336,7 +337,7 @@ export class CommunityService { visitedSpaceUuids.add(space.uuid); if (space.devices?.length) { - allDevices.push(...space.devices); + allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid)); } if (space.children?.length) { diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index 4d4efbb..f502816 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -67,6 +67,7 @@ import { ProjectParam } from '../dtos'; import { BatchDeviceTypeEnum } from '@app/common/constants/batch-device.enum'; import { DeviceTypeEnum } from '@app/common/constants/device-type.enum'; import { CommunityRepository } from '@app/common/modules/community/repositories'; +import { addSpaceUuidToDevices } from '@app/common/util/device-utils'; @Injectable() export class DeviceService { @@ -1786,7 +1787,8 @@ export class DeviceService { throw new NotFoundException('Space not found'); } - const allDevices: DeviceEntity[] = [...space.devices]; + const allDevices: DeviceEntity[] = []; + allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid)); // Recursive fetch function const fetchChildren = async (parentSpace: SpaceEntity) => { @@ -1796,7 +1798,7 @@ export class DeviceService { }); for (const child of children) { - allDevices.push(...child.devices); + allDevices.push(...addSpaceUuidToDevices(child.devices, child.uuid)); if (child.children.length > 0) { await fetchChildren(child); @@ -1835,7 +1837,7 @@ export class DeviceService { visitedSpaceUuids.add(space.uuid); if (space.devices?.length) { - allDevices.push(...space.devices); + allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid)); } if (space.children?.length) { diff --git a/src/space/services/space-device.service.ts b/src/space/services/space-device.service.ts index 49d965d..8f8a2cc 100644 --- a/src/space/services/space-device.service.ts +++ b/src/space/services/space-device.service.ts @@ -18,6 +18,7 @@ import { SpaceRepository } from '@app/common/modules/space'; import { DeviceEntity } from '@app/common/modules/device/entities'; import { SpaceEntity } from '@app/common/modules/space/entities/space.entity'; import { GetDevicesBySpaceDto } from '../dtos/device.space.dto'; +import { addSpaceUuidToDevices } from '@app/common/util/device-utils'; @Injectable() export class SpaceDeviceService { @@ -160,7 +161,8 @@ export class SpaceDeviceService { throw new NotFoundException('Space not found'); } - const allDevices: DeviceEntity[] = [...space.devices]; + const allDevices: DeviceEntity[] = []; + allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid)); // Recursive fetch function const fetchChildren = async (parentSpace: SpaceEntity) => { @@ -170,7 +172,7 @@ export class SpaceDeviceService { }); for (const child of children) { - allDevices.push(...child.devices); + allDevices.push(...addSpaceUuidToDevices(child.devices, child.uuid)); if (child.children.length > 0) { await fetchChildren(child);