116 lines
3.4 KiB
Python
116 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
AZA Empfang – Backend-Routen für Empfangs-/Rezeptionsnachrichten.
|
||
|
||
Desktop sendet Nachrichten per POST /empfang/send.
|
||
Empfangsoberfläche zeigt sie unter /empfang/ im Browser.
|
||
"""
|
||
|
||
import json
|
||
import os
|
||
import time
|
||
import uuid
|
||
from pathlib import Path
|
||
|
||
from fastapi import APIRouter, HTTPException, Request
|
||
from fastapi.responses import HTMLResponse, JSONResponse
|
||
from pydantic import BaseModel, Field
|
||
|
||
router = APIRouter()
|
||
|
||
_DATA_DIR = Path(__file__).resolve().parent / "data"
|
||
_EMPFANG_FILE = _DATA_DIR / "empfang_nachrichten.json"
|
||
|
||
|
||
def _ensure_data_dir():
|
||
_DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||
|
||
|
||
def _load_messages() -> list[dict]:
|
||
if not _EMPFANG_FILE.is_file():
|
||
return []
|
||
try:
|
||
with open(_EMPFANG_FILE, "r", encoding="utf-8") as f:
|
||
data = json.load(f)
|
||
return data if isinstance(data, list) else []
|
||
except Exception:
|
||
return []
|
||
|
||
|
||
def _save_messages(messages: list[dict]):
|
||
_ensure_data_dir()
|
||
tmp = str(_EMPFANG_FILE) + ".tmp"
|
||
with open(tmp, "w", encoding="utf-8") as f:
|
||
json.dump(messages, f, indent=2, ensure_ascii=False)
|
||
os.replace(tmp, str(_EMPFANG_FILE))
|
||
|
||
|
||
class EmpfangMessage(BaseModel):
|
||
medikamente: str = ""
|
||
therapieplan: str = ""
|
||
procedere: str = ""
|
||
kommentar: str = ""
|
||
patient: str = ""
|
||
absender: str = ""
|
||
zeitstempel: str = ""
|
||
extras: dict = Field(default_factory=dict)
|
||
|
||
|
||
@router.post("/send")
|
||
async def empfang_send(msg: EmpfangMessage):
|
||
entry = {
|
||
"id": uuid.uuid4().hex[:12],
|
||
"medikamente": msg.medikamente.strip(),
|
||
"therapieplan": msg.therapieplan.strip(),
|
||
"procedere": msg.procedere.strip(),
|
||
"kommentar": msg.kommentar.strip(),
|
||
"patient": msg.patient.strip(),
|
||
"absender": msg.absender.strip(),
|
||
"zeitstempel": msg.zeitstempel.strip() or time.strftime("%Y-%m-%d %H:%M:%S"),
|
||
"empfangen": time.strftime("%Y-%m-%d %H:%M:%S"),
|
||
"status": "offen",
|
||
}
|
||
if msg.extras:
|
||
entry["extras"] = msg.extras
|
||
|
||
messages = _load_messages()
|
||
messages.insert(0, entry)
|
||
_save_messages(messages)
|
||
|
||
return JSONResponse(content={"success": True, "id": entry["id"]})
|
||
|
||
|
||
@router.get("/messages")
|
||
async def empfang_list():
|
||
messages = _load_messages()
|
||
return JSONResponse(content={"success": True, "messages": messages})
|
||
|
||
|
||
@router.post("/messages/{msg_id}/done")
|
||
async def empfang_done(msg_id: str):
|
||
messages = _load_messages()
|
||
for m in messages:
|
||
if m.get("id") == msg_id:
|
||
m["status"] = "erledigt"
|
||
_save_messages(messages)
|
||
return JSONResponse(content={"success": True})
|
||
raise HTTPException(status_code=404, detail="Nachricht nicht gefunden")
|
||
|
||
|
||
@router.delete("/messages/{msg_id}")
|
||
async def empfang_delete(msg_id: str):
|
||
messages = _load_messages()
|
||
new = [m for m in messages if m.get("id") != msg_id]
|
||
if len(new) == len(messages):
|
||
raise HTTPException(status_code=404, detail="Nachricht nicht gefunden")
|
||
_save_messages(new)
|
||
return JSONResponse(content={"success": True})
|
||
|
||
|
||
@router.get("/", response_class=HTMLResponse)
|
||
async def empfang_page(request: Request):
|
||
html_path = Path(__file__).resolve().parent / "web" / "empfang.html"
|
||
if html_path.is_file():
|
||
return HTMLResponse(content=html_path.read_text(encoding="utf-8"))
|
||
return HTMLResponse(content="<h1>empfang.html nicht gefunden</h1>", status_code=404)
|