fix: include scheduled_time when logging medication intake from reminders
This commit is contained in:
@@ -33,6 +33,7 @@ def check_medication_reminders():
|
||||
"""Check for medications due now and send notifications."""
|
||||
try:
|
||||
from datetime import date as date_type
|
||||
|
||||
meds = postgres.select("medications", where={"active": True})
|
||||
|
||||
# Group by user so we only look up timezone once per user
|
||||
@@ -68,8 +69,14 @@ def check_medication_reminders():
|
||||
start = med.get("start_date")
|
||||
interval = med.get("interval_days")
|
||||
if start and interval:
|
||||
start_d = start if isinstance(start, date_type) else datetime.strptime(str(start), "%Y-%m-%d").date()
|
||||
if (today - start_d).days < 0 or (today - start_d).days % interval != 0:
|
||||
start_d = (
|
||||
start
|
||||
if isinstance(start, date_type)
|
||||
else datetime.strptime(str(start), "%Y-%m-%d").date()
|
||||
)
|
||||
if (today - start_d).days < 0 or (
|
||||
today - start_d
|
||||
).days % interval != 0:
|
||||
continue
|
||||
else:
|
||||
continue
|
||||
@@ -80,7 +87,9 @@ def check_medication_reminders():
|
||||
continue
|
||||
|
||||
# Already taken today? Check by created_at date
|
||||
logs = postgres.select("med_logs", where={"medication_id": med["id"], "action": "taken"})
|
||||
logs = postgres.select(
|
||||
"med_logs", where={"medication_id": med["id"], "action": "taken"}
|
||||
)
|
||||
already_taken = any(
|
||||
log.get("scheduled_time") == current_time
|
||||
and str(log.get("created_at", ""))[:10] == today_str
|
||||
@@ -91,8 +100,10 @@ def check_medication_reminders():
|
||||
|
||||
user_settings = notifications.getNotificationSettings(user_uuid)
|
||||
if user_settings:
|
||||
msg = f"Time to take {med['name']} ({med['dosage']} {med['unit']})"
|
||||
notifications._sendToEnabledChannels(user_settings, msg, user_uuid=user_uuid)
|
||||
msg = f"Time to take {med['name']} ({med['dosage']} {med['unit']}) · {current_time}"
|
||||
notifications._sendToEnabledChannels(
|
||||
user_settings, msg, user_uuid=user_uuid
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking medication reminders: {e}")
|
||||
|
||||
@@ -120,7 +131,9 @@ def check_routine_reminders():
|
||||
user_settings = notifications.getNotificationSettings(routine["user_uuid"])
|
||||
if user_settings:
|
||||
msg = f"Time to start your routine: {routine['name']}"
|
||||
notifications._sendToEnabledChannels(user_settings, msg, user_uuid=routine["user_uuid"])
|
||||
notifications._sendToEnabledChannels(
|
||||
user_settings, msg, user_uuid=routine["user_uuid"]
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking routine reminders: {e}")
|
||||
|
||||
@@ -135,7 +148,9 @@ def check_refills():
|
||||
user_settings = notifications.getNotificationSettings(med["user_uuid"])
|
||||
if user_settings:
|
||||
msg = f"Low on {med['name']}: only {qty} doses remaining. Time to refill!"
|
||||
notifications._sendToEnabledChannels(user_settings, msg, user_uuid=med["user_uuid"])
|
||||
notifications._sendToEnabledChannels(
|
||||
user_settings, msg, user_uuid=med["user_uuid"]
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking refills: {e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user