Fix VARCHAR vs TIME type mismatch in med_logs queries (#15)

The medication_schedules.adjusted_time column is a TIME type, but
med_logs.scheduled_time is VARCHAR. When adjusted_time values were
passed into queries against med_logs, PostgreSQL rejected the
comparison. Add _normalize_time() to convert datetime.time objects
to "HH:MM" strings at the entry points of should_send_nag,
record_nag_sent, and mark_med_taken.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-19 13:14:20 -06:00
parent d4adbde3df
commit d2b074d39b

View File

@@ -16,6 +16,15 @@ import core.postgres as postgres
from core.tz import user_now, user_today_for
def _normalize_time(val):
"""Convert datetime.time objects to 'HH:MM' strings for use in VARCHAR queries."""
if isinstance(val, time):
return val.strftime("%H:%M")
if val is not None:
return str(val)[:5]
return val
def get_adaptive_settings(user_uuid: str) -> Optional[Dict]:
"""Get user's adaptive medication settings."""
rows = postgres.select("adaptive_med_settings", {"user_uuid": user_uuid})
@@ -226,7 +235,7 @@ def calculate_adjusted_times(
def should_send_nag(
user_uuid: str, med_id: str, scheduled_time: str, current_time: datetime
user_uuid: str, med_id: str, scheduled_time, current_time: datetime
) -> Tuple[bool, str]:
"""
Determine if we should send a nag notification.
@@ -234,6 +243,8 @@ def should_send_nag(
Returns:
(should_nag: bool, reason: str)
"""
scheduled_time = _normalize_time(scheduled_time)
settings = get_adaptive_settings(user_uuid)
if not settings:
return False, "No settings"
@@ -301,8 +312,9 @@ def should_send_nag(
return True, "Time to nag"
def record_nag_sent(user_uuid: str, med_id: str, scheduled_time: str):
def record_nag_sent(user_uuid: str, med_id: str, scheduled_time):
"""Record that a nag was sent."""
scheduled_time = _normalize_time(scheduled_time)
today = user_today_for(user_uuid)
query = {"user_uuid": user_uuid, "medication_id": med_id, "adjustment_date": today}
@@ -353,8 +365,9 @@ def create_daily_schedule(user_uuid: str, med_id: str, base_times: List[str]):
postgres.insert("medication_schedules", data)
def mark_med_taken(user_uuid: str, med_id: str, scheduled_time: str):
def mark_med_taken(user_uuid: str, med_id: str, scheduled_time):
"""Mark a medication as taken."""
scheduled_time = _normalize_time(scheduled_time)
today = user_today_for(user_uuid)
postgres.update(