Initial commit: DMH Patient Registration Flask app
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
"""
|
||||
Amrita HIS Integration Adapter interface + HIS_MODE guard.
|
||||
Never fabricates a production UHID; MockHISAdapter is blocked in production.
|
||||
"""
|
||||
|
||||
import os
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class HISSearchResult:
|
||||
outcome: str # 'NONE' | 'CANDIDATE' | 'ERROR'
|
||||
candidate_uhid: str | None = None
|
||||
reason: str | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class HISCreateResult:
|
||||
outcome: str # 'SUCCESS' | 'REJECTED' | 'TIMEOUT'
|
||||
uhid: str | None = None
|
||||
reason: str | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class HISReconcileResult:
|
||||
outcome: str # 'FOUND' | 'ABSENT' | 'UNRESOLVED'
|
||||
uhid: str | None = None
|
||||
reason: str | None = None
|
||||
|
||||
|
||||
class BaseHISAdapter(ABC):
|
||||
@abstractmethod
|
||||
def search_patient(self, *, match_tokens: list[str], fname: str, lname: str, dob: str, mobile: str | None) -> HISSearchResult:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def create_patient(self, *, correlation_id: str, payload: dict) -> HISCreateResult:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def reconcile_registration(self, *, correlation_id: str, reference_form_id: str) -> HISReconcileResult:
|
||||
pass
|
||||
|
||||
|
||||
class MockHISAdapter(BaseHISAdapter):
|
||||
"""Deterministic mock adapter for local/test environments only."""
|
||||
|
||||
def search_patient(self, *, match_tokens: list[str], fname: str, lname: str, dob: str, mobile: str | None) -> HISSearchResult:
|
||||
if lname == "DUPLICATE":
|
||||
return HISSearchResult(outcome="CANDIDATE", candidate_uhid="UHID-900001", reason="Exact name+DOB match")
|
||||
return HISSearchResult(outcome="NONE")
|
||||
|
||||
def create_patient(self, *, correlation_id: str, payload: dict) -> HISCreateResult:
|
||||
return HISCreateResult(outcome="SUCCESS", uhid=f"MOCK-UHID-{payload['reference_form_id']}")
|
||||
|
||||
def reconcile_registration(self, *, correlation_id: str, reference_form_id: str) -> HISReconcileResult:
|
||||
return HISReconcileResult(outcome="ABSENT", reason="Not found in mock HIS")
|
||||
|
||||
|
||||
class DisabledHISAdapter(BaseHISAdapter):
|
||||
"""Safe no-op adapter when HIS_MODE=disabled: never issues fake UHIDs."""
|
||||
|
||||
def search_patient(self, *, match_tokens: list[str], fname: str, lname: str, dob: str, mobile: str | None) -> HISSearchResult:
|
||||
return HISSearchResult(outcome="NONE")
|
||||
|
||||
def create_patient(self, *, correlation_id: str, payload: dict) -> HISCreateResult:
|
||||
return HISCreateResult(outcome="TIMEOUT", reason="HIS integration disabled (HIS_MODE=disabled)")
|
||||
|
||||
def reconcile_registration(self, *, correlation_id: str, reference_form_id: str) -> HISReconcileResult:
|
||||
return HISReconcileResult(outcome="ABSENT", reason="HIS integration disabled")
|
||||
|
||||
|
||||
def get_his_adapter() -> BaseHISAdapter:
|
||||
"""Resolve HIS adapter from HIS_MODE, blocking mock mode in production."""
|
||||
mode = os.getenv("HIS_MODE", "disabled").strip().lower()
|
||||
env = os.getenv("FLASK_ENV", "development").strip().lower()
|
||||
|
||||
if mode == "mock":
|
||||
if env == "production":
|
||||
raise RuntimeError("HIS_MODE=mock is forbidden when FLASK_ENV=production.")
|
||||
return MockHISAdapter()
|
||||
if mode == "disabled":
|
||||
return DisabledHISAdapter()
|
||||
if mode == "amrita":
|
||||
raise NotImplementedError("Official Amrita HIS adapter is not yet configured.")
|
||||
raise ValueError(f"Unsupported HIS_MODE: {mode}")
|
||||
Reference in New Issue
Block a user