feat: add groupByDevice option to GetPowerClampBySpaceDto and update service logic

This commit is contained in:
faris Aljohari
2025-05-11 01:59:26 +03:00
parent 1479c74423
commit fb5084ba3a
2 changed files with 81 additions and 26 deletions

View File

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

View File

@ -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,44 +63,78 @@ 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);
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
}));
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 resultData = monthDate
? filterByMonth(formattedData, monthDate)
: formattedData;
const formattedData = data.map((item) => ({
...item,
date: new Date(item.date).toLocaleDateString('en-CA'), // YYYY-MM-DD
}));
return this.buildResponse(
`Power clamp data fetched successfully for ${spaceUuid ? 'space' : 'community'}`,
resultData,
);
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(',')],
);
// 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,