fixing snitch and adaptive medication timing persistance and logic

This commit is contained in:
2026-02-17 18:52:35 -06:00
parent cf2b4be033
commit f826e511d5
4 changed files with 47 additions and 46 deletions

View File

@@ -9,6 +9,7 @@ This module handles:
"""
import json
import uuid
from datetime import datetime, timedelta, time
from typing import Optional, Dict, List, Tuple
import core.postgres as postgres
@@ -50,6 +51,7 @@ def update_user_presence(user_uuid: str, discord_user_id: str, is_online: bool):
else:
# Create new record
data = {
"id": str(uuid.uuid4()),
"user_uuid": user_uuid,
"discord_user_id": discord_user_id,
"is_currently_online": is_online,
@@ -275,12 +277,18 @@ def should_send_nag(
if time_since_last_nag < nag_interval:
return False, f"Too soon ({int(time_since_last_nag)} < {nag_interval} min)"
# Check if medication was already taken today
# Check if this specific dose was already taken today
logs = postgres.select(
"med_logs", {"medication_id": med_id, "user_uuid": user_uuid, "action": "taken"}
"med_logs",
{
"medication_id": med_id,
"user_uuid": user_uuid,
"action": "taken",
"scheduled_time": scheduled_time,
},
)
# Filter to today's logs
# Filter to today's logs for this time slot
today_logs = [
log
for log in logs
@@ -297,10 +305,10 @@ def record_nag_sent(user_uuid: str, med_id: str, scheduled_time: str):
"""Record that a nag was sent."""
today = user_today_for(user_uuid)
schedules = postgres.select(
"medication_schedules",
{"user_uuid": user_uuid, "medication_id": med_id, "adjustment_date": today},
)
query = {"user_uuid": user_uuid, "medication_id": med_id, "adjustment_date": today}
if scheduled_time is not None:
query["adjusted_time"] = scheduled_time
schedules = postgres.select("medication_schedules", query)
if schedules:
schedule = schedules[0]
@@ -332,6 +340,7 @@ def create_daily_schedule(user_uuid: str, med_id: str, base_times: List[str]):
# Create schedule records for each time
for base_time, (adjusted_time, offset) in zip(base_times, adjusted_times):
data = {
"id": str(uuid.uuid4()),
"user_uuid": user_uuid,
"medication_id": med_id,
"base_time": base_time,