"""Drug reference of the medication module (data/meds/meds_reference.json, built by scripts/build_meds_reference.py). Read-only patient information collected from the Minzdrav clinical guidelines: when to expect the effect, how long the course lasts, common side effects, red flags. It never suggests a drug or a dose — the app only follows the doctor's prescription. Draft until reviewed by a psychiatrist (`status` in the file). """ import json from functools import lru_cache from typing import Any, Literal from app.config import get_settings Indication = Literal["depression", "ptsd"] @lru_cache(maxsize=1) def load_reference() -> dict[str, Any]: path = get_settings().data_dir / "meds" / "meds_reference.json" return json.loads(path.read_text(encoding="utf-8")) def find_drug(name: str) -> dict[str, Any] | None: """Reference entry for a prescription's drug (INN in Russian or the latin id), or None if it is not covered.""" key = name.strip().split()[0].casefold() if name.strip() else "" for drug in load_reference()["drugs"]: if key in (drug["id"], drug["name_ru"].casefold()): return drug return None def indication_for(modules: list[str], drug: dict[str, Any]) -> Indication | None: """The indication the patient is followed for, among those the drug has in the guidelines.""" preferred: list[Indication] = ["ptsd", "depression"] if "veteran" in modules else ["depression", "ptsd"] return next((i for i in preferred if i in drug["indications"]), None)