Handle duplicate weather data entries and improve historical fetching

Address the "duplicate key value violates unique constraint" error by adding a try-catch block around weather data creation to gracefully handle existing entries and fetch them instead. Also, improve error logging for historical weather data fetching.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: d43bd811-9372-45a7-8ac9-4a954c0538e1
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/d43bd811-9372-45a7-8ac9-4a954c0538e1/xi5G4qi
This commit is contained in:
michaelschal committed 2025-08-15 08:26:14 +00:00
1 parent d635bfb7c0
commit cc4d013dfc
3 files changed
+26 -11

No files matched your search

+20 -4
View File
@@ -2864,25 +2864,41 @@ RÉSUMÉ DU SCAN
// Fetch current year data if not in cache
if (!currentYearData) {
console.log("🌤️ Fetching current weather data from API");
console.log("🌤️ [FETCH] Fetching current weather data from API");
const apiData = await weatherService.fetchCurrentWeather(settings);
if (apiData) {
const weatherData = weatherService.convertApiDataToWeatherData(apiData, settings.location, true);
if (weatherData) {
currentYearData = await storage.createWeatherData(weatherData);
try {
currentYearData = await storage.createWeatherData(weatherData);
console.log("✅ [CACHE] Current year data saved to cache");
} catch (error: any) {
console.warn("⚠️ [CACHE] Could not save current year data (may already exist):", error.message);
// Récupérer les données existantes au lieu de créer
currentYearData = await storage.getWeatherData(today, true);
}
}
}
}
// Fetch previous year data if not in cache
if (!previousYearData) {
console.log("🌤️ Fetching previous year weather data from API");
console.log("🌤️ [FETCH] Fetching previous year weather data from API");
const apiData = await weatherService.fetchPreviousYearWeather(settings, previousYearDate);
if (apiData) {
const weatherData = weatherService.convertApiDataToWeatherData(apiData, settings.location, false);
if (weatherData) {
previousYearData = await storage.createWeatherData(weatherData);
try {
previousYearData = await storage.createWeatherData(weatherData);
console.log("✅ [CACHE] Previous year data saved to cache");
} catch (error: any) {
console.warn("⚠️ [CACHE] Could not save previous year data (may already exist):", error.message);
// Récupérer les données existantes au lieu de créer
previousYearData = await storage.getWeatherData(previousYearDate, false);
}
}
} else {
console.warn("⚠️ [HISTORY] Could not fetch historical data - continuing with current year only");
}
}
+5 -6
View File
@@ -1,4 +1,4 @@
import { WeatherData, WeatherSettings } from "../shared/schema.js";
import { WeatherData, WeatherSettings, InsertWeatherData } from "../shared/schema.js";
export interface WeatherApiResponse {
days: Array<{
@@ -72,7 +72,7 @@ export class WeatherService {
/**
* Convertit les données de l'API en format WeatherData
*/
convertApiDataToWeatherData(apiData: WeatherApiResponse, location: string, isCurrentYear: boolean): WeatherData | null {
convertApiDataToWeatherData(apiData: WeatherApiResponse, location: string, isCurrentYear: boolean): InsertWeatherData | null {
if (!apiData.days || apiData.days.length === 0) {
return null;
}
@@ -80,16 +80,15 @@ export class WeatherService {
const todayData = apiData.days[0];
return {
id: 0, // Sera généré par la base de données
// Pas d'ID - sera auto-généré par PostgreSQL serial
date: todayData.datetime,
location,
tempMax: todayData.tempmax.toString(),
tempMin: todayData.tempmin.toString(),
icon: todayData.icon,
conditions: todayData.conditions,
isCurrentYear,
createdAt: new Date(),
updatedAt: new Date()
isCurrentYear
// Pas de createdAt/updatedAt - gérés par la base de données
};
}