mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
Revert "DATA-date-param-moved"
This commit is contained in:
@ -1,9 +1,4 @@
|
||||
import {
|
||||
Injectable,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { AddCommunityDto, GetCommunityParams, ProjectParam } from '../dtos';
|
||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
@ -21,8 +16,6 @@ import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant';
|
||||
import { ILike, In, Not } from 'typeorm';
|
||||
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';
|
||||
|
||||
@Injectable()
|
||||
export class CommunityService {
|
||||
@ -310,58 +303,4 @@ export class CommunityService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async getAllDevicesByCommunity(
|
||||
communityUuid: string,
|
||||
): Promise<DeviceEntity[]> {
|
||||
// Fetch the community and its top-level spaces
|
||||
const community = await this.communityRepository.findOne({
|
||||
where: { uuid: communityUuid },
|
||||
relations: [
|
||||
'spaces',
|
||||
'spaces.children',
|
||||
'spaces.devices',
|
||||
'spaces.devices.productDevice',
|
||||
],
|
||||
});
|
||||
|
||||
if (!community) {
|
||||
throw new NotFoundException('Community not found');
|
||||
}
|
||||
|
||||
const allDevices: DeviceEntity[] = [];
|
||||
|
||||
// Recursive fetch function for spaces
|
||||
const fetchSpaceDevices = async (space: SpaceEntity) => {
|
||||
if (space.devices && space.devices.length > 0) {
|
||||
allDevices.push(...space.devices);
|
||||
}
|
||||
|
||||
if (space.children && space.children.length > 0) {
|
||||
for (const childSpace of space.children) {
|
||||
const fullChildSpace = await this.spaceRepository.findOne({
|
||||
where: { uuid: childSpace.uuid },
|
||||
relations: ['children', 'devices', 'devices.productDevice'],
|
||||
});
|
||||
|
||||
if (fullChildSpace) {
|
||||
await fetchSpaceDevices(fullChildSpace);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Start recursive fetch for all top-level spaces
|
||||
for (const space of community.spaces) {
|
||||
const fullSpace = await this.spaceRepository.findOne({
|
||||
where: { uuid: space.uuid },
|
||||
relations: ['children', 'devices', 'devices.productDevice'],
|
||||
});
|
||||
|
||||
if (fullSpace) {
|
||||
await fetchSpaceDevices(fullSpace);
|
||||
}
|
||||
}
|
||||
|
||||
return allDevices;
|
||||
}
|
||||
}
|
||||
|
@ -4,21 +4,14 @@ import {
|
||||
ApiBearerAuth,
|
||||
ApiOperation,
|
||||
ApiParam,
|
||||
ApiQuery,
|
||||
} from '@nestjs/swagger';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { PowerClampService } from '../services/power-clamp.service';
|
||||
import {
|
||||
GetPowerClampBySpaceDto,
|
||||
GetPowerClampDto,
|
||||
} from '../dto/get-power-clamp.dto';
|
||||
import { GetPowerClampDto } from '../dto/get-power-clamp.dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import {
|
||||
PowerClampParamsDto,
|
||||
ResourceParamsDto,
|
||||
} from '../dto/power-clamp-params.dto';
|
||||
import { PowerClampParamsDto } from '../dto/power-clamp-params.dto';
|
||||
|
||||
@ApiTags('Power Clamp Module')
|
||||
@Controller({
|
||||
@ -46,34 +39,4 @@ export class PowerClampController {
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.powerClampService.getPowerClampData(params, query);
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('historical')
|
||||
@ApiOperation({
|
||||
summary:
|
||||
ControllerRoute.PowerClamp.ACTIONS
|
||||
.GET_ENERGY_BY_COMMUNITY_OR_SPACE_SUMMARY,
|
||||
description:
|
||||
ControllerRoute.PowerClamp.ACTIONS
|
||||
.GET_ENERGY_BY_COMMUNITY_OR_SPACE_DESCRIPTION,
|
||||
})
|
||||
@ApiQuery({
|
||||
name: 'spaceUuid',
|
||||
description: 'UUID of the Space',
|
||||
required: false,
|
||||
})
|
||||
@ApiQuery({
|
||||
name: 'communityUuid',
|
||||
description: 'UUID of the Community',
|
||||
required: false,
|
||||
})
|
||||
async getPowerClampDataBySpaceOrCommunity(
|
||||
@Query() params: ResourceParamsDto,
|
||||
@Query() query: GetPowerClampBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.powerClampService.getPowerClampDataBySpaceOrCommunity(
|
||||
params,
|
||||
query,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsOptional, IsDateString, Matches, IsNotEmpty } from 'class-validator';
|
||||
import { IsOptional, IsDateString, Matches } from 'class-validator';
|
||||
|
||||
export class GetPowerClampDto {
|
||||
@ApiPropertyOptional({
|
||||
@ -33,13 +33,3 @@ export class GetPowerClampDto {
|
||||
})
|
||||
year?: string;
|
||||
}
|
||||
export class GetPowerClampBySpaceDto {
|
||||
@ApiPropertyOptional({
|
||||
description: 'monthDate must be in YYYY-MM format',
|
||||
example: '2025-04',
|
||||
required: true,
|
||||
})
|
||||
@IsDateString()
|
||||
@IsNotEmpty()
|
||||
public monthDate: string;
|
||||
}
|
||||
|
@ -1,23 +1,6 @@
|
||||
import { IsNotEmpty, IsOptional, IsUUID, ValidateIf } from 'class-validator';
|
||||
import { IsUUID } from 'class-validator';
|
||||
|
||||
export class PowerClampParamsDto {
|
||||
@IsUUID('4', { message: 'Invalid UUID format' })
|
||||
powerClampUuid: string;
|
||||
}
|
||||
export class SpaceParamsDto {
|
||||
@IsUUID('4', { message: 'Invalid UUID format' })
|
||||
spaceUuid: string;
|
||||
}
|
||||
export class ResourceParamsDto {
|
||||
@IsUUID('4', { message: 'Invalid UUID format' })
|
||||
@IsOptional()
|
||||
spaceUuid?: string;
|
||||
|
||||
@IsUUID('4', { message: 'Invalid UUID format' })
|
||||
@IsOptional()
|
||||
communityUuid?: string;
|
||||
|
||||
@ValidateIf((o) => !o.spaceUuid && !o.communityUuid)
|
||||
@IsNotEmpty({ message: 'Either spaceUuid or communityUuid must be provided' })
|
||||
requireEither?: never; // This ensures at least one of them is provided
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { PowerClampService as PowerClamp } from './services/power-clamp.service';
|
||||
import { PowerClampService } from './services/power-clamp.service';
|
||||
import { PowerClampController } from './controllers';
|
||||
import {
|
||||
PowerClampDailyRepository,
|
||||
@ -8,108 +8,16 @@ import {
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import {
|
||||
SpaceDeviceService,
|
||||
SpaceLinkService,
|
||||
SpaceService,
|
||||
SubspaceDeviceService,
|
||||
SubSpaceService,
|
||||
ValidationService,
|
||||
} from 'src/space/services';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import {
|
||||
InviteSpaceRepository,
|
||||
SpaceLinkRepository,
|
||||
SpaceProductAllocationRepository,
|
||||
SpaceRepository,
|
||||
TagRepository,
|
||||
} from '@app/common/modules/space';
|
||||
import { CommunityService } from 'src/community/services';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import {
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SpaceModelRepository,
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
SubspaceModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import { TagService } from 'src/tags/services';
|
||||
import {
|
||||
SpaceModelService,
|
||||
SubSpaceModelService,
|
||||
} from 'src/space-model/services';
|
||||
import { SpaceProductAllocationService } from 'src/space/services/space-product-allocation.service';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import {
|
||||
SubspaceProductAllocationRepository,
|
||||
SubspaceRepository,
|
||||
} from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { SubspaceProductAllocationService } from 'src/space/services/subspace/subspace-product-allocation.service';
|
||||
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
|
||||
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
|
||||
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
controllers: [PowerClampController],
|
||||
providers: [
|
||||
PowerClamp,
|
||||
PowerClampService,
|
||||
PowerClampDailyRepository,
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
DeviceRepository,
|
||||
SpaceDeviceService,
|
||||
TuyaService,
|
||||
ValidationService,
|
||||
DeviceService,
|
||||
SpaceRepository,
|
||||
CommunityService,
|
||||
ProjectRepository,
|
||||
CommunityRepository,
|
||||
SpaceModelRepository,
|
||||
SceneDeviceRepository,
|
||||
ProductRepository,
|
||||
DeviceStatusFirebaseService,
|
||||
SceneService,
|
||||
SpaceService,
|
||||
PowerClampService,
|
||||
DeviceStatusLogRepository,
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
AutomationRepository,
|
||||
InviteSpaceRepository,
|
||||
SpaceLinkService,
|
||||
SubSpaceService,
|
||||
TagService,
|
||||
SpaceModelService,
|
||||
SpaceProductAllocationService,
|
||||
SqlLoaderService,
|
||||
SpaceLinkRepository,
|
||||
SubspaceRepository,
|
||||
SubspaceDeviceService,
|
||||
SubspaceProductAllocationService,
|
||||
NewTagRepository,
|
||||
SubSpaceModelService,
|
||||
SpaceModelProductAllocationService,
|
||||
SpaceProductAllocationRepository,
|
||||
SubspaceProductAllocationRepository,
|
||||
TagRepository,
|
||||
SubspaceModelRepository,
|
||||
SubspaceModelProductAllocationService,
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
],
|
||||
exports: [PowerClamp],
|
||||
exports: [PowerClampService],
|
||||
})
|
||||
export class PowerClampModule {}
|
||||
|
@ -1,32 +1,13 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
GetPowerClampBySpaceDto,
|
||||
GetPowerClampDto,
|
||||
} from '../dto/get-power-clamp.dto';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { GetPowerClampDto } from '../dto/get-power-clamp.dto';
|
||||
import {
|
||||
PowerClampDailyRepository,
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import {
|
||||
PowerClampParamsDto,
|
||||
ResourceParamsDto,
|
||||
} from '../dto/power-clamp-params.dto';
|
||||
import { PowerClampParamsDto } from '../dto/power-clamp-params.dto';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { SpaceDeviceService } from 'src/space/services';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||
import { filterByMonth, toMMYYYY } from '@app/common/helper/date-format';
|
||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||
import { CommunityService } from 'src/community/services';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
|
||||
@Injectable()
|
||||
export class PowerClampService {
|
||||
@ -35,85 +16,8 @@ export class PowerClampService {
|
||||
private readonly powerClampHourlyRepository: PowerClampHourlyRepository,
|
||||
private readonly powerClampMonthlyRepository: PowerClampMonthlyRepository,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
private readonly spaceDeviceService: SpaceDeviceService,
|
||||
private readonly sqlLoader: SqlLoaderService,
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly communityService: CommunityService,
|
||||
) {}
|
||||
|
||||
async getPowerClampDataBySpaceOrCommunity(
|
||||
params: ResourceParamsDto,
|
||||
query: GetPowerClampBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { monthDate } = query;
|
||||
const { spaceUuid, communityUuid } = params;
|
||||
|
||||
try {
|
||||
// Validate we have at least one identifier
|
||||
if (!spaceUuid && !communityUuid) {
|
||||
throw new BadRequestException(
|
||||
'Either spaceUuid or communityUuid must be provided',
|
||||
);
|
||||
}
|
||||
|
||||
// Get devices based on space or community
|
||||
const devices = spaceUuid
|
||||
? await this.spaceDeviceService.getAllDevicesBySpace(spaceUuid)
|
||||
: await this.communityService.getAllDevicesByCommunity(communityUuid);
|
||||
|
||||
if (!devices?.length) {
|
||||
return this.buildResponse(
|
||||
'No power clamp devices found for the specified criteria',
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
||||
// Filter and prepare device UUIDs
|
||||
const deviceUuids = devices
|
||||
.filter((device) => device.productDevice?.prodType === ProductType.PC)
|
||||
.map((device) => device.uuid);
|
||||
|
||||
if (deviceUuids.length === 0) {
|
||||
return this.buildResponse(
|
||||
'No power clamp devices (PC type) found for the specified criteria',
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
||||
// Execute procedure
|
||||
const formattedMonthDate = toMMYYYY(monthDate);
|
||||
const data = await this.executeProcedure(
|
||||
'fact_daily_space_energy_consumed_procedure',
|
||||
[formattedMonthDate, deviceUuids.join(',')],
|
||||
);
|
||||
|
||||
// Format and filter data
|
||||
const formattedData = data.map((item) => ({
|
||||
...item,
|
||||
date: new Date(item.date).toLocaleDateString('en-CA'), // YYYY-MM-DD
|
||||
}));
|
||||
|
||||
const resultData = monthDate
|
||||
? filterByMonth(formattedData, monthDate)
|
||||
: formattedData;
|
||||
|
||||
return this.buildResponse(
|
||||
`Power clamp data fetched successfully for ${spaceUuid ? 'space' : 'community'}`,
|
||||
resultData,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error fetching power clamp data', {
|
||||
error,
|
||||
spaceUuid,
|
||||
communityUuid,
|
||||
});
|
||||
throw new HttpException(
|
||||
error.response?.message || 'Failed to fetch power clamp data',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async getPowerClampData(
|
||||
params: PowerClampParamsDto,
|
||||
query: GetPowerClampDto,
|
||||
@ -195,17 +99,4 @@ export class PowerClampService {
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
}
|
||||
private async executeProcedure(
|
||||
procedureFileName: string,
|
||||
params: (string | number | null)[],
|
||||
): Promise<any[]> {
|
||||
const query = this.loadQuery(
|
||||
'fact_space_energy_consumed',
|
||||
procedureFileName,
|
||||
);
|
||||
return await this.dataSource.query(query, params);
|
||||
}
|
||||
private loadQuery(folderName: string, fileName: string): string {
|
||||
return this.sqlLoader.loadQuery(folderName, fileName, SQL_PROCEDURES_PATH);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,6 @@
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
|
||||
import {
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { HttpException, HttpStatus, Injectable } 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';
|
||||
@ -14,9 +9,6 @@ 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 {
|
||||
@ -24,7 +16,6 @@ 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> {
|
||||
@ -130,37 +121,4 @@ 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', 'devices.productDevice'],
|
||||
});
|
||||
|
||||
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', 'devices.productDevice'],
|
||||
});
|
||||
|
||||
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