refactor(api): trailing newlines + hoist clearhouse.py parser imports
Self-review nits from Checkpoint 2b: - All three new router files lacked a trailing newline (same nit as 2a). - clearhouse.py had lazy imports of serialize_837 / parse / db as _db inside helper bodies. Hoisted serialize_837 and parse to module-level (no import cycle risk: clearhouse.py does not import from cyclone.api). Dropped the redundant db as _db alias — the top-level from cyclone import db is already in scope. No behavior change. test_clearhouse_api: 10 / 10 passing.
This commit is contained in:
@@ -23,6 +23,8 @@ from fastapi import APIRouter, HTTPException
|
|||||||
|
|
||||||
from cyclone import db
|
from cyclone import db
|
||||||
from cyclone.audit_log import AuditEvent, append_event
|
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
|
from cyclone.store import store
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
@@ -99,16 +101,12 @@ def submit_to_clearhouse(body: dict):
|
|||||||
|
|
||||||
|
|
||||||
def _serialize_claim_for_submit(claim_id: str) -> str:
|
def _serialize_claim_for_submit(claim_id: str) -> str:
|
||||||
"""Serialize a claim to X12 for SFTP submission. Lazy import of the
|
"""Serialize a claim to X12 for SFTP submission."""
|
||||||
serializer to avoid pulling FastAPI machinery at module import time."""
|
with db.SessionLocal()() as s:
|
||||||
from cyclone import db as _db
|
row = s.get(db.Claim, claim_id)
|
||||||
with _db.SessionLocal()() as s:
|
|
||||||
row = s.get(_db.Claim, claim_id)
|
|
||||||
if row is None:
|
if row is None:
|
||||||
raise ValueError(f"claim {claim_id!r} not found")
|
raise ValueError(f"claim {claim_id!r} not found")
|
||||||
# Re-parse the stored raw_json to get a ClaimOutput
|
|
||||||
raw = row.raw_json or {}
|
raw = row.raw_json or {}
|
||||||
# Reconstruct minimal ClaimOutput from raw_json; this is best-effort.
|
|
||||||
return _serialize_claim_from_raw(row, raw)
|
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.
|
"""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
|
For SP9 this delegates to the existing serialize_837 helper if the
|
||||||
claim has a complete raw_segments array. Otherwise it returns a
|
claim has a complete raw_segments array. Otherwise it raises.
|
||||||
minimal placeholder.
|
|
||||||
"""
|
"""
|
||||||
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.
|
# SP9 stub: if the claim has a `raw_json` with `x12_text`, use that.
|
||||||
if isinstance(raw, dict) and raw.get("x12_text"):
|
if isinstance(raw, dict) and raw.get("x12_text"):
|
||||||
result = parse(raw["x12_text"])
|
result = parse(raw["x12_text"])
|
||||||
if result.claims:
|
if result.claims:
|
||||||
return serialize_837(result.claims[0])
|
return serialize_837(result.claims[0])
|
||||||
# Fallback: raise so the caller sees an error.
|
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f"claim {claim_row.id!r} cannot be re-serialized: no stored x12_text"
|
f"claim {claim_row.id!r} cannot be re-serialized: no stored x12_text"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -53,4 +53,4 @@ def list_payer_configs(payer_id: str):
|
|||||||
live = store.get_payer_config(payer_id, tx)
|
live = store.get_payer_config(payer_id, tx)
|
||||||
if live is not None:
|
if live is not None:
|
||||||
configs.append({"transaction_type": tx, "config_json": live, "source": "db"})
|
configs.append({"transaction_type": tx, "config_json": live, "source": "db"})
|
||||||
return configs
|
return configs
|
||||||
|
|||||||
@@ -46,4 +46,4 @@ def list_providers(
|
|||||||
"total": total,
|
"total": total,
|
||||||
"returned": returned,
|
"returned": returned,
|
||||||
"has_more": has_more,
|
"has_more": has_more,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user