Fix dashboard 500 error and stale activeChild token after regen
- timer.py: MySQL returns timezone-naive datetimes; wrap occurred_at
with _utc() before arithmetic against datetime.now(timezone.utc) to
avoid TypeError on compute_block_elapsed / compute_break_elapsed.
This was causing a 500 on GET /api/dashboard/{token} for any child
with an active session, preventing schedule templates from loading.
- children.js: after regenerateToken, also update activeChild so it
doesn't hold a stale tv_token reference. Also sync activeChild on
fetchChildren re-fetch so navigation after token regen picks up the
new token immediately.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,11 @@ from sqlalchemy import select
|
|||||||
from app.models.session import TimerEvent
|
from app.models.session import TimerEvent
|
||||||
|
|
||||||
|
|
||||||
|
def _utc(dt: datetime) -> datetime:
|
||||||
|
"""Return dt as UTC-aware, tagging naive datetimes (from MySQL) as UTC."""
|
||||||
|
return dt.replace(tzinfo=timezone.utc) if dt.tzinfo is None else dt
|
||||||
|
|
||||||
|
|
||||||
async def compute_block_elapsed(
|
async def compute_block_elapsed(
|
||||||
db: AsyncSession, session_id: int, block_id: int
|
db: AsyncSession, session_id: int, block_id: int
|
||||||
) -> tuple[int, bool]:
|
) -> tuple[int, bool]:
|
||||||
@@ -33,9 +38,9 @@ async def compute_block_elapsed(
|
|||||||
elapsed = 0.0
|
elapsed = 0.0
|
||||||
last_start = None
|
last_start = None
|
||||||
elif e.event_type in ("start", "resume"):
|
elif e.event_type in ("start", "resume"):
|
||||||
last_start = e.occurred_at
|
last_start = _utc(e.occurred_at)
|
||||||
elif e.event_type == "pause" and last_start:
|
elif e.event_type == "pause" and last_start:
|
||||||
elapsed += (e.occurred_at - last_start).total_seconds()
|
elapsed += (_utc(e.occurred_at) - last_start).total_seconds()
|
||||||
last_start = None
|
last_start = None
|
||||||
running = last_start is not None
|
running = last_start is not None
|
||||||
if running:
|
if running:
|
||||||
@@ -67,11 +72,11 @@ async def compute_break_elapsed(
|
|||||||
for e in tick_events:
|
for e in tick_events:
|
||||||
if e.event_type == "break_reset":
|
if e.event_type == "break_reset":
|
||||||
elapsed = 0.0
|
elapsed = 0.0
|
||||||
last_start = e.occurred_at
|
last_start = _utc(e.occurred_at)
|
||||||
elif e.event_type in ("break_start", "break_resume"):
|
elif e.event_type in ("break_start", "break_resume"):
|
||||||
last_start = e.occurred_at
|
last_start = _utc(e.occurred_at)
|
||||||
elif e.event_type == "break_pause" and last_start:
|
elif e.event_type == "break_pause" and last_start:
|
||||||
elapsed += (e.occurred_at - last_start).total_seconds()
|
elapsed += (_utc(e.occurred_at) - last_start).total_seconds()
|
||||||
last_start = None
|
last_start = None
|
||||||
running = last_start is not None
|
running = last_start is not None
|
||||||
if running:
|
if running:
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ export const useChildrenStore = defineStore('children', () => {
|
|||||||
children.value = res.data
|
children.value = res.data
|
||||||
if (!activeChild.value && children.value.length > 0) {
|
if (!activeChild.value && children.value.length > 0) {
|
||||||
activeChild.value = children.value[0]
|
activeChild.value = children.value[0]
|
||||||
|
} else if (activeChild.value) {
|
||||||
|
// Sync activeChild to the fresh data so stale tokens don't persist
|
||||||
|
const updated = children.value.find((c) => c.id === activeChild.value.id)
|
||||||
|
if (updated) activeChild.value = updated
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,6 +40,9 @@ export const useChildrenStore = defineStore('children', () => {
|
|||||||
const res = await api.post(`/api/children/${id}/regenerate-token`)
|
const res = await api.post(`/api/children/${id}/regenerate-token`)
|
||||||
const idx = children.value.findIndex((c) => c.id === id)
|
const idx = children.value.findIndex((c) => c.id === id)
|
||||||
if (idx !== -1) children.value[idx] = res.data
|
if (idx !== -1) children.value[idx] = res.data
|
||||||
|
if (activeChild.value?.id === id) {
|
||||||
|
activeChild.value = res.data
|
||||||
|
}
|
||||||
return res.data
|
return res.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user