feat: AI patient onboarding backend and documentation

This commit is contained in:
2026-08-27 12:15:09 +05:30
parent b9440772d1
commit 59c1372fe3
49 changed files with 4012 additions and 0 deletions
@@ -0,0 +1 @@
"""Database package initialization."""
@@ -0,0 +1,47 @@
import os
from pathlib import Path
from dotenv import load_dotenv
from sqlalchemy import create_engine
from sqlalchemy.orm import DeclarativeBase, sessionmaker
load_dotenv()
# Ensure data directory exists relative to project root
BASE_DIR = Path(__file__).resolve().parent.parent.parent
DATA_DIR = BASE_DIR / "data"
DATA_DIR.mkdir(parents=True, exist_ok=True)
# Build absolute SQLite URI so it works regardless of process working directory
DEFAULT_DB_PATH = DATA_DIR / "onboarding.db"
env_db_url = os.getenv("DATABASE_URL")
if env_db_url and not env_db_url.startswith("sqlite:///./"):
DATABASE_URL = env_db_url
else:
# Use robust absolute path for SQLite
DATABASE_URL = f"sqlite:///{DEFAULT_DB_PATH.as_posix()}"
engine = create_engine(
DATABASE_URL,
connect_args={
"check_same_thread": False
},
)
SessionLocal = sessionmaker(
bind=engine,
autoflush=False,
autocommit=False,
)
class Base(DeclarativeBase):
pass
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@@ -0,0 +1,162 @@
from datetime import datetime, timezone
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from app.database.database import Base
class OnboardingSession(Base):
__tablename__ = "onboarding_sessions"
id: Mapped[int] = mapped_column(
Integer,
primary_key=True,
autoincrement=True,
)
session_id: Mapped[str] = mapped_column(
String(50),
unique=True,
nullable=False,
index=True,
)
patient_id: Mapped[str] = mapped_column(
String(100),
nullable=False,
index=True,
)
patient_name: Mapped[str | None] = mapped_column(
String(255),
nullable=True,
)
age: Mapped[int | None] = mapped_column(
Integer,
nullable=True,
)
gender: Mapped[str | None] = mapped_column(
String(50),
nullable=True,
)
category_type: Mapped[str] = mapped_column(
String(50),
nullable=False,
)
category_code: Mapped[str] = mapped_column(
String(100),
nullable=False,
)
category_name: Mapped[str] = mapped_column(
String(255),
nullable=False,
)
language: Mapped[str] = mapped_column(
String(20),
default="en",
nullable=False,
)
status: Mapped[str] = mapped_column(
String(50),
default="in_progress",
nullable=False,
)
stage: Mapped[str] = mapped_column(
String(100),
default="greeting",
nullable=False,
)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(timezone.utc),
nullable=False,
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(timezone.utc),
onupdate=lambda: datetime.now(timezone.utc),
nullable=False,
)
class OnboardingMessage(Base):
__tablename__ = "onboarding_messages"
id: Mapped[int] = mapped_column(
Integer,
primary_key=True,
autoincrement=True,
)
session_id: Mapped[str] = mapped_column(
String(50),
ForeignKey("onboarding_sessions.session_id"),
nullable=False,
index=True,
)
role: Mapped[str] = mapped_column(
String(20),
nullable=False,
)
message: Mapped[str] = mapped_column(
Text,
nullable=False,
)
stage: Mapped[str] = mapped_column(
String(100),
nullable=False,
)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(timezone.utc),
nullable=False,
)
class OnboardingState(Base):
__tablename__ = "onboarding_state"
id: Mapped[int] = mapped_column(
Integer,
primary_key=True,
autoincrement=True,
)
session_id: Mapped[str] = mapped_column(
String(50),
ForeignKey("onboarding_sessions.session_id"),
unique=True,
nullable=False,
index=True,
)
state_json: Mapped[str] = mapped_column(
Text,
nullable=False,
default="{}",
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(timezone.utc),
onupdate=lambda: datetime.now(timezone.utc),
nullable=False,
)