mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 09:34:54 +00:00
feat: enhance PowerClamp service and controller to support filtering by date, month, and year
This commit is contained in:
@ -1,9 +1,11 @@
|
|||||||
import { Controller, Get, UseGuards } from '@nestjs/common';
|
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
||||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
import { PowerClampService } from '../services/power-clamp.service';
|
import { PowerClampService } from '../services/power-clamp.service';
|
||||||
|
import { GetPowerClampDto } from '../dto/get-power-clamp.dto';
|
||||||
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
|
|
||||||
@ApiTags('Power Clamp Module')
|
@ApiTags('Power Clamp Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -15,12 +17,18 @@ export class PowerClampController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get()
|
@Get(':powerClampUuid')
|
||||||
@ApiOperation({
|
@ApiOperation({
|
||||||
summary: ControllerRoute.PowerClamp.ACTIONS.GET_ENERGY_SUMMARY,
|
summary: ControllerRoute.PowerClamp.ACTIONS.GET_ENERGY_SUMMARY,
|
||||||
description: ControllerRoute.PowerClamp.ACTIONS.GET_ENERGY_DESCRIPTION,
|
description: ControllerRoute.PowerClamp.ACTIONS.GET_ENERGY_DESCRIPTION,
|
||||||
})
|
})
|
||||||
async getPowerClampData() {
|
async getPowerClampData(
|
||||||
return await this.powerClampService.getPowerClampData();
|
@Param('powerClampUuid') powerClampUuid: string,
|
||||||
|
@Query() params: GetPowerClampDto,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
return await this.powerClampService.getPowerClampData(
|
||||||
|
powerClampUuid,
|
||||||
|
params,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
35
src/power-clamp/dto/get-power-clamp.dto.ts
Normal file
35
src/power-clamp/dto/get-power-clamp.dto.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||||
|
import { IsOptional, IsDateString, Matches } from 'class-validator';
|
||||||
|
|
||||||
|
export class GetPowerClampDto {
|
||||||
|
@ApiPropertyOptional({
|
||||||
|
description: 'Input date in ISO format (YYYY-MM-DD) to filter the data',
|
||||||
|
example: '2025-04-23',
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@IsDateString()
|
||||||
|
dayDate?: string;
|
||||||
|
|
||||||
|
@ApiPropertyOptional({
|
||||||
|
description: 'Month and year in format YYYY-MM',
|
||||||
|
example: '2025-03',
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@Matches(/^\d{4}-(0[1-9]|1[0-2])$/, {
|
||||||
|
message: 'monthDate must be in YYYY-MM format',
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
monthDate?: string;
|
||||||
|
|
||||||
|
@ApiPropertyOptional({
|
||||||
|
description: 'Input year in YYYY format to filter the data',
|
||||||
|
example: '2025',
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@Matches(/^\d{4}$/, {
|
||||||
|
message: 'Year must be in YYYY format',
|
||||||
|
})
|
||||||
|
year?: string;
|
||||||
|
}
|
||||||
@ -2,11 +2,20 @@ import { Module } from '@nestjs/common';
|
|||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
import { PowerClampService } from './services/power-clamp.service';
|
import { PowerClampService } from './services/power-clamp.service';
|
||||||
import { PowerClampController } from './controllers';
|
import { PowerClampController } from './controllers';
|
||||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
import {
|
||||||
|
PowerClampDailyRepository,
|
||||||
|
PowerClampHourlyRepository,
|
||||||
|
PowerClampMonthlyRepository,
|
||||||
|
} from '@app/common/modules/power-clamp/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule],
|
imports: [ConfigModule],
|
||||||
controllers: [PowerClampController],
|
controllers: [PowerClampController],
|
||||||
providers: [PowerClampService, SqlLoaderService],
|
providers: [
|
||||||
|
PowerClampService,
|
||||||
|
PowerClampDailyRepository,
|
||||||
|
PowerClampHourlyRepository,
|
||||||
|
PowerClampMonthlyRepository,
|
||||||
|
],
|
||||||
exports: [PowerClampService],
|
exports: [PowerClampService],
|
||||||
})
|
})
|
||||||
export class PowerClampModule {}
|
export class PowerClampModule {}
|
||||||
|
|||||||
@ -1,30 +1,88 @@
|
|||||||
import { SQL_QUERIES_PATH } from '@app/common/constants/sql-query-path';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
import { GetPowerClampDto } from '../dto/get-power-clamp.dto';
|
||||||
import { Injectable } from '@nestjs/common';
|
import {
|
||||||
import { DataSource } from 'typeorm';
|
PowerClampDailyRepository,
|
||||||
|
PowerClampHourlyRepository,
|
||||||
|
PowerClampMonthlyRepository,
|
||||||
|
} from '@app/common/modules/power-clamp/repositories';
|
||||||
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PowerClampService {
|
export class PowerClampService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly sqlLoader: SqlLoaderService,
|
private readonly powerClampDailyRepository: PowerClampDailyRepository,
|
||||||
private readonly dataSource: DataSource,
|
private readonly powerClampHourlyRepository: PowerClampHourlyRepository,
|
||||||
|
private readonly powerClampMonthlyRepository: PowerClampMonthlyRepository,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async getPowerClampData() {
|
async getPowerClampData(powerClampUuid: string, params: GetPowerClampDto) {
|
||||||
const sql = this.sqlLoader.loadQuery(
|
const { dayDate, monthDate, year } = params;
|
||||||
'fact_daily_energy_consumed',
|
|
||||||
'fact_daily_energy_consumed',
|
try {
|
||||||
SQL_QUERIES_PATH,
|
if (dayDate) {
|
||||||
|
const data = await this.powerClampHourlyRepository
|
||||||
|
.createQueryBuilder('hourly')
|
||||||
|
.where('hourly.deviceUuid = :deviceUuid', {
|
||||||
|
deviceUuid: powerClampUuid,
|
||||||
|
})
|
||||||
|
.andWhere('hourly.date = :date', { date: dayDate })
|
||||||
|
.orderBy('CAST(hourly.hour AS INTEGER)', 'ASC')
|
||||||
|
.getMany();
|
||||||
|
|
||||||
|
return this.buildResponse(
|
||||||
|
`Power clamp data for day ${dayDate} fetched successfully`,
|
||||||
|
data,
|
||||||
);
|
);
|
||||||
return this.dataSource.manager.query(sql);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getEnergyConsumed(code: string) {
|
if (monthDate) {
|
||||||
const sql = this.sqlLoader.loadQuery(
|
const data = await this.powerClampDailyRepository
|
||||||
'energy',
|
.createQueryBuilder('daily')
|
||||||
'energy_consumed_with_params',
|
.where('daily.deviceUuid = :deviceUuid', {
|
||||||
SQL_QUERIES_PATH,
|
deviceUuid: powerClampUuid,
|
||||||
|
})
|
||||||
|
.andWhere("TO_CHAR(daily.date, 'YYYY-MM') = :monthDate", {
|
||||||
|
monthDate,
|
||||||
|
})
|
||||||
|
.orderBy('daily.date', 'ASC')
|
||||||
|
.getMany();
|
||||||
|
|
||||||
|
return this.buildResponse(
|
||||||
|
`Power clamp data for month ${monthDate} fetched successfully`,
|
||||||
|
data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (year) {
|
||||||
|
const data = await this.powerClampMonthlyRepository
|
||||||
|
.createQueryBuilder('monthly')
|
||||||
|
.where('monthly.deviceUuid = :deviceUuid', {
|
||||||
|
deviceUuid: powerClampUuid,
|
||||||
|
})
|
||||||
|
.andWhere('RIGHT(monthly.month, 4) = :year', { year })
|
||||||
|
.orderBy('monthly.month', 'ASC')
|
||||||
|
.getMany();
|
||||||
|
|
||||||
|
return this.buildResponse(
|
||||||
|
`Power clamp data for year ${year} fetched successfully`,
|
||||||
|
data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.buildResponse(`Power clamp data fetched successfully`, []);
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error fetching power clamp data',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
return this.dataSource.manager.query(sql, [code]);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private buildResponse(message: string, data: any[]) {
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
message,
|
||||||
|
data,
|
||||||
|
statusCode: HttpStatus.OK,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user