fixing snitch and adaptive medication timing persistance and logic

This commit is contained in:
2026-02-17 18:52:35 -06:00
parent cf2b4be033
commit f826e511d5
4 changed files with 47 additions and 46 deletions

View File

@@ -5,10 +5,12 @@ Handles snitch triggers, contact selection, and notification delivery.
"""
import json
import uuid
from datetime import datetime, timedelta, date
from typing import Optional, Dict, List, Tuple
import core.postgres as postgres
import core.notifications as notifications
from core.tz import user_today_for
def get_snitch_settings(user_uuid: str) -> Optional[Dict]:
@@ -31,8 +33,8 @@ def get_snitch_contacts(user_uuid: str, active_only: bool = True) -> List[Dict]:
def get_todays_snitch_count(user_uuid: str) -> int:
"""Get number of snitches sent today."""
today = date.today()
"""Get number of snitches sent today (in the user's local timezone)."""
today = user_today_for(user_uuid)
# Query snitch log for today
rows = postgres.select("snitch_log", {"user_uuid": user_uuid})
@@ -249,6 +251,7 @@ def send_snitch(
# Log the snitch
log_data = {
"id": str(uuid.uuid4()),
"user_uuid": user_uuid,
"contact_id": contact_id,
"medication_id": med_id,
@@ -307,24 +310,19 @@ def _send_sms_snitch(phone: str, message: str) -> bool:
def update_consent(user_uuid: str, consent_given: bool):
"""Update user's snitch consent status."""
settings = get_snitch_settings(user_uuid)
if settings:
postgres.update(
"snitch_settings",
{"consent_given": consent_given, "updated_at": datetime.utcnow()},
{"user_uuid": user_uuid},
)
else:
# Create settings with consent
data = {
"user_uuid": user_uuid,
"snitch_enabled": False, # Disabled until fully configured
"consent_given": consent_given,
"created_at": datetime.utcnow(),
"updated_at": datetime.utcnow(),
}
postgres.insert("snitch_settings", data)
data = {
"id": str(uuid.uuid4()),
"user_uuid": user_uuid,
"snitch_enabled": False, # Disabled until fully configured
"consent_given": consent_given,
"created_at": datetime.utcnow(),
"updated_at": datetime.utcnow(),
}
postgres.upsert(
"snitch_settings",
data,
conflict_columns=["user_uuid"],
)
def get_snitch_history(user_uuid: str, days: int = 7) -> List[Dict]: