From cd3e9016f24c9771c5a698d07d7fc5ace97eb454 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Wed, 4 Jun 2025 01:08:33 -0600 Subject: [PATCH 1/3] fix: improve error handling in fetchWeatherDetails method --- src/space/services/space.service.ts | 31 +++++++++--------- src/weather/services/weather.service.ts | 43 +++++++++++++++---------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index 246f583..c001394 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -213,8 +213,8 @@ export class SpaceService { { incomingConnectionDisabled: false }, ) .leftJoinAndSelect('space.productAllocations', 'productAllocations') - .leftJoinAndSelect('productAllocations.tags', 'tags') - .leftJoinAndSelect('tags.product', 'tagProduct') + // .leftJoinAndSelect('productAllocations.tags', 'tags') + // .leftJoinAndSelect('productAllocations.product', 'product') .leftJoinAndSelect( 'space.subspaces', 'subspaces', @@ -225,8 +225,11 @@ export class SpaceService { 'subspaces.productAllocations', 'subspaceProductAllocations', ) - .leftJoinAndSelect('subspaceProductAllocations.tags', 'subspaceTags') - .leftJoinAndSelect('subspaceTags.product', 'subspaceTagProduct') + // .leftJoinAndSelect('subspaceProductAllocations.tags', 'subspaceTag') + // .leftJoinAndSelect( + // 'subspaceProductAllocations.product', + // 'subspaceProduct', + // ) .leftJoinAndSelect('space.spaceModel', 'spaceModel') .where('space.community_id = :communityUuid', { communityUuid }) .andWhere('space.spaceName != :orphanSpaceName', { @@ -264,9 +267,7 @@ export class SpaceService { }), ); } - - const transformedSpaces = spaces.map(this.transformSpace); - const spaceHierarchy = this.buildSpaceHierarchy(transformedSpaces); + const spaceHierarchy = this.buildSpaceHierarchy(spaces); return new SuccessResponseDto({ message: `Spaces in community ${communityUuid} successfully fetched in hierarchy`, @@ -326,13 +327,13 @@ export class SpaceService { 'incomingConnections.disabled = :incomingConnectionDisabled', { incomingConnectionDisabled: false }, ) - .leftJoinAndSelect( - 'space.tags', - 'tags', - 'tags.disabled = :tagDisabled', - { tagDisabled: false }, - ) - .leftJoinAndSelect('tags.product', 'tagProduct') + // .leftJoinAndSelect( + // 'space.tags', + // 'tags', + // 'tags.disabled = :tagDisabled', + // { tagDisabled: false }, + // ) + // .leftJoinAndSelect('tags.product', 'tagProduct') .leftJoinAndSelect( 'space.subspaces', 'subspaces', @@ -345,7 +346,7 @@ export class SpaceService { 'subspaceTags.disabled = :subspaceTagsDisabled', { subspaceTagsDisabled: false }, ) - .leftJoinAndSelect('subspaceTags.product', 'subspaceTagProduct') + // .leftJoinAndSelect('subspaceTags.product', 'subspaceTagProduct') .where('space.community_id = :communityUuid', { communityUuid }) .andWhere('space.spaceName != :orphanSpaceName', { orphanSpaceName: ORPHAN_SPACE_NAME, diff --git a/src/weather/services/weather.service.ts b/src/weather/services/weather.service.ts index 11e1677..d534057 100644 --- a/src/weather/services/weather.service.ts +++ b/src/weather/services/weather.service.ts @@ -1,4 +1,4 @@ -import { HttpStatus, Injectable } from '@nestjs/common'; +import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { HttpService } from '@nestjs/axios'; import { ConfigService } from '@nestjs/config'; import { firstValueFrom } from 'rxjs'; @@ -17,23 +17,32 @@ export class WeatherService { async fetchWeatherDetails( query: GetWeatherDetailsDto, ): Promise { - const { lat, lon } = query; - const weatherApiKey = this.configService.get( - 'OPEN_WEATHER_MAP_API_KEY', - ); - const url = `http://api.weatherapi.com/v1/current.json?key=${weatherApiKey}&q=${lat},${lon}&aqi=yes`; + try { + const { lat, lon } = query; + const weatherApiKey = this.configService.get( + 'OPEN_WEATHER_MAP_API_KEY', + ); + const url = `http://api.weatherapi.com/v1/current.json?key=${weatherApiKey}&q=${lat},${lon}&aqi=yes`; - const response = await firstValueFrom(this.httpService.get(url)); - const pm2_5 = response.data.current.air_quality.pm2_5; // Raw PM2.5 (µg/m³) + const response = await firstValueFrom(this.httpService.get(url)); + const pm2_5 = response.data.current.air_quality.pm2_5; // Raw PM2.5 (µg/m³) - return new SuccessResponseDto({ - message: `Weather details fetched successfully`, - data: { - aqi: calculateAQI(pm2_5), // Converted AQI (0-500) - temperature: response.data.current.temp_c, - humidity: response.data.current.humidity, - }, - statusCode: HttpStatus.OK, - }); + return new SuccessResponseDto({ + message: `Weather details fetched successfully`, + data: { + aqi: calculateAQI(pm2_5), // Converted AQI (0-500) + temperature: response.data.current.temp_c, + humidity: response.data.current.humidity, + }, + statusCode: HttpStatus.OK, + }); + } catch (error) { + console.log(`Error fetching weather data: ${error}`); + + throw new HttpException( + `Wrong latitude or longitude provided`, + error.response?.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } } } From ea021ad2280c113b8981627cf0e3d4e2913533fb Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Wed, 4 Jun 2025 01:09:49 -0600 Subject: [PATCH 2/3] fix: update error message for invalid latitude and longitude in fetchWeatherDetails method --- src/weather/services/weather.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weather/services/weather.service.ts b/src/weather/services/weather.service.ts index d534057..aa4ea0d 100644 --- a/src/weather/services/weather.service.ts +++ b/src/weather/services/weather.service.ts @@ -40,7 +40,7 @@ export class WeatherService { console.log(`Error fetching weather data: ${error}`); throw new HttpException( - `Wrong latitude or longitude provided`, + `Api can't handle these lat and lon values`, error.response?.status || HttpStatus.INTERNAL_SERVER_ERROR, ); } From 43dfaaa90d0e2775fbf979edbe282161b489f800 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Wed, 4 Jun 2025 01:32:01 -0600 Subject: [PATCH 3/3] fix: utilize WEATHER_API_URL in WeatherService for dynamic API endpoint --- src/config/weather.open.config.ts | 1 + src/weather/services/weather.service.ts | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/config/weather.open.config.ts b/src/config/weather.open.config.ts index 2182490..94b77d5 100644 --- a/src/config/weather.open.config.ts +++ b/src/config/weather.open.config.ts @@ -4,5 +4,6 @@ export default registerAs( 'openweather-config', (): Record => ({ OPEN_WEATHER_MAP_API_KEY: process.env.OPEN_WEATHER_MAP_API_KEY, + WEATHER_API_URL: process.env.WEATHER_API_URL, }), ); diff --git a/src/weather/services/weather.service.ts b/src/weather/services/weather.service.ts index aa4ea0d..3463b5c 100644 --- a/src/weather/services/weather.service.ts +++ b/src/weather/services/weather.service.ts @@ -9,10 +9,13 @@ import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; @Injectable() export class WeatherService { + private readonly weatherApiUrl: string; constructor( private readonly configService: ConfigService, private readonly httpService: HttpService, - ) {} + ) { + this.weatherApiUrl = this.configService.get('WEATHER_API_URL'); + } async fetchWeatherDetails( query: GetWeatherDetailsDto, @@ -22,7 +25,7 @@ export class WeatherService { const weatherApiKey = this.configService.get( '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 pm2_5 = response.data.current.air_quality.pm2_5; // Raw PM2.5 (µg/m³)