Fix presence tracking and med reminder bugs

This commit is contained in:
2026-02-19 20:31:43 -06:00
parent a19e30db68
commit cc1aace73d
3 changed files with 136 additions and 40 deletions

View File

@@ -270,7 +270,7 @@ def should_send_nag(
return False, "User offline"
# Get today's schedule record for this specific time slot
today = current_time.date()
today = user_today_for(user_uuid)
query = {"user_uuid": user_uuid, "medication_id": med_id, "adjustment_date": today}
if scheduled_time is not None:
query["adjusted_time"] = scheduled_time
@@ -304,19 +304,56 @@ def should_send_nag(
"medication_id": med_id,
"user_uuid": user_uuid,
"action": "taken",
"scheduled_time": scheduled_time,
},
)
# Filter to today's logs for this time slot
today_logs = [
log
for log in logs
if log.get("created_at") and log["created_at"].date() == today
]
# Get medication times to calculate dose interval for proximity check
med = postgres.select_one("medications", {"id": med_id})
dose_interval_minutes = 60 # default fallback
if med and med.get("times"):
times = med["times"]
if len(times) >= 2:
time_minutes = []
for t in times:
t = _normalize_time(t)
if t:
h, m = int(t[:2]), int(t[3:5])
time_minutes.append(h * 60 + m)
time_minutes.sort()
intervals = []
for i in range(1, len(time_minutes)):
intervals.append(time_minutes[i] - time_minutes[i - 1])
if intervals:
dose_interval_minutes = min(intervals)
if today_logs:
return False, "Already taken today"
proximity_window = max(30, dose_interval_minutes // 2)
# Filter to today's logs and check for this specific dose
for log in logs:
created_at = log.get("created_at")
if not created_at:
continue
if created_at.date() != today:
continue
log_scheduled_time = log.get("scheduled_time")
if log_scheduled_time:
log_scheduled_time = _normalize_time(log_scheduled_time)
if log_scheduled_time == scheduled_time:
return False, "Already taken today"
else:
if scheduled_time and created_at:
log_hour = created_at.hour
log_min = created_at.minute
sched_hour, sched_min = (
int(scheduled_time[:2]),
int(scheduled_time[3:5]),
)
diff_minutes = abs(
(log_hour * 60 + log_min) - (sched_hour * 60 + sched_min)
)
if diff_minutes <= proximity_window:
return False, "Already taken today"
return True, "Time to nag"