asdf
This commit is contained in:
@@ -49,32 +49,73 @@ router.get('/:propertyId', async (req, res, next) => {
|
|||||||
|
|
||||||
const weatherData = weatherResponse.data;
|
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];
|
const today = new Date().toISOString().split('T')[0];
|
||||||
await pool.query(
|
const values = [
|
||||||
`INSERT INTO weather_data
|
propertyId,
|
||||||
(property_id, date, temperature_high, temperature_low, humidity, wind_speed, precipitation, conditions)
|
today,
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
Math.round(weatherData.main.temp),
|
||||||
ON CONFLICT (property_id, date)
|
Math.round(weatherData.main.temp_min),
|
||||||
DO UPDATE SET
|
weatherData.main.humidity,
|
||||||
temperature_high = EXCLUDED.temperature_high,
|
weatherData.wind?.speed || 0,
|
||||||
temperature_low = EXCLUDED.temperature_low,
|
weatherData.rain?.['1h'] || 0,
|
||||||
humidity = EXCLUDED.humidity,
|
weatherData.weather[0].description
|
||||||
wind_speed = EXCLUDED.wind_speed,
|
];
|
||||||
precipitation = EXCLUDED.precipitation,
|
|
||||||
conditions = EXCLUDED.conditions,
|
const upsertSql = `
|
||||||
created_at = CURRENT_TIMESTAMP`,
|
INSERT INTO weather_data
|
||||||
[
|
(property_id, date, temperature_high, temperature_low, humidity, wind_speed, precipitation, conditions)
|
||||||
propertyId,
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||||
today,
|
ON CONFLICT (property_id, date)
|
||||||
Math.round(weatherData.main.temp),
|
DO UPDATE SET
|
||||||
Math.round(weatherData.main.temp_min),
|
temperature_high = EXCLUDED.temperature_high,
|
||||||
weatherData.main.humidity,
|
temperature_low = EXCLUDED.temperature_low,
|
||||||
weatherData.wind?.speed || 0,
|
humidity = EXCLUDED.humidity,
|
||||||
weatherData.rain?.['1h'] || 0,
|
wind_speed = EXCLUDED.wind_speed,
|
||||||
weatherData.weather[0].description
|
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({
|
res.json({
|
||||||
success: true,
|
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