mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 07:07:21 +00:00
fix: improve error handling in fetchWeatherDetails method
This commit is contained in:
@ -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,
|
||||
|
@ -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<BaseResponseDto> {
|
||||
const { lat, lon } = query;
|
||||
const weatherApiKey = this.configService.get<string>(
|
||||
'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<string>(
|
||||
'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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user