|
|
|
@@ -23,6 +23,8 @@ from fastapi import APIRouter, HTTPException
|
|
|
|
|
|
|
|
|
|
from cyclone import db
|
|
|
|
|
from cyclone.audit_log import AuditEvent, append_event
|
|
|
|
|
from cyclone.parsers.parse_837 import parse
|
|
|
|
|
from cyclone.parsers.serialize_837 import serialize_837
|
|
|
|
|
from cyclone.store import store
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
@@ -99,16 +101,12 @@ def submit_to_clearhouse(body: dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _serialize_claim_for_submit(claim_id: str) -> str:
|
|
|
|
|
"""Serialize a claim to X12 for SFTP submission. Lazy import of the
|
|
|
|
|
serializer to avoid pulling FastAPI machinery at module import time."""
|
|
|
|
|
from cyclone import db as _db
|
|
|
|
|
with _db.SessionLocal()() as s:
|
|
|
|
|
row = s.get(_db.Claim, claim_id)
|
|
|
|
|
"""Serialize a claim to X12 for SFTP submission."""
|
|
|
|
|
with db.SessionLocal()() as s:
|
|
|
|
|
row = s.get(db.Claim, claim_id)
|
|
|
|
|
if row is None:
|
|
|
|
|
raise ValueError(f"claim {claim_id!r} not found")
|
|
|
|
|
# Re-parse the stored raw_json to get a ClaimOutput
|
|
|
|
|
raw = row.raw_json or {}
|
|
|
|
|
# Reconstruct minimal ClaimOutput from raw_json; this is best-effort.
|
|
|
|
|
return _serialize_claim_from_raw(row, raw)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -116,19 +114,13 @@ def _serialize_claim_from_raw(claim_row, raw: dict) -> str:
|
|
|
|
|
"""Best-effort serializer that uses the stored raw_json to emit a fresh 837.
|
|
|
|
|
|
|
|
|
|
For SP9 this delegates to the existing serialize_837 helper if the
|
|
|
|
|
claim has a complete raw_segments array. Otherwise it returns a
|
|
|
|
|
minimal placeholder.
|
|
|
|
|
claim has a complete raw_segments array. Otherwise it raises.
|
|
|
|
|
"""
|
|
|
|
|
from cyclone.parsers.serialize_837 import serialize_837
|
|
|
|
|
from cyclone.parsers.parse_837 import parse
|
|
|
|
|
|
|
|
|
|
# Re-parse the original batch text (need to re-derive from store).
|
|
|
|
|
# SP9 stub: if the claim has a `raw_json` with `x12_text`, use that.
|
|
|
|
|
if isinstance(raw, dict) and raw.get("x12_text"):
|
|
|
|
|
result = parse(raw["x12_text"])
|
|
|
|
|
if result.claims:
|
|
|
|
|
return serialize_837(result.claims[0])
|
|
|
|
|
# Fallback: raise so the caller sees an error.
|
|
|
|
|
raise RuntimeError(
|
|
|
|
|
f"claim {claim_row.id!r} cannot be re-serialized: no stored x12_text"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|