fixing snitch and adaptive medication timing persistance and logic
This commit is contained in:
@@ -3,6 +3,7 @@ api/routes/adaptive_meds.py - API endpoints for adaptive medication settings
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import uuid
|
||||||
import flask
|
import flask
|
||||||
import jwt
|
import jwt
|
||||||
import os
|
import os
|
||||||
@@ -103,16 +104,11 @@ def register(app):
|
|||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Check if settings exist
|
|
||||||
existing = adaptive_meds.get_adaptive_settings(user_uuid)
|
|
||||||
|
|
||||||
if existing:
|
|
||||||
postgres.update(
|
|
||||||
"adaptive_med_settings", update_data, {"user_uuid": user_uuid}
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
update_data["user_uuid"] = user_uuid
|
update_data["user_uuid"] = user_uuid
|
||||||
postgres.insert("adaptive_med_settings", update_data)
|
update_data.setdefault("id", str(uuid.uuid4()))
|
||||||
|
postgres.upsert(
|
||||||
|
"adaptive_med_settings", update_data, conflict_columns=["user_uuid"]
|
||||||
|
)
|
||||||
|
|
||||||
return flask.jsonify({"success": True}), 200
|
return flask.jsonify({"success": True}), 200
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
api/routes/snitch.py - API endpoints for snitch system
|
api/routes/snitch.py - API endpoints for snitch system
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import uuid
|
||||||
import flask
|
import flask
|
||||||
import jwt
|
import jwt
|
||||||
import os
|
import os
|
||||||
@@ -89,15 +90,10 @@ def register(app):
|
|||||||
"updated_at": datetime.utcnow(),
|
"updated_at": datetime.utcnow(),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check if settings exist
|
|
||||||
existing = snitch_core.get_snitch_settings(user_uuid)
|
|
||||||
|
|
||||||
if existing:
|
|
||||||
postgres.update("snitch_settings", update_data, {"user_uuid": user_uuid})
|
|
||||||
else:
|
|
||||||
update_data["user_uuid"] = user_uuid
|
update_data["user_uuid"] = user_uuid
|
||||||
update_data["created_at"] = datetime.utcnow()
|
update_data.setdefault("id", str(uuid.uuid4()))
|
||||||
postgres.insert("snitch_settings", update_data)
|
update_data.setdefault("created_at", datetime.utcnow())
|
||||||
|
postgres.upsert("snitch_settings", update_data, conflict_columns=["user_uuid"])
|
||||||
|
|
||||||
return flask.jsonify({"success": True}), 200
|
return flask.jsonify({"success": True}), 200
|
||||||
|
|
||||||
@@ -161,6 +157,7 @@ def register(app):
|
|||||||
), 400
|
), 400
|
||||||
|
|
||||||
contact_data = {
|
contact_data = {
|
||||||
|
"id": str(uuid.uuid4()),
|
||||||
"user_uuid": user_uuid,
|
"user_uuid": user_uuid,
|
||||||
"contact_name": data["contact_name"],
|
"contact_name": data["contact_name"],
|
||||||
"contact_type": data["contact_type"],
|
"contact_type": data["contact_type"],
|
||||||
@@ -277,6 +274,7 @@ def register(app):
|
|||||||
|
|
||||||
# Insert into snitch_log so the bot will pick it up and send it
|
# Insert into snitch_log so the bot will pick it up and send it
|
||||||
log_data = {
|
log_data = {
|
||||||
|
"id": str(uuid.uuid4()),
|
||||||
"user_uuid": user_uuid,
|
"user_uuid": user_uuid,
|
||||||
"contact_id": contact.get("id"),
|
"contact_id": contact.get("id"),
|
||||||
"medication_id": None, # Test snitch, no actual medication
|
"medication_id": None, # Test snitch, no actual medication
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ This module handles:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import uuid
|
||||||
from datetime import datetime, timedelta, time
|
from datetime import datetime, timedelta, time
|
||||||
from typing import Optional, Dict, List, Tuple
|
from typing import Optional, Dict, List, Tuple
|
||||||
import core.postgres as postgres
|
import core.postgres as postgres
|
||||||
@@ -50,6 +51,7 @@ def update_user_presence(user_uuid: str, discord_user_id: str, is_online: bool):
|
|||||||
else:
|
else:
|
||||||
# Create new record
|
# Create new record
|
||||||
data = {
|
data = {
|
||||||
|
"id": str(uuid.uuid4()),
|
||||||
"user_uuid": user_uuid,
|
"user_uuid": user_uuid,
|
||||||
"discord_user_id": discord_user_id,
|
"discord_user_id": discord_user_id,
|
||||||
"is_currently_online": is_online,
|
"is_currently_online": is_online,
|
||||||
@@ -275,12 +277,18 @@ def should_send_nag(
|
|||||||
if time_since_last_nag < nag_interval:
|
if time_since_last_nag < nag_interval:
|
||||||
return False, f"Too soon ({int(time_since_last_nag)} < {nag_interval} min)"
|
return False, f"Too soon ({int(time_since_last_nag)} < {nag_interval} min)"
|
||||||
|
|
||||||
# Check if medication was already taken today
|
# Check if this specific dose was already taken today
|
||||||
logs = postgres.select(
|
logs = postgres.select(
|
||||||
"med_logs", {"medication_id": med_id, "user_uuid": user_uuid, "action": "taken"}
|
"med_logs",
|
||||||
|
{
|
||||||
|
"medication_id": med_id,
|
||||||
|
"user_uuid": user_uuid,
|
||||||
|
"action": "taken",
|
||||||
|
"scheduled_time": scheduled_time,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
# Filter to today's logs
|
# Filter to today's logs for this time slot
|
||||||
today_logs = [
|
today_logs = [
|
||||||
log
|
log
|
||||||
for log in logs
|
for log in logs
|
||||||
@@ -297,10 +305,10 @@ def record_nag_sent(user_uuid: str, med_id: str, scheduled_time: str):
|
|||||||
"""Record that a nag was sent."""
|
"""Record that a nag was sent."""
|
||||||
today = user_today_for(user_uuid)
|
today = user_today_for(user_uuid)
|
||||||
|
|
||||||
schedules = postgres.select(
|
query = {"user_uuid": user_uuid, "medication_id": med_id, "adjustment_date": today}
|
||||||
"medication_schedules",
|
if scheduled_time is not None:
|
||||||
{"user_uuid": user_uuid, "medication_id": med_id, "adjustment_date": today},
|
query["adjusted_time"] = scheduled_time
|
||||||
)
|
schedules = postgres.select("medication_schedules", query)
|
||||||
|
|
||||||
if schedules:
|
if schedules:
|
||||||
schedule = schedules[0]
|
schedule = schedules[0]
|
||||||
@@ -332,6 +340,7 @@ def create_daily_schedule(user_uuid: str, med_id: str, base_times: List[str]):
|
|||||||
# Create schedule records for each time
|
# Create schedule records for each time
|
||||||
for base_time, (adjusted_time, offset) in zip(base_times, adjusted_times):
|
for base_time, (adjusted_time, offset) in zip(base_times, adjusted_times):
|
||||||
data = {
|
data = {
|
||||||
|
"id": str(uuid.uuid4()),
|
||||||
"user_uuid": user_uuid,
|
"user_uuid": user_uuid,
|
||||||
"medication_id": med_id,
|
"medication_id": med_id,
|
||||||
"base_time": base_time,
|
"base_time": base_time,
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ Handles snitch triggers, contact selection, and notification delivery.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import uuid
|
||||||
from datetime import datetime, timedelta, date
|
from datetime import datetime, timedelta, date
|
||||||
from typing import Optional, Dict, List, Tuple
|
from typing import Optional, Dict, List, Tuple
|
||||||
import core.postgres as postgres
|
import core.postgres as postgres
|
||||||
import core.notifications as notifications
|
import core.notifications as notifications
|
||||||
|
from core.tz import user_today_for
|
||||||
|
|
||||||
|
|
||||||
def get_snitch_settings(user_uuid: str) -> Optional[Dict]:
|
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:
|
def get_todays_snitch_count(user_uuid: str) -> int:
|
||||||
"""Get number of snitches sent today."""
|
"""Get number of snitches sent today (in the user's local timezone)."""
|
||||||
today = date.today()
|
today = user_today_for(user_uuid)
|
||||||
|
|
||||||
# Query snitch log for today
|
# Query snitch log for today
|
||||||
rows = postgres.select("snitch_log", {"user_uuid": user_uuid})
|
rows = postgres.select("snitch_log", {"user_uuid": user_uuid})
|
||||||
@@ -249,6 +251,7 @@ def send_snitch(
|
|||||||
|
|
||||||
# Log the snitch
|
# Log the snitch
|
||||||
log_data = {
|
log_data = {
|
||||||
|
"id": str(uuid.uuid4()),
|
||||||
"user_uuid": user_uuid,
|
"user_uuid": user_uuid,
|
||||||
"contact_id": contact_id,
|
"contact_id": contact_id,
|
||||||
"medication_id": med_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):
|
def update_consent(user_uuid: str, consent_given: bool):
|
||||||
"""Update user's snitch consent status."""
|
"""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 = {
|
data = {
|
||||||
|
"id": str(uuid.uuid4()),
|
||||||
"user_uuid": user_uuid,
|
"user_uuid": user_uuid,
|
||||||
"snitch_enabled": False, # Disabled until fully configured
|
"snitch_enabled": False, # Disabled until fully configured
|
||||||
"consent_given": consent_given,
|
"consent_given": consent_given,
|
||||||
"created_at": datetime.utcnow(),
|
"created_at": datetime.utcnow(),
|
||||||
"updated_at": datetime.utcnow(),
|
"updated_at": datetime.utcnow(),
|
||||||
}
|
}
|
||||||
postgres.insert("snitch_settings", data)
|
postgres.upsert(
|
||||||
|
"snitch_settings",
|
||||||
|
data,
|
||||||
|
conflict_columns=["user_uuid"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_snitch_history(user_uuid: str, days: int = 7) -> List[Dict]:
|
def get_snitch_history(user_uuid: str, days: int = 7) -> List[Dict]:
|
||||||
|
|||||||
Reference in New Issue
Block a user