mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
Add utility function to associate space UUID with devices in community and device services
This commit is contained in:
11
libs/common/src/util/device-utils.ts
Normal file
11
libs/common/src/util/device-utils.ts
Normal 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;
|
||||||
|
});
|
||||||
|
}
|
@ -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) {
|
||||||
|
@ -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) {
|
||||||
|
@ -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);
|
||||||
|
Reference in New Issue
Block a user