Merge pull request #396 from SyncrowIOT/fix-some-issue-in-get-commuinty-and-weather-apis

Fix some issue in get commuinty and weather apis
This commit is contained in:
faljawhary
2025-06-04 01:32:21 -06:00
committed by GitHub
3 changed files with 47 additions and 33 deletions

View File

@ -4,5 +4,6 @@ export default registerAs(
'openweather-config', 'openweather-config',
(): Record<string, any> => ({ (): Record<string, any> => ({
OPEN_WEATHER_MAP_API_KEY: process.env.OPEN_WEATHER_MAP_API_KEY, OPEN_WEATHER_MAP_API_KEY: process.env.OPEN_WEATHER_MAP_API_KEY,
WEATHER_API_URL: process.env.WEATHER_API_URL,
}), }),
); );

View File

@ -213,8 +213,8 @@ export class SpaceService {
{ incomingConnectionDisabled: false }, { incomingConnectionDisabled: false },
) )
.leftJoinAndSelect('space.productAllocations', 'productAllocations') .leftJoinAndSelect('space.productAllocations', 'productAllocations')
.leftJoinAndSelect('productAllocations.tags', 'tags') // .leftJoinAndSelect('productAllocations.tags', 'tags')
.leftJoinAndSelect('tags.product', 'tagProduct') // .leftJoinAndSelect('productAllocations.product', 'product')
.leftJoinAndSelect( .leftJoinAndSelect(
'space.subspaces', 'space.subspaces',
'subspaces', 'subspaces',
@ -225,8 +225,11 @@ export class SpaceService {
'subspaces.productAllocations', 'subspaces.productAllocations',
'subspaceProductAllocations', 'subspaceProductAllocations',
) )
.leftJoinAndSelect('subspaceProductAllocations.tags', 'subspaceTags') // .leftJoinAndSelect('subspaceProductAllocations.tags', 'subspaceTag')
.leftJoinAndSelect('subspaceTags.product', 'subspaceTagProduct') // .leftJoinAndSelect(
// 'subspaceProductAllocations.product',
// 'subspaceProduct',
// )
.leftJoinAndSelect('space.spaceModel', 'spaceModel') .leftJoinAndSelect('space.spaceModel', 'spaceModel')
.where('space.community_id = :communityUuid', { communityUuid }) .where('space.community_id = :communityUuid', { communityUuid })
.andWhere('space.spaceName != :orphanSpaceName', { .andWhere('space.spaceName != :orphanSpaceName', {
@ -264,9 +267,7 @@ export class SpaceService {
}), }),
); );
} }
const spaceHierarchy = this.buildSpaceHierarchy(spaces);
const transformedSpaces = spaces.map(this.transformSpace);
const spaceHierarchy = this.buildSpaceHierarchy(transformedSpaces);
return new SuccessResponseDto({ return new SuccessResponseDto({
message: `Spaces in community ${communityUuid} successfully fetched in hierarchy`, message: `Spaces in community ${communityUuid} successfully fetched in hierarchy`,
@ -326,13 +327,13 @@ export class SpaceService {
'incomingConnections.disabled = :incomingConnectionDisabled', 'incomingConnections.disabled = :incomingConnectionDisabled',
{ incomingConnectionDisabled: false }, { incomingConnectionDisabled: false },
) )
.leftJoinAndSelect( // .leftJoinAndSelect(
'space.tags', // 'space.tags',
'tags', // 'tags',
'tags.disabled = :tagDisabled', // 'tags.disabled = :tagDisabled',
{ tagDisabled: false }, // { tagDisabled: false },
) // )
.leftJoinAndSelect('tags.product', 'tagProduct') // .leftJoinAndSelect('tags.product', 'tagProduct')
.leftJoinAndSelect( .leftJoinAndSelect(
'space.subspaces', 'space.subspaces',
'subspaces', 'subspaces',
@ -345,7 +346,7 @@ export class SpaceService {
'subspaceTags.disabled = :subspaceTagsDisabled', 'subspaceTags.disabled = :subspaceTagsDisabled',
{ subspaceTagsDisabled: false }, { subspaceTagsDisabled: false },
) )
.leftJoinAndSelect('subspaceTags.product', 'subspaceTagProduct') // .leftJoinAndSelect('subspaceTags.product', 'subspaceTagProduct')
.where('space.community_id = :communityUuid', { communityUuid }) .where('space.community_id = :communityUuid', { communityUuid })
.andWhere('space.spaceName != :orphanSpaceName', { .andWhere('space.spaceName != :orphanSpaceName', {
orphanSpaceName: ORPHAN_SPACE_NAME, orphanSpaceName: ORPHAN_SPACE_NAME,

View File

@ -1,4 +1,4 @@
import { HttpStatus, Injectable } from '@nestjs/common'; import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios'; import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { firstValueFrom } from 'rxjs'; import { firstValueFrom } from 'rxjs';
@ -9,19 +9,23 @@ import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
@Injectable() @Injectable()
export class WeatherService { export class WeatherService {
private readonly weatherApiUrl: string;
constructor( constructor(
private readonly configService: ConfigService, private readonly configService: ConfigService,
private readonly httpService: HttpService, private readonly httpService: HttpService,
) {} ) {
this.weatherApiUrl = this.configService.get<string>('WEATHER_API_URL');
}
async fetchWeatherDetails( async fetchWeatherDetails(
query: GetWeatherDetailsDto, query: GetWeatherDetailsDto,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
try {
const { lat, lon } = query; const { lat, lon } = query;
const weatherApiKey = this.configService.get<string>( const weatherApiKey = this.configService.get<string>(
'OPEN_WEATHER_MAP_API_KEY', 'OPEN_WEATHER_MAP_API_KEY',
); );
const url = `http://api.weatherapi.com/v1/current.json?key=${weatherApiKey}&q=${lat},${lon}&aqi=yes`; const url = `${this.weatherApiUrl}/current.json?key=${weatherApiKey}&q=${lat},${lon}&aqi=yes`;
const response = await firstValueFrom(this.httpService.get(url)); const response = await firstValueFrom(this.httpService.get(url));
const pm2_5 = response.data.current.air_quality.pm2_5; // Raw PM2.5 (µg/m³) const pm2_5 = response.data.current.air_quality.pm2_5; // Raw PM2.5 (µg/m³)
@ -35,5 +39,13 @@ export class WeatherService {
}, },
statusCode: HttpStatus.OK, statusCode: HttpStatus.OK,
}); });
} catch (error) {
console.log(`Error fetching weather data: ${error}`);
throw new HttpException(
`Api can't handle these lat and lon values`,
error.response?.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }