Update config.py
Browse files
config.py
CHANGED
@@ -4,6 +4,7 @@ from fastapi import FastAPI
|
|
4 |
from txagent.txagent import TxAgent
|
5 |
from db.mongo import get_mongo_client
|
6 |
import asyncio
|
|
|
7 |
|
8 |
# Logging
|
9 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
@@ -36,9 +37,33 @@ users_collection = db["users"]
|
|
36 |
patients_collection = db["patients"]
|
37 |
analysis_collection = db["patient_analysis_results"]
|
38 |
alerts_collection = db["clinical_alerts"]
|
39 |
-
notifications_collection = db["notifications"]
|
40 |
logger.info("📡 Connected to MongoDB synchronously")
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# JWT settings
|
43 |
SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key")
|
44 |
ALGORITHM = "HS256"
|
|
|
4 |
from txagent.txagent import TxAgent
|
5 |
from db.mongo import get_mongo_client
|
6 |
import asyncio
|
7 |
+
from pyfcm import FCMNotification
|
8 |
|
9 |
# Logging
|
10 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
|
37 |
patients_collection = db["patients"]
|
38 |
analysis_collection = db["patient_analysis_results"]
|
39 |
alerts_collection = db["clinical_alerts"]
|
40 |
+
notifications_collection = db["notifications"]
|
41 |
logger.info("📡 Connected to MongoDB synchronously")
|
42 |
|
43 |
+
# FCM settings
|
44 |
+
FCM_API_KEY = os.getenv("FCM_API_KEY", "your-fcm-server-key")
|
45 |
+
push_service = FCMNotification(api_key=FCM_API_KEY)
|
46 |
+
|
47 |
+
async def send_push_notification(recipient_email, message):
|
48 |
+
try:
|
49 |
+
# Fetch the user's device token
|
50 |
+
user = await users_collection.find_one({"email": recipient_email})
|
51 |
+
device_token = user.get("device_token") if user and "device_token" in user else None
|
52 |
+
if not device_token:
|
53 |
+
logger.warning(f"No device token found for {recipient_email}")
|
54 |
+
return
|
55 |
+
|
56 |
+
# Send push notification
|
57 |
+
result = push_service.notify_single_device(
|
58 |
+
registration_id=device_token,
|
59 |
+
message_title="Risk Alert",
|
60 |
+
message_body=message,
|
61 |
+
sound="default" # Optional: Plays a sound on the device
|
62 |
+
)
|
63 |
+
logger.info(f"Push notification sent to {recipient_email}: {result}")
|
64 |
+
except Exception as e:
|
65 |
+
logger.error(f"Failed to send push notification to {recipient_email}: {str(e)}")
|
66 |
+
|
67 |
# JWT settings
|
68 |
SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key")
|
69 |
ALGORITHM = "HS256"
|