mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:54:54 +00:00
refactor: remove AQI module and related files
This commit is contained in:
@ -1,11 +0,0 @@
|
|||||||
import { Module } from '@nestjs/common';
|
|
||||||
import { ConfigModule } from '@nestjs/config';
|
|
||||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
|
||||||
import { AqiService } from './services';
|
|
||||||
import { AqiController } from './controllers';
|
|
||||||
@Module({
|
|
||||||
imports: [ConfigModule],
|
|
||||||
controllers: [AqiController],
|
|
||||||
providers: [AqiService, SqlLoaderService],
|
|
||||||
})
|
|
||||||
export class AqiModule {}
|
|
||||||
@ -1,64 +0,0 @@
|
|||||||
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
|
||||||
import {
|
|
||||||
ApiTags,
|
|
||||||
ApiBearerAuth,
|
|
||||||
ApiOperation,
|
|
||||||
ApiParam,
|
|
||||||
} 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 { AqiService } from '../services/aqi.service';
|
|
||||||
import {
|
|
||||||
GetAqiDailyBySpaceDto,
|
|
||||||
GetAqiPollutantBySpaceDto,
|
|
||||||
} from '../dto/get-aqi.dto';
|
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
|
||||||
import { SpaceParamsDto } from '../dto/aqi-params.dto';
|
|
||||||
|
|
||||||
@ApiTags('AQI Module')
|
|
||||||
@Controller({
|
|
||||||
version: EnableDisableStatusEnum.ENABLED,
|
|
||||||
path: ControllerRoute.AQI.ROUTE,
|
|
||||||
})
|
|
||||||
export class AqiController {
|
|
||||||
constructor(private readonly aqiService: AqiService) {}
|
|
||||||
|
|
||||||
@ApiBearerAuth()
|
|
||||||
@UseGuards(JwtAuthGuard)
|
|
||||||
@Get('range/space/:spaceUuid')
|
|
||||||
@ApiOperation({
|
|
||||||
summary: ControllerRoute.AQI.ACTIONS.GET_AQI_RANGE_DATA_SUMMARY,
|
|
||||||
description: ControllerRoute.AQI.ACTIONS.GET_AQI_RANGE_DATA_DESCRIPTION,
|
|
||||||
})
|
|
||||||
@ApiParam({
|
|
||||||
name: 'spaceUuid',
|
|
||||||
description: 'UUID of the Space',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
async getAQIRangeDataBySpace(
|
|
||||||
@Param() params: SpaceParamsDto,
|
|
||||||
@Query() query: GetAqiDailyBySpaceDto,
|
|
||||||
): Promise<BaseResponseDto> {
|
|
||||||
return await this.aqiService.getAQIRangeDataBySpace(params, query);
|
|
||||||
}
|
|
||||||
@ApiBearerAuth()
|
|
||||||
@UseGuards(JwtAuthGuard)
|
|
||||||
@Get('distribution/space/:spaceUuid')
|
|
||||||
@ApiOperation({
|
|
||||||
summary: ControllerRoute.AQI.ACTIONS.GET_AQI_DISTRIBUTION_DATA_SUMMARY,
|
|
||||||
description:
|
|
||||||
ControllerRoute.AQI.ACTIONS.GET_AQI_DISTRIBUTION_DATA_DESCRIPTION,
|
|
||||||
})
|
|
||||||
@ApiParam({
|
|
||||||
name: 'spaceUuid',
|
|
||||||
description: 'UUID of the Space',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
async getAQIDistributionDataBySpace(
|
|
||||||
@Param() params: SpaceParamsDto,
|
|
||||||
@Query() query: GetAqiPollutantBySpaceDto,
|
|
||||||
): Promise<BaseResponseDto> {
|
|
||||||
return await this.aqiService.getAQIDistributionDataBySpace(params, query);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1 +0,0 @@
|
|||||||
export * from './aqi.controller';
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
import { IsNotEmpty, IsUUID } from 'class-validator';
|
|
||||||
|
|
||||||
export class SpaceParamsDto {
|
|
||||||
@IsUUID('4', { message: 'Invalid UUID format' })
|
|
||||||
@IsNotEmpty()
|
|
||||||
spaceUuid: string;
|
|
||||||
}
|
|
||||||
@ -1,37 +0,0 @@
|
|||||||
import { PollutantType } from '@app/common/constants/pollutants.enum';
|
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
|
||||||
import { Matches, IsNotEmpty, IsString } from 'class-validator';
|
|
||||||
|
|
||||||
export class GetAqiDailyBySpaceDto {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'Month and year in format YYYY-MM',
|
|
||||||
example: '2025-03',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@Matches(/^\d{4}-(0[1-9]|1[0-2])$/, {
|
|
||||||
message: 'monthDate must be in YYYY-MM format',
|
|
||||||
})
|
|
||||||
@IsNotEmpty()
|
|
||||||
monthDate: string;
|
|
||||||
}
|
|
||||||
export class GetAqiPollutantBySpaceDto {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'Pollutant Type',
|
|
||||||
enum: PollutantType,
|
|
||||||
example: PollutantType.AQI,
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public pollutantType: string;
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'Month and year in format YYYY-MM',
|
|
||||||
example: '2025-03',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@Matches(/^\d{4}-(0[1-9]|1[0-2])$/, {
|
|
||||||
message: 'monthDate must be in YYYY-MM format',
|
|
||||||
})
|
|
||||||
@IsNotEmpty()
|
|
||||||
monthDate: string;
|
|
||||||
}
|
|
||||||
@ -1,138 +0,0 @@
|
|||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
||||||
import {
|
|
||||||
GetAqiDailyBySpaceDto,
|
|
||||||
GetAqiPollutantBySpaceDto,
|
|
||||||
} from '../dto/get-aqi.dto';
|
|
||||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
|
||||||
import { SpaceParamsDto } from '../dto/aqi-params.dto';
|
|
||||||
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 { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
|
||||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
|
||||||
import { PollutantType } from '@app/common/constants/pollutants.enum';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class AqiService {
|
|
||||||
constructor(
|
|
||||||
private readonly sqlLoader: SqlLoaderService,
|
|
||||||
private readonly dataSource: DataSource,
|
|
||||||
) {}
|
|
||||||
async getAQIDistributionDataBySpace(
|
|
||||||
params: SpaceParamsDto,
|
|
||||||
query: GetAqiPollutantBySpaceDto,
|
|
||||||
): Promise<BaseResponseDto> {
|
|
||||||
const { monthDate, pollutantType } = query;
|
|
||||||
const { spaceUuid } = params;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const data = await this.executeProcedure(
|
|
||||||
'fact_daily_space_aqi',
|
|
||||||
'proceduce_select_daily_space_aqi',
|
|
||||||
[spaceUuid, monthDate],
|
|
||||||
);
|
|
||||||
|
|
||||||
const categories = [
|
|
||||||
'good',
|
|
||||||
'moderate',
|
|
||||||
'unhealthy_sensitive',
|
|
||||||
'unhealthy',
|
|
||||||
'very_unhealthy',
|
|
||||||
'hazardous',
|
|
||||||
];
|
|
||||||
|
|
||||||
const transformedData = data.map((item) => {
|
|
||||||
const date = new Date(item.event_date).toLocaleDateString('en-CA'); // YYYY-MM-DD
|
|
||||||
|
|
||||||
const categoryData = categories.map((category) => {
|
|
||||||
const key = `${category}_${pollutantType.toLowerCase()}_percentage`;
|
|
||||||
return {
|
|
||||||
type: category,
|
|
||||||
percentage: item[key] ?? 0,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
return { date, data: categoryData };
|
|
||||||
});
|
|
||||||
|
|
||||||
const response = this.buildResponse(
|
|
||||||
`AQI distribution data fetched successfully for ${spaceUuid} space and pollutant ${pollutantType}`,
|
|
||||||
transformedData,
|
|
||||||
);
|
|
||||||
return response;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to fetch AQI distribution data', {
|
|
||||||
error,
|
|
||||||
spaceUuid,
|
|
||||||
});
|
|
||||||
throw new HttpException(
|
|
||||||
error.response?.message || 'Failed to fetch AQI distribution data',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async getAQIRangeDataBySpace(
|
|
||||||
params: SpaceParamsDto,
|
|
||||||
query: GetAqiDailyBySpaceDto,
|
|
||||||
): Promise<BaseResponseDto> {
|
|
||||||
const { monthDate } = query;
|
|
||||||
const { spaceUuid } = params;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const data = await this.executeProcedure(
|
|
||||||
'fact_daily_space_aqi',
|
|
||||||
'proceduce_select_daily_space_aqi',
|
|
||||||
[spaceUuid, monthDate],
|
|
||||||
);
|
|
||||||
|
|
||||||
// Define pollutants dynamically
|
|
||||||
const pollutants = Object.values(PollutantType);
|
|
||||||
|
|
||||||
const transformedData = data.map((item) => {
|
|
||||||
const date = new Date(item.event_date).toLocaleDateString('en-CA'); // YYYY-MM-DD
|
|
||||||
const dailyData = pollutants.map((type) => ({
|
|
||||||
type,
|
|
||||||
min: item[`daily_min_${type}`],
|
|
||||||
max: item[`daily_max_${type}`],
|
|
||||||
average: item[`daily_avg_${type}`],
|
|
||||||
}));
|
|
||||||
return { date, data: dailyData };
|
|
||||||
});
|
|
||||||
|
|
||||||
const response = this.buildResponse(
|
|
||||||
`AQI data fetched successfully for ${spaceUuid} space`,
|
|
||||||
transformedData,
|
|
||||||
);
|
|
||||||
return convertKeysToCamelCase(response);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to fetch AQI data', {
|
|
||||||
error,
|
|
||||||
spaceUuid,
|
|
||||||
});
|
|
||||||
throw new HttpException(
|
|
||||||
error.response?.message || 'Failed to fetch AQI data',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private buildResponse(message: string, data: any[]) {
|
|
||||||
return new SuccessResponseDto({
|
|
||||||
message,
|
|
||||||
data,
|
|
||||||
statusCode: HttpStatus.OK,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
private async executeProcedure(
|
|
||||||
procedureFolderName: string,
|
|
||||||
procedureFileName: string,
|
|
||||||
params: (string | number | null)[],
|
|
||||||
): Promise<any[]> {
|
|
||||||
const query = this.loadQuery(procedureFolderName, 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 +0,0 @@
|
|||||||
export * from './aqi.service';
|
|
||||||
Reference in New Issue
Block a user