Fix offset-naive/aware datetime error in nag checker

Make last_nag_at timezone-aware before subtracting from the tz-aware
current_time, and store future nag timestamps with timezone.utc to
prevent the mismatch recurring.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-19 19:43:32 -06:00
parent 4c4ff5add3
commit e9a2f96f91

View File

@@ -10,7 +10,7 @@ This module handles:
import json
import uuid
from datetime import datetime, timedelta, time
from datetime import datetime, timedelta, time, timezone
from typing import Optional, Dict, List, Tuple
import core.postgres as postgres
from core.tz import user_now, user_today_for
@@ -291,6 +291,8 @@ def should_send_nag(
nag_interval = settings.get("nag_interval_minutes", 15)
if last_nag:
if isinstance(last_nag, datetime) and last_nag.tzinfo is None:
last_nag = last_nag.replace(tzinfo=timezone.utc)
time_since_last_nag = (current_time - last_nag).total_seconds() / 60
if time_since_last_nag < nag_interval:
return False, f"Too soon ({int(time_since_last_nag)} < {nag_interval} min)"
@@ -335,7 +337,7 @@ def record_nag_sent(user_uuid: str, med_id: str, scheduled_time):
postgres.update(
"medication_schedules",
{"nag_count": new_nag_count, "last_nag_at": datetime.utcnow()},
{"nag_count": new_nag_count, "last_nag_at": datetime.now(timezone.utc)},
{"id": schedule["id"]},
)