lots of changes leave me alone its better now
This commit is contained in:
@@ -17,6 +17,7 @@ import api.routes.routine_sessions_extended as routine_sessions_extended_routes
|
||||
import api.routes.routine_templates as routine_templates_routes
|
||||
import api.routes.routine_stats as routine_stats_routes
|
||||
import api.routes.routine_tags as routine_tags_routes
|
||||
import api.routes.notifications as notifications_routes
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
CORS(app)
|
||||
@@ -29,6 +30,7 @@ ROUTE_MODULES = [
|
||||
routine_templates_routes,
|
||||
routine_stats_routes,
|
||||
routine_tags_routes,
|
||||
notifications_routes,
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -41,6 +41,11 @@ def _wrap_json(data):
|
||||
return {k: Json(v) if k in _JSON_COLS and isinstance(v, (list, dict)) else v for k, v in data.items()}
|
||||
|
||||
|
||||
def _filter_times_in_range(times, start, end):
|
||||
"""Return times (HH:MM strings) that fall within [start, end] inclusive."""
|
||||
return [t for t in times if start <= t <= end]
|
||||
|
||||
|
||||
def _is_med_due_today(med, today, current_day):
|
||||
"""Check if a medication is scheduled for today based on its frequency."""
|
||||
freq = med.get("frequency", "daily")
|
||||
@@ -306,7 +311,12 @@ def register(app):
|
||||
|
||||
@app.route("/api/medications/today", methods=["GET"])
|
||||
def api_todaysMeds():
|
||||
"""Get today's medication schedule with taken/pending status."""
|
||||
"""Get today's medication schedule with taken/pending status.
|
||||
|
||||
Includes cross-midnight lookahead:
|
||||
- After 22:00: includes next-day meds scheduled 00:00-02:00 (is_next_day)
|
||||
- Before 02:00: includes previous-day meds scheduled 22:00-23:59 (is_previous_day)
|
||||
"""
|
||||
user_uuid = _auth(flask.request)
|
||||
if not user_uuid:
|
||||
return flask.jsonify({"error": "unauthorized"}), 401
|
||||
@@ -316,8 +326,12 @@ def register(app):
|
||||
today = date.today()
|
||||
today_str = today.isoformat()
|
||||
current_day = now.strftime("%a").lower() # "mon","tue", etc.
|
||||
current_hour = now.hour
|
||||
|
||||
result = []
|
||||
seen_med_ids = set()
|
||||
|
||||
# Main pass: today's meds
|
||||
for med in meds:
|
||||
if not _is_med_due_today(med, today, current_day):
|
||||
continue
|
||||
@@ -325,7 +339,6 @@ def register(app):
|
||||
freq = med.get("frequency", "daily")
|
||||
is_prn = freq == "as_needed"
|
||||
|
||||
# Get today's taken times by filtering on created_at date
|
||||
all_logs = postgres.select(
|
||||
"med_logs",
|
||||
where={"medication_id": med["id"], "action": "taken"},
|
||||
@@ -342,6 +355,82 @@ def register(app):
|
||||
"taken_times": today_taken,
|
||||
"is_prn": is_prn,
|
||||
})
|
||||
seen_med_ids.add(med["id"])
|
||||
|
||||
# Late night pass (22:00+): include next-day meds scheduled 00:00-02:00
|
||||
if current_hour >= 22:
|
||||
tomorrow = today + timedelta(days=1)
|
||||
tomorrow_str = tomorrow.isoformat()
|
||||
tomorrow_day = (now + timedelta(days=1)).strftime("%a").lower()
|
||||
for med in meds:
|
||||
if med["id"] in seen_med_ids:
|
||||
continue
|
||||
if not _is_med_due_today(med, tomorrow, tomorrow_day):
|
||||
continue
|
||||
freq = med.get("frequency", "daily")
|
||||
if freq == "as_needed":
|
||||
continue
|
||||
times = med.get("times", [])
|
||||
early_times = _filter_times_in_range(times, "00:00", "02:00")
|
||||
if not early_times:
|
||||
continue
|
||||
|
||||
all_logs = postgres.select(
|
||||
"med_logs",
|
||||
where={"medication_id": med["id"], "action": "taken"},
|
||||
)
|
||||
tomorrow_taken = [
|
||||
log.get("scheduled_time", "")
|
||||
for log in all_logs
|
||||
if str(log.get("created_at", ""))[:10] == tomorrow_str
|
||||
]
|
||||
|
||||
result.append({
|
||||
"medication": med,
|
||||
"scheduled_times": early_times,
|
||||
"taken_times": tomorrow_taken,
|
||||
"is_prn": False,
|
||||
"is_next_day": True,
|
||||
})
|
||||
seen_med_ids.add(med["id"])
|
||||
|
||||
# Early morning pass (<02:00): include previous-day meds scheduled 22:00-23:59
|
||||
if current_hour < 2:
|
||||
yesterday = today - timedelta(days=1)
|
||||
yesterday_str = yesterday.isoformat()
|
||||
yesterday_day = (now - timedelta(days=1)).strftime("%a").lower()
|
||||
for med in meds:
|
||||
if med["id"] in seen_med_ids:
|
||||
continue
|
||||
if not _is_med_due_today(med, yesterday, yesterday_day):
|
||||
continue
|
||||
freq = med.get("frequency", "daily")
|
||||
if freq == "as_needed":
|
||||
continue
|
||||
times = med.get("times", [])
|
||||
late_times = _filter_times_in_range(times, "22:00", "23:59")
|
||||
if not late_times:
|
||||
continue
|
||||
|
||||
all_logs = postgres.select(
|
||||
"med_logs",
|
||||
where={"medication_id": med["id"], "action": "taken"},
|
||||
)
|
||||
yesterday_taken = [
|
||||
log.get("scheduled_time", "")
|
||||
for log in all_logs
|
||||
if str(log.get("created_at", ""))[:10] == yesterday_str
|
||||
]
|
||||
|
||||
result.append({
|
||||
"medication": med,
|
||||
"scheduled_times": late_times,
|
||||
"taken_times": yesterday_taken,
|
||||
"is_prn": False,
|
||||
"is_previous_day": True,
|
||||
})
|
||||
seen_med_ids.add(med["id"])
|
||||
|
||||
return flask.jsonify(result), 200
|
||||
|
||||
# ── Adherence Stats ───────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user