- #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:
@@ -649,6 +649,40 @@ def register(app):
|
||||
)
|
||||
return flask.jsonify(result), 200
|
||||
|
||||
def _check_schedule_conflicts(user_uuid, new_days, new_time, exclude_routine_id=None):
|
||||
"""Check if the proposed schedule conflicts with existing routines or medications.
|
||||
Returns (has_conflict, conflict_message) tuple.
|
||||
"""
|
||||
if not new_days or not new_time:
|
||||
return False, None
|
||||
|
||||
# Check conflicts with other routines
|
||||
user_routines = postgres.select("routines", {"user_uuid": user_uuid})
|
||||
for r in user_routines:
|
||||
if r["id"] == exclude_routine_id:
|
||||
continue
|
||||
other_sched = postgres.select_one("routine_schedules", {"routine_id": r["id"]})
|
||||
if other_sched and other_sched.get("time") == new_time:
|
||||
other_days = json.loads(other_sched.get("days", "[]"))
|
||||
if any(d in other_days for d in new_days):
|
||||
return True, f"Time conflicts with routine: {r.get('name', 'Unnamed routine')}"
|
||||
|
||||
# Check conflicts with medications
|
||||
user_meds = postgres.select("medications", {"user_uuid": user_uuid, "active": True})
|
||||
for med in user_meds:
|
||||
med_times = med.get("times", [])
|
||||
if isinstance(med_times, str):
|
||||
med_times = json.loads(med_times)
|
||||
if new_time in med_times:
|
||||
# Check if medication runs on any of the same days
|
||||
med_days = med.get("days_of_week", [])
|
||||
if isinstance(med_days, str):
|
||||
med_days = json.loads(med_days)
|
||||
if not med_days or any(d in med_days for d in new_days):
|
||||
return True, f"Time conflicts with medication: {med.get('name', 'Unnamed medication')}"
|
||||
|
||||
return False, None
|
||||
|
||||
@app.route("/api/routines/<routine_id>/schedule", methods=["PUT"])
|
||||
def api_setRoutineSchedule(routine_id):
|
||||
"""Set when this routine should run. Body: {days: ["mon","tue",...], time: "08:00", remind: true}"""
|
||||
@@ -663,6 +697,16 @@ def register(app):
|
||||
data = flask.request.get_json()
|
||||
if not data:
|
||||
return flask.jsonify({"error": "missing body"}), 400
|
||||
|
||||
# Check for schedule conflicts
|
||||
new_days = data.get("days", [])
|
||||
new_time = data.get("time")
|
||||
has_conflict, conflict_msg = _check_schedule_conflicts(
|
||||
user_uuid, new_days, new_time, exclude_routine_id=routine_id
|
||||
)
|
||||
if has_conflict:
|
||||
return flask.jsonify({"error": conflict_msg}), 409
|
||||
|
||||
existing = postgres.select_one("routine_schedules", {"routine_id": routine_id})
|
||||
schedule_data = {
|
||||
"routine_id": routine_id,
|
||||
|
||||
Reference in New Issue
Block a user