Fix issues #8, #9, #10, #11: scheduling conflicts, med reminders, adaptive timing, nagging

- #11: Add validation to prevent simultaneous scheduling of routines and medications
  - Added _check_schedule_conflicts() in routines.py
  - Added _check_med_schedule_conflicts() in medications.py
  - Returns HTTP 409 with descriptive error on conflict

- #10: Fix medication reminders not being sent
  - Added call to check_adaptive_medication_reminders() in daemon poll loop

- #9: Fix can't enable adaptive timing
  - Added proper error handling and logging in update_adaptive_settings()
  - Returns meaningful error message on database failures

- #8: Fix nagging not working
  - Added debug logging for missing settings
  - Auto-create medication schedules if they don't exist
  - Improved error logging (warning -> error)
This commit is contained in:
Chelsea
2026-02-17 04:20:34 +00:00
parent 3aad7a4867
commit a2c7940a5c
4 changed files with 166 additions and 25 deletions

View File

@@ -305,22 +305,56 @@ def check_nagging():
# Get user's settings
settings = adaptive_meds.get_adaptive_settings(user_uuid)
if not settings or not settings.get("nagging_enabled"):
if not settings:
logger.debug(f"No adaptive settings for user {user_uuid}")
continue
if not settings.get("nagging_enabled"):
logger.debug(f"Nagging disabled for user {user_uuid}")
continue
now = datetime.utcnow()
today = now.date()
# Get today's schedules
schedules = postgres.select(
"medication_schedules",
where={
"user_uuid": user_uuid,
"medication_id": med_id,
"adjustment_date": today,
"status": "pending",
},
)
try:
schedules = postgres.select(
"medication_schedules",
where={
"user_uuid": user_uuid,
"medication_id": med_id,
"adjustment_date": today,
"status": "pending",
},
)
except Exception as e:
logger.warning(f"Could not query medication_schedules for {med_id}: {e}")
# Table may not exist yet
continue
# If no schedules exist, try to create them
if not schedules:
logger.info(f"No schedules found for medication {med_id}, attempting to create")
times = med.get("times", [])
if times:
try:
adaptive_meds.create_daily_schedule(user_uuid, med_id, times)
# Re-query for schedules
schedules = postgres.select(
"medication_schedules",
where={
"user_uuid": user_uuid,
"medication_id": med_id,
"adjustment_date": today,
"status": "pending",
},
)
except Exception as e:
logger.warning(f"Could not create schedules for {med_id}: {e}")
continue
if not schedules:
logger.debug(f"No pending schedules for medication {med_id}")
continue
for sched in schedules:
# Check if we should nag
@@ -406,16 +440,19 @@ def poll_callback():
f"Could not create adaptive schedules (tables may not exist): {e}"
)
# Check reminders - ALWAYS use original check for now
# (adaptive check requires database migration)
logger.info("Checking medication reminders (using original method)")
# Check reminders - use both original and adaptive checks
logger.info("Checking medication reminders")
check_medication_reminders()
try:
check_adaptive_medication_reminders()
except Exception as e:
logger.warning(f"Adaptive medication reminder check failed: {e}")
# Check for nags
# Check for nags - log as error to help with debugging
try:
check_nagging()
except Exception as e:
logger.warning(f"Nagging check failed: {e}")
logger.error(f"Nagging check failed: {e}")
# Original checks
check_routine_reminders()