From 6aa440d3e4eb05a6923e9c7d0374d1513138895e Mon Sep 17 00:00:00 2001 From: Tyler Date: Sun, 21 Jun 2026 02:30:11 -0600 Subject: [PATCH] refactor(api): trailing newlines + hoist clearhouse.py parser imports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- backend/src/cyclone/api_routers/clearhouse.py | 22 ++++++------------- backend/src/cyclone/api_routers/config.py | 2 +- backend/src/cyclone/api_routers/providers.py | 2 +- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/backend/src/cyclone/api_routers/clearhouse.py b/backend/src/cyclone/api_routers/clearhouse.py index 597f931..4ad72a1 100644 --- a/backend/src/cyclone/api_routers/clearhouse.py +++ b/backend/src/cyclone/api_routers/clearhouse.py @@ -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" - ) \ No newline at end of file + ) diff --git a/backend/src/cyclone/api_routers/config.py b/backend/src/cyclone/api_routers/config.py index 6804c27..9ed5f9d 100644 --- a/backend/src/cyclone/api_routers/config.py +++ b/backend/src/cyclone/api_routers/config.py @@ -53,4 +53,4 @@ def list_payer_configs(payer_id: str): live = store.get_payer_config(payer_id, tx) if live is not None: configs.append({"transaction_type": tx, "config_json": live, "source": "db"}) - return configs \ No newline at end of file + return configs diff --git a/backend/src/cyclone/api_routers/providers.py b/backend/src/cyclone/api_routers/providers.py index 91e9c74..62eea9a 100644 --- a/backend/src/cyclone/api_routers/providers.py +++ b/backend/src/cyclone/api_routers/providers.py @@ -46,4 +46,4 @@ def list_providers( "total": total, "returned": returned, "has_more": has_more, - } \ No newline at end of file + }