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

+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
};
}