Add utility function to associate space UUID with devices in community and device services

This commit is contained in:
faris Aljohari
2025-06-01 21:56:08 -06:00
parent 81e017430e
commit 0fe6c80731
4 changed files with 22 additions and 6 deletions

View File

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

View File

@ -23,6 +23,7 @@ import { SpaceService } from 'src/space/services';
import { SpaceRepository } from '@app/common/modules/space'; import { SpaceRepository } from '@app/common/modules/space';
import { DeviceEntity } from '@app/common/modules/device/entities'; import { DeviceEntity } from '@app/common/modules/device/entities';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity'; import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
@Injectable() @Injectable()
export class CommunityService { export class CommunityService {
@ -336,7 +337,7 @@ export class CommunityService {
visitedSpaceUuids.add(space.uuid); visitedSpaceUuids.add(space.uuid);
if (space.devices?.length) { if (space.devices?.length) {
allDevices.push(...space.devices); allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid));
} }
if (space.children?.length) { if (space.children?.length) {

View File

@ -67,6 +67,7 @@ import { ProjectParam } from '../dtos';
import { BatchDeviceTypeEnum } from '@app/common/constants/batch-device.enum'; import { BatchDeviceTypeEnum } from '@app/common/constants/batch-device.enum';
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum'; import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
import { CommunityRepository } from '@app/common/modules/community/repositories'; import { CommunityRepository } from '@app/common/modules/community/repositories';
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
@Injectable() @Injectable()
export class DeviceService { export class DeviceService {
@ -1786,7 +1787,8 @@ export class DeviceService {
throw new NotFoundException('Space not found'); throw new NotFoundException('Space not found');
} }
const allDevices: DeviceEntity[] = [...space.devices]; const allDevices: DeviceEntity[] = [];
allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid));
// Recursive fetch function // Recursive fetch function
const fetchChildren = async (parentSpace: SpaceEntity) => { const fetchChildren = async (parentSpace: SpaceEntity) => {
@ -1796,7 +1798,7 @@ export class DeviceService {
}); });
for (const child of children) { for (const child of children) {
allDevices.push(...child.devices); allDevices.push(...addSpaceUuidToDevices(child.devices, child.uuid));
if (child.children.length > 0) { if (child.children.length > 0) {
await fetchChildren(child); await fetchChildren(child);
@ -1835,7 +1837,7 @@ export class DeviceService {
visitedSpaceUuids.add(space.uuid); visitedSpaceUuids.add(space.uuid);
if (space.devices?.length) { if (space.devices?.length) {
allDevices.push(...space.devices); allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid));
} }
if (space.children?.length) { if (space.children?.length) {

View File

@ -18,6 +18,7 @@ import { SpaceRepository } from '@app/common/modules/space';
import { DeviceEntity } from '@app/common/modules/device/entities'; import { DeviceEntity } from '@app/common/modules/device/entities';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity'; import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { GetDevicesBySpaceDto } from '../dtos/device.space.dto'; import { GetDevicesBySpaceDto } from '../dtos/device.space.dto';
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
@Injectable() @Injectable()
export class SpaceDeviceService { export class SpaceDeviceService {
@ -160,7 +161,8 @@ export class SpaceDeviceService {
throw new NotFoundException('Space not found'); throw new NotFoundException('Space not found');
} }
const allDevices: DeviceEntity[] = [...space.devices]; const allDevices: DeviceEntity[] = [];
allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid));
// Recursive fetch function // Recursive fetch function
const fetchChildren = async (parentSpace: SpaceEntity) => { const fetchChildren = async (parentSpace: SpaceEntity) => {
@ -170,7 +172,7 @@ export class SpaceDeviceService {
}); });
for (const child of children) { for (const child of children) {
allDevices.push(...child.devices); allDevices.push(...addSpaceUuidToDevices(child.devices, child.uuid));
if (child.children.length > 0) { if (child.children.length > 0) {
await fetchChildren(child); await fetchChildren(child);