- #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:
@@ -2,6 +2,7 @@
|
||||
Medications API - medication scheduling, logging, and adherence tracking
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import uuid
|
||||
from datetime import datetime, date, timedelta, timezone
|
||||
@@ -144,6 +145,35 @@ def register(app):
|
||||
meds = postgres.select("medications", where={"user_uuid": user_uuid}, order_by="name")
|
||||
return flask.jsonify(meds), 200
|
||||
|
||||
def _check_med_schedule_conflicts(user_uuid, new_times, new_days=None, exclude_med_id=None):
|
||||
"""Check if the proposed medication schedule conflicts with existing routines or medications.
|
||||
Returns (has_conflict, conflict_message) tuple.
|
||||
"""
|
||||
if not new_times:
|
||||
return False, None
|
||||
|
||||
# Check conflicts with routines
|
||||
user_routines = postgres.select("routines", {"user_uuid": user_uuid})
|
||||
for r in user_routines:
|
||||
sched = postgres.select_one("routine_schedules", {"routine_id": r["id"]})
|
||||
if sched and sched.get("time") in new_times:
|
||||
routine_days = json.loads(sched.get("days", "[]"))
|
||||
if not new_days or any(d in routine_days for d in new_days):
|
||||
return True, f"Time conflicts with routine: {r.get('name', 'Unnamed routine')}"
|
||||
|
||||
# Check conflicts with other medications
|
||||
user_meds = postgres.select("medications", {"user_uuid": user_uuid, "active": True})
|
||||
for med in user_meds:
|
||||
if med["id"] == exclude_med_id:
|
||||
continue
|
||||
med_times = med.get("times", [])
|
||||
if isinstance(med_times, str):
|
||||
med_times = json.loads(med_times)
|
||||
if any(t in med_times for t in new_times):
|
||||
return True, f"Time conflicts with medication: {med.get('name', 'Unnamed medication')}"
|
||||
|
||||
return False, None
|
||||
|
||||
@app.route("/api/medications", methods=["POST"])
|
||||
def api_addMedication():
|
||||
"""Add a medication. Body: {name, dosage, unit, frequency, times?, days_of_week?, interval_days?, start_date?, notes?}"""
|
||||
@@ -158,6 +188,15 @@ def register(app):
|
||||
if missing:
|
||||
return flask.jsonify({"error": f"missing required fields: {', '.join(missing)}"}), 400
|
||||
|
||||
# Check for schedule conflicts
|
||||
new_times = data.get("times", [])
|
||||
new_days = data.get("days_of_week", [])
|
||||
has_conflict, conflict_msg = _check_med_schedule_conflicts(
|
||||
user_uuid, new_times, new_days
|
||||
)
|
||||
if has_conflict:
|
||||
return flask.jsonify({"error": conflict_msg}), 409
|
||||
|
||||
row = {
|
||||
"id": str(uuid.uuid4()),
|
||||
"user_uuid": user_uuid,
|
||||
@@ -217,6 +256,17 @@ def register(app):
|
||||
"name", "dosage", "unit", "frequency", "times", "notes", "active",
|
||||
"days_of_week", "interval_days", "start_date", "next_dose_date",
|
||||
]
|
||||
|
||||
# Check for schedule conflicts if times are being updated
|
||||
if "times" in data:
|
||||
new_times = data.get("times", [])
|
||||
new_days = data.get("days_of_week") or existing.get("days_of_week", [])
|
||||
has_conflict, conflict_msg = _check_med_schedule_conflicts(
|
||||
user_uuid, new_times, new_days, exclude_med_id=med_id
|
||||
)
|
||||
if has_conflict:
|
||||
return flask.jsonify({"error": conflict_msg}), 409
|
||||
|
||||
updates = {k: v for k, v in data.items() if k in allowed}
|
||||
if not updates:
|
||||
return flask.jsonify({"error": "no valid fields to update"}), 400
|
||||
|
||||
Reference in New Issue
Block a user