Update endpoints.py
Browse files- endpoints.py +34 -0
endpoints.py
CHANGED
@@ -175,6 +175,28 @@ def create_router(agent, logger, patients_collection, analysis_collection, users
|
|
175 |
logger.error(f"Error fetching chats: {e}")
|
176 |
raise HTTPException(status_code=500, detail="Failed to retrieve chats")
|
177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
@router.post("/voice/transcribe")
|
179 |
async def transcribe_voice(
|
180 |
audio: UploadFile = File(...),
|
@@ -323,6 +345,18 @@ def create_router(agent, logger, patients_collection, analysis_collection, users
|
|
323 |
file_content=file_content
|
324 |
)
|
325 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
326 |
if "_id" in analysis and isinstance(analysis["_id"], ObjectId):
|
327 |
analysis["_id"] = str(analysis["_id"])
|
328 |
if "timestamp" in analysis and isinstance(analysis["timestamp"], datetime):
|
|
|
175 |
logger.error(f"Error fetching chats: {e}")
|
176 |
raise HTTPException(status_code=500, detail="Failed to retrieve chats")
|
177 |
|
178 |
+
@router.get("/notifications")
|
179 |
+
async def get_notifications(
|
180 |
+
current_user: dict = Depends(get_current_user)
|
181 |
+
):
|
182 |
+
logger.info(f"Fetching notifications for {current_user['email']}")
|
183 |
+
try:
|
184 |
+
# Fetch notifications for the current user (e.g., alerts related to their patients)
|
185 |
+
notifications = await notifications_collection.find({"user_id": current_user["email"]}).sort("timestamp", -1).to_list(length=10)
|
186 |
+
return [
|
187 |
+
{
|
188 |
+
"id": str(notification["_id"]),
|
189 |
+
"message": notification.get("message", "No message"),
|
190 |
+
"timestamp": notification.get("timestamp", datetime.utcnow()).isoformat(),
|
191 |
+
"patient_id": notification.get("patient_id"),
|
192 |
+
"severity": notification.get("severity", "info")
|
193 |
+
}
|
194 |
+
for notification in notifications
|
195 |
+
]
|
196 |
+
except Exception as e:
|
197 |
+
logger.error(f"Error fetching notifications: {e}")
|
198 |
+
raise HTTPException(status_code=500, detail="Failed to retrieve notifications")
|
199 |
+
|
200 |
@router.post("/voice/transcribe")
|
201 |
async def transcribe_voice(
|
202 |
audio: UploadFile = File(...),
|
|
|
345 |
file_content=file_content
|
346 |
)
|
347 |
|
348 |
+
# Create a notification if suicide risk is detected
|
349 |
+
if analysis.get("suicide_risk", {}).get("level") != "none":
|
350 |
+
notification = {
|
351 |
+
"user_id": current_user["email"],
|
352 |
+
"message": f"Suicide risk alert for patient {patient_id}: {analysis['suicide_risk']['level'].upper()} (Score: {analysis['suicide_risk']['score']})",
|
353 |
+
"patient_id": patient_id,
|
354 |
+
"timestamp": datetime.utcnow(),
|
355 |
+
"severity": "high" if analysis["suicide_risk"]["level"] in ["moderate", "severe"] else "medium"
|
356 |
+
}
|
357 |
+
await notifications_collection.insert_one(notification)
|
358 |
+
logger.info(f"✅ Created notification for suicide risk alert: {notification}")
|
359 |
+
|
360 |
if "_id" in analysis and isinstance(analysis["_id"], ObjectId):
|
361 |
analysis["_id"] = str(analysis["_id"])
|
362 |
if "timestamp" in analysis and isinstance(analysis["timestamp"], datetime):
|