refactor: update PowerClampController and service to use PowerClampParamsDto for parameter handling

This commit is contained in:
faris Aljohari
2025-04-28 01:37:04 +03:00
parent 94881f0555
commit 588d4cb98e
4 changed files with 31 additions and 11 deletions

View File

@ -6,6 +6,7 @@ import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
import { PowerClampService } from '../services/power-clamp.service';
import { GetPowerClampDto } from '../dto/get-power-clamp.dto';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { PowerClampParamsDto } from '../dto/power-clamp-params.dto';
@ApiTags('Power Clamp Module')
@Controller({
@ -23,12 +24,9 @@ export class PowerClampController {
description: ControllerRoute.PowerClamp.ACTIONS.GET_ENERGY_DESCRIPTION,
})
async getPowerClampData(
@Param('powerClampUuid') powerClampUuid: string,
@Query() params: GetPowerClampDto,
@Param() params: PowerClampParamsDto,
@Query() query: GetPowerClampDto,
): Promise<BaseResponseDto> {
return await this.powerClampService.getPowerClampData(
powerClampUuid,
params,
);
return await this.powerClampService.getPowerClampData(params, query);
}
}

View File

@ -0,0 +1,6 @@
import { IsUUID } from 'class-validator';
export class PowerClampParamsDto {
@IsUUID('4', { message: 'Invalid UUID format' })
powerClampUuid: string;
}

View File

@ -7,6 +7,7 @@ import {
PowerClampHourlyRepository,
PowerClampMonthlyRepository,
} from '@app/common/modules/power-clamp/repositories';
import { DeviceRepository } from '@app/common/modules/device/repositories';
@Module({
imports: [ConfigModule],
controllers: [PowerClampController],
@ -15,6 +16,7 @@ import {
PowerClampDailyRepository,
PowerClampHourlyRepository,
PowerClampMonthlyRepository,
DeviceRepository,
],
exports: [PowerClampService],
})

View File

@ -6,6 +6,8 @@ import {
PowerClampMonthlyRepository,
} from '@app/common/modules/power-clamp/repositories';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { PowerClampParamsDto } from '../dto/power-clamp-params.dto';
import { DeviceRepository } from '@app/common/modules/device/repositories';
@Injectable()
export class PowerClampService {
@ -13,12 +15,24 @@ export class PowerClampService {
private readonly powerClampDailyRepository: PowerClampDailyRepository,
private readonly powerClampHourlyRepository: PowerClampHourlyRepository,
private readonly powerClampMonthlyRepository: PowerClampMonthlyRepository,
private readonly deviceRepository: DeviceRepository,
) {}
async getPowerClampData(powerClampUuid: string, params: GetPowerClampDto) {
const { dayDate, monthDate, year } = params;
async getPowerClampData(
params: PowerClampParamsDto,
query: GetPowerClampDto,
) {
const { dayDate, monthDate, year } = query;
const { powerClampUuid } = params;
try {
const device = await this.deviceRepository.findOne({
where: {
uuid: powerClampUuid,
},
});
if (!device) {
throw new HttpException('Power clamp not found', HttpStatus.NOT_FOUND);
}
if (dayDate) {
const data = await this.powerClampHourlyRepository
.createQueryBuilder('hourly')
@ -72,8 +86,8 @@ export class PowerClampService {
return this.buildResponse(`Power clamp data fetched successfully`, []);
} catch (error) {
throw new HttpException(
'Error fetching power clamp data',
HttpStatus.INTERNAL_SERVER_ERROR,
error.message || 'Error fetching power clamp data',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}