Bookability: run date refresh synchronously, update timestamp immediately

Backend now awaits the refresh in a thread executor instead of queuing a
background task, so the response returns only after data is written. Frontend
drops the 3s setTimeout and invalidates the cache immediately on success.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-05 10:08:20 +00:00
parent 7223ae9c30
commit 1612468122
2 changed files with 7 additions and 9 deletions

View file

@ -607,17 +607,18 @@ def _refresh_date_sync(rate_date: date):
@router.post("/refresh-date/{rate_date}")
async def refresh_single_date(
rate_date: str,
background_tasks: BackgroundTasks,
current_user: dict = Depends(get_current_user)
):
"""
Trigger a refresh of rates for a single date from Newbook.
Runs in background - returns immediately.
Runs synchronously so the client can invalidate its cache immediately on response.
"""
try:
target_date = date.fromisoformat(rate_date)
except ValueError:
raise HTTPException(status_code=400, detail="Invalid date format. Use YYYY-MM-DD")
background_tasks.add_task(_refresh_date_sync, target_date)
return {"status": "queued", "date": rate_date, "message": f"Refreshing rates for {rate_date}"}
import asyncio
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, _refresh_date_sync, target_date)
return {"status": "success", "date": rate_date, "message": f"Rates refreshed for {rate_date}"}

View file

@ -250,11 +250,8 @@ const Bookability: React.FC = () => {
return res.data
},
onSuccess: () => {
// Refetch after a short delay to allow background task to complete
setTimeout(() => {
queryClient.invalidateQueries({ queryKey: ['rate-matrix'] })
setRefreshingDate(null)
}, 3000)
queryClient.invalidateQueries({ queryKey: ['rate-matrix'] })
setRefreshingDate(null)
},
onError: () => setRefreshingDate(null),
})