asdf
This commit is contained in:
@@ -49,32 +49,73 @@ router.get('/:propertyId', async (req, res, next) => {
|
||||
|
||||
const weatherData = weatherResponse.data;
|
||||
|
||||
// Store weather data in cache
|
||||
// Store weather data in cache (UPSERT). If unique index is missing, fallback to update-then-insert.
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
await pool.query(
|
||||
`INSERT INTO weather_data
|
||||
(property_id, date, temperature_high, temperature_low, humidity, wind_speed, precipitation, conditions)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
ON CONFLICT (property_id, date)
|
||||
DO UPDATE SET
|
||||
temperature_high = EXCLUDED.temperature_high,
|
||||
temperature_low = EXCLUDED.temperature_low,
|
||||
humidity = EXCLUDED.humidity,
|
||||
wind_speed = EXCLUDED.wind_speed,
|
||||
precipitation = EXCLUDED.precipitation,
|
||||
conditions = EXCLUDED.conditions,
|
||||
created_at = CURRENT_TIMESTAMP`,
|
||||
[
|
||||
propertyId,
|
||||
today,
|
||||
Math.round(weatherData.main.temp),
|
||||
Math.round(weatherData.main.temp_min),
|
||||
weatherData.main.humidity,
|
||||
weatherData.wind?.speed || 0,
|
||||
weatherData.rain?.['1h'] || 0,
|
||||
weatherData.weather[0].description
|
||||
]
|
||||
);
|
||||
const values = [
|
||||
propertyId,
|
||||
today,
|
||||
Math.round(weatherData.main.temp),
|
||||
Math.round(weatherData.main.temp_min),
|
||||
weatherData.main.humidity,
|
||||
weatherData.wind?.speed || 0,
|
||||
weatherData.rain?.['1h'] || 0,
|
||||
weatherData.weather[0].description
|
||||
];
|
||||
|
||||
const upsertSql = `
|
||||
INSERT INTO weather_data
|
||||
(property_id, date, temperature_high, temperature_low, humidity, wind_speed, precipitation, conditions)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
ON CONFLICT (property_id, date)
|
||||
DO UPDATE SET
|
||||
temperature_high = EXCLUDED.temperature_high,
|
||||
temperature_low = EXCLUDED.temperature_low,
|
||||
humidity = EXCLUDED.humidity,
|
||||
wind_speed = EXCLUDED.wind_speed,
|
||||
precipitation = EXCLUDED.precipitation,
|
||||
conditions = EXCLUDED.conditions,
|
||||
created_at = CURRENT_TIMESTAMP`;
|
||||
|
||||
try {
|
||||
await pool.query(upsertSql, values);
|
||||
} catch (e) {
|
||||
// Fallback when unique index is absent: try UPDATE first, then INSERT
|
||||
const msg = String(e?.message || '');
|
||||
if (e?.code === '42P10' || msg.includes('no unique or exclusion constraint')) {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
const upd = await client.query(
|
||||
`UPDATE weather_data
|
||||
SET temperature_high = $3,
|
||||
temperature_low = $4,
|
||||
humidity = $5,
|
||||
wind_speed = $6,
|
||||
precipitation = $7,
|
||||
conditions = $8,
|
||||
created_at = CURRENT_TIMESTAMP
|
||||
WHERE property_id = $1 AND date = $2`,
|
||||
values
|
||||
);
|
||||
if (upd.rowCount === 0) {
|
||||
await client.query(
|
||||
`INSERT INTO weather_data
|
||||
(property_id, date, temperature_high, temperature_low, humidity, wind_speed, precipitation, conditions)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
|
||||
values
|
||||
);
|
||||
}
|
||||
await client.query('COMMIT');
|
||||
} catch (inner) {
|
||||
await client.query('ROLLBACK');
|
||||
throw inner;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
@@ -451,4 +492,4 @@ router.get('/conditions/suitable/:propertyId', async (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user