feat: implement date formatting function and enhance PowerClampService with space-based data retrieval

This commit is contained in:
faris Aljohari
2025-05-04 22:28:38 +03:00
parent e932d8a4a4
commit d197bf2bb4
9 changed files with 271 additions and 19 deletions

View File

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