mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 01:14:53 +00:00
feat: implement date formatting function and enhance PowerClampService with space-based data retrieval
This commit is contained in:
@ -1,6 +1,11 @@
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { GetSpaceParam } from '../dtos';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
@ -9,6 +14,9 @@ import { ValidationService } from './space-validation.service';
|
||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||
import { BatteryStatus } from '@app/common/constants/battery-status.enum';
|
||||
import { DeviceService } from 'src/device/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';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceDeviceService {
|
||||
@ -16,6 +24,7 @@ export class SpaceDeviceService {
|
||||
private readonly tuyaService: TuyaService,
|
||||
private readonly validationService: ValidationService,
|
||||
private readonly deviceService: DeviceService,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
) {}
|
||||
|
||||
async listDevicesInSpace(params: GetSpaceParam): Promise<BaseResponseDto> {
|
||||
@ -121,4 +130,37 @@ export class SpaceDeviceService {
|
||||
);
|
||||
return batteryStatus ? batteryStatus.value : null;
|
||||
}
|
||||
async getAllDevicesBySpace(spaceUuid: string): Promise<DeviceEntity[]> {
|
||||
const space = await this.spaceRepository.findOne({
|
||||
where: { uuid: spaceUuid },
|
||||
relations: ['children', 'devices'],
|
||||
});
|
||||
|
||||
if (!space) {
|
||||
throw new NotFoundException('Space not found');
|
||||
}
|
||||
|
||||
const allDevices: DeviceEntity[] = [...space.devices];
|
||||
|
||||
// Recursive fetch function
|
||||
const fetchChildren = async (parentSpace: SpaceEntity) => {
|
||||
const children = await this.spaceRepository.find({
|
||||
where: { parent: { uuid: parentSpace.uuid } },
|
||||
relations: ['children', 'devices'],
|
||||
});
|
||||
|
||||
for (const child of children) {
|
||||
allDevices.push(...child.devices);
|
||||
|
||||
if (child.children.length > 0) {
|
||||
await fetchChildren(child);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Start recursive fetch
|
||||
await fetchChildren(space);
|
||||
|
||||
return allDevices;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user