mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
feat: add groupByDevice option to GetPowerClampBySpaceDto and update service logic
This commit is contained in:
@ -1,5 +1,13 @@
|
||||
import { BooleanValues } from '@app/common/constants/boolean-values.enum';
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsOptional, IsDateString, Matches, IsNotEmpty } from 'class-validator';
|
||||
import { Transform } from 'class-transformer';
|
||||
import {
|
||||
IsOptional,
|
||||
IsDateString,
|
||||
Matches,
|
||||
IsNotEmpty,
|
||||
IsBoolean,
|
||||
} from 'class-validator';
|
||||
|
||||
export class GetPowerClampDto {
|
||||
@ApiPropertyOptional({
|
||||
@ -42,4 +50,17 @@ export class GetPowerClampBySpaceDto {
|
||||
@IsDateString()
|
||||
@IsNotEmpty()
|
||||
public monthDate: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
example: true,
|
||||
description: 'Whether to group results by device or not',
|
||||
required: false,
|
||||
default: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@Transform((value) => {
|
||||
return value.obj.groupByDevice === BooleanValues.TRUE;
|
||||
})
|
||||
public groupByDevice?: boolean = false;
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ export class PowerClampService {
|
||||
params: ResourceParamsDto,
|
||||
query: GetPowerClampBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { monthDate } = query;
|
||||
const { monthDate, groupByDevice } = query;
|
||||
const { spaceUuid, communityUuid } = params;
|
||||
|
||||
try {
|
||||
@ -63,25 +63,58 @@ export class PowerClampService {
|
||||
|
||||
if (!devices?.length) {
|
||||
return this.buildResponse(
|
||||
'No power clamp devices found for the specified criteria',
|
||||
`No devices found for ${spaceUuid ? 'space' : 'community'}`,
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
||||
// Filter and prepare device UUIDs
|
||||
const deviceUuids = devices
|
||||
.filter((device) => device.productDevice?.prodType === ProductType.PC)
|
||||
.map((device) => device.uuid);
|
||||
// Filter power clamp devices
|
||||
const powerClampDevices = devices.filter(
|
||||
(device) => device.productDevice?.prodType === ProductType.PC,
|
||||
);
|
||||
|
||||
if (deviceUuids.length === 0) {
|
||||
if (powerClampDevices.length === 0) {
|
||||
return this.buildResponse(
|
||||
'No power clamp devices (PC type) found for the specified criteria',
|
||||
`No power clamp devices found for ${spaceUuid ? 'space' : 'community'}`,
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
||||
// Execute procedure
|
||||
const formattedMonthDate = toMMYYYY(monthDate);
|
||||
|
||||
if (groupByDevice) {
|
||||
// Handle per-device response
|
||||
const deviceDataPromises = powerClampDevices.map(async (device) => {
|
||||
const data = await this.executeProcedure(
|
||||
'fact_daily_space_energy_consumed_procedure',
|
||||
[formattedMonthDate, device.uuid],
|
||||
);
|
||||
|
||||
const formattedData = data.map((item) => ({
|
||||
...item,
|
||||
date: new Date(item.date).toLocaleDateString('en-CA'), // YYYY-MM-DD
|
||||
}));
|
||||
|
||||
const filteredData = monthDate
|
||||
? filterByMonth(formattedData, monthDate)
|
||||
: formattedData;
|
||||
|
||||
return {
|
||||
deviceUuid: device.uuid,
|
||||
deviceName: device.name || `Power Clamp Device`,
|
||||
data: filteredData,
|
||||
};
|
||||
});
|
||||
|
||||
const deviceDataResults = await Promise.all(deviceDataPromises);
|
||||
|
||||
return this.buildResponse(
|
||||
`Power clamp data fetched successfully for ${spaceUuid ? 'space' : 'community'}`,
|
||||
deviceDataResults,
|
||||
);
|
||||
} else {
|
||||
// Original behavior - all devices together
|
||||
const deviceUuids = powerClampDevices.map((device) => device.uuid);
|
||||
const data = await this.executeProcedure(
|
||||
'fact_daily_space_energy_consumed_procedure',
|
||||
[formattedMonthDate, deviceUuids.join(',')],
|
||||
@ -101,6 +134,7 @@ export class PowerClampService {
|
||||
`Power clamp data fetched successfully for ${spaceUuid ? 'space' : 'community'}`,
|
||||
resultData,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching power clamp data', {
|
||||
error,
|
||||
|
Reference in New Issue
Block a user