Compare commits
133 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c9588f782d | |||
| 2c43a82390 | |||
| a8aef7b9b2 | |||
| b3a608a939 | |||
| f5713640e4 | |||
| 36cac9af78 | |||
| 7d257911d2 | |||
| b5b715d1c9 | |||
| df3e5e401d | |||
| 9e422fdadb | |||
| daf55d9a38 | |||
| ea68ae81f7 | |||
| ad6629c7ab | |||
| afd6cead06 | |||
| 95d04f9991 | |||
| 5fe61fddd3 | |||
| 56e14341c2 | |||
| 23fc85cad2 | |||
| 61e90b64b3 | |||
| e9e0b68746 | |||
| be5824ec31 | |||
| ee1a397bd5 | |||
| 019e09aff6 | |||
| e35e5e03b3 | |||
| 579e5d7438 | |||
| 1978e696d7 | |||
| ce61b36504 | |||
| 26c948e7fc | |||
| 309e6c016a | |||
| 8790e0a439 | |||
| 467f53046e | |||
| cfe9c25d9d | |||
| b0ad0c27b8 | |||
| b0d03bc10e | |||
| 9beab5b4fc | |||
| adb8dcb137 | |||
| a15547fb0e | |||
| 262ad00481 | |||
| cc41c73d2a | |||
| 30aee61956 | |||
| b19f43f448 | |||
| 2dfe213af0 | |||
| 83d4df413c | |||
| bd5530539d | |||
| 53df3f0684 | |||
| 0e130579b4 | |||
| 13dd240328 | |||
| 543422c343 | |||
| 39f578aa5e | |||
| 4d396d40b6 | |||
| fcac090885 | |||
| 45b6c6291a | |||
| a3831213cc | |||
| e946a4f7aa | |||
| 30d13876cd | |||
| ea7acd2e66 | |||
| 3b4f18eef6 | |||
| 69b760e890 | |||
| f8e2f0933e | |||
| 52795adfb1 | |||
| acf3b506ec | |||
| e7098b99b2 | |||
| 7838ed2a4c | |||
| c3ef4f3847 | |||
| cfca1d8975 | |||
| b652b18458 | |||
| 0815a4ad35 | |||
| 7218ec76a8 | |||
| b237b9cfc4 | |||
| c5604fdfa0 | |||
| f21201a22e | |||
| 0436820023 | |||
| 055d95224a | |||
| 77f73cf73e | |||
| 08c0430ee7 | |||
| 3400ed5ec2 | |||
| 0493814489 | |||
| 538fad211b | |||
| 353adfc08b | |||
| d328c0b224 | |||
| 8516b90601 | |||
| 8a251d20a9 | |||
| 01e5ee781e | |||
| 28835e2f1d | |||
| 0ccc396e5e | |||
| c8f5af3ba5 | |||
| fa034b1cd7 | |||
| 9c0aa577fb | |||
| 83a31b6fea | |||
| 07ea7ca1d6 | |||
| 76923a79f5 | |||
| 9ef749c783 | |||
| ad14b56732 | |||
| 8890627014 | |||
| d776a4a7ec | |||
| 893a6629a8 | |||
| f62e1a7881 | |||
| dc5bff617d | |||
| abaf23c122 | |||
| ce0df8ac24 | |||
| cc02cb99c5 | |||
| aff3a13016 | |||
| 7cb0278be0 | |||
| 4c85166734 | |||
| f1dc06ec3b | |||
| dc990cfde3 | |||
| ec065a9082 | |||
| 5d4e06b863 | |||
| 298200fe71 | |||
| 7ad190c267 | |||
| 3a58f561d4 | |||
| 0067ecc5da | |||
| 698f8660bf | |||
| 178898a8e1 | |||
| 710104f491 | |||
| 4b56416414 | |||
| f005494606 | |||
| 69b338234d | |||
| 17736ccffa | |||
| 5f5ac875f1 | |||
| 97145f313b | |||
| 4770c04021 | |||
| cad4f1fe2d | |||
| 7abe94a3a8 | |||
| 6a5dbdf88a | |||
| 5028628269 | |||
| 299c1a85a3 | |||
| 9429d11b5f | |||
| 85791e0df7 | |||
| 21066ad0bf | |||
| a963d3ce25 | |||
| a52a85c7a2 | |||
| 95f5e91ade |
@@ -18,3 +18,23 @@ VITE_API_BASE_URL=
|
||||
# Optional. Set to 1 to disable auth entirely (DEV ONLY). When set,
|
||||
# the backend auto-grants admin access without checking credentials.
|
||||
# CYCLONE_AUTH_DISABLED=0
|
||||
|
||||
# ─── Gainwell / HCPF SFTP credentials (operator convention) ─────────────
|
||||
# These mirror what /home/tyler/EDI/scripts/upload_claims.sh exports and
|
||||
# what the operator's SFTP client uses. Cyclone's secrets module looks up
|
||||
# CYCLONE_SFTP_PASSWORD (or _FILE), not GAINWELL_SFTP_PASS — to bridge:
|
||||
#
|
||||
# export CYCLONE_SFTP_PASSWORD="$GAINWELL_SFTP_PASS"
|
||||
#
|
||||
# or for the daemon, set CYCLONE_SFTP_PASSWORD_FILE to a 0600 file.
|
||||
GAINWELL_SFTP_USER=colorado-fts\coxix_prod_11525703
|
||||
GAINWELL_SFTP_HOST=mft.gainwelltechnologies.com
|
||||
GAINWELL_SFTP_PASS=
|
||||
GAINWELL_REMOTE_DIR=/CO XIX/PROD/coxix_prod_11525703/ToHPE
|
||||
|
||||
# CYCLONE_SFTP_PASSWORD= # mirror of GAINWELL_SFTP_PASS
|
||||
# CYCLONE_SFTP_PASSWORD_FILE= # path to a 0600 file containing it
|
||||
|
||||
# CYCLONE_SCHEDULER_AUTOSTART= # 1/true/yes to start the MFT poll loop on API launch
|
||||
# CYCLONE_SCHEDULER_POLL_SECONDS=60 # poll interval when autostart is on
|
||||
# CYCLONE_TAIL_HEARTBEAT_S=15 # NDJSON stream heartbeat interval
|
||||
+21
@@ -20,10 +20,24 @@ build/
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# macOS extraction residue (AppleDouble / __MACOSX from unzip on macOS).
|
||||
# These are paired with every regular file in an extracted zip — never data.
|
||||
__MACOSX/
|
||||
._*
|
||||
|
||||
# Operator drop zone (untracked working dir; contents are HCPF-delivered
|
||||
# inbound files, never source). Use `mkdir -p ingest && touch ingest/.gitkeep`
|
||||
# if you want the directory itself in the repo.
|
||||
ingest/
|
||||
|
||||
# Local config
|
||||
.env
|
||||
.env.local
|
||||
.env.*.local
|
||||
# Edifabric / EdiNation operator-supplied dev key (env file).
|
||||
# Lives at backend/.env.cyclone-edifabric; source it in the shell.
|
||||
.env.cyclone-edifabric
|
||||
backend/.env.cyclone-edifabric
|
||||
|
||||
# Production data (handled by ops, not committed)
|
||||
docs/prodfiles/*/
|
||||
@@ -42,3 +56,10 @@ claims_output/
|
||||
# SP33+ scratch / production-data ingest. Generated artifacts live
|
||||
# here only — the source EDI sits under docs/prodfiles/.
|
||||
ingest/
|
||||
|
||||
# SP41 dev/ scratch dir — generated 837P artifacts from rebill pipeline
|
||||
# runs. Hundreds of thousands of files; never source. The scripts that
|
||||
# build them are tracked (dev/unbilled-july2026/scripts/...) but the
|
||||
# generated x12 artifacts are not.
|
||||
dev/rebills/
|
||||
dev/rebills/2026-*
|
||||
|
||||
@@ -125,7 +125,7 @@ Frontend triplet for any live page: `use<X>(params)` (initial fetch) + `useTailS
|
||||
|
||||
## Backend at a glance
|
||||
|
||||
`backend/src/cyclone/` is a single namespace. The two largest files are `api.py` (~3,548 LOC, the only large file) and `store.py` (~2,423 LOC, the `CycloneStore` facade — SP21 is in flight to split it into a `cyclone/store/` subpackage; the public API stays unchanged). Subpackages: `api_routers/` (acks, admin, health, ta1_acks), `clearhouse/` (Clearhouse + SftpClient), `edi/` (filenames), `parsers/` (X12 transaction parsers + models + validators + serializers), `workflow/` (placeholder for future sub-project 6).
|
||||
`backend/src/cyclone/` is a single namespace. The two largest files are `api.py` (~3,548 LOC, the only large file) and `store.py` (~2,423 LOC, the `CycloneStore` facade — SP21 is in flight to split it into a `cyclone/store/` subpackage; the public API stays unchanged). Subpackages: `api_routers/` (acks, admin, health, ta1_acks), `clearhouse/` (Clearhouse + SftpClient), `edi/` (filenames), `parsers/` (X12 transaction parsers + models + validators + serializers), `submission/` (SP37 — canonical `submit_file` helper shared by `cyclone submit-batch` CLI + `POST /api/submit-batch` HTTP endpoint; owns parse → DB-write → SFTP-upload → audit per file), `workflow/` (placeholder for future sub-project 6).
|
||||
|
||||
The store is the only read/write surface for the database; every mutating endpoint goes through it. All persistence flows through SQLAlchemy sessions via `db.SessionLocal()()`. SQLAlchemy ORM models live in `db.py`; 12 SQL migrations under `migrations/` (0001_initial through 0012_backups) are walked in order by `db_migrate.py`.
|
||||
|
||||
@@ -137,6 +137,34 @@ Config: `config/payers.yaml` is the on-disk source for providers / payers / clea
|
||||
|
||||
Secrets live in the macOS Keychain (via `keyring` + `cyclone.secrets`): SQLCipher key (service `cyclone`, account `cyclone.db.key`), SFTP password, backup passphrase. No secrets on disk in plaintext.
|
||||
|
||||
## Production SFTP posture (this box: manual mode)
|
||||
|
||||
This host runs in **manual SFTP mode** against Gainwell's MOVEit Transfer MFT at `mft.gainwelltechnologies.com`. The seeded `dzinesco` clearhouse keeps `sftp_block.stub: true`; the operator moves files to/from Gainwell with their SFTP client and drops inbound files into `ingest/` for cyclone to ingest. Do NOT flip `stub` to `false` from this host — see "Auth caveat" below.
|
||||
|
||||
### Env var convention
|
||||
|
||||
The operator's shell exports the Gainwell creds as `GAINWELL_SFTP_USER`, `GAINWELL_SFTP_PASS`, `GAINWELL_SFTP_HOST`, `GAINWELL_REMOTE_DIR`. Cyclone's `secrets.get_secret()` looks up `CYCLONE_SFTP_PASSWORD` (or `CYCLONE_SFTP_PASSWORD_FILE`) per `secrets._ENV_NAME_FOR`. The two are NOT the same name — bridge them per-call or in your shell rc:
|
||||
|
||||
```bash
|
||||
export CYCLONE_SFTP_PASSWORD="$GAINWELL_SFTP_PASS"
|
||||
```
|
||||
|
||||
### Inbound drop zone
|
||||
|
||||
`/home/tyler/dev/cyclone/ingest/` is the operator-maintained staging dir for inbound MFT files (999 acks, TA1, 835 remittances, 277CA claim acks). Files here are NOT auto-watched; processing happens via the procedure in `docs/RUNBOOK.md` § "Manual SFTP mode". At time of writing this dir holds ~1500 unprocessed 999 acks + 1 TA1 + 1 835 from early July 2026 — the local ingest flow clears them.
|
||||
|
||||
### Auth caveat (do not retry)
|
||||
|
||||
Paramiko reaches `MOVEit Transfer SFTP` cleanly (SSH kex completes, MOVEit offers password auth) but `AuthenticationException: Authentication failed` is returned despite the operator confirming the credentials are known-good from another host. Most likely cause: MOVEit Transfer's IP-based access control — this host's public IP `103.14.26.95` is not whitelisted. Repeated auth attempts risk account lockout. Do NOT keep guessing. Resolve by either: (a) whitelisting this IP with Gainwell's MFT admin, or (b) staying in manual mode and moving files via the operator's SFTP client.
|
||||
|
||||
### Inbound ingestion paths
|
||||
|
||||
The canonical way to write 999/TA1/277CA/835 rows into the DB is `Scheduler.process_inbound_files`, exposed as the CLI `cyclone pull-inbound --date YYYYMMDD` and the HTTP `POST /api/admin/scheduler/pull-inbound`. **There is no `parse-999` / `parse-ta1` / `parse-277ca` CLI command** (CLAUDE.md's "CLI" section above is aspirational on those three). The `parse-837` and `parse-835` CLIs exist but only emit JSON files to `--output-dir`; they do NOT write to the DB. For DB writes, use `pull-inbound` (which dedupes via `processed_inbound_files`).
|
||||
|
||||
### Daemon hot-reload
|
||||
|
||||
`python -m cyclone serve` runs as root (started by `tini`) and keeps the SFTP block in memory. Flipping `stub` directly in the DB (`store.update_clearhouse(...)`) does NOT auto-reload the daemon's view — either restart the daemon or use `PATCH /api/clearhouse` (which calls `scheduler.reconfigure_scheduler` to hot-reload).
|
||||
|
||||
## Frontend at a glance
|
||||
|
||||
`src/` is React 18 + TypeScript + Vite. Routes register in `src/App.tsx` (11 pages, all under a `<Layout>` route wrapper). Pages are pure renderers — every page pairs with a `use<X>` data hook in `src/hooks/` and renders a `<PageHeader>` + a table/list/KPI grid. Drawers (`ClaimDrawer/`, `RemitDrawer/`, plus the new `ProviderDrawer/` and `AckDrawer/`) are mounted by the page and their open/close state is mirrored to the URL via `useDrawerUrlState` so deep-links round-trip. Drill-stack navigation is provided by `<DrillStackProvider>` in `src/components/drill/`.
|
||||
|
||||
@@ -7,3 +7,4 @@ __pycache__/
|
||||
venv/
|
||||
build/
|
||||
dist/
|
||||
var/
|
||||
|
||||
+56
-4019
File diff suppressed because it is too large
Load Diff
@@ -1 +1,59 @@
|
||||
"""Resource-group routers. Imported and registered by ``cyclone.api``."""
|
||||
"""Per-resource FastAPI routers.
|
||||
|
||||
`api.py` does `for r in routers: app.include_router(r)`. New
|
||||
routers register themselves here in alphabetical order. Helpers
|
||||
shared by 2+ routers live in `_shared.py` (private to the package).
|
||||
|
||||
Every router except ``health`` declares its own
|
||||
``APIRouter(dependencies=[Depends(matrix_gate)])`` — keep that
|
||||
invariant here so adding a new router doesn't silently miss the gate.
|
||||
"""
|
||||
from fastapi import APIRouter
|
||||
|
||||
from cyclone.api_routers import (
|
||||
acks,
|
||||
activity,
|
||||
admin,
|
||||
batches,
|
||||
claim_acks,
|
||||
claims,
|
||||
clearhouse,
|
||||
config,
|
||||
dashboard,
|
||||
eligibility,
|
||||
health,
|
||||
inbox,
|
||||
parse,
|
||||
payers,
|
||||
providers,
|
||||
rebill,
|
||||
reconciliation,
|
||||
remittances,
|
||||
submission,
|
||||
ta1_acks,
|
||||
)
|
||||
|
||||
routers: list[APIRouter] = [
|
||||
acks.router, # gated
|
||||
activity.router, # gated
|
||||
admin.router, # gated
|
||||
batches.router, # gated
|
||||
claim_acks.router, # gated
|
||||
claims.router, # gated
|
||||
clearhouse.router, # gated
|
||||
config.router, # gated
|
||||
dashboard.router, # gated
|
||||
eligibility.router, # gated
|
||||
health.router, # public — health probes must work pre-auth
|
||||
inbox.router, # gated
|
||||
parse.router, # gated
|
||||
payers.router, # gated
|
||||
providers.router, # gated
|
||||
rebill.router, # gated (SP41)
|
||||
reconciliation.router, # gated
|
||||
remittances.router, # gated
|
||||
submission.router, # gated
|
||||
ta1_acks.router, # gated
|
||||
]
|
||||
|
||||
__all__ = ["routers"]
|
||||
|
||||
@@ -0,0 +1,273 @@
|
||||
"""Cross-router helpers for the api_routers package.
|
||||
|
||||
Private to the package (leading underscore). Only routers in this
|
||||
package import from here. Helpers graduate here when at least two
|
||||
routers need them — single-router helpers stay in the router that
|
||||
uses them.
|
||||
|
||||
Helpers currently promoted here:
|
||||
|
||||
- :func:`_actor_user_id` — promoted early (during SP36 Task 11
|
||||
/ clearhouse extraction) because the clearhouse router needs
|
||||
it and the 2 remaining call-sites in ``api.py`` (parse-999 ack
|
||||
block, parse-277ca ack block) are both inside the parse
|
||||
surface that Task 16 will extract. Promoting now is cheaper
|
||||
than leaving a cross-module ``from cyclone.api import _actor_user_id``
|
||||
that would create an import-cycle at registry load time.
|
||||
|
||||
- :data:`PAYER_FACTORIES` / :data:`PAYER_FACTORIES_835` — SP36
|
||||
Task 16: payer config dicts lifted from ``api.py`` alongside
|
||||
the ``_resolve_payer`` / ``_resolve_payer_835`` helpers that
|
||||
consume them. The two helpers each touch a single ``PAYER_FACTORIES*``
|
||||
dict; keeping both halves of the pair in one module removes a
|
||||
circular import (parse.py → _shared._resolve_payer → api.PAYER_FACTORIES).
|
||||
|
||||
- :func:`_resolve_payer` / :func:`_resolve_payer_835` — used by
|
||||
``parse-837`` and ``parse-835`` endpoints respectively. Promoted
|
||||
in SP36 Task 16 alongside the PAYER_FACTORIES dicts.
|
||||
|
||||
- :func:`_transaction_set_id_from_segments` — used by
|
||||
``parse-837`` and ``parse-835`` envelope guards. Promoted in
|
||||
SP36 Task 16.
|
||||
|
||||
- :func:`_build_and_persist_ack` — used by ``parse-837`` (when
|
||||
``?ack=true``). Promoted in SP36 Task 16.
|
||||
|
||||
- :func:`_reconciliation_summary_for_batch` — used by
|
||||
``parse-835``. Promoted in SP36 Task 16.
|
||||
|
||||
- :func:`_ta1_synthetic_source_batch_id` — used by ``parse-ta1``.
|
||||
Promoted in SP36 Task 16.
|
||||
|
||||
- :func:`_serialize_ta1` — used by ``parse-ta1`` to build the
|
||||
raw TA1 round-trip text. Promoted in SP36 Task 16 per the
|
||||
plan's "8 helpers" specification; technically a single-router
|
||||
helper per D4 but moved here for symmetry with the other parse
|
||||
serializers.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from fastapi import HTTPException, Request
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.parsers.batch_ack_builder import build_ack_for_batch
|
||||
from cyclone.parsers.payer import PayerConfig, PayerConfig835
|
||||
from cyclone.parsers.serialize_999 import serialize_999
|
||||
from cyclone.store import store
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Actor user id (SP36 Task 11 — early-promoted)
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def _actor_user_id(request: Request) -> int | None:
|
||||
"""Return the acting user's id from ``request.state.user``, or None.
|
||||
|
||||
``get_current_user``/``matrix_gate`` populate ``request.state.user``
|
||||
for both the authenticated path and the AUTH_DISABLED escape hatch.
|
||||
Returns None when the state hasn't been set (e.g. background jobs
|
||||
or unit tests that bypass auth). Used to stamp ``user_id`` onto
|
||||
audit events without crashing the request.
|
||||
"""
|
||||
user = getattr(request.state, "user", None)
|
||||
if user is None:
|
||||
return None
|
||||
return getattr(user, "id", None)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Payer config dicts (SP36 Task 16)
|
||||
# --------------------------------------------------------------------------- #
|
||||
#
|
||||
# Mirror cli._PAYER_FACTORIES. Kept here (not in the parse router) so
|
||||
# the ``_resolve_payer`` / ``_resolve_payer_835`` helpers can import
|
||||
# their backing dicts without a circular import.
|
||||
|
||||
|
||||
PAYER_FACTORIES: dict[str, Any] = {
|
||||
"co_medicaid": PayerConfig.co_medicaid,
|
||||
"generic_837p": PayerConfig.generic_837p,
|
||||
}
|
||||
|
||||
PAYER_FACTORIES_835: dict[str, Any] = {
|
||||
"co_medicaid_835": PayerConfig835.co_medicaid_835,
|
||||
"generic_835": PayerConfig835.generic_835,
|
||||
}
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Payer resolution (SP36 Task 16)
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def _resolve_payer(name: str) -> PayerConfig:
|
||||
if name not in PAYER_FACTORIES:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail={
|
||||
"error": "Unknown payer",
|
||||
"detail": f"Unknown payer {name!r}. Choose from: {', '.join(PAYER_FACTORIES)}",
|
||||
},
|
||||
)
|
||||
return PAYER_FACTORIES[name]()
|
||||
|
||||
|
||||
def _resolve_payer_835(name: str) -> PayerConfig835:
|
||||
if name not in PAYER_FACTORIES_835:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail={
|
||||
"error": "Unknown payer",
|
||||
"detail": f"Unknown payer {name!r}. Choose from: {', '.join(PAYER_FACTORIES_835)}",
|
||||
},
|
||||
)
|
||||
return PAYER_FACTORIES_835[name]()
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Envelope detection (SP36 Task 16)
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def _transaction_set_id_from_segments(segments: list[list[str]]) -> str | None:
|
||||
"""Return the ST01 transaction-set id (``"837"``, ``"835"``, ``"999"``...).
|
||||
|
||||
SP35 helper: scans the first few tokenized segments for the ST
|
||||
segment and returns its second element (ST01). Returns None when no
|
||||
ST is present — e.g. a TA1 file, which uses the bare TA1 segment
|
||||
and no ST envelope. The endpoint-level envelope guards treat
|
||||
``None`` as "no ST found; let the parser decide" so TA1 files
|
||||
routed through the wrong endpoint still surface a parse error
|
||||
rather than a misleading "expected 837p, got ''" message.
|
||||
"""
|
||||
for seg in segments[:5]: # ST is always the second segment after ISA
|
||||
if seg and seg[0] == "ST" and len(seg) > 1:
|
||||
return seg[1]
|
||||
return None
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# 999 ACK builder (SP36 Task 16)
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def _build_and_persist_ack(batch_id: str) -> dict | None:
|
||||
"""Build a 999 ACK for ``batch_id`` and persist the row.
|
||||
|
||||
Returns the ack payload dict (matches the ``/api/parse-999``
|
||||
response shape so the JSON and NDJSON clients can share the
|
||||
schema) or None if the build failed. The build is fail-soft:
|
||||
errors are logged but never abort the 837 ingest, because the
|
||||
user-visible 837 result is still correct.
|
||||
"""
|
||||
try:
|
||||
ack_result = build_ack_for_batch(batch_id)
|
||||
except Exception:
|
||||
log.exception("build_ack_for_batch failed for %s", batch_id)
|
||||
return None
|
||||
fg = ack_result.functional_group_acks[0] if ack_result.functional_group_acks else None
|
||||
if fg is None:
|
||||
return None
|
||||
raw_text = serialize_999(ack_result, interchange_control_number=ack_result.envelope.control_number)
|
||||
row = store.add_ack(
|
||||
source_batch_id=batch_id,
|
||||
accepted_count=fg.accepted_count,
|
||||
rejected_count=fg.rejected_count,
|
||||
received_count=fg.received_count,
|
||||
ack_code=fg.ack_code,
|
||||
raw_json=json.loads(ack_result.model_dump_json()),
|
||||
)
|
||||
return {
|
||||
"id": row.id,
|
||||
"accepted_count": fg.accepted_count,
|
||||
"rejected_count": fg.rejected_count,
|
||||
"received_count": fg.received_count,
|
||||
"ack_code": fg.ack_code,
|
||||
"source_batch_id": batch_id,
|
||||
"raw_999_text": raw_text,
|
||||
}
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Reconciliation summary (SP36 Task 16)
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def _reconciliation_summary_for_batch(batch_id: str) -> dict:
|
||||
"""Return ``{matched, unmatched_claims, unmatched_remittances, skipped}`` for a batch.
|
||||
|
||||
Reads from the DB after ``store.add()`` has already run reconciliation
|
||||
synchronously (SP27 Task 10: ``reconcile.run(s, batch_id)`` inside the
|
||||
ingest session, before commit). Counts are observed at this moment;
|
||||
a subsequent manual match/unmatch will not be reflected until the
|
||||
next request.
|
||||
|
||||
``skipped`` is reserved for future use — the orchestrator tracks
|
||||
skipped claims internally but does not surface a queryable count.
|
||||
"""
|
||||
from sqlalchemy import func, select
|
||||
from cyclone.db import Match, Remittance
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
matched = s.execute(
|
||||
select(func.count(Match.id)).where(
|
||||
Match.remittance_id.in_(
|
||||
select(Remittance.id).where(Remittance.batch_id == batch_id)
|
||||
)
|
||||
)
|
||||
).scalar_one()
|
||||
|
||||
# Pull unmatched via the store (small result set; cheap).
|
||||
unmatched = store.list_unmatched(kind="both")
|
||||
return {
|
||||
"matched": matched,
|
||||
"unmatched_claims": len(unmatched["claims"]),
|
||||
"unmatched_remittances": len(unmatched["remittances"]),
|
||||
"skipped": 0, # reserved — T10 does not persist a skipped count
|
||||
}
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# TA1 synthetic source batch id (SP36 Task 16)
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def _ta1_synthetic_source_batch_id(interchange_control_number: str) -> str:
|
||||
"""Return a synthetic ``batches.id`` for a received TA1 with no source batch.
|
||||
|
||||
Mirrors :func:`_ack_synthetic_source_batch_id` (in
|
||||
``cyclone.handlers._ack_id``). The ta1_acks.source_batch_id
|
||||
FK requires a row in batches; for received TA1s we synthesize an
|
||||
id of the form ``TA1-<ISA13>``. The row is NOT created in batches
|
||||
(same FK-is-no-op convention as the 999 path).
|
||||
"""
|
||||
return f"TA1-{(interchange_control_number or '').strip() or '000000001'}"
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# TA1 serializer (SP36 Task 16)
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def _serialize_ta1(result) -> str:
|
||||
"""Render a TA1 file from a ParseResultTa1 for the ``raw_ta1_text`` field.
|
||||
|
||||
Mirrors what the parser consumed: ISA envelope → TA1 → IEA. We
|
||||
rebuild minimal ISA fields from the envelope plus the TA1 segment
|
||||
verbatim. The serializer is intentionally tiny — TA1 has no GS/ST,
|
||||
so there's no functional-group structure to round-trip.
|
||||
"""
|
||||
ta1 = result.ta1
|
||||
parts = [
|
||||
f"TA1*{ta1.control_number}*{ta1.interchange_date.strftime('%y%m%d') if ta1.interchange_date else ''}*"
|
||||
f"{ta1.interchange_time or ''}*{ta1.ack_code}*{ta1.note_code or ''}*"
|
||||
f"{ta1.ack_generated_date.strftime('%y%m%d') if ta1.ack_generated_date else ''}",
|
||||
]
|
||||
return "~".join(parts) + "~"
|
||||
@@ -1,12 +1,17 @@
|
||||
"""``/api/acks`` — list, detail, and live-tail stream for 999 ACKs.
|
||||
"""``/api/acks`` and ``/api/277ca-acks`` — list, detail, and live-tail.
|
||||
|
||||
These are the persisted acknowledgment rows produced by
|
||||
999 ACKs are the persisted acknowledgment rows produced by
|
||||
``POST /api/parse-999``. The frontend ``useAcks`` hook re-shapes the
|
||||
list payload to its ``Ack`` interface in ``src/types/index.ts``.
|
||||
|
||||
The detail endpoint returns the full ``raw_json`` payload plus the
|
||||
regenerated ``raw_999_text`` so the UI can show "view source" without a
|
||||
second round-trip.
|
||||
277CA ACKs are the persisted Claim Acknowledgment rows produced by
|
||||
``POST /api/parse-277ca``. The frontend ``useAcks`` hook treats them
|
||||
as a second source alongside 999 — the row shape is normalised in
|
||||
``cyclone.store.ui.to_ui_two77ca_ack``.
|
||||
|
||||
The 999 detail endpoint returns the full ``raw_json`` payload plus
|
||||
the regenerated ``raw_999_text`` so the UI can show "view source"
|
||||
without a second round-trip.
|
||||
|
||||
SP25: ``/api/acks/stream`` joins the live-tail triplet — the Acks
|
||||
page mounts ``useTailStream("acks")`` and ``useMergedTail("acks", …)``
|
||||
@@ -18,7 +23,7 @@ from __future__ import annotations
|
||||
import logging
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from cyclone import db
|
||||
@@ -28,12 +33,13 @@ from cyclone.api_helpers import (
|
||||
tail_events,
|
||||
wants_ndjson,
|
||||
)
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.parsers.models_999 import ParseResult999
|
||||
from cyclone.parsers.serialize_999 import serialize_999
|
||||
from cyclone.pubsub import EventBus
|
||||
from cyclone.store import store, to_ui_ack
|
||||
from cyclone.store import store, to_ui_ack, to_ui_two77ca_ack
|
||||
|
||||
router = APIRouter()
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -177,3 +183,41 @@ def get_ack_endpoint(ack_id: int) -> dict:
|
||||
else:
|
||||
body["raw_999_text"] = None
|
||||
return body
|
||||
|
||||
|
||||
# -- 277CA ACKs -------------------------------------------------------------
|
||||
# SP36 Task 2: these two endpoints moved here from api.py (lines 1278-1318).
|
||||
# 277CA ACKs share the same shape-of-life contract as 999 ACKs: persisted by
|
||||
# a parse endpoint, listed newest-first, detail returns raw_json so the UI
|
||||
# can show "view source" without a round-trip.
|
||||
|
||||
|
||||
@router.get("/api/277ca-acks")
|
||||
def list_277ca_acks_endpoint(
|
||||
limit: int = Query(100, ge=1, le=5000),
|
||||
) -> Any:
|
||||
"""Return the list of persisted 277CA ACKs, newest first.
|
||||
|
||||
SP28: each item gains ``linked_claim_ids`` (batch-fetched via
|
||||
the shared ``_find_linked_claim_ids_for_acks`` helper — one
|
||||
SELECT keyed on ``ack_kind``, no N+1) so the Acks page row
|
||||
can render the "🔗 N claims" badge inline.
|
||||
"""
|
||||
rows = store.list_277ca_acks()
|
||||
items = [to_ui_two77ca_ack(r) for r in rows[:limit]]
|
||||
ack_ids = [r.id for r in rows]
|
||||
linked_map = _find_linked_claim_ids_for_acks(ack_ids, kind="277ca")
|
||||
for item, aid in zip(items, ack_ids[:limit]):
|
||||
item["linked_claim_ids"] = linked_map.get(aid, [])
|
||||
return {"total": len(rows), "items": items}
|
||||
|
||||
|
||||
@router.get("/api/277ca-acks/{ack_id}")
|
||||
def get_277ca_ack_endpoint(ack_id: int) -> dict:
|
||||
"""Return one persisted 277CA ACK row with its parsed detail."""
|
||||
row = store.get_277ca_ack(ack_id)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail=f"277CA ACK {ack_id} not found")
|
||||
body = to_ui_two77ca_ack(row)
|
||||
body["raw_json"] = row.raw_json
|
||||
return body
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
"""``/api/activity`` and ``/api/activity/stream`` — operator-facing event log.
|
||||
|
||||
Two endpoints, both gated by ``matrix_gate``:
|
||||
|
||||
- ``GET /api/activity`` — paginated event list with ``kind`` /
|
||||
``since`` filters, plus an NDJSON variant when the caller sends
|
||||
``Accept: application/x-ndjson``.
|
||||
- ``GET /api/activity/stream`` — NDJSON live-tail: snapshot of the
|
||||
most recent N events, then ``activity_recorded`` events as they
|
||||
hit the store. Default ``limit`` is 50 (smaller than the list
|
||||
endpoint's 200) because activity is high-volume — callers usually
|
||||
want the most recent handful, not a full replay.
|
||||
|
||||
The snapshot halves of ``/api/activity`` and ``/api/activity/stream``
|
||||
share the same in-memory filter logic (``kind`` + ``since``) so the
|
||||
two endpoints are interchangeable for the snapshot half.
|
||||
|
||||
SP36 Task 10: this block moved here from ``api.py:2606`` (the
|
||||
``/api/activity*`` pair).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
from fastapi import APIRouter, Depends, Query, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from cyclone.api_helpers import (
|
||||
ndjson_line as _ndjson_line,
|
||||
ndjson_stream_list as _ndjson_stream_list,
|
||||
tail_events as _tail_events,
|
||||
wants_ndjson as _wants_ndjson,
|
||||
)
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.pubsub import EventBus
|
||||
from cyclone.store import store
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
|
||||
@router.get("/api/activity")
|
||||
def list_activity(
|
||||
request: Request,
|
||||
kind: str | None = Query(None),
|
||||
since: str | None = Query(None),
|
||||
limit: int = Query(200, ge=1, le=5000),
|
||||
) -> Any:
|
||||
events = store.recent_activity(limit=limit)
|
||||
if kind is not None:
|
||||
events = [e for e in events if e["kind"] == kind]
|
||||
if since is not None:
|
||||
events = [e for e in events if e["timestamp"] >= since]
|
||||
total = len(events)
|
||||
has_more = False
|
||||
if _wants_ndjson(request):
|
||||
return StreamingResponse(
|
||||
_ndjson_stream_list(events, total, total, has_more),
|
||||
media_type="application/x-ndjson",
|
||||
)
|
||||
return {
|
||||
"items": events,
|
||||
"total": total,
|
||||
"returned": total,
|
||||
"has_more": has_more,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/api/activity/stream")
|
||||
async def activity_stream(
|
||||
request: Request,
|
||||
kind: str | None = Query(None),
|
||||
since: str | None = Query(None),
|
||||
limit: int = Query(50, ge=1, le=5000),
|
||||
) -> StreamingResponse:
|
||||
"""Stream Activity events as NDJSON: snapshot first, then live events.
|
||||
|
||||
Subscribes to ``activity_recorded``. Default ``limit`` is 50
|
||||
(smaller than the list endpoint's 200) because activity is
|
||||
high-volume — callers usually want the most recent handful, not a
|
||||
full replay.
|
||||
"""
|
||||
bus: EventBus = request.app.state.event_bus
|
||||
|
||||
async def gen() -> AsyncIterator[bytes]:
|
||||
# Snapshot reuses the same in-memory filter as ``list_activity``
|
||||
# so the two endpoints are interchangeable for the snapshot
|
||||
# half.
|
||||
events = store.recent_activity(limit=limit)
|
||||
if kind is not None:
|
||||
events = [e for e in events if e["kind"] == kind]
|
||||
if since is not None:
|
||||
events = [e for e in events if e["timestamp"] >= since]
|
||||
for ev in events:
|
||||
yield _ndjson_line({"type": "item", "data": ev})
|
||||
yield _ndjson_line({
|
||||
"type": "snapshot_end", "data": {"count": len(events)},
|
||||
})
|
||||
|
||||
async for chunk in _tail_events(request, bus, ["activity_recorded"]):
|
||||
yield chunk
|
||||
|
||||
return StreamingResponse(gen(), media_type="application/x-ndjson")
|
||||
@@ -1,23 +1,45 @@
|
||||
"""``/api/admin/validate-provider`` — NPI + Tax ID liveness probe (SP20).
|
||||
"""``/api/admin/*`` — operator-only endpoints.
|
||||
|
||||
Pure read-only endpoint that runs the local NPI Luhn + EIN format checks
|
||||
without touching the DB. Useful for:
|
||||
- operators vetting a new provider before adding them to the registry,
|
||||
- the dashboard's "validate" button on a Provider row,
|
||||
- smoke-testing the SP20 checks after a deploy.
|
||||
The /api/admin namespace covers:
|
||||
|
||||
Both query params are optional; omitting one just skips that check.
|
||||
Returns the per-check result dict so the caller can distinguish "bad
|
||||
format" from "bad checksum".
|
||||
* **audit-log** (SP11): list + chain-verify the tamper-evident log
|
||||
* **db/rotate-key**: SQLCipher key rotation (SP12)
|
||||
* **backup**: create / list / status / verify / restore / prune / scheduler (SP15)
|
||||
* **scheduler**: backup & main scheduler control + processed-files log
|
||||
* **reload-config**: hot-reload ``config/payers.yaml`` after edits
|
||||
* **validate-provider**: NPI + Tax ID liveness probe (SP20)
|
||||
|
||||
All routes on this router are gated by ``matrix_gate`` — declared
|
||||
once on the ``APIRouter`` constructor. ``/api/admin/validate-provider``
|
||||
lives here too because it's an admin-shaped read-only probe.
|
||||
|
||||
SP36 Task 3: the 20 admin endpoints formerly in ``cyclone.api``
|
||||
(audit-log ×2, db/rotate-key ×1, backup ×10, scheduler ×6,
|
||||
reload-config ×1) moved here from ``api.py:3321-4052`` and
|
||||
``api.py:4269-4276``. The non-admin ``/api/config/*`` and
|
||||
``/api/payers/{id}/summary`` routes that bracketed those blocks
|
||||
stay in ``api.py`` for now — they're extracted in Tasks 5 & 6.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Query
|
||||
import json
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, Query, UploadFile
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from cyclone import db, edifabric
|
||||
from cyclone.audit_log import verify_chain
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.clearhouse import InboundFile
|
||||
from cyclone.npi import is_valid_npi, is_valid_tax_id, normalize_tax_id
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
router = APIRouter()
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@router.get("/api/admin/validate-provider")
|
||||
@@ -58,3 +80,803 @@ def validate_provider(
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# SP36 Task 3: the 20 admin endpoints below were moved here from api.py. #
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# SP11: tamper-evident audit log (admin)
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
@router.get("/api/admin/audit-log")
|
||||
def list_audit_log_endpoint(
|
||||
entity_type: str | None = Query(default=None),
|
||||
entity_id: str | None = Query(default=None),
|
||||
event_type: str | None = Query(default=None),
|
||||
limit: int = Query(default=100, ge=1, le=1000),
|
||||
) -> Any:
|
||||
"""List audit-log rows, newest first, with optional filters.
|
||||
|
||||
Filters match the (entity_type, entity_id) pair (typical use:
|
||||
"show me everything that happened to claim C-123") or a single
|
||||
event_type (typical use: "show me all clearhouse.submitted
|
||||
events today").
|
||||
"""
|
||||
with db.SessionLocal()() as s:
|
||||
q = s.query(db.AuditLog)
|
||||
if entity_type:
|
||||
q = q.filter(db.AuditLog.entity_type == entity_type)
|
||||
if entity_id:
|
||||
q = q.filter(db.AuditLog.entity_id == entity_id)
|
||||
if event_type:
|
||||
q = q.filter(db.AuditLog.event_type == event_type)
|
||||
rows = q.order_by(db.AuditLog.id.desc()).limit(limit).all()
|
||||
return {
|
||||
"total": len(rows),
|
||||
"items": [
|
||||
{
|
||||
"id": r.id,
|
||||
"event_type": r.event_type,
|
||||
"entity_type": r.entity_type,
|
||||
"entity_id": r.entity_id,
|
||||
"actor": r.actor,
|
||||
"payload": json.loads(r.payload_json) if r.payload_json else None,
|
||||
"created_at": r.created_at.isoformat() if r.created_at else None,
|
||||
"prev_hash": r.prev_hash,
|
||||
"hash": r.hash,
|
||||
}
|
||||
for r in rows
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@router.get("/api/admin/audit-log/verify")
|
||||
def verify_audit_log_endpoint() -> Any:
|
||||
"""Walk the audit-log chain and verify every row's hash.
|
||||
|
||||
Returns ``{"ok": true, "checked": N}`` for a clean chain, or
|
||||
``{"ok": false, "checked": K, "first_bad_id": X, "reason": "..."}``
|
||||
for a broken chain. This is the operator's "did anyone tamper?"
|
||||
endpoint; run it on demand or via a nightly cron job.
|
||||
"""
|
||||
with db.SessionLocal()() as s:
|
||||
result = verify_chain(s)
|
||||
return {
|
||||
"ok": result.ok,
|
||||
"checked": result.checked,
|
||||
"first_bad_id": result.first_bad_id,
|
||||
"reason": result.reason,
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# SP15: SQLCipher key rotation
|
||||
#
|
||||
# Re-encrypts the DB in place with a fresh key, then updates the
|
||||
# Keychain so subsequent connections open with the new key. This is
|
||||
# a 1-time operation per rotation; for routine read/write the rest
|
||||
# of the API is unchanged.
|
||||
#
|
||||
# Concurrency: the rotation holds a module-level lock so two
|
||||
# concurrent requests can't race and end up with mismatched Keychain
|
||||
# + DB. The lock is a simple threading.Lock; a process restart
|
||||
# resets it (intentional — the operator's next start-up opens with
|
||||
# whatever key is in the Keychain).
|
||||
# ---------------------------------------------------------------------------
|
||||
from cyclone import db_crypto as _db_crypto
|
||||
from cyclone import secrets as _secrets
|
||||
|
||||
_db_rotate_lock = threading.Lock()
|
||||
|
||||
|
||||
@router.post("/api/admin/db/rotate-key")
|
||||
def rotate_db_key_endpoint(body: dict | None = None) -> Any:
|
||||
"""Generate a fresh DB key, re-encrypt the DB, update the Keychain.
|
||||
|
||||
Request body (optional):
|
||||
actor: who initiated the rotation. Defaults to "operator".
|
||||
reason: human-readable reason. Written to the audit log.
|
||||
|
||||
Returns:
|
||||
``{ok, old_fingerprint, new_fingerprint, rotated_at, table_count}``
|
||||
on success. On failure (DB not encrypted, rekey failed,
|
||||
Keychain update failed) returns the same shape with
|
||||
``ok=false`` and a ``reason``. HTTP 503 is returned if the
|
||||
rekey fails or encryption is not enabled.
|
||||
|
||||
The Keychain write happens *after* the rekey succeeds. If the
|
||||
Keychain write fails, the DB has the new key but the Keychain
|
||||
still has the old one — the endpoint returns 503 with a
|
||||
"keychain update failed" reason and the operator must restore
|
||||
the old key manually (``cyclone db restore-key <old_key>``) to
|
||||
avoid being locked out.
|
||||
"""
|
||||
body = body or {}
|
||||
actor = body.get("actor") or "operator"
|
||||
reason = body.get("reason") or ""
|
||||
|
||||
if not _db_crypto.is_encryption_enabled():
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="encryption not enabled (sqlcipher3 missing or no Keychain key)",
|
||||
)
|
||||
|
||||
# Acquire the lock; non-blocking so a stuck rotation doesn't
|
||||
# silently hold up other requests.
|
||||
if not _db_rotate_lock.acquire(blocking=False):
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail="another key rotation is in progress",
|
||||
)
|
||||
try:
|
||||
url = db._resolve_url()
|
||||
old_key = _db_crypto.get_db_key()
|
||||
if not old_key:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="no DB key in Keychain; cannot rotate",
|
||||
)
|
||||
|
||||
new_key = _db_crypto.generate_db_key()
|
||||
result = _db_crypto.rotate_db_key(
|
||||
url=url, old_key=old_key, new_key=new_key,
|
||||
)
|
||||
if not result.ok:
|
||||
# Rekey failed. The DB still has the old key. The
|
||||
# Keychain is unchanged. Caller should NOT retry with
|
||||
# the same new key (it's lost); generate a fresh one.
|
||||
log.error("SQLCipher rotate failed: %s", result.reason)
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail={
|
||||
"ok": False,
|
||||
"old_fingerprint": result.old_fingerprint,
|
||||
"new_fingerprint": result.new_fingerprint,
|
||||
"rotated_at": result.rotated_at,
|
||||
"reason": result.reason,
|
||||
},
|
||||
)
|
||||
|
||||
# Rekey succeeded. Now update the Keychain. If this fails
|
||||
# the DB is locked behind the new key — operator must
|
||||
# restore the old key manually.
|
||||
if not _secrets.set_secret(_db_crypto.KEYCHAIN_ACCOUNT, new_key):
|
||||
log.error("Keychain update failed after successful rekey!")
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail={
|
||||
"ok": False,
|
||||
"old_fingerprint": result.old_fingerprint,
|
||||
"new_fingerprint": result.new_fingerprint,
|
||||
"rotated_at": result.rotated_at,
|
||||
"reason": (
|
||||
"rekey succeeded but Keychain update failed — "
|
||||
"the DB is now encrypted with the new key but "
|
||||
"the Keychain still has the old one. "
|
||||
"Restore the old key to the Keychain to recover."
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
# Store the old key in the "previous" account for a grace
|
||||
# period so the operator can roll back if they discover the
|
||||
# new key is broken (e.g. the Keychain entry got truncated).
|
||||
_secrets.set_secret(_db_crypto.KEYCHAIN_ACCOUNT_PREVIOUS, old_key)
|
||||
|
||||
# Rebuild the engine so subsequent connections use the new
|
||||
# key. dispose_engine() closes every pooled connection that
|
||||
# was using the old key; init_db() opens new ones with the
|
||||
# new key from the (now-updated) Keychain.
|
||||
db.reinit_engine()
|
||||
|
||||
# Audit log the rotation. We do this after the engine is
|
||||
# rebuilt so the audit event is written with the new key —
|
||||
# proving that the new key works for new writes.
|
||||
try:
|
||||
from cyclone.audit_log import append_event, AuditEvent
|
||||
with db.SessionLocal()() as s:
|
||||
append_event(s, AuditEvent(
|
||||
event_type="db.key_rotated",
|
||||
entity_type="database",
|
||||
entity_id="cyclone.db",
|
||||
actor=actor,
|
||||
payload={
|
||||
"old_fingerprint": result.old_fingerprint,
|
||||
"new_fingerprint": result.new_fingerprint,
|
||||
"table_count": result.table_count,
|
||||
"reason": reason,
|
||||
},
|
||||
))
|
||||
s.commit()
|
||||
except Exception as exc: # noqa: BLE001
|
||||
# Audit append is best-effort; rotation already succeeded.
|
||||
log.warning("could not write audit event for rotation: %s", exc)
|
||||
|
||||
return {
|
||||
"ok": True,
|
||||
"old_fingerprint": result.old_fingerprint,
|
||||
"new_fingerprint": result.new_fingerprint,
|
||||
"rotated_at": result.rotated_at,
|
||||
"table_count": result.table_count,
|
||||
}
|
||||
finally:
|
||||
_db_rotate_lock.release()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# SP17: encrypted DB backups (admin)
|
||||
#
|
||||
# The actual encryption + lifecycle lives in :mod:`cyclone.backup` and
|
||||
# :mod:`cyclone.backup_service`. The scheduler (separate from the
|
||||
# MFT scheduler) lives in :mod:`cyclone.backup_scheduler`. These
|
||||
# endpoints expose the operator's manual controls plus a tick for
|
||||
# "take a backup right now."
|
||||
#
|
||||
# Restore is intentionally two-step: an idle browser tab can't nuke
|
||||
# the live DB. The first call returns a ``restore_token`` (a one-shot
|
||||
# 64-char hex) and a preview of the backup's fingerprint + table
|
||||
# count plus the live DB's. The second call with the token performs
|
||||
# the actual swap.
|
||||
# ---------------------------------------------------------------------------
|
||||
from cyclone import backup_service as _backup_svc_mod
|
||||
from cyclone import backup_scheduler as _backup_sched_mod
|
||||
|
||||
|
||||
def _backup_or_503():
|
||||
try:
|
||||
return _backup_svc_mod.get_backup_service()
|
||||
except RuntimeError as exc:
|
||||
raise HTTPException(status_code=503, detail=str(exc))
|
||||
|
||||
|
||||
@router.post("/api/admin/backup/create")
|
||||
def backup_create() -> Any:
|
||||
"""Take an encrypted backup right now. Returns the new backup metadata."""
|
||||
from cyclone import audit_log as _audit
|
||||
svc = _backup_or_503()
|
||||
try:
|
||||
result = svc.create_now()
|
||||
except Exception as exc: # noqa: BLE001
|
||||
# Surface a 503 with the reason so the operator sees what
|
||||
# went wrong without grepping server logs.
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail=f"backup failed: {type(exc).__name__}: {exc}",
|
||||
)
|
||||
# Audit the create. Best-effort; failure here doesn't roll back
|
||||
# the backup (already on disk).
|
||||
try:
|
||||
with db.SessionLocal()() as s:
|
||||
_audit.append_event(s, _audit.AuditEvent(
|
||||
event_type="db.backup_created",
|
||||
entity_type="database",
|
||||
entity_id="cyclone.db",
|
||||
actor="operator",
|
||||
payload={
|
||||
"backup_id": result.backup.id,
|
||||
"db_fingerprint": result.backup.db_fingerprint,
|
||||
"table_count": result.backup.table_count,
|
||||
"triggered_by": "api",
|
||||
},
|
||||
))
|
||||
s.commit()
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning("could not write backup_created audit event: %s", exc)
|
||||
|
||||
return {
|
||||
"ok": True,
|
||||
"backup": {
|
||||
"id": result.backup.id,
|
||||
"filename": result.backup.filename,
|
||||
"size_bytes": result.backup.size_bytes,
|
||||
"db_fingerprint": result.backup.db_fingerprint,
|
||||
"table_count": result.backup.table_count,
|
||||
"created_at": result.backup.created_at.isoformat(),
|
||||
"key_fingerprint": result.backup.key_fingerprint,
|
||||
},
|
||||
"sidecar": {
|
||||
"format_version": result.sidecar.format_version,
|
||||
"kdf": result.sidecar.kdf,
|
||||
"kdf_iterations": result.sidecar.kdf_iterations,
|
||||
"cipher": result.sidecar.cipher,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@router.get("/api/admin/backup/list")
|
||||
def backup_list(
|
||||
limit: int = Query(default=100, ge=1, le=1000),
|
||||
status: str | None = Query(default=None),
|
||||
) -> Any:
|
||||
"""List ``db_backups`` rows, newest first. Filters by status."""
|
||||
svc = _backup_or_503()
|
||||
rows = svc.list_backups(limit=limit, status=status)
|
||||
return {
|
||||
"count": len(rows),
|
||||
"files": [
|
||||
{
|
||||
"id": r.id,
|
||||
"filename": r.filename,
|
||||
"backup_dir": r.backup_dir,
|
||||
"size_bytes": r.size_bytes,
|
||||
"db_fingerprint": r.db_fingerprint,
|
||||
"table_count": r.table_count,
|
||||
"created_at": r.created_at.isoformat() if r.created_at else None,
|
||||
"completed_at": r.completed_at.isoformat() if r.completed_at else None,
|
||||
"status": r.status,
|
||||
"error_message": r.error_message,
|
||||
"key_fingerprint": r.key_fingerprint,
|
||||
}
|
||||
for r in rows
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@router.get("/api/admin/backup/status")
|
||||
def backup_status() -> Any:
|
||||
"""Snapshot of the backup subsystem (counts, disk usage, last run)."""
|
||||
svc = _backup_or_503()
|
||||
snap = svc.status()
|
||||
# Also include the BackupScheduler's snapshot if configured.
|
||||
try:
|
||||
sched = _backup_sched_mod.get_backup_scheduler()
|
||||
snap["scheduler"] = sched.status().as_dict()
|
||||
except RuntimeError:
|
||||
snap["scheduler"] = None
|
||||
return snap
|
||||
|
||||
|
||||
@router.post("/api/admin/backup/{backup_id}/verify")
|
||||
def backup_verify(backup_id: int) -> Any:
|
||||
"""Decrypt + checksum-verify a backup against its sidecar."""
|
||||
svc = _backup_or_503()
|
||||
try:
|
||||
v = svc.verify(backup_id)
|
||||
except _backup_svc_mod.BackupError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc))
|
||||
return {
|
||||
"backup_id": v.backup_id,
|
||||
"filename": v.filename,
|
||||
"ok": v.ok,
|
||||
"expected_fingerprint": v.expected_fingerprint,
|
||||
"actual_fingerprint": v.actual_fingerprint,
|
||||
"table_count": v.table_count,
|
||||
"reason": v.reason,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/api/admin/backup/{backup_id}/restore/initiate")
|
||||
def backup_restore_initiate(backup_id: int) -> Any:
|
||||
"""First step of the two-step restore. Returns a ``restore_token``."""
|
||||
svc = _backup_or_503()
|
||||
try:
|
||||
init = svc.restore_initiate(backup_id)
|
||||
except _backup_svc_mod.BackupError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
return {
|
||||
"backup_id": init.backup_id,
|
||||
"filename": init.filename,
|
||||
"size_bytes": init.size_bytes,
|
||||
"restore_token": init.restore_token,
|
||||
"expires_at": init.expires_at.isoformat(),
|
||||
"preview": {
|
||||
"backup_db_fingerprint": init.db_fingerprint,
|
||||
"backup_table_count": init.table_count,
|
||||
"current_db_fingerprint": init.current_db_fingerprint,
|
||||
"current_table_count": init.current_table_count,
|
||||
},
|
||||
"warning": (
|
||||
"Confirming will dispose the live engine and replace the DB "
|
||||
"file with the backup. In-flight requests will error. "
|
||||
"Re-issue the call with the restore_token within 5 minutes."
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
@router.post("/api/admin/backup/{backup_id}/restore/confirm")
|
||||
def backup_restore_confirm(
|
||||
backup_id: int,
|
||||
body: dict | None = None,
|
||||
) -> Any:
|
||||
"""Second step of the two-step restore. Performs the swap."""
|
||||
body = body or {}
|
||||
token = body.get("restore_token")
|
||||
if not token or not isinstance(token, str):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="missing or invalid restore_token in request body",
|
||||
)
|
||||
actor = body.get("actor") or "operator"
|
||||
|
||||
svc = _backup_or_503()
|
||||
try:
|
||||
result = svc.restore_confirm(backup_id, token, actor=actor)
|
||||
except _backup_svc_mod.BackupError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
|
||||
# Audit the restore. Best-effort.
|
||||
try:
|
||||
from cyclone import audit_log as _audit
|
||||
with db.SessionLocal()() as s:
|
||||
_audit.append_event(s, _audit.AuditEvent(
|
||||
event_type="db.backup_restored",
|
||||
entity_type="database",
|
||||
entity_id="cyclone.db",
|
||||
actor=actor,
|
||||
payload={
|
||||
"backup_id": result.backup_id,
|
||||
"filename": result.filename,
|
||||
"restored_from_fingerprint": result.restored_from_fingerprint,
|
||||
"new_db_fingerprint": result.new_db_fingerprint,
|
||||
"restored_at": result.restored_at.isoformat(),
|
||||
},
|
||||
))
|
||||
s.commit()
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning("could not write backup_restored audit event: %s", exc)
|
||||
|
||||
return {
|
||||
"ok": True,
|
||||
"backup_id": result.backup_id,
|
||||
"filename": result.filename,
|
||||
"restored_from_fingerprint": result.restored_from_fingerprint,
|
||||
"restored_at": result.restored_at.isoformat(),
|
||||
"new_db_fingerprint": result.new_db_fingerprint,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/api/admin/backup/prune")
|
||||
def backup_prune() -> Any:
|
||||
"""Apply the retention policy now. Returns the deleted paths."""
|
||||
from cyclone import audit_log as _audit
|
||||
svc = _backup_or_503()
|
||||
deleted = svc.prune()
|
||||
actor = "operator"
|
||||
if deleted:
|
||||
try:
|
||||
with db.SessionLocal()() as s:
|
||||
_audit.append_event(s, _audit.AuditEvent(
|
||||
event_type="db.backup_pruned",
|
||||
entity_type="database",
|
||||
entity_id="cyclone.db",
|
||||
actor=actor,
|
||||
payload={"deleted_paths": deleted},
|
||||
))
|
||||
s.commit()
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning("could not write backup_pruned audit event: %s", exc)
|
||||
return {"ok": True, "deleted_count": len(deleted), "deleted_paths": deleted}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# SP17: backup scheduler (admin)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@router.post("/api/admin/backup/scheduler/start")
|
||||
async def backup_scheduler_start() -> Any:
|
||||
"""Begin the backup scheduler loop."""
|
||||
try:
|
||||
sched = _backup_sched_mod.get_backup_scheduler()
|
||||
except RuntimeError as exc:
|
||||
raise HTTPException(status_code=503, detail=str(exc))
|
||||
await sched.start()
|
||||
return {"status": sched.status().as_dict()}
|
||||
|
||||
|
||||
@router.post("/api/admin/backup/scheduler/stop")
|
||||
async def backup_scheduler_stop() -> Any:
|
||||
"""Stop the backup scheduler loop."""
|
||||
try:
|
||||
sched = _backup_sched_mod.get_backup_scheduler()
|
||||
except RuntimeError as exc:
|
||||
raise HTTPException(status_code=503, detail=str(exc))
|
||||
await sched.stop()
|
||||
return {"status": sched.status().as_dict()}
|
||||
|
||||
|
||||
@router.post("/api/admin/backup/scheduler/tick")
|
||||
async def backup_scheduler_tick() -> Any:
|
||||
"""Run one backup tick now (create + prune + audit)."""
|
||||
try:
|
||||
sched = _backup_sched_mod.get_backup_scheduler()
|
||||
except RuntimeError as exc:
|
||||
raise HTTPException(status_code=503, detail=str(exc))
|
||||
result = await sched.tick()
|
||||
return {"ok": result.ok, "tick": result.as_dict()}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# SP16: live MFT polling scheduler (admin)
|
||||
#
|
||||
# The scheduler lives in :mod:`cyclone.scheduler` and is configured by
|
||||
# the lifespan handler. The endpoints below expose start / stop /
|
||||
# one-shot tick / status / history so an operator (or a cron job)
|
||||
# can drive the scheduler without touching the DB.
|
||||
#
|
||||
# Note: the scheduler is OFF by default. Auto-start is opt-in via
|
||||
# ``CYCLONE_SCHEDULER_AUTOSTART=true`` at launch. These endpoints
|
||||
# are the operator's manual controls.
|
||||
# ---------------------------------------------------------------------------
|
||||
from cyclone import scheduler as _scheduler_mod
|
||||
|
||||
|
||||
def _scheduler_or_503():
|
||||
"""Return the configured scheduler or raise 503."""
|
||||
try:
|
||||
return _scheduler_mod.get_scheduler()
|
||||
except RuntimeError as exc:
|
||||
raise HTTPException(status_code=503, detail=str(exc))
|
||||
|
||||
|
||||
@router.post("/api/admin/scheduler/start")
|
||||
async def scheduler_start() -> Any:
|
||||
"""Begin polling the MFT inbound path every poll_interval_seconds."""
|
||||
sched = _scheduler_or_503()
|
||||
await sched.start()
|
||||
return {"status": sched.status().as_dict()}
|
||||
|
||||
|
||||
@router.post("/api/admin/scheduler/stop")
|
||||
async def scheduler_stop() -> Any:
|
||||
"""Stop polling. Waits up to 30s for the current tick to finish."""
|
||||
sched = _scheduler_or_503()
|
||||
await sched.stop()
|
||||
return {"status": sched.status().as_dict()}
|
||||
|
||||
|
||||
@router.post("/api/admin/scheduler/tick")
|
||||
async def scheduler_tick() -> Any:
|
||||
"""Run a single poll cycle synchronously and return the result.
|
||||
|
||||
Useful for: forcing a poll without waiting for the next interval;
|
||||
verifying SFTP connectivity; running a one-shot import from the
|
||||
CLI (``curl -X POST .../api/admin/scheduler/tick``).
|
||||
"""
|
||||
sched = _scheduler_or_503()
|
||||
result = await sched.tick()
|
||||
return {"ok": True, "tick": result.as_dict()}
|
||||
|
||||
|
||||
@router.post("/api/admin/scheduler/pull-inbound")
|
||||
async def scheduler_pull_inbound(
|
||||
date: str = Query(
|
||||
..., pattern=r"^\d{8}$",
|
||||
description="Date filter as YYYYMMDD; only filenames whose 8-digit "
|
||||
"timestamp (the 9th positional group in the inbound "
|
||||
"filename) matches are downloaded and processed.",
|
||||
),
|
||||
file_types: str | None = Query(
|
||||
default=None,
|
||||
description="Optional comma-separated whitelist of file_types "
|
||||
"(999, TA1, 277, 277CA, 835). Defaults to 999+TA1.",
|
||||
),
|
||||
limit: int = Query(default=2000, ge=1, le=10000),
|
||||
) -> Any:
|
||||
"""Targeted pull: list, filter to a date, download, and process.
|
||||
|
||||
Bypasses the alphabetical full-listing pass. Workflow:
|
||||
1. ``SftpClient.list_inbound_names()`` — sub-second metadata-only
|
||||
listing of the inbound MFT dir (skips ``*_warn.txt``).
|
||||
2. Client-side filter: keep files whose 8-digit timestamp
|
||||
substring equals ``date`` and whose ``file_type`` is in the
|
||||
allowlist.
|
||||
3. ``SftpClient.download_inbound(f)`` for each — fetches bytes
|
||||
into the local cache.
|
||||
4. ``Scheduler.process_inbound_files(files)`` — runs the same
|
||||
per-file pipeline as a regular tick (already-processed files
|
||||
are deduped via ``processed_inbound_files``).
|
||||
|
||||
Use this for the daily "process today's 999s" workflow without
|
||||
paying the cost of downloading the full inbound set.
|
||||
|
||||
Returns ``{"ok": True, "summary": {...}}`` with
|
||||
``listed / matched / downloaded / processed / skipped / errored``
|
||||
counters and the date / file_type filters applied.
|
||||
"""
|
||||
|
||||
from cyclone.clearhouse import SftpClient
|
||||
from cyclone.edi.filenames import (
|
||||
ALLOWED_FILE_TYPES,
|
||||
parse_inbound_filename,
|
||||
)
|
||||
from cyclone.providers import SftpBlock
|
||||
|
||||
sched = _scheduler_or_503()
|
||||
block: SftpBlock = sched._sftp_block # noqa: SLF001 — internal but stable
|
||||
client = SftpClient(block)
|
||||
|
||||
if file_types:
|
||||
wanted = {t.strip().upper() for t in file_types.split(",") if t.strip()}
|
||||
unknown = wanted - ALLOWED_FILE_TYPES
|
||||
if unknown:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"file_types {sorted(unknown)!r} not in "
|
||||
f"{sorted(ALLOWED_FILE_TYPES)}",
|
||||
)
|
||||
else:
|
||||
wanted = {"999", "TA1"} # daily default — what the operator needs
|
||||
|
||||
started = time.monotonic()
|
||||
try:
|
||||
# Single SFTP listdir — fast, no download.
|
||||
all_files = await asyncio.to_thread(client.list_inbound_names)
|
||||
except Exception as exc:
|
||||
log.exception("SFTP list_inbound_names failed")
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail=f"SFTP list failed: {type(exc).__name__}: {exc}",
|
||||
) from exc
|
||||
|
||||
listed = len(all_files)
|
||||
matched: list[InboundFile] = []
|
||||
for f in all_files:
|
||||
if f.name.find(date) == -1:
|
||||
continue
|
||||
try:
|
||||
parsed = parse_inbound_filename(f.name)
|
||||
except ValueError:
|
||||
continue
|
||||
if parsed.file_type not in wanted:
|
||||
continue
|
||||
matched.append(f)
|
||||
if len(matched) >= limit:
|
||||
break
|
||||
|
||||
# Download in parallel-ish via to_thread (SftpClient serializes per
|
||||
# connection; the overhead is dominated by the SFTP round trip).
|
||||
downloaded = 0
|
||||
download_errors: list[str] = []
|
||||
for f in matched:
|
||||
try:
|
||||
await asyncio.to_thread(client.download_inbound, f)
|
||||
downloaded += 1
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning("Failed to download %s: %s", f.name, exc)
|
||||
download_errors.append(f"{f.name}: {type(exc).__name__}: {exc}")
|
||||
|
||||
# Hand off to the scheduler pipeline (idempotent; dedupes via
|
||||
# processed_inbound_files).
|
||||
tick = await sched.process_inbound_files(matched)
|
||||
duration = round(time.monotonic() - started, 3)
|
||||
return {
|
||||
"ok": True,
|
||||
"summary": {
|
||||
"date": date,
|
||||
"file_types": sorted(wanted),
|
||||
"limit": limit,
|
||||
"listed": listed,
|
||||
"matched": len(matched),
|
||||
"downloaded": downloaded,
|
||||
"download_errors": download_errors,
|
||||
"processed": tick.files_processed,
|
||||
"skipped": tick.files_skipped,
|
||||
"errored": tick.files_errored,
|
||||
"duration_s": duration,
|
||||
},
|
||||
"tick": tick.as_dict(),
|
||||
}
|
||||
|
||||
|
||||
@router.get("/api/admin/scheduler/status")
|
||||
def scheduler_status() -> Any:
|
||||
"""Return the scheduler's runtime snapshot (running, counters, last tick)."""
|
||||
sched = _scheduler_or_503()
|
||||
return sched.status().as_dict()
|
||||
|
||||
|
||||
@router.get("/api/admin/scheduler/processed-files")
|
||||
def scheduler_processed_files(
|
||||
limit: int = Query(default=100, ge=1, le=1000),
|
||||
status: str | None = Query(default=None),
|
||||
) -> Any:
|
||||
"""List rows from ``processed_inbound_files``, newest first.
|
||||
|
||||
The operator's "what did the scheduler do?" view. Filters by
|
||||
``status`` (``ok`` / ``error`` / ``skipped`` / ``pending``).
|
||||
Returns ``{"count": N, "files": [...]}`` where ``files[i]``
|
||||
matches the ORM row as a JSON dict.
|
||||
"""
|
||||
from cyclone.db import ProcessedInboundFile
|
||||
from cyclone.scheduler import STATUS_OK, STATUS_ERROR, STATUS_SKIPPED, STATUS_PENDING
|
||||
|
||||
valid_statuses = {STATUS_OK, STATUS_ERROR, STATUS_SKIPPED, STATUS_PENDING}
|
||||
if status is not None and status not in valid_statuses:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"status must be one of {sorted(valid_statuses)}",
|
||||
)
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
q = s.query(db.ProcessedInboundFile)
|
||||
if status is not None:
|
||||
q = q.filter(db.ProcessedInboundFile.status == status)
|
||||
rows = q.order_by(db.ProcessedInboundFile.id.desc()).limit(limit).all()
|
||||
return {
|
||||
"count": len(rows),
|
||||
"files": [
|
||||
{
|
||||
"id": r.id,
|
||||
"sftp_block_name": r.sftp_block_name,
|
||||
"name": r.name,
|
||||
"size": r.size,
|
||||
"modified_at": r.modified_at.isoformat() if r.modified_at else None,
|
||||
"file_type": r.file_type,
|
||||
"processed_at": r.processed_at.isoformat() if r.processed_at else None,
|
||||
"parser_used": r.parser_used,
|
||||
"claim_count": r.claim_count,
|
||||
"status": r.status,
|
||||
"error_message": r.error_message,
|
||||
}
|
||||
for r in rows
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@router.post("/api/admin/reload-config")
|
||||
def reload_config():
|
||||
"""Re-read ``config/payers.yaml`` and revalidate. Returns counts."""
|
||||
from cyclone import payers as payer_loader
|
||||
try:
|
||||
configs = payer_loader.load_payer_configs()
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
return {"ok": True, "loaded": len(configs), "errors": []}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# SP40: POST /api/admin/validate-837
|
||||
#
|
||||
# Admin-only Edifabric validation probe: upload an 837P file via
|
||||
# multipart, hit /v2/x12/read → /v2/x12/validate, return the raw
|
||||
# OperationResult JSON (Status, Details, LastIndex). Mirrors the
|
||||
# `cyclone validate-837 <file>` CLI behavior — the wire shape comes
|
||||
# straight through so callers don't have to translate between the two.
|
||||
#
|
||||
# HTTP status codes:
|
||||
# 200 — OperationResult returned (Status may be success / warning /
|
||||
# error; the caller decides whether the file is acceptable).
|
||||
# 400 — uploaded file is empty / undecodable (defense-in-depth, same
|
||||
# shape as /api/parse-837).
|
||||
# 502 — Edifabric upstream 4xx/5xx — caller can surface the body.
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@router.post("/api/admin/validate-837")
|
||||
async def validate_837_endpoint(
|
||||
file: UploadFile = File(...),
|
||||
) -> Any:
|
||||
"""Validate an uploaded 837P file via Edifabric /v2/x12/validate.
|
||||
|
||||
Multipart upload (``file=...``); the file is read into bytes and
|
||||
passed to :func:`cyclone.edifabric.validate_edi`. The API key is
|
||||
resolved server-side from ``cyclone.secrets.get_secret('edifabric.api_key')``
|
||||
so the key never leaves the backend.
|
||||
"""
|
||||
raw = await file.read()
|
||||
if not raw:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Empty file", "detail": "Uploaded file contained no bytes."},
|
||||
)
|
||||
try:
|
||||
result = edifabric.validate_edi(raw)
|
||||
except edifabric.EdifabricError as exc:
|
||||
# status_code=0 is a client-side config problem (missing API
|
||||
# key); 4xx/5xx upstream become 502. Surface the Edifabric body
|
||||
# verbatim so the operator can see what went wrong.
|
||||
http_status = 502 if exc.status_code else 503
|
||||
return JSONResponse(
|
||||
status_code=http_status,
|
||||
content={
|
||||
"error": "Edifabric validation failed",
|
||||
"upstream_status": exc.status_code,
|
||||
"upstream_body": exc.body,
|
||||
},
|
||||
)
|
||||
return JSONResponse(content=result)
|
||||
|
||||
@@ -0,0 +1,515 @@
|
||||
"""``/api/batches*`` — read views + ZIP export over the parsed-batch population.
|
||||
|
||||
Three endpoints, all gated by ``matrix_gate``:
|
||||
|
||||
- ``POST /api/batches/{batch_id}/export-837`` — download a ZIP of
|
||||
regenerated X12 837 files for the requested claim ids. Per-claim
|
||||
payer config + clearhouse identity drive the submitter/receiver
|
||||
blocks; per-claim millisecond offset on the filename keeps every
|
||||
file in the bundle unique (HCPF requires this). Read-only — does
|
||||
NOT mutate Claim state (compare with
|
||||
``/api/inbox/rejected/resubmit?download=true`` which DOES flip
|
||||
``REJECTED → SUBMITTED``).
|
||||
- ``GET /api/batches`` — summary list,
|
||||
newest-first, capped at ``limit``. Each row includes ``claimIds``
|
||||
(837P only, so the Upload page can render a one-click Re-export
|
||||
button per row without a round-trip to ``/api/batches/{id}``).
|
||||
SP30: also returns billing-outcome fields
|
||||
(``acceptedCount`` / ``rejectedCount`` / ``pendingCount`` /
|
||||
``billedTotal`` / ``topRejectionReason`` / ``hasProblem``) so the
|
||||
Dashboard "Recent batches" widget can render one row per batch
|
||||
without an N+1 fetch.
|
||||
- ``GET /api/batches/{batch_id}`` — full batch record
|
||||
(parsed envelope + claims). 404 when unknown.
|
||||
|
||||
Three single-router helpers stay in this file (per spec D4):
|
||||
|
||||
- :func:`_batch_summary_claim_count` — claim count (837P or 835).
|
||||
- :func:`_batch_summary_claim_ids` — per-claim ids (837P only).
|
||||
- :func:`_batch_summary_billing_outcomes` — per-batch GROUP BY state
|
||||
aggregate plus the most-recent rejection reason.
|
||||
|
||||
Inline imports inside handlers (preserved verbatim per spec D5):
|
||||
``zipfile``, ``datetime``, ``ZoneInfo``, ``build_outbound_filename``,
|
||||
``PayerConfigORM``, ``func`` (sqlalchemy).
|
||||
|
||||
SP36 Task 14: this block moved here from ``api.py:1280`` (the 3
|
||||
``/api/batches*`` routes + 3 single-router helpers + the SP30
|
||||
state-bucket tuples and the ``# 277CA STC category`` note).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.api_helpers import (
|
||||
ndjson_stream_list as _ndjson_stream_list,
|
||||
wants_ndjson as _wants_ndjson,
|
||||
)
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.db import Batch, Claim, ClaimState
|
||||
from cyclone.parsers.models import ClaimOutput
|
||||
from cyclone.parsers.serialize_837 import SerializeError as SerializeError837
|
||||
from cyclone.parsers.serialize_837 import serialize_837_for_resubmit
|
||||
from cyclone.store import BatchRecord, store
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
|
||||
@router.post("/api/batches/{batch_id}/export-837")
|
||||
def export_batch_837(request: Request, batch_id: str, body: dict):
|
||||
"""Download a ZIP of regenerated X12 837 files for the requested claim_ids.
|
||||
|
||||
Body shape: ``{"claim_ids": [str, ...]}``.
|
||||
|
||||
Each successfully serialized claim becomes an entry in the ZIP named
|
||||
per the HCPF X12 File Naming Standards:
|
||||
``tp{tpid}-837P-{yyyymmddhhmmssSSS}-1of1.x12`` (with a per-claim
|
||||
millisecond offset so every file in the bundle has a unique name).
|
||||
The ``serialize_837_for_resubmit`` serializer is used so every file
|
||||
gets a unique interchange / group control number — back-to-back
|
||||
exports of the same set must produce different envelopes (required
|
||||
by X12).
|
||||
|
||||
The submitter block (Loop 1000A — NM1*41 + PER) is populated from
|
||||
the clearhouse singleton (dzinesco's identity in the seeded config)
|
||||
and the receiver block (NM1*40) is populated from the per-payer
|
||||
config. Without this wiring, the serializer falls back to
|
||||
``CYCLONE`` / ``RECEIVER`` placeholders and HCPF rejects the file.
|
||||
|
||||
No DB state is mutated by this endpoint — it is read-only. Compare
|
||||
with ``/api/inbox/rejected/resubmit?download=true`` which ALSO flips
|
||||
``ClaimState.REJECTED → SUBMITTED``; the two endpoints are
|
||||
intentionally separate.
|
||||
|
||||
Responses:
|
||||
200 — ``application/zip`` with the .x12 entries. Per-claim failures
|
||||
are surfaced via the ``X-Cyclone-Serialize-Errors`` header
|
||||
(JSON-encoded array of ``{claim_id, reason}``).
|
||||
400 — ``claim_ids`` missing or empty.
|
||||
404 — ``batch_id`` unknown.
|
||||
422 — every claim failed to serialize; body is JSON listing all
|
||||
failures (``{"detail": {"serialize_errors": [...]}}``).
|
||||
"""
|
||||
import zipfile
|
||||
from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from cyclone.edi.filenames import build_outbound_filename
|
||||
|
||||
ids = body.get("claim_ids") or []
|
||||
if not ids:
|
||||
raise HTTPException(400, "claim_ids required")
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
batch = s.get(Batch, batch_id)
|
||||
if batch is None:
|
||||
raise HTTPException(404, f"unknown batch: {batch_id}")
|
||||
|
||||
serialize_errors: list[dict] = []
|
||||
ordered_rows: list[tuple[str, "Claim"]] = []
|
||||
for cid in ids:
|
||||
c = s.get(Claim, cid)
|
||||
if c is None:
|
||||
serialize_errors.append({"claim_id": cid, "reason": "unknown claim_id"})
|
||||
continue
|
||||
ordered_rows.append((cid, c))
|
||||
|
||||
# Pull clearhouse identity (submitter). If unseeded, the serializer
|
||||
# falls back to placeholder defaults — degraded but not a hard error.
|
||||
ch = store.get_clearhouse()
|
||||
submitter_kwargs: dict = {}
|
||||
if ch is not None:
|
||||
submitter_kwargs = {
|
||||
"sender_id": ch.tpid,
|
||||
"submitter_name": ch.submitter_name,
|
||||
"submitter_contact_name": ch.submitter_contact_name,
|
||||
"submitter_contact_email": ch.submitter_contact_email,
|
||||
}
|
||||
# Submitter phone is not in the clearhouse config today, but if
|
||||
# it ever is, wire it here. Email is the canonical contact
|
||||
# channel for HCPF submissions per the SP9 spec.
|
||||
if getattr(ch, "submitter_contact_phone", None):
|
||||
submitter_kwargs["submitter_contact_phone"] = ch.submitter_contact_phone
|
||||
|
||||
# Resolve per-claim payer config so each file's receiver (NM1*40)
|
||||
# and SBR09 are correct. Cache so we don't re-query the same payer.
|
||||
from cyclone.db import PayerConfigORM as _PayerConfigORM
|
||||
_payer_cache: dict[str, dict | None] = {}
|
||||
|
||||
def _resolve_payer_cfg(claim_obj: ClaimOutput) -> dict | None:
|
||||
pid = (claim_obj.payer.id or "").strip() if claim_obj.payer else ""
|
||||
pname = (claim_obj.payer.name or "").strip() if claim_obj.payer else ""
|
||||
cache_key = pid or pname
|
||||
if cache_key in _payer_cache:
|
||||
return _payer_cache[cache_key]
|
||||
cfg: dict | None = None
|
||||
with db.SessionLocal()() as ss:
|
||||
# 1. Exact match on (payer_id, "837P")
|
||||
if pid:
|
||||
row = ss.get(_PayerConfigORM, (pid, "837P"))
|
||||
if row is not None:
|
||||
cfg = dict(row.config_json)
|
||||
# 2. Fallback: any row whose payer_id matches the parsed payer.name
|
||||
# (HCPF files emit "SKCO0" in NM109 but the canonical
|
||||
# payer_id in the DB is "CO_TXIX" — name-matching is the
|
||||
# pragmatic lookup for that case).
|
||||
if cfg is None and pname:
|
||||
row = (
|
||||
ss.query(_PayerConfigORM)
|
||||
.filter(_PayerConfigORM.transaction_type == "837P")
|
||||
.all()
|
||||
)
|
||||
for r in row:
|
||||
cj = dict(r.config_json)
|
||||
if cj.get("submitter_name") and pname.lower() in str(cj).lower():
|
||||
cfg = cj
|
||||
break
|
||||
if (r.payer_id or "").upper() == pname.upper():
|
||||
cfg = cj
|
||||
break
|
||||
# 3. Last resort: first 837P row in the table.
|
||||
if cfg is None:
|
||||
row = (
|
||||
ss.query(_PayerConfigORM)
|
||||
.filter(_PayerConfigORM.transaction_type == "837P")
|
||||
.first()
|
||||
)
|
||||
if row is not None:
|
||||
cfg = dict(row.config_json)
|
||||
_payer_cache[cache_key] = cfg
|
||||
return cfg
|
||||
|
||||
# Build per-claim kwargs (receiver + SBR09) lazily. Receiver
|
||||
# defaults to the parsed payer name/ID if no config row matches.
|
||||
def _serialize_kwargs(claim_obj: ClaimOutput) -> dict:
|
||||
payer_cfg = _resolve_payer_cfg(claim_obj) or {}
|
||||
receiver_id = (
|
||||
payer_cfg.get("receiver_id")
|
||||
or (claim_obj.payer.id if claim_obj.payer else None)
|
||||
or "RECEIVER"
|
||||
)
|
||||
receiver_name = (
|
||||
payer_cfg.get("receiver_name")
|
||||
or (claim_obj.payer.name if claim_obj.payer else None)
|
||||
or receiver_id
|
||||
)
|
||||
sbr09 = payer_cfg.get("sbr09_default") or "MC"
|
||||
return {
|
||||
"receiver_id": receiver_id,
|
||||
"receiver_name": receiver_name,
|
||||
"claim_filing_indicator_code": sbr09,
|
||||
}
|
||||
|
||||
# Base MT timestamp for HCPF filenames. We add a per-claim
|
||||
# millisecond offset so each file in the ZIP has a unique 17-digit
|
||||
# ts (HCPF requires that; the spec also enforces "1of1" for the
|
||||
# sequence element).
|
||||
base_ts = datetime.now(ZoneInfo("America/Denver"))
|
||||
|
||||
def _per_claim_filename(idx: int, cid: str) -> str:
|
||||
if ch is None:
|
||||
# No clearhouse — fall back to a per-claim friendly name.
|
||||
return f"claim-{cid}.x12"
|
||||
# Millisecond offset, with second/minute rollover.
|
||||
offset_ms = (idx - 1) * 1 # 1 ms per claim is enough within an export
|
||||
ts_mt = base_ts.fromtimestamp(
|
||||
base_ts.timestamp() + offset_ms / 1000.0, tz=ZoneInfo("America/Denver")
|
||||
)
|
||||
return build_outbound_filename(ch.tpid, "837P", now_mt=ts_mt)
|
||||
|
||||
buf = io.BytesIO()
|
||||
with zipfile.ZipFile(buf, mode="w", compression=zipfile.ZIP_DEFLATED) as zf:
|
||||
for idx, (cid, c) in enumerate(ordered_rows, start=1):
|
||||
if not c.raw_json:
|
||||
serialize_errors.append({"claim_id": cid, "reason": "no raw_json"})
|
||||
continue
|
||||
try:
|
||||
claim_obj = ClaimOutput.model_validate(c.raw_json)
|
||||
except Exception as exc:
|
||||
serialize_errors.append(
|
||||
{"claim_id": cid, "reason": f"raw_json invalid: {exc}"}
|
||||
)
|
||||
continue
|
||||
try:
|
||||
kwargs = {**submitter_kwargs, **_serialize_kwargs(claim_obj)}
|
||||
text = serialize_837_for_resubmit(
|
||||
claim_obj, interchange_index=idx, **kwargs
|
||||
)
|
||||
except SerializeError837 as exc:
|
||||
serialize_errors.append({"claim_id": cid, "reason": str(exc)})
|
||||
continue
|
||||
zf.writestr(_per_claim_filename(idx, cid), text)
|
||||
|
||||
success_count = len(ids) - len(serialize_errors)
|
||||
if serialize_errors and success_count == 0:
|
||||
# Every claim failed — surface the failure list in the body so the
|
||||
# UI can render a useful error toast (the response is not a ZIP).
|
||||
raise HTTPException(
|
||||
422,
|
||||
detail={"serialize_errors": serialize_errors},
|
||||
)
|
||||
|
||||
buf.seek(0)
|
||||
headers = {
|
||||
"Content-Disposition": (
|
||||
f'attachment; filename="batch-{batch_id}-{success_count}-claims.zip"'
|
||||
),
|
||||
}
|
||||
if serialize_errors:
|
||||
headers["X-Cyclone-Serialize-Errors"] = json.dumps(serialize_errors)
|
||||
return Response(
|
||||
content=buf.getvalue(),
|
||||
media_type="application/zip",
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
def _batch_summary_claim_count(rec: BatchRecord) -> int:
|
||||
"""Return the number of claims on a batch, handling both 837P and 835."""
|
||||
if rec.kind == "837p":
|
||||
return len(rec.result.claims) # type: ignore[attr-defined]
|
||||
if rec.kind == "835":
|
||||
return len(rec.result.claims) # type: ignore[attr-defined]
|
||||
return 0
|
||||
|
||||
|
||||
def _batch_summary_claim_ids(rec: BatchRecord) -> list[str]:
|
||||
"""Return per-claim ids for an 837P batch, or ``[]`` otherwise.
|
||||
|
||||
The Upload page's History tab renders a one-click Re-export ZIP
|
||||
button per row; that button calls
|
||||
``POST /api/batches/{id}/export-837`` with the row's claim ids.
|
||||
Carrying them in the list response avoids an extra round-trip
|
||||
to ``/api/batches/{id}`` for every row. 835 has no re-export
|
||||
endpoint, so the list is empty for those — the UI uses the
|
||||
empty list as the signal to hide the button.
|
||||
"""
|
||||
if rec.kind != "837p":
|
||||
return []
|
||||
return [
|
||||
c.claim_id
|
||||
for c in rec.result.claims # type: ignore[attr-defined]
|
||||
if getattr(c, "claim_id", None)
|
||||
]
|
||||
|
||||
|
||||
# SP30: state buckets the Dashboard widget (and any future "how the
|
||||
# last batch billed" surface) reads at a glance. Keep these in sync
|
||||
# with ClaimState — adding a new state here is a deliberate decision
|
||||
# the operator needs to see, not a coincidence.
|
||||
_BATCH_SUMMARY_ACCEPTED_STATES: tuple[ClaimState, ...] = (
|
||||
ClaimState.PAID,
|
||||
ClaimState.RECEIVED,
|
||||
ClaimState.RECONCILED,
|
||||
ClaimState.PARTIAL,
|
||||
)
|
||||
_BATCH_SUMMARY_REJECTED_STATES: tuple[ClaimState, ...] = (
|
||||
ClaimState.REJECTED,
|
||||
ClaimState.DENIED,
|
||||
ClaimState.REVERSED,
|
||||
)
|
||||
_BATCH_SUMMARY_PENDING_STATES: tuple[ClaimState, ...] = (
|
||||
ClaimState.SUBMITTED,
|
||||
)
|
||||
# 277CA STC category A4/A6/A7 — payer-side rejections that may not
|
||||
# yet have flipped Claim.state (the operator hasn't acknowledged).
|
||||
# The Dashboard widget treats these as problems too, mirroring the
|
||||
# Inbox `rejected + payer_rejected` aggregation.
|
||||
_BATCH_SUMMARY_PAYER_REJECT_CODES: tuple[str, ...] = ("A4", "A6", "A7")
|
||||
|
||||
|
||||
def _batch_summary_billing_outcomes(
|
||||
records: list[BatchRecord],
|
||||
) -> dict[str, dict]:
|
||||
"""Compute per-batch billing outcome for the Dashboard widget.
|
||||
|
||||
Returns ``{batch_id: {accepted, rejected, pending, billed,
|
||||
top_rejection_reason, has_problem}}`` for every batch in
|
||||
``records``. Empty input → empty dict.
|
||||
|
||||
Two SQL queries, both bounded by the supplied batch ids:
|
||||
|
||||
1. One GROUP BY ``(batch_id, state)`` aggregate that produces
|
||||
the accepted/rejected/pending counts and the sum of
|
||||
``charge_amount`` (the billed total). Single pass — no N+1.
|
||||
2. One ordered scan over the rejected + payer-rejected subset
|
||||
to pick the most recent rejection reason (truncated to 60
|
||||
chars). Skipped when the first query found no rejections
|
||||
and no payer-rejects, so the happy path stays at one query.
|
||||
|
||||
835 batches have no Claim rows — the GROUP BY returns no
|
||||
rows for them, so the dict entry for an 835 batch is
|
||||
``{accepted:0, rejected:0, pending:0, billed:0.0,
|
||||
top_rejection_reason:None, has_problem:False}`` (filled by
|
||||
the caller's ``.get(id, defaults)`` pattern).
|
||||
"""
|
||||
if not records:
|
||||
return {}
|
||||
from sqlalchemy import func # local import to keep top-of-file light
|
||||
|
||||
batch_ids = [r.id for r in records]
|
||||
outcome: dict[str, dict] = {
|
||||
bid: {
|
||||
"accepted": 0,
|
||||
"rejected": 0,
|
||||
"pending": 0,
|
||||
"billed": 0.0,
|
||||
"top_rejection_reason": None,
|
||||
"has_problem": False,
|
||||
}
|
||||
for bid in batch_ids
|
||||
}
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
# ---- 1. GROUP BY (batch_id, state) for counts + billed total ----
|
||||
rows = (
|
||||
s.query(
|
||||
Claim.batch_id,
|
||||
Claim.state,
|
||||
func.count(Claim.id),
|
||||
func.coalesce(func.sum(Claim.charge_amount), 0),
|
||||
)
|
||||
.filter(Claim.batch_id.in_(batch_ids))
|
||||
.group_by(Claim.batch_id, Claim.state)
|
||||
.all()
|
||||
)
|
||||
any_rejection_or_payer = False
|
||||
for batch_id, state, count, billed in rows:
|
||||
slot = outcome.get(batch_id)
|
||||
if slot is None:
|
||||
continue # batch has no row in our pre-allocated dict
|
||||
count = int(count or 0)
|
||||
billed_f = float(billed or 0)
|
||||
slot["billed"] += billed_f
|
||||
if state in _BATCH_SUMMARY_ACCEPTED_STATES:
|
||||
slot["accepted"] += count
|
||||
elif state in _BATCH_SUMMARY_REJECTED_STATES:
|
||||
slot["rejected"] += count
|
||||
any_rejection_or_payer = True
|
||||
elif state in _BATCH_SUMMARY_PENDING_STATES:
|
||||
slot["pending"] += count
|
||||
# everything else (DRAFT, etc.) is excluded from the widget.
|
||||
|
||||
# ---- 2. Most-recent rejection reason + payer-reject probe ----
|
||||
# Only run when we know there IS at least one rejection OR a
|
||||
# payer-reject claim somewhere in the batch set; otherwise
|
||||
# the first query alone is enough.
|
||||
if any_rejection_or_payer:
|
||||
rej_rows = (
|
||||
s.query(
|
||||
Claim.batch_id,
|
||||
Claim.rejection_reason,
|
||||
Claim.payer_rejected_status_code,
|
||||
)
|
||||
.filter(
|
||||
Claim.batch_id.in_(batch_ids),
|
||||
Claim.state.in_(_BATCH_SUMMARY_REJECTED_STATES)
|
||||
| Claim.payer_rejected_status_code.in_(
|
||||
_BATCH_SUMMARY_PAYER_REJECT_CODES
|
||||
),
|
||||
)
|
||||
.order_by(Claim.rejected_at.desc().nullslast())
|
||||
.all()
|
||||
)
|
||||
seen_reason: set[str] = set()
|
||||
for batch_id, reason, payer_code in rej_rows:
|
||||
slot = outcome.get(batch_id)
|
||||
if slot is None:
|
||||
continue
|
||||
if payer_code in _BATCH_SUMMARY_PAYER_REJECT_CODES:
|
||||
slot["has_problem"] = True
|
||||
# Capture the first non-null reason for this batch
|
||||
# (rej_rows is ordered newest-first, so the first
|
||||
# non-null wins). Truncate to 60 chars + ellipsis.
|
||||
if (
|
||||
slot["top_rejection_reason"] is None
|
||||
and reason
|
||||
and batch_id not in seen_reason
|
||||
):
|
||||
r = reason.strip()
|
||||
if len(r) > 60:
|
||||
r = r[:60] + "…"
|
||||
slot["top_rejection_reason"] = r
|
||||
seen_reason.add(batch_id)
|
||||
if (
|
||||
slot["rejected"] > 0
|
||||
or payer_code in _BATCH_SUMMARY_PAYER_REJECT_CODES
|
||||
):
|
||||
slot["has_problem"] = True
|
||||
|
||||
return outcome
|
||||
|
||||
|
||||
@router.get("/api/batches")
|
||||
def list_batches(
|
||||
request: Request,
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
) -> Any:
|
||||
"""Summary of all parsed batches, newest first.
|
||||
|
||||
Each item includes ``claimIds`` (837P only) so the History tab
|
||||
on the Upload page can render a one-click re-export button per
|
||||
row without an extra round-trip to ``/api/batches/{id}``. The
|
||||
list is still capped at ``limit`` claims; see the full result
|
||||
via the by-id endpoint when more is needed.
|
||||
|
||||
SP30: also returns billing-outcome fields
|
||||
(``acceptedCount`` / ``rejectedCount`` / ``pendingCount`` /
|
||||
``billedTotal`` / ``topRejectionReason`` / ``hasProblem``) so
|
||||
the Dashboard "Recent batches" widget can render one row per
|
||||
batch without an N+1 fetch. See
|
||||
:func:`_batch_summary_billing_outcomes`.
|
||||
"""
|
||||
records = store.list(limit=limit)
|
||||
outcomes = _batch_summary_billing_outcomes(records)
|
||||
items = [
|
||||
{
|
||||
"id": r.id,
|
||||
"kind": r.kind,
|
||||
"inputFilename": r.input_filename,
|
||||
"parsedAt": r.parsed_at.isoformat().replace("+00:00", "Z"),
|
||||
"claimCount": _batch_summary_claim_count(r),
|
||||
"claimIds": _batch_summary_claim_ids(r),
|
||||
"acceptedCount": outcomes.get(r.id, {}).get("accepted", 0),
|
||||
"rejectedCount": outcomes.get(r.id, {}).get("rejected", 0),
|
||||
"pendingCount": outcomes.get(r.id, {}).get("pending", 0),
|
||||
"billedTotal": round(outcomes.get(r.id, {}).get("billed", 0.0), 2),
|
||||
"topRejectionReason": outcomes.get(r.id, {}).get(
|
||||
"top_rejection_reason"
|
||||
),
|
||||
"hasProblem": outcomes.get(r.id, {}).get("has_problem", False),
|
||||
}
|
||||
for r in records
|
||||
]
|
||||
all_records = store.all()
|
||||
total = len(all_records)
|
||||
returned = len(items)
|
||||
has_more = total > returned
|
||||
if _wants_ndjson(request):
|
||||
return StreamingResponse(
|
||||
_ndjson_stream_list(items, total, returned, has_more),
|
||||
media_type="application/x-ndjson",
|
||||
)
|
||||
return {
|
||||
"items": items,
|
||||
"total": total,
|
||||
"returned": returned,
|
||||
"has_more": has_more,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/api/batches/{batch_id}")
|
||||
def get_batch(batch_id: str) -> Any:
|
||||
rec = store.get(batch_id)
|
||||
if rec is None:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail={"error": "Not found", "detail": f"Batch {batch_id} not found"},
|
||||
)
|
||||
return json.loads(rec.result.model_dump_json())
|
||||
@@ -21,16 +21,17 @@ from __future__ import annotations
|
||||
import logging
|
||||
from typing import Any, AsyncIterator, Literal
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.api_helpers import ndjson_line, tail_events
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.pubsub import EventBus
|
||||
from cyclone.store import store, to_ui_claim_ack
|
||||
|
||||
router = APIRouter()
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -291,7 +292,7 @@ def list_ack_orphans_endpoint(
|
||||
"""List acks with no resolvable Claim row of their own kind.
|
||||
|
||||
Used by the Inbox "Ack orphans" lane for the operator's manual
|
||||
reconciliation flow. Mirrors ``/api/inbox/remit-orphans``.
|
||||
reconciliation flow. Filters by kind: ``999``, ``277ca``, ``ta1``.
|
||||
"""
|
||||
if kind is not None:
|
||||
items = store.find_ack_orphans(kind)
|
||||
|
||||
@@ -0,0 +1,539 @@
|
||||
"""``/api/claims*`` — Claims list / detail / streaming / serialize / line-reconciliation.
|
||||
|
||||
Five endpoints, all gated by ``matrix_gate``:
|
||||
|
||||
- ``GET /api/claims`` — paginated list
|
||||
with filter+sort, plus an NDJSON variant when the caller sends
|
||||
``Accept: application/x-ndjson``. SP27: counts the full filtered
|
||||
population, not a page-limited sample.
|
||||
- ``GET /api/claims/stream`` — NDJSON live-tail
|
||||
on ``claim_written``. Snapshot first (eager
|
||||
``store.iter_claims``), then ``tail_events`` subscribes + emits
|
||||
heartbeats. Registered before ``/api/claims/{claim_id}`` so the
|
||||
literal ``stream`` segment isn't captured as a claim id.
|
||||
- ``GET /api/claims/{claim_id}`` — full drawer
|
||||
context (SP4) with the SP28 ``ack_links`` block pre-attached.
|
||||
404 on missing id — never 500.
|
||||
- ``GET /api/claims/{claim_id}/serialize-837`` — regenerate X12
|
||||
837P from the stored ``raw_json`` payload. 404 unknown claim, 422
|
||||
no-``raw_json`` / unparseable / serializer failure.
|
||||
- ``GET /api/claims/{claim_id}/line-reconciliation`` — per-line 837
|
||||
vs 835 side-by-side with CAS adjustments and a summary block.
|
||||
|
||||
Three single-router helpers stay in this file (per spec D4):
|
||||
|
||||
- :func:`_compact_ack_links_for_claim` — slim form
|
||||
``{ack_id, ack_kind, set_accept_reject_code, …}`` for the drawer
|
||||
Acknowledgments panel.
|
||||
- :func:`_claim_line_dict` — project an 837 service-line
|
||||
dict from ``Claim.raw_json`` to wire shape.
|
||||
- :func:`_svc_to_dict` — project an ORM
|
||||
``ServiceLinePayment`` to wire shape.
|
||||
|
||||
Inline imports inside handlers (preserved verbatim per spec D5):
|
||||
``select`` (sqlalchemy), ``LineReconciliation``/``ServiceLinePayment``/
|
||||
``CasAdjustment`` (cyclone.db), ``json as _json``, ``Decimal``.
|
||||
|
||||
SP36 Task 15: this block moved here from ``api.py:1278`` (the 5
|
||||
``/api/claims*`` routes + 3 single-router helpers).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from decimal import Decimal
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
from fastapi.responses import JSONResponse, Response, StreamingResponse
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.api_helpers import (
|
||||
ndjson_line as _ndjson_line,
|
||||
ndjson_stream_list as _ndjson_stream_list,
|
||||
tail_events as _tail_events,
|
||||
wants_ndjson as _wants_ndjson,
|
||||
)
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.db import Claim
|
||||
from cyclone.parsers.models import ClaimOutput
|
||||
from cyclone.parsers.serialize_837 import SerializeError as SerializeError837
|
||||
from cyclone.parsers.serialize_837 import serialize_837
|
||||
from cyclone.pubsub import EventBus
|
||||
from cyclone.store import store
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
|
||||
@router.get("/api/claims")
|
||||
def list_claims(
|
||||
request: Request,
|
||||
batch_id: str | None = Query(None),
|
||||
status: str | None = Query(None),
|
||||
provider_npi: str | None = Query(None),
|
||||
payer: str | None = Query(None),
|
||||
date_from: str | None = Query(None),
|
||||
date_to: str | None = Query(None),
|
||||
sort: str | None = Query(None),
|
||||
order: str = Query("desc"),
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
offset: int = Query(0, ge=0),
|
||||
) -> Any:
|
||||
common = dict(
|
||||
batch_id=batch_id,
|
||||
status=status,
|
||||
provider_npi=provider_npi,
|
||||
payer=payer,
|
||||
date_from=date_from,
|
||||
date_to=date_to,
|
||||
)
|
||||
items = list(store.iter_claims(
|
||||
sort=sort, order=order, limit=limit, offset=offset, **common,
|
||||
))
|
||||
# SP27 Task 13b: count the full population, not a 100-row sample.
|
||||
# `iter_claims` defaults to limit=100; counting its output silently
|
||||
# capped the reported total at 100 even when the DB held 60k rows.
|
||||
total = store.count_claims(**common)
|
||||
returned = len(items)
|
||||
has_more = total > offset + returned
|
||||
if _wants_ndjson(request):
|
||||
return StreamingResponse(
|
||||
_ndjson_stream_list(items, total, returned, has_more),
|
||||
media_type="application/x-ndjson",
|
||||
)
|
||||
return {
|
||||
"items": items,
|
||||
"total": total,
|
||||
"returned": returned,
|
||||
"has_more": has_more,
|
||||
}
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Live-tail NDJSON streaming endpoints (Phase 3 — SP5)
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
@router.get("/api/claims/stream")
|
||||
async def claims_stream(
|
||||
request: Request,
|
||||
status: str | None = Query(None),
|
||||
provider_npi: str | None = Query(None),
|
||||
payer: str | None = Query(None),
|
||||
date_from: str | None = Query(None),
|
||||
date_to: str | None = Query(None),
|
||||
sort: str | None = Query(None),
|
||||
order: str = Query("desc"),
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
) -> StreamingResponse:
|
||||
"""Stream Claims as NDJSON: snapshot first, then live events.
|
||||
|
||||
Wire format:
|
||||
* ``{"type":"item","data":<claim>}`` per snapshot row, then per
|
||||
new ``claim_written`` event
|
||||
* ``{"type":"snapshot_end","data":{"count":N}}`` after the snapshot
|
||||
* ``{"type":"heartbeat","data":{"ts":<iso>}}`` every
|
||||
``CYCLONE_TAIL_HEARTBEAT_S`` seconds when idle
|
||||
|
||||
Query params mirror :func:`list_claims` so a frontend can swap a
|
||||
one-shot fetch for a tail with no URL surgery.
|
||||
|
||||
NOTE: registered before ``/api/claims/{claim_id}`` so the literal
|
||||
``stream`` path segment doesn't get matched as a claim id.
|
||||
"""
|
||||
bus: EventBus = request.app.state.event_bus
|
||||
|
||||
async def gen() -> AsyncIterator[bytes]:
|
||||
# 1. Snapshot (eager — iter_claims returns a list already).
|
||||
rows = store.iter_claims(
|
||||
status=status, provider_npi=provider_npi, payer=payer,
|
||||
date_from=date_from, date_to=date_to,
|
||||
sort=sort or "-submission_date", order=order, limit=limit,
|
||||
)
|
||||
for row in rows:
|
||||
yield _ndjson_line({"type": "item", "data": row})
|
||||
yield _ndjson_line({"type": "snapshot_end", "data": {"count": len(rows)}})
|
||||
|
||||
# 2. Subscribe + heartbeats.
|
||||
async for chunk in _tail_events(request, bus, ["claim_written"]):
|
||||
yield chunk
|
||||
|
||||
return StreamingResponse(gen(), media_type="application/x-ndjson")
|
||||
|
||||
|
||||
@router.get("/api/claims/{claim_id}")
|
||||
def get_claim_detail_endpoint(claim_id: str) -> dict:
|
||||
"""Return one claim with full drawer context (SP4).
|
||||
|
||||
Body shape is produced by :meth:`CycloneStore.get_claim_detail`:
|
||||
header, state, service lines, diagnoses, parties, validation,
|
||||
raw segments, ``stateHistory`` (most-recent-first, capped at 50),
|
||||
and a populated ``matchedRemittance`` block when paired.
|
||||
|
||||
SP28: response gains ``ack_links: list[dict]`` (compact form:
|
||||
``[{ack_id, ack_kind, set_accept_reject_code, parsed_at}]``)
|
||||
so the ``ClaimDrawer`` Acknowledgments panel can render on
|
||||
initial load. TA1 batch-level rows (``claim_id IS NULL``) are
|
||||
excluded — those don't belong to a specific claim.
|
||||
|
||||
Path param is ``claim_id`` (matches the SP3 ``/api/acks/{ack_id}``
|
||||
convention). Returns 404 — never 500 — on a missing claim so the
|
||||
UI can distinguish "doesn't exist" from a transient fetch error.
|
||||
"""
|
||||
body = store.get_claim_detail(claim_id)
|
||||
if body is None:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail={
|
||||
"error": "Not found",
|
||||
"detail": f"Claim {claim_id} not found",
|
||||
},
|
||||
)
|
||||
# SP28: attach ack_links (compact form for the drawer panel).
|
||||
body["ack_links"] = _compact_ack_links_for_claim(claim_id)
|
||||
return body
|
||||
|
||||
|
||||
def _compact_ack_links_for_claim(claim_id: str) -> list[dict]:
|
||||
"""Return compact ack_links for one claim, newest first.
|
||||
|
||||
TA1 batch-level rows (claim_id IS NULL) are filtered out — those
|
||||
hang off the originating 837 batch, not a specific claim. The
|
||||
shape is the slimmer ``{ack_id, ack_kind,
|
||||
set_accept_reject_code, parsed_at, ak2_index}`` form so the
|
||||
ClaimDrawer can render without an N+1 round-trip per row.
|
||||
"""
|
||||
rows = store.list_acks_for_claim(claim_id)
|
||||
out: list[dict] = []
|
||||
for row in rows:
|
||||
if row.claim_id is None:
|
||||
continue
|
||||
out.append({
|
||||
"id": row.id,
|
||||
"ack_id": row.ack_id,
|
||||
"ack_kind": row.ack_kind,
|
||||
"ak2_index": row.ak2_index,
|
||||
"set_control_number": row.set_control_number,
|
||||
"set_accept_reject_code": row.set_accept_reject_code,
|
||||
"linked_at": (
|
||||
row.linked_at.isoformat().replace("+00:00", "Z")
|
||||
if row.linked_at is not None else ""
|
||||
),
|
||||
"linked_by": row.linked_by,
|
||||
})
|
||||
return out
|
||||
|
||||
|
||||
def _serialize_kwargs_for_claim(claim_obj) -> dict:
|
||||
"""SP40: build the ``serialize_837`` kwargs from the live clearhouse
|
||||
+ per-payer ``PayerConfigORM`` config. Same as the bulk export in
|
||||
``batches.py:_serialize_kwargs`` so single-claim download mirrors
|
||||
production byte-faithfulness.
|
||||
|
||||
Without this, ``serialize_837(claim_obj)`` falls back to
|
||||
placeholder strings (CYCLONE/RECEIVER/CUSTOMER SERVICE/8005550100)
|
||||
which the operator rightly rejected as not production-ready.
|
||||
Returns an empty dict only when the clearhouse / payer config is
|
||||
not seeded — the serializer's own SP40 fallback then fires.
|
||||
"""
|
||||
out: dict = {}
|
||||
try:
|
||||
ch = store.get_clearhouse()
|
||||
except Exception:
|
||||
ch = None
|
||||
if ch is not None:
|
||||
out["sender_id"] = ch.tpid
|
||||
out["submitter_name"] = ch.submitter_name
|
||||
out["submitter_contact_name"] = ch.submitter_contact_name
|
||||
out["submitter_contact_email"] = ch.submitter_contact_email
|
||||
if getattr(ch, "submitter_contact_phone", None):
|
||||
out["submitter_contact_phone"] = ch.submitter_contact_phone
|
||||
|
||||
# Per-payer receiver + SBR-09 from the 837P PayerConfigORM row.
|
||||
pid = (getattr(claim_obj.payer, "id", "") or "").strip()
|
||||
if pid:
|
||||
try:
|
||||
from cyclone.db import PayerConfigORM, Payer as PayerORM
|
||||
with db.SessionLocal()() as ss:
|
||||
pcfg = ss.get(PayerConfigORM, (pid, "837P"))
|
||||
payer_row = ss.get(PayerORM, pid)
|
||||
if pcfg is not None and isinstance(pcfg.config_json, dict):
|
||||
cfg = pcfg.config_json
|
||||
if cfg.get("receiver_name"):
|
||||
out["receiver_name"] = cfg["receiver_name"]
|
||||
if cfg.get("receiver_id"):
|
||||
out["receiver_id"] = cfg["receiver_id"]
|
||||
if cfg.get("sbr09_default"):
|
||||
out["claim_filing_indicator_code"] = cfg["sbr09_default"]
|
||||
if payer_row is not None:
|
||||
if "receiver_name" not in out and payer_row.receiver_name:
|
||||
out["receiver_name"] = payer_row.receiver_name
|
||||
if "receiver_id" not in out and payer_row.receiver_id:
|
||||
out["receiver_id"] = payer_row.receiver_id
|
||||
except Exception:
|
||||
# Fall through; the serializer's own SP40 default
|
||||
# (MC for SBR-09) covers the most common case.
|
||||
pass
|
||||
return out
|
||||
|
||||
|
||||
@router.get("/api/claims/{claim_id}/serialize-837")
|
||||
def serialize_claim_as_837(claim_id: str):
|
||||
"""Return the claim as a regenerated X12 837P file (SP8).
|
||||
|
||||
Loads the ClaimOutput from the persisted ``raw_json`` and runs the
|
||||
outbound serializer. Returns 404 if the claim doesn't exist, 422 if
|
||||
the stored payload has no parseable ClaimOutput (data integrity
|
||||
issue, not a transient failure).
|
||||
|
||||
SP40: threads the clearhouse submitter + CO_TXIX PayerConfig
|
||||
receiver kwargs through to ``serialize_837`` so the regenerated
|
||||
file has real ``NM1*41`` (Dzinesco / TPID ``11525703``),
|
||||
``PER*IC*Tyler Martinez*EM*tyler@dzinesco.com``, ``NM1*40``
|
||||
(HCPF) and ``SBR*P*18*******MC`` segments — not the
|
||||
``CYCLONE/RECEIVER/CUSTOMER SERVICE/8005550100`` placeholders the
|
||||
bare-args call site would emit. Mirrors the regen-script path in
|
||||
``dev/unbilled-july2026/scripts/regen_corrected_files.py``.
|
||||
"""
|
||||
with db.SessionLocal()() as s:
|
||||
row = s.get(Claim, claim_id)
|
||||
if row is None:
|
||||
return JSONResponse(
|
||||
{"error": "Not found", "detail": f"Claim {claim_id} not found"},
|
||||
status_code=404,
|
||||
)
|
||||
if not row.raw_json:
|
||||
return JSONResponse(
|
||||
{
|
||||
"error": "Unprocessable",
|
||||
"detail": f"Claim {claim_id} has no raw_json; cannot serialize",
|
||||
},
|
||||
status_code=422,
|
||||
)
|
||||
try:
|
||||
claim_obj = ClaimOutput.model_validate(row.raw_json)
|
||||
except Exception as exc:
|
||||
return JSONResponse(
|
||||
{
|
||||
"error": "Unprocessable",
|
||||
"detail": f"Claim {claim_id} raw_json is malformed: {exc}",
|
||||
},
|
||||
status_code=422,
|
||||
)
|
||||
|
||||
# SP40: build the same kwargs the bulk export uses, so the
|
||||
# single-claim download mirrors production byte-faithfulness (real
|
||||
# submitter, real contact, real receiver, real SBR-09).
|
||||
serialize_kwargs = _serialize_kwargs_for_claim(claim_obj)
|
||||
|
||||
try:
|
||||
text = serialize_837(claim_obj, **serialize_kwargs)
|
||||
except SerializeError837 as exc:
|
||||
return JSONResponse(
|
||||
{"error": "Unprocessable", "detail": str(exc)},
|
||||
status_code=422,
|
||||
)
|
||||
|
||||
return Response(
|
||||
content=text,
|
||||
media_type="text/x12",
|
||||
headers={
|
||||
"Content-Disposition": f'attachment; filename="claim-{claim_id}.x12"'
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/claims/{claim_id}/line-reconciliation")
|
||||
def get_claim_line_reconciliation(claim_id: str) -> dict:
|
||||
"""Per-line reconciliation view for the ClaimDrawer tab.
|
||||
|
||||
Spec §5.1. Returns the 837 service lines and 835 SVC composites
|
||||
side-by-side, with per-line CAS adjustments and a summary block.
|
||||
|
||||
Architecture note: 837 service lines live in ``Claim.raw_json``
|
||||
(not a separate ORM table), so the 837-side rows are read from the
|
||||
JSON blob; the 835-side rows come from ``ServiceLinePayment`` ORM.
|
||||
``LineReconciliation.claim_service_line_number`` stores the 1-based
|
||||
line number to join them.
|
||||
"""
|
||||
from sqlalchemy import select
|
||||
from cyclone.db import (
|
||||
LineReconciliation, ServiceLinePayment, CasAdjustment,
|
||||
)
|
||||
import json as _json
|
||||
from decimal import Decimal
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
claim = s.get(db.Claim, claim_id)
|
||||
if claim is None:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail={"error": "Not found", "detail": f"Claim {claim_id} not found"},
|
||||
)
|
||||
|
||||
# 837 service lines: from raw_json.
|
||||
raw = claim.raw_json or {}
|
||||
claim_lines_raw = raw.get("service_lines") or []
|
||||
# Normalize to dicts for the response.
|
||||
claim_lines = [_claim_line_dict(d) for d in claim_lines_raw]
|
||||
|
||||
# 835 service payments: ORM rows from the matched remit.
|
||||
remits = list(
|
||||
s.execute(
|
||||
select(db.Remittance).where(db.Remittance.claim_id == claim_id)
|
||||
).scalars().all()
|
||||
)
|
||||
svc_payments: list[dict] = []
|
||||
svc_ids: list[int] = []
|
||||
if remits:
|
||||
svc_rows = list(
|
||||
s.execute(
|
||||
select(ServiceLinePayment).where(
|
||||
ServiceLinePayment.remittance_id.in_([r.id for r in remits])
|
||||
).order_by(ServiceLinePayment.line_number)
|
||||
).scalars().all()
|
||||
)
|
||||
for svc in svc_rows:
|
||||
d = _svc_to_dict(svc)
|
||||
svc_payments.append(d)
|
||||
svc_ids.append(svc.id)
|
||||
|
||||
# LineReconciliation rows.
|
||||
lrs = list(
|
||||
s.execute(
|
||||
select(LineReconciliation).where(LineReconciliation.claim_id == claim_id)
|
||||
).scalars().all()
|
||||
)
|
||||
# Index by claim_service_line_number and service_line_payment_id.
|
||||
lr_by_claim_num: dict[int, LineReconciliation] = {
|
||||
lr.claim_service_line_number: lr for lr in lrs if lr.claim_service_line_number is not None
|
||||
}
|
||||
lr_by_svc: dict[int, LineReconciliation] = {
|
||||
lr.service_line_payment_id: lr for lr in lrs if lr.service_line_payment_id is not None
|
||||
}
|
||||
|
||||
# CAS rows grouped by svc id.
|
||||
cas_by_svc: dict[int, list[CasAdjustment]] = {}
|
||||
if svc_ids:
|
||||
cas_rows = list(
|
||||
s.execute(
|
||||
select(CasAdjustment).where(CasAdjustment.service_line_payment_id.in_(svc_ids))
|
||||
).scalars().all()
|
||||
)
|
||||
for c in cas_rows:
|
||||
cas_by_svc.setdefault(c.service_line_payment_id, []).append(c)
|
||||
|
||||
# Build output lines array, preserving 837 order then 835-only.
|
||||
svc_by_id: dict[int, dict] = {d["id"]: d for d in svc_payments}
|
||||
lines_out: list[dict] = []
|
||||
billed_total = Decimal("0")
|
||||
paid_total = Decimal("0")
|
||||
adjustment_total = Decimal("0")
|
||||
matched_count = 0
|
||||
used_svc_ids: set[int] = set()
|
||||
|
||||
for cl in claim_lines:
|
||||
billed_total += Decimal(str(cl["charge"]))
|
||||
lr = lr_by_claim_num.get(cl["line_number"])
|
||||
if lr is None:
|
||||
lines_out.append({
|
||||
"claim_service_line": cl,
|
||||
"service_line_payment": None,
|
||||
"status": "unmatched_837_only",
|
||||
"adjustments": [],
|
||||
})
|
||||
continue
|
||||
svc_id = lr.service_line_payment_id
|
||||
svc = svc_by_id.get(svc_id) if svc_id else None
|
||||
if svc_id is not None:
|
||||
used_svc_ids.add(svc_id)
|
||||
cas_list = cas_by_svc.get(svc_id, []) if svc_id is not None else []
|
||||
cas_total = sum((Decimal(str(c.amount)) for c in cas_list), Decimal("0"))
|
||||
if svc:
|
||||
paid_total += Decimal(str(svc["payment"]))
|
||||
adjustment_total += cas_total
|
||||
if lr.status == "matched":
|
||||
matched_count += 1
|
||||
lines_out.append({
|
||||
"claim_service_line": cl,
|
||||
"service_line_payment": svc,
|
||||
"status": lr.status,
|
||||
"adjustments": [
|
||||
{"group_code": c.group_code, "reason_code": c.reason_code,
|
||||
"amount": str(Decimal(str(c.amount)))}
|
||||
for c in cas_list
|
||||
],
|
||||
})
|
||||
|
||||
# 835-only lines (no claim match).
|
||||
for lr in lrs:
|
||||
if lr.claim_service_line_number is not None:
|
||||
continue
|
||||
svc_id = lr.service_line_payment_id
|
||||
if svc_id is None:
|
||||
continue
|
||||
if svc_id in used_svc_ids:
|
||||
continue
|
||||
svc = svc_by_id.get(svc_id)
|
||||
cas_list = cas_by_svc.get(svc_id, [])
|
||||
cas_total = sum((Decimal(str(c.amount)) for c in cas_list), Decimal("0"))
|
||||
if svc:
|
||||
paid_total += Decimal(str(svc["payment"]))
|
||||
adjustment_total += cas_total
|
||||
lines_out.append({
|
||||
"claim_service_line": None,
|
||||
"service_line_payment": svc,
|
||||
"status": lr.status,
|
||||
"adjustments": [
|
||||
{"group_code": c.group_code, "reason_code": c.reason_code,
|
||||
"amount": str(Decimal(str(c.amount)))}
|
||||
for c in cas_list
|
||||
],
|
||||
})
|
||||
|
||||
return {
|
||||
"claim_id": claim_id,
|
||||
"summary": {
|
||||
"billed_total": str(billed_total),
|
||||
"paid_total": str(paid_total),
|
||||
"adjustment_total": str(adjustment_total),
|
||||
"matched_lines": matched_count,
|
||||
"total_lines": len(claim_lines),
|
||||
},
|
||||
"lines": lines_out,
|
||||
}
|
||||
|
||||
|
||||
def _claim_line_dict(d: dict) -> dict:
|
||||
"""Project an 837 service-line dict from ``Claim.raw_json`` to wire shape."""
|
||||
from decimal import Decimal
|
||||
proc = d.get("procedure") or {}
|
||||
charge = d.get("charge")
|
||||
units = d.get("units")
|
||||
return {
|
||||
"line_number": d.get("line_number"),
|
||||
"procedure_qualifier": proc.get("qualifier", "HC"),
|
||||
"procedure_code": proc.get("code", ""),
|
||||
"modifiers": proc.get("modifiers") or [],
|
||||
"charge": str(Decimal(str(charge))) if charge is not None else "0",
|
||||
"units": str(Decimal(str(units))) if units is not None else None,
|
||||
"unit_type": d.get("unit_type"),
|
||||
"service_date": d.get("service_date"),
|
||||
}
|
||||
|
||||
|
||||
def _svc_to_dict(svc) -> dict:
|
||||
"""Project an ORM ``ServiceLinePayment`` to wire shape."""
|
||||
import json as _json
|
||||
from decimal import Decimal
|
||||
return {
|
||||
"id": svc.id,
|
||||
"line_number": svc.line_number,
|
||||
"procedure_qualifier": svc.procedure_qualifier,
|
||||
"procedure_code": svc.procedure_code,
|
||||
"modifiers": _json.loads(svc.modifiers_json or "[]"),
|
||||
"charge": str(Decimal(str(svc.charge))),
|
||||
"payment": str(Decimal(str(svc.payment))),
|
||||
"units": str(Decimal(str(svc.units))) if svc.units is not None else None,
|
||||
"unit_type": svc.unit_type,
|
||||
"service_date": svc.service_date.isoformat() if svc.service_date else None,
|
||||
}
|
||||
@@ -0,0 +1,310 @@
|
||||
"""``/api/clearhouse*`` — singleton clearhouse config + SFTP submission.
|
||||
|
||||
Three endpoints, all gated by ``matrix_gate``:
|
||||
|
||||
- ``GET /api/clearhouse`` — read the singleton clearhouse row
|
||||
(dzinesco's identity, SFTP block, filename block). 404 when
|
||||
unseeded.
|
||||
- ``PATCH /api/clearhouse`` — full-row replacement of the
|
||||
singleton (SP25). Strict-validates ``sftp_block`` first (Pydantic
|
||||
v2 default mode coerces strings-to-bools and would hide a real
|
||||
operator mistake), then validates the whole body in loose mode.
|
||||
Hot-reloads the running scheduler via
|
||||
``scheduler.reconfigure_scheduler`` so the next tick picks up the
|
||||
new ``SftpBlock`` without a process restart.
|
||||
- ``POST /api/clearhouse/submit`` — submit a batch of claims to
|
||||
the clearhouse. Stub: serializes via the SP7 serializer, builds
|
||||
an HCPF-compliant outbound filename, copies the result to the
|
||||
staging path. Per-claim audit events stamped with
|
||||
``actor="clearhouse-submit"``.
|
||||
|
||||
Three single-router helpers stay in this file (per spec D4):
|
||||
|
||||
- :func:`_load_claim_row` — load a ``Claim`` row by id.
|
||||
- :func:`_serialize_claim_for_submit` — re-serialize a claim to X12
|
||||
with optional per-call kwargs (submitter, receiver, SBR09, etc).
|
||||
- :func:`_serialize_claim_from_raw` — best-effort serializer that
|
||||
re-parses stored ``x12_text`` and re-emits.
|
||||
|
||||
SP36 Task 11: this block moved here from ``api.py:2484`` (the 3
|
||||
``/api/clearhouse*`` routes + 3 single-router helpers).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.api_routers._shared import _actor_user_id
|
||||
from cyclone.audit_log import AuditEvent, append_event
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.db import Claim
|
||||
from cyclone.store import store
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
|
||||
@router.get("/api/clearhouse")
|
||||
def get_clearhouse():
|
||||
"""Return the singleton clearhouse config (dzinesco's identity, SFTP block, filename block)."""
|
||||
ch = store.get_clearhouse()
|
||||
if ch is None:
|
||||
raise HTTPException(status_code=404, detail="clearhouse not seeded")
|
||||
return json.loads(ch.model_dump_json())
|
||||
|
||||
|
||||
@router.patch("/api/clearhouse")
|
||||
async def patch_clearhouse(body: dict) -> Any:
|
||||
"""Replace the singleton clearhouse row (SP25).
|
||||
|
||||
The full ``Clearhouse`` model is required — we don't accept partial
|
||||
updates because the operator-facing use case is "I'm switching the
|
||||
loop to real MFT" or "I'm pointing at a different MFT server",
|
||||
not "I'm tweaking one field at a time." Validation errors are
|
||||
returned as 422 (Pydantic default).
|
||||
|
||||
After a successful write, the running scheduler is hot-reloaded
|
||||
via ``scheduler.reconfigure_scheduler()`` so the next tick uses
|
||||
the new SftpBlock without a process restart.
|
||||
"""
|
||||
from cyclone import scheduler as _scheduler_mod
|
||||
from cyclone.providers import Clearhouse as _Clearhouse, SftpBlock as _SftpBlock
|
||||
|
||||
# Strict-validate the sftp_block sub-dict FIRST. Pydantic v2's
|
||||
# default mode coerces strings to bools (e.g. ``"stub": "yes"``
|
||||
# silently becomes True), which would hide a real operator
|
||||
# mistake. The Clearhouse model itself stays in loose mode so
|
||||
# ISO-string ``updated_at`` (the JSON round-trip shape) keeps
|
||||
# parsing.
|
||||
raw_sb = body.get("sftp_block", {})
|
||||
try:
|
||||
_SftpBlock.model_validate(raw_sb, strict=True)
|
||||
except Exception as exc:
|
||||
raise HTTPException(
|
||||
status_code=422, detail=f"invalid sftp_block: {exc}",
|
||||
) from exc
|
||||
|
||||
# Now validate the full body in loose mode.
|
||||
try:
|
||||
parsed = _Clearhouse.model_validate(body)
|
||||
except Exception as exc:
|
||||
raise HTTPException(
|
||||
status_code=422, detail=str(exc),
|
||||
) from exc
|
||||
|
||||
# SP25: when sftp_block.stub=false, the block must carry an auth
|
||||
# account name and a non-empty host. The Pydantic model catches
|
||||
# some of these; this catches the "empty password_keychain_account"
|
||||
# case (which Pydantic allows because it's a free-form dict).
|
||||
sb = parsed.sftp_block
|
||||
if not sb.stub:
|
||||
if not sb.host:
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail="sftp_block.host is required when stub=false",
|
||||
)
|
||||
auth = sb.auth or {}
|
||||
if not auth.get("password_keychain_account") and not auth.get("key_file"):
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail=(
|
||||
"sftp_block.auth must contain either "
|
||||
"'password_keychain_account' or 'key_file' when stub=false"
|
||||
),
|
||||
)
|
||||
|
||||
updated = store.update_clearhouse(parsed)
|
||||
await _scheduler_mod.reconfigure_scheduler(
|
||||
updated.sftp_block,
|
||||
sftp_block_name=updated.name or "default",
|
||||
)
|
||||
return json.loads(updated.model_dump_json())
|
||||
|
||||
|
||||
@router.post("/api/clearhouse/submit")
|
||||
def submit_to_clearhouse(request: Request, body: dict):
|
||||
"""Submit a batch of claims to the clearhouse (SFTP). SP9: stub.
|
||||
|
||||
Body: ``{"claim_ids": [...], "payer_id": "CO_TXIX"}``
|
||||
|
||||
Stub behavior: serializes each claim via the SP7 serializer, builds
|
||||
an HCPF-compliant outbound filename, and copies the result to
|
||||
``{staging_dir}/{outbound_path}/{filename}`` instead of opening a
|
||||
real SFTP connection. Returns a receipt per claim.
|
||||
"""
|
||||
from cyclone.clearhouse import make_client
|
||||
from cyclone.edi.filenames import build_outbound_filename
|
||||
|
||||
claim_ids = body.get("claim_ids", [])
|
||||
payer_id = body.get("payer_id")
|
||||
if not claim_ids:
|
||||
raise HTTPException(status_code=400, detail="claim_ids required")
|
||||
if not payer_id:
|
||||
raise HTTPException(status_code=400, detail="payer_id required")
|
||||
|
||||
ch = store.get_clearhouse()
|
||||
if ch is None:
|
||||
raise HTTPException(status_code=500, detail="clearhouse not seeded")
|
||||
|
||||
# Submitter (Loop 1000A) comes from the clearhouse config. The
|
||||
# receiver (NM1*40) and SBR09 come from the per-payer config and
|
||||
# are resolved per-claim below. Without this wiring, the
|
||||
# serializer would emit "CYCLONE" / "RECEIVER" placeholders and
|
||||
# the file would be rejected by HCPF.
|
||||
submitter_kwargs = {
|
||||
"sender_id": ch.tpid,
|
||||
"submitter_name": ch.submitter_name,
|
||||
"submitter_contact_name": ch.submitter_contact_name,
|
||||
"submitter_contact_email": ch.submitter_contact_email,
|
||||
}
|
||||
if getattr(ch, "submitter_contact_phone", None):
|
||||
submitter_kwargs["submitter_contact_phone"] = ch.submitter_contact_phone
|
||||
|
||||
# Build a payer_id → PayerConfig837 map once so we can look up the
|
||||
# receiver + SBR09 default for each claim.
|
||||
from cyclone.db import PayerConfigORM as _PayerConfigORM
|
||||
|
||||
def _resolve_payer_cfg(claim_obj) -> dict | None:
|
||||
pid = (claim_obj.payer.id or "").strip() if claim_obj.payer else ""
|
||||
pname = (claim_obj.payer.name or "").strip() if claim_obj.payer else ""
|
||||
with db.SessionLocal()() as ss:
|
||||
if pid:
|
||||
row = ss.get(_PayerConfigORM, (pid, "837P"))
|
||||
if row is not None:
|
||||
return dict(row.config_json)
|
||||
if pname:
|
||||
rows = (
|
||||
ss.query(_PayerConfigORM)
|
||||
.filter(_PayerConfigORM.transaction_type == "837P")
|
||||
.all()
|
||||
)
|
||||
for r in rows:
|
||||
cj = dict(r.config_json)
|
||||
if (r.payer_id or "").upper() == pname.upper():
|
||||
return cj
|
||||
if pname.lower() in str(cj).lower():
|
||||
return cj
|
||||
row = (
|
||||
ss.query(_PayerConfigORM)
|
||||
.filter(_PayerConfigORM.transaction_type == "837P")
|
||||
.first()
|
||||
)
|
||||
return dict(row.config_json) if row else None
|
||||
|
||||
client = make_client(ch.sftp_block)
|
||||
results = []
|
||||
for cid in claim_ids:
|
||||
try:
|
||||
x12_text = _serialize_claim_for_submit(cid)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
results.append({"claim_id": cid, "ok": False, "error": str(exc)})
|
||||
continue
|
||||
# Re-resolve the claim so we can look up its payer config. We
|
||||
# re-parse the stored x12_text to get a ClaimOutput (same path
|
||||
# the serializer uses).
|
||||
try:
|
||||
from cyclone.parsers.parse_837 import parse as _parse837
|
||||
claim_row_obj = _load_claim_row(cid)
|
||||
if claim_row_obj is None or not (claim_row_obj.raw_json or {}).get("x12_text"):
|
||||
raise RuntimeError("no stored x12_text for claim")
|
||||
parsed = _parse837(claim_row_obj.raw_json["x12_text"])
|
||||
claim_obj = parsed.claims[0] if parsed.claims else None
|
||||
except Exception:
|
||||
claim_obj = None
|
||||
if claim_obj is not None:
|
||||
payer_cfg = _resolve_payer_cfg(claim_obj) or {}
|
||||
receiver_id = payer_cfg.get("receiver_id") or (claim_obj.payer.id if claim_obj.payer else None) or "RECEIVER"
|
||||
receiver_name = payer_cfg.get("receiver_name") or (claim_obj.payer.name if claim_obj.payer else None) or receiver_id
|
||||
sbr09 = payer_cfg.get("sbr09_default") or "MC"
|
||||
# Re-serialize with the proper envelope values.
|
||||
try:
|
||||
x12_text = _serialize_claim_for_submit(
|
||||
cid,
|
||||
**{**submitter_kwargs, "receiver_id": receiver_id,
|
||||
"receiver_name": receiver_name,
|
||||
"claim_filing_indicator_code": sbr09},
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
results.append({"claim_id": cid, "ok": False, "error": str(exc)})
|
||||
continue
|
||||
filename = build_outbound_filename(ch.tpid, "837P")
|
||||
remote = f"{ch.sftp_block.paths['outbound']}/{filename}"
|
||||
staging_path = client.write_file(remote, x12_text.encode("utf-8"))
|
||||
results.append({
|
||||
"claim_id": cid,
|
||||
"ok": True,
|
||||
"filename": filename,
|
||||
"staging_path": str(staging_path),
|
||||
"remote_path": remote,
|
||||
})
|
||||
# SP11: audit trail for each successful clearhouse submission.
|
||||
with db.SessionLocal()() as audit_s:
|
||||
append_event(audit_s, AuditEvent(
|
||||
event_type="clearhouse.submitted",
|
||||
entity_type="claim",
|
||||
entity_id=cid,
|
||||
payload={
|
||||
"filename": filename,
|
||||
"remote_path": remote,
|
||||
"tpid": ch.tpid,
|
||||
"stub": ch.sftp_block.stub,
|
||||
},
|
||||
actor="clearhouse-submit",
|
||||
user_id=_actor_user_id(request),
|
||||
))
|
||||
audit_s.commit()
|
||||
return {"ok": True, "submitted": results, "stub": ch.sftp_block.stub}
|
||||
|
||||
|
||||
def _load_claim_row(claim_id: str):
|
||||
"""Helper: load a Claim row by id (or return None)."""
|
||||
with db.SessionLocal()() as s:
|
||||
return s.get(Claim, claim_id)
|
||||
|
||||
|
||||
def _serialize_claim_for_submit(claim_id: str, **kwargs) -> str:
|
||||
"""Serialize a claim to X12 for SFTP submission. Lazy import of the
|
||||
serializer to avoid pulling FastAPI machinery at module import time.
|
||||
|
||||
Optional ``**kwargs`` are forwarded to the serializer — used to
|
||||
pass through clearhouse submitter info and per-payer receiver info
|
||||
so the regenerated file matches what the HCPF MFT expects.
|
||||
"""
|
||||
from cyclone.parsers.serialize_837 import serialize_837
|
||||
from cyclone import db
|
||||
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
|
||||
from cyclone.parsers.models import ClaimOutput, Envelope, Subscriber, Payer, BillingProvider
|
||||
from cyclone.parsers.parse_837 import parse
|
||||
raw = row.raw_json or {}
|
||||
# Reconstruct minimal ClaimOutput from raw_json; this is best-effort.
|
||||
return _serialize_claim_from_raw(row, raw, **kwargs)
|
||||
|
||||
|
||||
def _serialize_claim_from_raw(claim_row, raw: dict, **kwargs) -> 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. ``**kwargs`` are forwarded to the serializer
|
||||
so callers can pass through submitter / receiver / SBR09 values
|
||||
from the clearhouse and per-payer configs.
|
||||
"""
|
||||
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], **kwargs)
|
||||
# Fallback: raise so the caller sees an error.
|
||||
raise RuntimeError(
|
||||
f"claim {claim_row.id!r} cannot be re-serialized: no stored x12_text"
|
||||
)
|
||||
@@ -0,0 +1,54 @@
|
||||
"""``/api/config/payers`` and ``/api/config/payers/{payer_id}/configs`` — payer-config read views.
|
||||
|
||||
Both endpoints are read-only configuration surfaces used by the UI's
|
||||
"Edit payers" page:
|
||||
|
||||
- ``GET /api/config/payers?is_active=...`` lists all configured
|
||||
payers (PayerConfig records) — the set of payers the operator has
|
||||
registered, regardless of whether they have inbound config blocks.
|
||||
- ``GET /api/config/payers/{payer_id}/configs`` returns the full
|
||||
list of ``(transaction_type, config_json)`` blocks for a given
|
||||
payer. Each block has a ``source``: ``"yaml"`` for the on-disk
|
||||
``config/payers.yaml`` default, ``"db"`` for any runtime override
|
||||
recorded via ``/api/admin/reload-config``.
|
||||
|
||||
These are configuration surfaces, not claim-processing surfaces.
|
||||
They live here (under ``/api/config/``) rather than under
|
||||
``/api/payers/`` because the latter is the drill-down rollup
|
||||
(see ``api_routers/payers.py``).
|
||||
|
||||
SP36 Task 7: this block moved here from ``api.py:3167`` (after
|
||||
the SP21 provider-detail helper, before the Auth routers divider).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
|
||||
from cyclone import store
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
|
||||
@router.get("/api/config/payers")
|
||||
def list_configured_payers(is_active: bool | None = Query(default=True)):
|
||||
return [json.loads(p.model_dump_json()) for p in store.list_payers(is_active=is_active)]
|
||||
|
||||
|
||||
@router.get("/api/config/payers/{payer_id}/configs")
|
||||
def list_payer_configs(payer_id: str):
|
||||
"""List all (transaction_type, config_json) blocks for a payer."""
|
||||
from cyclone import payers as payer_loader
|
||||
configs = [
|
||||
{"transaction_type": tx, "config_json": block, "source": "yaml"}
|
||||
for (pid, tx), block in payer_loader.all_configs().items()
|
||||
if pid == payer_id
|
||||
]
|
||||
# Also check the DB for runtime-overridden configs
|
||||
for tx in ("837P", "835", "277CA", "999", "TA1"):
|
||||
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
|
||||
@@ -0,0 +1,39 @@
|
||||
"""``/api/dashboard/kpis`` — server-aggregated Dashboard tiles.
|
||||
|
||||
Backs the Dashboard's "Claims / Billed / Received / Pending AR /
|
||||
Denial rate" tiles + the monthly sparkline series + the
|
||||
top-providers and top-denials lists.
|
||||
|
||||
Why this exists instead of ``GET /api/claims?limit=N``:
|
||||
The Dashboard's KPIs are aggregates over *every* claim — billed,
|
||||
received, denial rate, pending count, monthly billed/received. With
|
||||
60k+ claims in production, paginating ``/api/claims`` and reducing
|
||||
client-side silently produces wrong numbers (denial rate sampled,
|
||||
billed summed from the first 100 rows). This endpoint does the
|
||||
aggregation server-side in a single read so the Dashboard's numbers
|
||||
are always correct regardless of dataset size.
|
||||
|
||||
SP36 Task 4: this single endpoint moved here from ``api.py:2732``.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.store import dashboard_kpis
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
|
||||
@router.get("/api/dashboard/kpis")
|
||||
def get_dashboard_kpis(
|
||||
months: int = Query(6, ge=1, le=24),
|
||||
top_n_providers: int = Query(4, ge=0, le=50),
|
||||
top_n_denials: int = Query(5, ge=0, le=50),
|
||||
) -> dict:
|
||||
"""Server-aggregated Dashboard KPIs over the whole claim population."""
|
||||
return dashboard_kpis(
|
||||
months=months,
|
||||
top_n_providers=top_n_providers,
|
||||
top_n_denials=top_n_denials,
|
||||
)
|
||||
@@ -0,0 +1,223 @@
|
||||
"""``/api/eligibility/request`` and ``/api/eligibility/parse-271`` — API-only eligibility pair.
|
||||
|
||||
Builds a 270 inquiry from a small JSON body and parses a 271 response.
|
||||
Nothing is persisted to the DB — these are operator-driven, ephemeral
|
||||
operations per SP3 (P4 T23–T24). The 270 serializer pulls X12 from a
|
||||
``ParseResult270`` Pydantic; the 271 parser builds the same structure
|
||||
in reverse from the wire format.
|
||||
|
||||
Why these are not ``GET /api/eligibility/...``: the 270 build is
|
||||
operator-initiated (pay-portal paste-back), so the inbound surface is
|
||||
a JSON ``POST``. The 271 inbound is a multipart file upload — same
|
||||
shape as ``/api/parse-999`` — so the file can be the actual 271 text
|
||||
saved from the payer portal.
|
||||
|
||||
SP36 Task 5: this block moved here from ``api.py:2832`` (``270 / 271
|
||||
eligibility`` divider).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from datetime import date as _date
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.parsers.exceptions import CycloneParseError
|
||||
from cyclone.parsers.models import BatchSummary, Envelope
|
||||
from cyclone.parsers.models_270 import (
|
||||
EligibilityBenefitInquiry,
|
||||
InformationReceiver270,
|
||||
InformationSource270,
|
||||
ParseResult270,
|
||||
Subscriber270,
|
||||
)
|
||||
from cyclone.parsers.parse_271 import parse as parse_271_text
|
||||
from cyclone.parsers.serialize_270 import serialize_270
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
|
||||
def _validate_eligibility_request(body: dict) -> tuple[ParseResult270, str]:
|
||||
"""Build a :class:`ParseResult270` from a request body dict.
|
||||
|
||||
The body shape is the minimum surface needed to build a valid 270
|
||||
inquiry (per spec section 3.4 — operator-driven, ephemeral):
|
||||
|
||||
::
|
||||
|
||||
{
|
||||
"subscriber": {first_name, last_name, member_id, dob},
|
||||
"provider": {npi, name},
|
||||
"payer": {id, name},
|
||||
"service_type_code": "1"
|
||||
}
|
||||
|
||||
Returns ``(ParseResult270, service_type_code)``. Raises
|
||||
:class:`HTTPException` (400) when the body is missing required
|
||||
fields.
|
||||
"""
|
||||
subscriber_in = body.get("subscriber") or {}
|
||||
provider_in = body.get("provider") or {}
|
||||
payer_in = body.get("payer") or {}
|
||||
service_type_code = (body.get("service_type_code") or "").strip()
|
||||
|
||||
# Required-field checks. We surface a single 400 with the first
|
||||
# missing field name to match the rest of the API's error contract.
|
||||
if not service_type_code:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail={"error": "Bad request", "detail": "service_type_code is required"},
|
||||
)
|
||||
if not subscriber_in.get("member_id"):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail={"error": "Bad request", "detail": "subscriber.member_id is required"},
|
||||
)
|
||||
if not provider_in.get("npi"):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail={"error": "Bad request", "detail": "provider.npi is required"},
|
||||
)
|
||||
if not payer_in.get("name"):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail={"error": "Bad request", "detail": "payer.name is required"},
|
||||
)
|
||||
|
||||
# Build the Pydantic models. The serializer handles all envelope
|
||||
# generation (sender_id/receiver_id/control_number/transaction_date
|
||||
# are filled in by the serializer with sensible defaults).
|
||||
subscriber_dob_raw = subscriber_in.get("dob")
|
||||
subscriber_dob: _date | None = None
|
||||
if subscriber_dob_raw:
|
||||
try:
|
||||
subscriber_dob = _date.fromisoformat(subscriber_dob_raw)
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail={
|
||||
"error": "Bad request",
|
||||
"detail": f"subscriber.dob must be YYYY-MM-DD: {exc}",
|
||||
},
|
||||
) from exc
|
||||
|
||||
result = ParseResult270(
|
||||
envelope=Envelope(
|
||||
sender_id="SUBMITTERID",
|
||||
receiver_id=str(payer_in.get("id") or "RECEIVERID"),
|
||||
control_number="000000001",
|
||||
transaction_date=_date.today(),
|
||||
implementation_guide="005010X279A1",
|
||||
),
|
||||
information_source=InformationSource270(
|
||||
name=str(payer_in["name"]),
|
||||
id=str(payer_in.get("id") or "") or None,
|
||||
),
|
||||
information_receiver=InformationReceiver270(
|
||||
name=str(provider_in.get("name") or ""),
|
||||
npi=str(provider_in["npi"]),
|
||||
),
|
||||
subscriber=Subscriber270(
|
||||
member_id=str(subscriber_in["member_id"]),
|
||||
first_name=str(subscriber_in.get("first_name") or "") or None,
|
||||
last_name=str(subscriber_in.get("last_name") or "") or None,
|
||||
dob=subscriber_dob,
|
||||
),
|
||||
inquiries=[EligibilityBenefitInquiry(service_type_code=service_type_code)],
|
||||
summary=BatchSummary(
|
||||
input_file="eligibility_request",
|
||||
control_number="000000001",
|
||||
transaction_date=_date.today(),
|
||||
total_claims=1,
|
||||
passed=1,
|
||||
failed=0,
|
||||
),
|
||||
)
|
||||
return result, service_type_code
|
||||
|
||||
|
||||
@router.post("/api/eligibility/request")
|
||||
def post_eligibility_request(body: dict) -> Any:
|
||||
"""Build a 270 eligibility inquiry from a small JSON body.
|
||||
|
||||
Returns ``{"raw_270_text": <X12>, "parsed": <ParseResult270>}``
|
||||
so the operator can either download the raw text (paste into a
|
||||
payer portal) or render the parsed fields directly. Per spec
|
||||
section 3.4, nothing is persisted to the DB.
|
||||
"""
|
||||
try:
|
||||
result, _ = _validate_eligibility_request(body)
|
||||
except HTTPException:
|
||||
raise
|
||||
except (KeyError, TypeError, ValueError) as exc:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail={"error": "Bad request", "detail": f"Malformed body: {exc}"},
|
||||
) from exc
|
||||
|
||||
raw_270_text = serialize_270(result)
|
||||
return {
|
||||
"raw_270_text": raw_270_text,
|
||||
"parsed": json.loads(result.model_dump_json()),
|
||||
}
|
||||
|
||||
|
||||
@router.post("/api/eligibility/parse-271")
|
||||
async def post_eligibility_parse_271(
|
||||
file: UploadFile = File(...),
|
||||
) -> Any:
|
||||
"""Parse a 271 eligibility response and return the structured summary.
|
||||
|
||||
Accepts the raw 271 text as a file upload (multipart/form-data),
|
||||
mirrors the ``/api/parse-999`` contract. Per spec section 3.4 the
|
||||
result is NOT persisted — the operator re-pastes the 271 each
|
||||
time they need a fresh read.
|
||||
|
||||
The response body is a JSON object with three top-level keys:
|
||||
``coverage_benefits``, ``subscriber``, and ``summary``. 400 is
|
||||
returned on empty / undecodable / malformed EDI; 200 on success.
|
||||
"""
|
||||
raw = await file.read()
|
||||
if not raw:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Empty file", "detail": "Uploaded file contained no bytes."},
|
||||
)
|
||||
try:
|
||||
text = raw.decode("utf-8")
|
||||
except UnicodeDecodeError as exc:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Encoding error", "detail": str(exc)},
|
||||
)
|
||||
|
||||
try:
|
||||
result = parse_271_text(text, input_file=file.filename or "")
|
||||
except CycloneParseError as exc:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Parse error", "detail": str(exc)},
|
||||
)
|
||||
except Exception as exc: # pragma: no cover - safety net
|
||||
log.exception("Unexpected parser failure on 271")
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"error": "Internal server error", "detail": str(exc)},
|
||||
)
|
||||
|
||||
return {
|
||||
"coverage_benefits": [
|
||||
json.loads(cb.model_dump_json()) for cb in result.coverage_benefits
|
||||
],
|
||||
"subscriber": json.loads(result.subscriber.model_dump_json()),
|
||||
"summary": json.loads(result.summary.model_dump_json()),
|
||||
"envelope": json.loads(result.envelope.model_dump_json()),
|
||||
"information_source": json.loads(result.information_source.model_dump_json()),
|
||||
"information_receiver": json.loads(result.information_receiver.model_dump_json()),
|
||||
}
|
||||
@@ -0,0 +1,342 @@
|
||||
"""``/api/inbox*`` — operator-facing Inbox surface (SP6 + SP14).
|
||||
|
||||
Six endpoints, all gated by ``matrix_gate``:
|
||||
|
||||
- ``GET /api/inbox/lanes`` — all lanes in one
|
||||
call (``compute_lanes`` from :mod:`cyclone.inbox_lanes`).
|
||||
- ``POST /api/inbox/candidates/{remit_id}/match`` — manually link a
|
||||
remit to a claim; surfaces 409 with the current state when the
|
||||
claim is already matched.
|
||||
- ``POST /api/inbox/candidates/dismiss`` — add candidate
|
||||
pairs to the session-scoped dismissed set (mutates
|
||||
``request.app.state.dismissed_pairs``).
|
||||
- ``POST /api/inbox/payer-rejected/acknowledge`` — SP14: mark
|
||||
Payer-Rejected claims as acknowledged. Idempotent; returns the
|
||||
count actually transitioned vs. already-acked / not-found /
|
||||
not-rejected.
|
||||
- ``POST /api/inbox/rejected/resubmit`` — bulk move
|
||||
REJECTED claims back to SUBMITTED. With
|
||||
``?download=true`` returns a ZIP of regenerated 837 files
|
||||
(``serialize_837_for_resubmit`` with per-claim ``interchange_index``
|
||||
for unique control numbers). Conflicts are omitted from the ZIP
|
||||
and surfaced via the ``X-Cyclone-Serialize-Errors`` header.
|
||||
- ``GET /api/inbox/export.csv`` — stream a CSV for
|
||||
a single lane (rejected / candidates / unmatched / done_today).
|
||||
|
||||
All endpoints use ``request.app.state`` rather than the module-level
|
||||
``app`` global so they're robust against ``importlib.reload`` of
|
||||
the api module — the reload rebinds ``app`` to a new instance, but
|
||||
``request.app`` always points at the instance actually serving the
|
||||
current request.
|
||||
|
||||
SP36 Task 13: this block moved here from ``api.py:1280`` (the 6
|
||||
``/api/inbox*`` routes, with the SP14 comment block preserved
|
||||
verbatim).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import io
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.db import Claim, ClaimState, Remittance
|
||||
from cyclone.parsers.models import ClaimOutput
|
||||
from cyclone.parsers.serialize_837 import SerializeError as SerializeError837
|
||||
from cyclone.parsers.serialize_837 import serialize_837_for_resubmit
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
|
||||
@router.get("/api/inbox/lanes")
|
||||
def inbox_lanes(request: Request):
|
||||
"""Return all Inbox lanes in one call.
|
||||
|
||||
Uses ``request.app.state`` rather than the module-level ``app``
|
||||
global so the endpoint is robust against ``importlib.reload`` of
|
||||
this module (some tests do this to mutate the CORS allow-list).
|
||||
After a reload, the module-level ``app`` rebinds to a new
|
||||
FastAPI instance; ``request.app`` always points at the instance
|
||||
that is actually serving the current request, so per-request
|
||||
state stays consistent with the test's TestClient target.
|
||||
"""
|
||||
dismissed_pairs = getattr(request.app.state, "dismissed_pairs", set())
|
||||
with db.SessionLocal()() as session:
|
||||
from cyclone.inbox_lanes import compute_lanes
|
||||
lanes = compute_lanes(session, dismissed_pairs=dismissed_pairs)
|
||||
return {
|
||||
"rejected": lanes.rejected,
|
||||
# SP10: payer-rejected lane (277CA STC A4/A6/A7). Distinct from
|
||||
# the 999 envelope rejection in ``rejected`` above.
|
||||
"payer_rejected": lanes.payer_rejected,
|
||||
"candidates": lanes.candidates,
|
||||
"unmatched": lanes.unmatched,
|
||||
"done_today": lanes.done_today,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/api/inbox/candidates/{remit_id}/match")
|
||||
def inbox_match_candidate(remit_id: str, body: dict):
|
||||
"""Manually link a remit to a claim."""
|
||||
claim_id = body.get("claim_id")
|
||||
if not claim_id:
|
||||
raise HTTPException(400, "claim_id required")
|
||||
with db.SessionLocal()() as s:
|
||||
claim = s.get(Claim, claim_id)
|
||||
remit = s.get(Remittance, remit_id)
|
||||
if claim is None or remit is None:
|
||||
raise HTTPException(404, "claim or remit not found")
|
||||
if claim.matched_remittance_id and claim.matched_remittance_id != remit_id:
|
||||
raise HTTPException(
|
||||
409,
|
||||
detail={
|
||||
"error": "claim_already_matched",
|
||||
"current_state": (
|
||||
claim.state.value if hasattr(claim.state, "value")
|
||||
else str(claim.state)
|
||||
),
|
||||
"matched_remittance_id": claim.matched_remittance_id,
|
||||
},
|
||||
)
|
||||
claim.matched_remittance_id = remit_id
|
||||
remit.claim_id = claim_id
|
||||
s.commit()
|
||||
return {"ok": True, "claim_id": claim_id, "remit_id": remit_id}
|
||||
|
||||
|
||||
@router.post("/api/inbox/candidates/dismiss")
|
||||
def inbox_dismiss_candidates(body: dict, request: Request):
|
||||
"""Add candidate pairs to the session-scoped dismissed set.
|
||||
|
||||
Uses ``request.app.state`` rather than the module-level ``app``
|
||||
global so the endpoint is robust against ``importlib.reload`` of
|
||||
this module (some tests do this to mutate the CORS allow-list).
|
||||
After a reload, the module-level ``app`` rebinds to a new
|
||||
FastAPI instance; ``request.app`` always points at the instance
|
||||
that is actually serving the current request, so the test's
|
||||
TestClient target is the one whose state we mutate.
|
||||
"""
|
||||
pairs = body.get("pairs") or []
|
||||
if not hasattr(request.app.state, "dismissed_pairs"):
|
||||
request.app.state.dismissed_pairs = set()
|
||||
for p in pairs:
|
||||
cid = p.get("claim_id")
|
||||
rid = p.get("remit_id")
|
||||
if cid and rid:
|
||||
request.app.state.dismissed_pairs.add(frozenset({cid, rid}))
|
||||
return {"ok": True, "dismissed_count": len(pairs)}
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# SP14: Payer-Rejected acknowledge
|
||||
#
|
||||
# Operator hits "Acknowledge" on the Payer-Rejected Inbox lane to clear
|
||||
# the claim from the working surface. We don't delete the rejection
|
||||
# (the original payer_rejected_* fields stay for SP11 audit), we just
|
||||
# set payer_rejected_acknowledged_at so the lane query filters it out.
|
||||
#
|
||||
# Idempotent: re-acknowledging an already-acknowledged claim is a noop
|
||||
# (the timestamp is not bumped). Returns the count actually transitioned
|
||||
# so the UI can show "3 of 5 were already acknowledged".
|
||||
# --------------------------------------------------------------------------- #
|
||||
@router.post("/api/inbox/payer-rejected/acknowledge")
|
||||
def inbox_acknowledge_payer_rejected(body: dict):
|
||||
"""Mark Payer-Rejected claims as acknowledged by the operator."""
|
||||
claim_ids = body.get("claim_ids") or []
|
||||
actor = body.get("actor") or "operator"
|
||||
if not isinstance(claim_ids, list) or not claim_ids:
|
||||
raise HTTPException(400, "claim_ids must be a non-empty list")
|
||||
if not all(isinstance(c, str) for c in claim_ids):
|
||||
raise HTTPException(400, "claim_ids must be a list of strings")
|
||||
|
||||
with db.SessionLocal()() as session:
|
||||
from cyclone.db import Claim
|
||||
now = datetime.now(timezone.utc)
|
||||
transitioned = 0
|
||||
already_acked = 0
|
||||
not_found = 0
|
||||
not_rejected = 0
|
||||
for cid in claim_ids:
|
||||
claim = session.get(Claim, cid)
|
||||
if claim is None:
|
||||
not_found += 1
|
||||
continue
|
||||
if claim.payer_rejected_at is None:
|
||||
not_rejected += 1
|
||||
continue
|
||||
if claim.payer_rejected_acknowledged_at is not None:
|
||||
already_acked += 1
|
||||
continue
|
||||
claim.payer_rejected_acknowledged_at = now
|
||||
claim.payer_rejected_acknowledged_actor = actor
|
||||
transitioned += 1
|
||||
# SP11: audit event for the acknowledge action.
|
||||
try:
|
||||
from cyclone.audit_log import append_event, AuditEvent
|
||||
append_event(session, AuditEvent(
|
||||
event_type="claim.payer_rejected_acknowledged",
|
||||
entity_type="claim",
|
||||
entity_id=claim.id,
|
||||
actor=actor,
|
||||
payload={
|
||||
"payer_rejected_status_code": claim.payer_rejected_status_code,
|
||||
"payer_rejected_by_277ca_id": claim.payer_rejected_by_277ca_id,
|
||||
},
|
||||
))
|
||||
except Exception: # noqa: BLE001
|
||||
# Audit append is best-effort; don't block the operator's
|
||||
# acknowledge action on an audit-log failure.
|
||||
pass
|
||||
if transitioned:
|
||||
session.commit()
|
||||
|
||||
return {
|
||||
"ok": True,
|
||||
"transitioned": transitioned,
|
||||
"already_acked": already_acked,
|
||||
"not_found": not_found,
|
||||
"not_rejected": not_rejected,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/api/inbox/rejected/resubmit")
|
||||
def inbox_resubmit_rejected(
|
||||
request: Request,
|
||||
body: dict,
|
||||
download: bool = Query(False, description="When true, return a ZIP of regenerated 837 files for the resubmitted claims (instead of JSON)."),
|
||||
):
|
||||
"""Bulk move REJECTED claims back to SUBMITTED.
|
||||
|
||||
With ``?download=true``, the response is a ``application/zip`` archive
|
||||
containing one ``claim-{id}.x12`` per successfully resubmitted claim
|
||||
(regenerated via ``serialize_837_for_resubmit`` so each file gets a
|
||||
unique interchange/group control number). Conflicts are omitted from
|
||||
the ZIP — they remain visible to the caller via the JSON shape of the
|
||||
non-download path. Empty resubmit + download → 200 with an empty zip
|
||||
so the UI can still hand the user a downloadable artifact.
|
||||
"""
|
||||
ids = body.get("claim_ids") or []
|
||||
if not ids:
|
||||
raise HTTPException(400, "claim_ids required")
|
||||
accepted: list[str] = []
|
||||
conflicts: list[dict] = []
|
||||
# Track which claims are about to be resubmitted (and their index in
|
||||
# the bundle) so the download path can serialize them with unique
|
||||
# control numbers — back-to-back resubmits in the same file would
|
||||
# otherwise all share ISA13/GS06 = "000000001".
|
||||
accepted_with_rows: list[tuple[str, "Claim"]] = []
|
||||
with db.SessionLocal()() as s:
|
||||
for cid in ids:
|
||||
c = s.get(Claim, cid)
|
||||
if c is None:
|
||||
continue
|
||||
if c.state != ClaimState.REJECTED:
|
||||
conflicts.append({
|
||||
"claim_id": cid,
|
||||
"current_state": (
|
||||
c.state.value if hasattr(c.state, "value")
|
||||
else str(c.state)
|
||||
),
|
||||
})
|
||||
continue
|
||||
c.state = ClaimState.SUBMITTED
|
||||
c.state_changed_at = datetime.now(timezone.utc)
|
||||
c.rejection_reason = None
|
||||
c.rejected_at = None
|
||||
c.resubmit_count = (c.resubmit_count or 0) + 1
|
||||
accepted.append(cid)
|
||||
accepted_with_rows.append((cid, c))
|
||||
s.commit()
|
||||
|
||||
if not download:
|
||||
return {"ok": True, "resubmitted": accepted, "conflicts": conflicts}
|
||||
|
||||
# Build a ZIP of regenerated 837s for the accepted claims. Conflicts
|
||||
# and missing ids are deliberately excluded — the user already saw
|
||||
# them in the JSON response on prior actions; the download is the
|
||||
# "give me the files I asked for" payload.
|
||||
import zipfile
|
||||
buf = io.BytesIO()
|
||||
serialize_errors: list[dict] = []
|
||||
with zipfile.ZipFile(buf, mode="w", compression=zipfile.ZIP_DEFLATED) as zf:
|
||||
for idx, (cid, c) in enumerate(accepted_with_rows, start=1):
|
||||
if not c.raw_json:
|
||||
serialize_errors.append({"claim_id": cid, "reason": "no raw_json"})
|
||||
continue
|
||||
try:
|
||||
claim_obj = ClaimOutput.model_validate(c.raw_json)
|
||||
except Exception as exc:
|
||||
serialize_errors.append({"claim_id": cid, "reason": f"raw_json invalid: {exc}"})
|
||||
continue
|
||||
try:
|
||||
text = serialize_837_for_resubmit(claim_obj, interchange_index=idx)
|
||||
except SerializeError837 as exc:
|
||||
serialize_errors.append({"claim_id": cid, "reason": str(exc)})
|
||||
continue
|
||||
zf.writestr(f"claim-{cid}.x12", text)
|
||||
buf.seek(0)
|
||||
headers = {
|
||||
"Content-Disposition": (
|
||||
f'attachment; filename="resubmit-{len(accepted)}-claims.zip"'
|
||||
),
|
||||
}
|
||||
# Surface per-claim serialization failures as a custom response header
|
||||
# so the UI can show "10 resubmitted, 2 couldn't be regenerated" without
|
||||
# parsing the binary. The header value is JSON-encoded; the UI is
|
||||
# expected to JSON.parse it after a fetch with response.ok.
|
||||
if serialize_errors:
|
||||
headers["X-Cyclone-Serialize-Errors"] = json.dumps(serialize_errors)
|
||||
return Response(
|
||||
content=buf.getvalue(),
|
||||
media_type="application/zip",
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/inbox/export.csv")
|
||||
def inbox_export_csv(lane: str, request: Request):
|
||||
"""Stream a CSV for a single lane.
|
||||
|
||||
Uses ``request.app.state`` rather than the module-level ``app``
|
||||
global so the endpoint is robust against ``importlib.reload`` of
|
||||
this module (see ``inbox_dismiss_candidates`` for context).
|
||||
"""
|
||||
if lane not in {"rejected", "candidates", "unmatched", "done_today"}:
|
||||
raise HTTPException(400, f"unknown lane: {lane}")
|
||||
dismissed_pairs = getattr(request.app.state, "dismissed_pairs", set())
|
||||
with db.SessionLocal()() as session:
|
||||
from cyclone.inbox_lanes import compute_lanes
|
||||
lanes = compute_lanes(session, dismissed_pairs=dismissed_pairs)
|
||||
rows = getattr(lanes, lane)
|
||||
|
||||
buf = io.StringIO()
|
||||
writer = csv.writer(buf)
|
||||
writer.writerow([
|
||||
"id", "kind", "patient_control_number", "charge_amount",
|
||||
"payer_id", "provider_npi", "state", "rejection_reason",
|
||||
"service_date", "score",
|
||||
])
|
||||
for r in rows:
|
||||
writer.writerow([
|
||||
r.get("id") or r.get("payer_claim_control_number"),
|
||||
r.get("kind"),
|
||||
r.get("patient_control_number"),
|
||||
r.get("charge_amount"),
|
||||
r.get("payer_id"),
|
||||
r.get("provider_npi") or r.get("rendering_provider_npi"),
|
||||
r.get("state"),
|
||||
r.get("rejection_reason"),
|
||||
r.get("service_date_from") or r.get("service_date"),
|
||||
r.get("score"),
|
||||
])
|
||||
buf.seek(0)
|
||||
return StreamingResponse(
|
||||
iter([buf.getvalue()]),
|
||||
media_type="text/csv",
|
||||
headers={"Content-Disposition": f'attachment; filename="inbox-{lane}.csv"'},
|
||||
)
|
||||
@@ -0,0 +1,831 @@
|
||||
"""Parse endpoints — accept X12 uploads and ingest them.
|
||||
|
||||
Five routes:
|
||||
|
||||
* ``POST /api/parse-837`` — 837P professional claim ingest (the
|
||||
primary upload path)
|
||||
* ``POST /api/parse-835`` — 835 ERA remittance ingest
|
||||
* ``POST /api/parse-999`` — 999 ACK ingest + auto-link claims
|
||||
* ``POST /api/parse-ta1`` — TA1 envelope ACK ingest + envelope-link batches
|
||||
* ``POST /api/parse-277ca`` — 277CA Claim Acknowledgment ingest
|
||||
|
||||
The 7 cross-router helpers these endpoints need (and the two
|
||||
PAYER_FACTORIES dicts they consume) live in
|
||||
:mod:`cyclone.api_routers._shared`.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import uuid
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, File, Query, Request, UploadFile
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.api_helpers import (
|
||||
client_wants_json as _client_wants_json,
|
||||
drop_raw_segments_837 as _drop_raw_segments,
|
||||
drop_raw_segments_835 as _drop_raw_segments_835,
|
||||
has_claim_validation_errors as _has_claim_validation_errors,
|
||||
has_835_validation_errors as _has_835_validation_errors,
|
||||
ndjson_stream_837 as _ndjson_stream,
|
||||
ndjson_stream_835 as _ndjson_stream_835,
|
||||
strict_rewrite_837 as _strict_rewrite,
|
||||
strict_rewrite_835 as _strict_rewrite_835,
|
||||
)
|
||||
from cyclone.api_routers._shared import (
|
||||
_actor_user_id,
|
||||
_build_and_persist_ack,
|
||||
_reconciliation_summary_for_batch,
|
||||
_resolve_payer,
|
||||
_resolve_payer_835,
|
||||
_serialize_ta1,
|
||||
_ta1_synthetic_source_batch_id,
|
||||
_transaction_set_id_from_segments,
|
||||
)
|
||||
from cyclone.audit_log import AuditEvent, append_event
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.claim_acks import (
|
||||
apply_277ca_acks as _apply_277ca_acks,
|
||||
apply_999_acceptances as _apply_999_acceptances,
|
||||
apply_ta1_envelope_link as _apply_ta1_envelope_link,
|
||||
)
|
||||
from cyclone.db import Batch, Claim
|
||||
from cyclone.handlers._ack_id import (
|
||||
ack_count_summary as _ack_count_summary,
|
||||
ack_synthetic_source_batch_id as _ack_synthetic_source_batch_id,
|
||||
two77ca_synthetic_source_batch_id as _277ca_synthetic_source_batch_id,
|
||||
)
|
||||
from cyclone.inbox_state import apply_999_rejections
|
||||
from cyclone.inbox_state_277ca import apply_277ca_rejections
|
||||
from cyclone.parsers.exceptions import CycloneParseError
|
||||
from cyclone.parsers.parse_277ca import parse_277ca_text
|
||||
from cyclone.parsers.parse_837 import parse
|
||||
from cyclone.parsers.parse_835 import parse as parse_835
|
||||
from cyclone.parsers.parse_999 import parse_999_text
|
||||
from cyclone.parsers.parse_ta1 import parse_ta1_text
|
||||
from cyclone.parsers.segments import tokenize as _tokenize_segments
|
||||
from cyclone.parsers.serialize_999 import serialize_999
|
||||
from cyclone.parsers.validator_835 import validate as validate_835
|
||||
from cyclone.store import BatchRecord, store, utcnow
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
|
||||
@router.post("/api/parse-837")
|
||||
async def parse_837(
|
||||
request: Request,
|
||||
file: UploadFile = File(...),
|
||||
payer: str = Query("co_medicaid"),
|
||||
include_raw_segments: bool = Query(True),
|
||||
strict: bool = Query(False),
|
||||
ack: bool = Query(False),
|
||||
) -> Any:
|
||||
# SP35: defense-in-depth input guards. Layer A (UI auto-detect) lives
|
||||
# in src/pages/Upload.tsx; the server-side checks below are the
|
||||
# authoritative fix because they protect every caller of the API
|
||||
# (Upload page, CLI ingestion, any future bulk-import tool). Without
|
||||
# these, an 835 file dropped on the Upload page while the dropdown
|
||||
# still says "837p" produces a BatchRecord with claims=[] and a bogus
|
||||
# row on the History tab. The fix is two checks run BEFORE we persist
|
||||
# anything:
|
||||
#
|
||||
# 1. Envelope check — ST01 must be "837" or "837P". Anything else
|
||||
# (an 835, a 999, a 270, garbage that happens to have an ISA)
|
||||
# → 400 with error="Mismatched file kind", expected="837p",
|
||||
# detected_st=<whatever was there>.
|
||||
# 2. Empty-claims check — even with the right envelope, if the
|
||||
# parser produced zero CLM segments (truncated file, header-only
|
||||
# test fixture) → 400 with error="No claims parsed". A real
|
||||
# production 837 batch with zero claims is never valid.
|
||||
raw = await file.read()
|
||||
if not raw:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Empty file", "detail": "Uploaded file contained no bytes."},
|
||||
)
|
||||
try:
|
||||
text = raw.decode("utf-8")
|
||||
except UnicodeDecodeError as exc:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Encoding error", "detail": str(exc)},
|
||||
)
|
||||
|
||||
config = _resolve_payer(payer)
|
||||
|
||||
# SP35 guard 1: envelope check. Tokenize first so we can return a
|
||||
# precise 400 (vs. relying on the parser's "no ISA envelope" error
|
||||
# which is correct but doesn't say "you sent an 835 to the 837
|
||||
# endpoint"). If tokenization itself fails we fall through to the
|
||||
# parser, which raises CycloneParseError → 400 "Parse error" path.
|
||||
try:
|
||||
_segments = _tokenize_segments(text)
|
||||
detected_st = _transaction_set_id_from_segments(_segments) or ""
|
||||
except CycloneParseError:
|
||||
detected_st = ""
|
||||
|
||||
if detected_st and not detected_st.upper().startswith("837"):
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={
|
||||
"error": "Mismatched file kind",
|
||||
"expected": "837p",
|
||||
"detected_st": detected_st,
|
||||
"detail": (
|
||||
f"File declares ST*{detected_st}* but this endpoint "
|
||||
f"expects ST*837*. Pick the matching endpoint on the "
|
||||
f"Upload page (or let auto-detect choose for you)."
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
try:
|
||||
result = parse(text, config, input_file=file.filename or "")
|
||||
except CycloneParseError as exc:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Parse error", "detail": str(exc)},
|
||||
)
|
||||
except Exception as exc: # pragma: no cover - safety net
|
||||
log.exception("Unexpected parser failure")
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"error": "Internal server error", "detail": str(exc)},
|
||||
)
|
||||
|
||||
# SP35 guard 2: empty-claims check. With the envelope validated, the
|
||||
# only way to land here is a header-only file (real, but useless)
|
||||
# or a file whose CLM loops the parser couldn't extract. Either way
|
||||
# we refuse to persist — a BatchRecord with claims=[] is what the
|
||||
# original bug produced and is never what the operator wanted.
|
||||
if not result.claims:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={
|
||||
"error": "No claims parsed",
|
||||
"detail": (
|
||||
"The file passed the envelope check but contained no "
|
||||
"CLM segments. Refusing to persist an empty batch."
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
if strict:
|
||||
result = _strict_rewrite(result)
|
||||
if not include_raw_segments:
|
||||
result = _drop_raw_segments(result)
|
||||
|
||||
if _has_claim_validation_errors(result):
|
||||
# Per spec: 422 when claims failed validation.
|
||||
# Body still includes the full ParseResult so the client can show errors.
|
||||
# Validation-failed parses are NOT persisted (the data is suspect);
|
||||
# only parses that survive validation end up in the store.
|
||||
return JSONResponse(
|
||||
status_code=422,
|
||||
content=json.loads(result.model_dump_json()),
|
||||
)
|
||||
|
||||
# Persist the cleaned-up result so the cleaned result is what clients
|
||||
# retrieve via /api/batches/{id}.
|
||||
rec = BatchRecord(
|
||||
id=uuid.uuid4().hex,
|
||||
kind="837p",
|
||||
input_filename=file.filename or "upload.txt",
|
||||
parsed_at=utcnow(),
|
||||
result=result,
|
||||
)
|
||||
try:
|
||||
store.add(rec, event_bus=request.app.state.event_bus)
|
||||
except IntegrityError as exc:
|
||||
# ``(batch_id, patient_control_number)`` is UNIQUE — fires when a
|
||||
# single batch file contains the same CLM01 control number twice,
|
||||
# or when the same claim id has already been ingested in a prior
|
||||
# batch. Surface as 409 with the batch id so the caller can look
|
||||
# it up; do NOT 500 (a 500 without CORS headers is misreported by
|
||||
# browsers as a CORS error and hides the real cause).
|
||||
log.warning("Duplicate claim while persisting batch %s: %s", rec.id, exc)
|
||||
return JSONResponse(
|
||||
status_code=409,
|
||||
content={
|
||||
"error": "Duplicate claim",
|
||||
"detail": (
|
||||
"This file (or one previously ingested with the same "
|
||||
"claim control number) collides with an existing "
|
||||
"record. Inspect the file for duplicate CLM01 "
|
||||
"control numbers, or remove the existing batch "
|
||||
"before retrying."
|
||||
),
|
||||
"batch_id": rec.id,
|
||||
},
|
||||
)
|
||||
|
||||
if _client_wants_json(request):
|
||||
body = json.loads(result.model_dump_json())
|
||||
if ack:
|
||||
ack_body = _build_and_persist_ack(rec.id)
|
||||
if ack_body is not None:
|
||||
body["ack"] = ack_body
|
||||
# Surface the server-side batch id so the frontend can call
|
||||
# /api/batches/{id}/export-837 (and any other batch-scoped
|
||||
# endpoint) without a separate listBatches round-trip.
|
||||
body["batch_id"] = rec.id
|
||||
return JSONResponse(content=body)
|
||||
|
||||
# Default: NDJSON stream. Pass the server-side batch id so the
|
||||
# streaming client (the React Upload page) can call batch-scoped
|
||||
# endpoints like /api/batches/{id}/export-837 without a separate
|
||||
# GET /api/batches round-trip.
|
||||
return StreamingResponse(
|
||||
_ndjson_stream(result, batch_id=rec.id),
|
||||
media_type="application/x-ndjson",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/api/parse-835")
|
||||
async def parse_835_endpoint(
|
||||
request: Request,
|
||||
file: UploadFile = File(...),
|
||||
payer: str = Query("co_medicaid_835"),
|
||||
include_raw_segments: bool = Query(True),
|
||||
strict: bool = Query(False),
|
||||
) -> Any:
|
||||
raw = await file.read()
|
||||
if not raw:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Empty file", "detail": "Uploaded file contained no bytes."},
|
||||
)
|
||||
try:
|
||||
text = raw.decode("utf-8")
|
||||
except UnicodeDecodeError as exc:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Encoding error", "detail": str(exc)},
|
||||
)
|
||||
|
||||
config = _resolve_payer_835(payer)
|
||||
|
||||
# SP35 guard 1: envelope check. Mirrors the parse-837 path: tokenize,
|
||||
# read ST01, reject anything that doesn't start with "835". Same
|
||||
# defense-in-depth rationale — the UI auto-detect (src/pages/Upload.tsx)
|
||||
# is layer A, but server-side guards protect every API caller.
|
||||
try:
|
||||
_segments_835 = _tokenize_segments(text)
|
||||
detected_st_835 = _transaction_set_id_from_segments(_segments_835) or ""
|
||||
except CycloneParseError:
|
||||
detected_st_835 = ""
|
||||
|
||||
if detected_st_835 and not detected_st_835.upper().startswith("835"):
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={
|
||||
"error": "Mismatched file kind",
|
||||
"expected": "835",
|
||||
"detected_st": detected_st_835,
|
||||
"detail": (
|
||||
f"File declares ST*{detected_st_835}* but this endpoint "
|
||||
f"expects ST*835*. Pick the matching endpoint on the "
|
||||
f"Upload page (or let auto-detect choose for you)."
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
try:
|
||||
result = parse_835(text, config, input_file=file.filename or "")
|
||||
except CycloneParseError as exc:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Parse error", "detail": str(exc)},
|
||||
)
|
||||
except Exception as exc: # pragma: no cover - safety net
|
||||
log.exception("Unexpected parser failure")
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"error": "Internal server error", "detail": str(exc)},
|
||||
)
|
||||
|
||||
# SP35 guard 2: empty-claims check. Same as parse-837: a BatchRecord
|
||||
# with claims=[] is never a valid production 835 batch and we refuse
|
||||
# to persist it.
|
||||
if not result.claims:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={
|
||||
"error": "No claims parsed",
|
||||
"detail": (
|
||||
"The file passed the envelope check but contained no "
|
||||
"CLP segments. Refusing to persist an empty batch."
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
# Always run the validator; attach the report so the JSON path can
|
||||
# surface it and the NDJSON path can fold the counts into the summary.
|
||||
# 835 validation is batch-level, so pass/fail applies uniformly to every
|
||||
# claim payment in the batch (passed=N or 0, failed=0 or N).
|
||||
report = validate_835(result, config)
|
||||
n = len(result.claims)
|
||||
claim_ids = [c.payer_claim_control_number for c in result.claims]
|
||||
if report.passed:
|
||||
passed, failed, failed_claim_ids = n, 0, []
|
||||
else:
|
||||
passed, failed, failed_claim_ids = 0, n, claim_ids
|
||||
result = result.model_copy(update={
|
||||
"validation": report,
|
||||
"summary": result.summary.model_copy(update={
|
||||
"passed": passed,
|
||||
"failed": failed,
|
||||
"failed_claim_ids": failed_claim_ids,
|
||||
}),
|
||||
})
|
||||
|
||||
if strict:
|
||||
result = _strict_rewrite_835(result)
|
||||
if not include_raw_segments:
|
||||
result = _drop_raw_segments_835(result)
|
||||
|
||||
if _has_835_validation_errors(result):
|
||||
return JSONResponse(
|
||||
status_code=422,
|
||||
content=json.loads(result.model_dump_json()),
|
||||
)
|
||||
|
||||
# Persist the cleaned-up result so the cleaned result is what clients
|
||||
# retrieve via /api/batches/{id}.
|
||||
rec = BatchRecord(
|
||||
id=uuid.uuid4().hex,
|
||||
kind="835",
|
||||
input_filename=file.filename or "upload.txt",
|
||||
parsed_at=utcnow(),
|
||||
result=result,
|
||||
)
|
||||
try:
|
||||
store.add(rec, event_bus=request.app.state.event_bus)
|
||||
except IntegrityError as exc:
|
||||
log.warning("Duplicate remittance while persisting batch %s: %s", rec.id, exc)
|
||||
return JSONResponse(
|
||||
status_code=409,
|
||||
content={
|
||||
"error": "Duplicate remittance",
|
||||
"detail": (
|
||||
"This 835 file (or one previously ingested with the "
|
||||
"same payer claim control number) collides with an "
|
||||
"existing record. Remove the existing remittance "
|
||||
"before retrying."
|
||||
),
|
||||
"batch_id": rec.id,
|
||||
},
|
||||
)
|
||||
|
||||
if _client_wants_json(request):
|
||||
body = json.loads(result.model_dump_json())
|
||||
body["reconciliation"] = _reconciliation_summary_for_batch(rec.id)
|
||||
return JSONResponse(content=body)
|
||||
|
||||
# Default: NDJSON stream. Pass the server-side batch id so the
|
||||
# streaming client can call batch-scoped endpoints without a
|
||||
# separate GET /api/batches round-trip (see /api/parse-837 for the
|
||||
# parallel change).
|
||||
return StreamingResponse(
|
||||
_ndjson_stream_835(result, batch_id=rec.id),
|
||||
media_type="application/x-ndjson",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/api/parse-999")
|
||||
async def parse_999_endpoint(
|
||||
request: Request,
|
||||
file: UploadFile = File(...),
|
||||
) -> Any:
|
||||
"""Parse a 999 ACK file, persist a row, and return JSON.
|
||||
|
||||
Behavior mirrors ``/api/parse-835``:
|
||||
- 400 on empty / undecodable / malformed EDI (never 500).
|
||||
- 200 on success with ``{"ack": {id, accepted_count, rejected_count,
|
||||
received_count, ack_code, raw_999_text}, "parsed": <ParseResult999>}``.
|
||||
|
||||
The persisted ``acks.source_batch_id`` is a synthetic id
|
||||
(``999-<ISA13>``) because a received 999 has no inbound 837 batch
|
||||
to FK to. The dashboard's `/acks` list surfaces these; operators
|
||||
can still see which interchange each row came from.
|
||||
"""
|
||||
raw = await file.read()
|
||||
if not raw:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Empty file", "detail": "Uploaded file contained no bytes."},
|
||||
)
|
||||
try:
|
||||
text = raw.decode("utf-8")
|
||||
except UnicodeDecodeError as exc:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Encoding error", "detail": str(exc)},
|
||||
)
|
||||
|
||||
try:
|
||||
result = parse_999_text(text, input_file=file.filename or "")
|
||||
except CycloneParseError as exc:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Parse error", "detail": str(exc)},
|
||||
)
|
||||
except Exception as exc: # pragma: no cover - safety net
|
||||
log.exception("Unexpected parser failure on 999")
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"error": "Internal server error", "detail": str(exc)},
|
||||
)
|
||||
|
||||
received, accepted, rejected, ack_code = _ack_count_summary(result)
|
||||
icn = result.envelope.control_number
|
||||
synthetic_id = _ack_synthetic_source_batch_id(icn)
|
||||
|
||||
# Build the raw 999 text from the parsed result (round-trip).
|
||||
raw_999_text = serialize_999(result, interchange_control_number=icn or "000000001")
|
||||
|
||||
# SP6 T4: move claims whose 999 set was rejected into ClaimState.REJECTED.
|
||||
# The 999's set_control_number (AK202) is the source 837's ST02; in
|
||||
# practice we look it up against patient_control_number because that's
|
||||
# the field 999 ACKs cross-reference in this product.
|
||||
with db.SessionLocal()() as session:
|
||||
def _lookup(pcn: str):
|
||||
return session.query(Claim).filter_by(patient_control_number=pcn).first()
|
||||
_rejection_result = apply_999_rejections(
|
||||
session, result, claim_lookup=_lookup,
|
||||
)
|
||||
if _rejection_result.matched:
|
||||
bus = request.app.state.event_bus
|
||||
for cid in _rejection_result.matched:
|
||||
await bus.publish("claim.rejected", {"claim_id": cid})
|
||||
if _rejection_result.orphans:
|
||||
log.warning(
|
||||
"999 had %d orphan set refs: %s",
|
||||
len(_rejection_result.orphans),
|
||||
_rejection_result.orphans[:5],
|
||||
)
|
||||
|
||||
row = store.add_ack(
|
||||
source_batch_id=synthetic_id,
|
||||
accepted_count=accepted,
|
||||
rejected_count=rejected,
|
||||
received_count=received,
|
||||
ack_code=ack_code,
|
||||
raw_json=json.loads(result.model_dump_json()),
|
||||
event_bus=request.app.state.event_bus,
|
||||
)
|
||||
|
||||
# SP11: append one audit row per rejected claim. Each row chains
|
||||
# to the previous one — see cyclone.audit_log.
|
||||
if _rejection_result.matched:
|
||||
with db.SessionLocal()() as audit_s:
|
||||
for cid in _rejection_result.matched:
|
||||
append_event(audit_s, AuditEvent(
|
||||
event_type="claim.rejected",
|
||||
entity_type="claim",
|
||||
entity_id=cid,
|
||||
payload={"source_batch_id": synthetic_id, "ack_id": row.id},
|
||||
actor="999-parser",
|
||||
user_id=_actor_user_id(request),
|
||||
))
|
||||
audit_s.commit()
|
||||
|
||||
# SP28: auto-link the 999 AK2 set-responses to claims via the
|
||||
# D10 two-pass join (ST02 via batch envelope index primary,
|
||||
# Claim.patient_control_number fallback). Each created ClaimAck
|
||||
# row publishes claim_ack_written so the live-tail subscribers
|
||||
# on the claim and ack side see the link immediately.
|
||||
claim_ack_links_count = 0
|
||||
with db.SessionLocal()() as link_s:
|
||||
batch_index = store.batch_envelope_index()
|
||||
|
||||
def _pcn_lookup(pcn: str):
|
||||
return (
|
||||
link_s.query(Claim)
|
||||
.filter(Claim.patient_control_number == pcn)
|
||||
.first()
|
||||
)
|
||||
|
||||
link_result = _apply_999_acceptances(
|
||||
link_s, result, ack_id=row.id,
|
||||
batch_envelope_index=batch_index,
|
||||
pc_claim_lookup=_pcn_lookup,
|
||||
)
|
||||
for link_row in link_result.linked:
|
||||
store.add_claim_ack(
|
||||
claim_id=link_row.claim_id,
|
||||
batch_id=link_row.batch_id,
|
||||
ack_id=row.id,
|
||||
ack_kind="999",
|
||||
ak2_index=link_row.ak2_index,
|
||||
set_control_number=link_row.set_control_number,
|
||||
set_accept_reject_code=link_row.set_accept_reject_code,
|
||||
linked_by="auto",
|
||||
event_bus=request.app.state.event_bus,
|
||||
)
|
||||
claim_ack_links_count += 1
|
||||
|
||||
return JSONResponse(content={
|
||||
"ack": {
|
||||
"id": row.id,
|
||||
"accepted_count": accepted,
|
||||
"rejected_count": rejected,
|
||||
"received_count": received,
|
||||
"ack_code": ack_code,
|
||||
"source_batch_id": synthetic_id,
|
||||
"raw_999_text": raw_999_text,
|
||||
"claim_ack_links_count": claim_ack_links_count,
|
||||
},
|
||||
"parsed": json.loads(result.model_dump_json()),
|
||||
})
|
||||
|
||||
|
||||
@router.post("/api/parse-ta1")
|
||||
async def parse_ta1_endpoint(
|
||||
request: Request,
|
||||
file: UploadFile = File(...),
|
||||
) -> Any:
|
||||
"""Parse a TA1 (Interchange Acknowledgment) file, persist a row, return JSON.
|
||||
|
||||
Mirrors ``/api/parse-999`` but for the lower-level envelope ack:
|
||||
- 400 on empty / undecodable / malformed EDI (never 500).
|
||||
- 200 on success with ``{"ta1": {id, control_number, ack_code,
|
||||
note_code, interchange_date, interchange_time, sender_id,
|
||||
receiver_id, source_batch_id, raw_ta1_text}, "parsed":
|
||||
<ParseResultTa1>}``.
|
||||
|
||||
The persisted ``ta1_acks.source_batch_id`` is a synthetic id
|
||||
(``TA1-<ISA13>``) because a received TA1 has no inbound batch to
|
||||
FK to. The dashboard's ``/ta1-acks`` list surfaces these.
|
||||
|
||||
SP25: threads ``event_bus=request.app.state.event_bus`` into
|
||||
``store.add_ta1_ack`` so the live-tail ``ta1_ack_received``
|
||||
stream fires on manual uploads (not just on the SFTP poller).
|
||||
"""
|
||||
raw = await file.read()
|
||||
if not raw:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Empty file", "detail": "Uploaded file contained no bytes."},
|
||||
)
|
||||
try:
|
||||
text = raw.decode("utf-8")
|
||||
except UnicodeDecodeError as exc:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Encoding error", "detail": str(exc)},
|
||||
)
|
||||
|
||||
try:
|
||||
result = parse_ta1_text(text, input_file=file.filename or "")
|
||||
except CycloneParseError as exc:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Parse error", "detail": str(exc)},
|
||||
)
|
||||
except Exception as exc: # pragma: no cover - safety net
|
||||
log.exception("Unexpected parser failure on TA1")
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"error": "Internal server error", "detail": str(exc)},
|
||||
)
|
||||
|
||||
# Build the raw TA1 text from the parsed result (round-trip).
|
||||
raw_ta1_text = _serialize_ta1(result)
|
||||
|
||||
row = store.add_ta1_ack(
|
||||
source_batch_id=result.source_batch_id,
|
||||
control_number=result.ta1.control_number,
|
||||
interchange_date=result.ta1.interchange_date,
|
||||
interchange_time=result.ta1.interchange_time,
|
||||
ack_code=result.ta1.ack_code,
|
||||
note_code=result.ta1.note_code,
|
||||
ack_generated_date=result.ta1.ack_generated_date,
|
||||
sender_id=result.envelope.sender_id,
|
||||
receiver_id=result.envelope.receiver_id,
|
||||
raw_json=json.loads(result.model_dump_json()),
|
||||
event_bus=request.app.state.event_bus,
|
||||
)
|
||||
|
||||
# SP28: TA1 envelope-level link to the originating Batch. The
|
||||
# closure here matches the most-recent Batch whose envelope
|
||||
# sender_id/receiver_id matches the TA1 — see spec §D4.
|
||||
claim_ack_links_count = 0
|
||||
with db.SessionLocal()() as link_s:
|
||||
def _batch_lookup(sender_id, receiver_id):
|
||||
rows = (
|
||||
link_s.query(Batch)
|
||||
.filter(
|
||||
Batch.kind == "837p",
|
||||
Batch.raw_result_json.isnot(None),
|
||||
)
|
||||
.order_by(Batch.parsed_at.desc())
|
||||
.all()
|
||||
)
|
||||
for row in rows:
|
||||
env = (row.raw_result_json or {}).get("envelope") or {}
|
||||
if (
|
||||
env.get("sender_id") == sender_id
|
||||
and env.get("receiver_id") == receiver_id
|
||||
):
|
||||
return row
|
||||
return None
|
||||
|
||||
link_result = _apply_ta1_envelope_link(
|
||||
link_s, result, ack_id=row.id,
|
||||
batch_lookup=_batch_lookup,
|
||||
)
|
||||
for link_row in link_result.linked:
|
||||
store.add_claim_ack(
|
||||
claim_id=link_row.claim_id,
|
||||
batch_id=link_row.batch_id,
|
||||
ack_id=row.id,
|
||||
ack_kind="ta1",
|
||||
ak2_index=link_row.ak2_index,
|
||||
set_control_number=link_row.set_control_number,
|
||||
set_accept_reject_code=link_row.set_accept_reject_code,
|
||||
linked_by="auto",
|
||||
event_bus=request.app.state.event_bus,
|
||||
)
|
||||
claim_ack_links_count += 1
|
||||
|
||||
return JSONResponse(content={
|
||||
"ta1": {
|
||||
"id": row.id,
|
||||
"control_number": result.ta1.control_number,
|
||||
"ack_code": result.ta1.ack_code,
|
||||
"note_code": result.ta1.note_code,
|
||||
"interchange_date": result.ta1.interchange_date.isoformat()
|
||||
if result.ta1.interchange_date else None,
|
||||
"interchange_time": result.ta1.interchange_time,
|
||||
"sender_id": result.envelope.sender_id,
|
||||
"receiver_id": result.envelope.receiver_id,
|
||||
"source_batch_id": result.source_batch_id,
|
||||
"raw_ta1_text": raw_ta1_text,
|
||||
"claim_ack_links_count": claim_ack_links_count,
|
||||
},
|
||||
"parsed": json.loads(result.model_dump_json()),
|
||||
})
|
||||
|
||||
|
||||
@router.post("/api/parse-277ca")
|
||||
async def parse_277ca_endpoint(
|
||||
request: Request,
|
||||
file: UploadFile = File(...),
|
||||
) -> Any:
|
||||
"""Parse a 277CA Claim Acknowledgment file, persist a row, and stamp rejections.
|
||||
|
||||
Behavior mirrors ``/api/parse-999``:
|
||||
- 400 on empty / undecodable / malformed EDI (never 500).
|
||||
- 200 on success with ``{"ack": {id, control_number, accepted_count,
|
||||
rejected_count, payer_claim_control_numbers, raw_277ca_text},
|
||||
"parsed": <ParseResult277CA>}``.
|
||||
|
||||
After parse, runs :func:`apply_277ca_rejections` to stamp the
|
||||
payer-rejected fields on each matching claim row. The Inbox
|
||||
Payer-Rejected lane lights up as a side-effect of this call.
|
||||
"""
|
||||
raw = await file.read()
|
||||
if not raw:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Empty file", "detail": "Uploaded file contained no bytes."},
|
||||
)
|
||||
try:
|
||||
text = raw.decode("utf-8")
|
||||
except UnicodeDecodeError as exc:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Encoding error", "detail": str(exc)},
|
||||
)
|
||||
|
||||
try:
|
||||
result = parse_277ca_text(text, input_file=file.filename or "")
|
||||
except CycloneParseError as exc:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": "Parse error", "detail": str(exc)},
|
||||
)
|
||||
except Exception as exc: # pragma: no cover - safety net
|
||||
log.exception("Unexpected parser failure on 277CA")
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"error": "Internal server error", "detail": str(exc)},
|
||||
)
|
||||
|
||||
icn = result.envelope.control_number
|
||||
synthetic_id = _277ca_synthetic_source_batch_id(icn)
|
||||
|
||||
accepted = sum(1 for s in result.claim_statuses if s.classification == "accepted")
|
||||
paid = sum(1 for s in result.claim_statuses if s.classification == "paid")
|
||||
rejected = sum(1 for s in result.claim_statuses if s.classification == "rejected")
|
||||
pended = sum(1 for s in result.claim_statuses if s.classification == "pended")
|
||||
|
||||
# Persist the 277CA row first so we have an id to attach to claims.
|
||||
# SP25: thread the event bus so ``two77ca_ack_received`` fires.
|
||||
row = store.add_277ca_ack(
|
||||
source_batch_id=synthetic_id,
|
||||
control_number=icn,
|
||||
accepted_count=accepted,
|
||||
rejected_count=rejected,
|
||||
paid_count=paid,
|
||||
pended_count=pended,
|
||||
raw_json=json.loads(result.model_dump_json()),
|
||||
event_bus=request.app.state.event_bus,
|
||||
)
|
||||
|
||||
# Stamp payer-rejection fields on matching claims. The 277CA's
|
||||
# REF*1K carries the patient's claim control number we sent in
|
||||
# CLM01 — same convention the 999 ACK uses, so the lookup hits
|
||||
# Claim.patient_control_number (mirrors apply_999_rejections).
|
||||
with db.SessionLocal()() as session:
|
||||
def _lookup(pcn: str):
|
||||
return (
|
||||
session.query(Claim)
|
||||
.filter(Claim.patient_control_number == pcn)
|
||||
.first()
|
||||
)
|
||||
apply_result = apply_277ca_rejections(
|
||||
session, result, claim_lookup=_lookup, two77ca_id=row.id,
|
||||
)
|
||||
if apply_result.matched:
|
||||
bus = request.app.state.event_bus
|
||||
for cid in apply_result.matched:
|
||||
await bus.publish("claim.payer_rejected", {"claim_id": cid})
|
||||
# SP11: audit trail for each payer-rejected claim.
|
||||
with db.SessionLocal()() as audit_s:
|
||||
for cid in apply_result.matched:
|
||||
append_event(audit_s, AuditEvent(
|
||||
event_type="claim.payer_rejected",
|
||||
entity_type="claim",
|
||||
entity_id=cid,
|
||||
payload={
|
||||
"source_batch_id": synthetic_id,
|
||||
"277ca_id": row.id,
|
||||
},
|
||||
actor="277ca-parser",
|
||||
user_id=_actor_user_id(request),
|
||||
))
|
||||
audit_s.commit()
|
||||
if apply_result.orphans:
|
||||
log.warning(
|
||||
"277CA had %d orphan status entries (no matching claim): %s",
|
||||
len(apply_result.orphans),
|
||||
apply_result.orphans[:5],
|
||||
)
|
||||
|
||||
# SP28: auto-link the 277CA ClaimStatus entries to claims via
|
||||
# the D10 two-pass join. Each ClaimAck row publishes
|
||||
# claim_ack_written so the live-tail subscribers on the claim
|
||||
# and ack side see the link immediately.
|
||||
claim_ack_links_count = 0
|
||||
with db.SessionLocal()() as link_s:
|
||||
batch_index = store.batch_envelope_index()
|
||||
|
||||
def _pcn_lookup(pcn: str):
|
||||
return (
|
||||
link_s.query(Claim)
|
||||
.filter(Claim.patient_control_number == pcn)
|
||||
.first()
|
||||
)
|
||||
|
||||
link_result = _apply_277ca_acks(
|
||||
link_s, result, ack_id=row.id,
|
||||
batch_envelope_index=batch_index,
|
||||
pc_claim_lookup=_pcn_lookup,
|
||||
)
|
||||
for link_row in link_result.linked:
|
||||
store.add_claim_ack(
|
||||
claim_id=link_row.claim_id,
|
||||
batch_id=link_row.batch_id,
|
||||
ack_id=row.id,
|
||||
ack_kind="277ca",
|
||||
ak2_index=link_row.ak2_index,
|
||||
set_control_number=link_row.set_control_number,
|
||||
set_accept_reject_code=link_row.set_accept_reject_code,
|
||||
linked_by="auto",
|
||||
event_bus=request.app.state.event_bus,
|
||||
)
|
||||
claim_ack_links_count += 1
|
||||
|
||||
return JSONResponse(content={
|
||||
"ack": {
|
||||
"id": row.id,
|
||||
"control_number": icn,
|
||||
"accepted_count": accepted,
|
||||
"rejected_count": rejected,
|
||||
"paid_count": paid,
|
||||
"pended_count": pended,
|
||||
"source_batch_id": synthetic_id,
|
||||
"matched_claim_ids": apply_result.matched,
|
||||
"orphan_status_codes": apply_result.orphans,
|
||||
"claim_ack_links_count": claim_ack_links_count,
|
||||
},
|
||||
"parsed": json.loads(result.model_dump_json()),
|
||||
})
|
||||
@@ -0,0 +1,149 @@
|
||||
"""``GET /api/payers/{payer_id}/summary`` — payer-level rollup for the drill-down panel.
|
||||
|
||||
Returns billed/received totals, denial rate, and the top providers for
|
||||
a given ``payer_id`` (the X12 NM1*PR*PI qualifier, e.g. ``SKCO0``).
|
||||
Cached in-process for ``_SUMMARY_TTL_S`` seconds via a per-payer_id
|
||||
memo. The drill-down UI hammers this on every hover; the underlying
|
||||
query is O(claims) per payer, so the TTL keeps the panel responsive.
|
||||
|
||||
Pubsub invalidation is intentionally NOT wired (see the long comment
|
||||
in the body). The 60s TTL keeps staleness bounded; a follow-up can
|
||||
wire targeted invalidation if the UI proves TTL-bounded staleness is
|
||||
unacceptable.
|
||||
|
||||
404 when no claims AND no remits reference this payer_id — the UI
|
||||
uses that to distinguish a typo from a payer with zero traffic (the
|
||||
latter would still return a valid 200 with zeroed totals).
|
||||
|
||||
SP36 Task 6: this block moved here from ``api.py:3188`` (the
|
||||
``SP21 Task 1.5: payer-level summary`` divider).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from time import monotonic
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.db import Claim, ClaimState, Remittance
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
# Per-payer_id memoization for /api/payers/{payer_id}/summary. The drill-
|
||||
# down UI hammers this on every hover; the underlying query is O(claims)
|
||||
# per payer so a 60s TTL keeps the panel responsive. Process-local on
|
||||
# purpose — invalidation is driven by the TTL alone for now (see note
|
||||
# below).
|
||||
_SUMMARY_TTL_S = 60.0
|
||||
_summary_cache: dict[str, tuple[float, dict]] = {}
|
||||
|
||||
|
||||
def _clear_summary_cache() -> None:
|
||||
"""Test hook: wipe the process-local cache.
|
||||
|
||||
The 60s TTL means tests that want to assert on a recomputed payload
|
||||
must clear the cache between requests. Not used by production code.
|
||||
"""
|
||||
_summary_cache.clear()
|
||||
|
||||
|
||||
# Pubsub invalidation is intentionally NOT wired. The ``claim_written``
|
||||
# and ``remittance_written`` payloads emitted by ``store.add`` are the
|
||||
# UI-shaped dicts from ``to_ui_claim_from_orm`` / ``to_ui_remittance_from_orm``,
|
||||
# neither of which carries the X12 ``payer_id`` (NM1*PR*PI qualifier).
|
||||
# They carry ``payerName`` only, which is the human-readable name and not
|
||||
# the URL key we cache on. Wiring a subscriber here would either need a
|
||||
# DB lookup per event (re-deriving payer_id from Claim.id) or a different
|
||||
# cache key — both are out of scope for this task. The 60s TTL keeps
|
||||
# staleness bounded; a follow-up can wire targeted invalidation if the
|
||||
# UI proves TTL-bounded staleness is unacceptable.
|
||||
|
||||
|
||||
@router.get("/api/payers/{payer_id}/summary")
|
||||
def get_payer_summary(payer_id: str):
|
||||
"""Payer-level rollup for the drill-down panel.
|
||||
|
||||
Returns billed/received totals, denial rate, and top providers for
|
||||
the given ``payer_id`` (the X12 NM1*PR*PI qualifier, e.g. ``SKCO0``).
|
||||
Cached in-process for ``_SUMMARY_TTL_S`` seconds.
|
||||
|
||||
404 when no claims AND no remits reference this payer_id — the UI
|
||||
uses that to distinguish a typo from a payer with zero traffic
|
||||
(the latter would still return a valid 200 with zeroed totals).
|
||||
"""
|
||||
now = monotonic()
|
||||
cached = _summary_cache.get(payer_id)
|
||||
if cached and (now - cached[0]) < _SUMMARY_TTL_S:
|
||||
return cached[1]
|
||||
|
||||
log.debug("payer summary cache miss", extra={"payer_id": payer_id})
|
||||
|
||||
# Query claims + remits scoped to this payer_id. We bypass
|
||||
# ``store.iter_claims`` because that helper filters by payer NAME
|
||||
# substring, not by the X12 PI qualifier we use as the URL key.
|
||||
with db.SessionLocal()() as s:
|
||||
claim_rows: list[Claim] = (
|
||||
s.query(Claim).filter(Claim.payer_id == payer_id).all()
|
||||
)
|
||||
claim_ids = [c.id for c in claim_rows]
|
||||
remit_rows: list[Remittance] = []
|
||||
if claim_ids:
|
||||
remit_rows = (
|
||||
s.query(Remittance)
|
||||
.filter(Remittance.claim_id.in_(claim_ids))
|
||||
.all()
|
||||
)
|
||||
|
||||
if not claim_rows and not remit_rows:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Payer {payer_id!r} not found",
|
||||
)
|
||||
|
||||
billed_total = sum(float(c.charge_amount or 0) for c in claim_rows)
|
||||
received_total = sum(float(r.total_paid or 0) for r in remit_rows)
|
||||
denied = sum(
|
||||
1 for c in claim_rows if c.state == ClaimState.DENIED
|
||||
)
|
||||
claim_count = len(claim_rows)
|
||||
denial_rate = (denied / claim_count) if claim_count else 0.0
|
||||
|
||||
provider_counts: dict[str, int] = {}
|
||||
for c in claim_rows:
|
||||
npi = c.provider_npi
|
||||
if not npi:
|
||||
continue
|
||||
provider_counts[npi] = provider_counts.get(npi, 0) + 1
|
||||
top_providers = [
|
||||
{"npi": npi, "count": count}
|
||||
for npi, count in sorted(
|
||||
provider_counts.items(), key=lambda kv: -kv[1]
|
||||
)[:5]
|
||||
]
|
||||
|
||||
# Best-effort payer display name from the first claim's raw
|
||||
# payer object (NM1*PR name element, e.g. "COHCPF"). Falls
|
||||
# back to the id when no parsed envelope is available.
|
||||
payer_name = payer_id
|
||||
for c in claim_rows:
|
||||
raw = c.raw_json or {}
|
||||
p = raw.get("payer") if isinstance(raw, dict) else None
|
||||
if isinstance(p, dict) and p.get("name"):
|
||||
payer_name = p["name"]
|
||||
break
|
||||
|
||||
payload = {
|
||||
"payer_id": payer_id,
|
||||
"name": payer_name,
|
||||
"claim_count": claim_count,
|
||||
"billed_total": billed_total,
|
||||
"received_total": received_total,
|
||||
"denial_rate": denial_rate,
|
||||
"top_providers": top_providers,
|
||||
}
|
||||
_summary_cache[payer_id] = (now, payload)
|
||||
return payload
|
||||
@@ -0,0 +1,150 @@
|
||||
"""``/api/providers`` and ``/api/config/providers*`` — read views over providers.
|
||||
|
||||
Three endpoints across two URL prefixes, all gated by ``matrix_gate``:
|
||||
|
||||
- ``GET /api/providers`` — distinct provider list
|
||||
derived from the Claims population (``store.distinct_providers()``),
|
||||
with ``npi`` / ``state`` filters, pagination, and an NDJSON variant.
|
||||
- ``GET /api/config/providers`` — the configured provider
|
||||
rows (3 NPIs for SP9), filtered by ``is_active``.
|
||||
- ``GET /api/config/providers/{npi}`` — one configured provider
|
||||
plus a small drill-down block: ``recent_claims`` (top-10 by
|
||||
``submissionDate``) and ``recent_activity`` (top-10, joined via
|
||||
``Claim.id`` because ``ActivityEvent`` has no ``provider_npi``
|
||||
column; the outer-join via ``Remittance.claim_id`` surfaces the
|
||||
orphan ``remit_received`` events that were recorded pre-match).
|
||||
|
||||
SP36 Task 12: this block moved here from ``api.py:2448`` (the
|
||||
``/api/providers`` list, the ``/api/config/providers`` list, and
|
||||
the ``/api/config/providers/{npi}`` detail) — three URL prefixes,
|
||||
one router per spec.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy import desc, or_
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.api_helpers import (
|
||||
ndjson_stream_list as _ndjson_stream_list,
|
||||
wants_ndjson as _wants_ndjson,
|
||||
)
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.db import Claim, Remittance
|
||||
from cyclone.store import store
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
|
||||
@router.get("/api/providers")
|
||||
def list_providers(
|
||||
request: Request,
|
||||
npi: str | None = Query(None),
|
||||
state: str | None = Query(None),
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
offset: int = Query(0, ge=0),
|
||||
) -> Any:
|
||||
items = store.distinct_providers()
|
||||
if npi is not None:
|
||||
items = [p for p in items if p["npi"] == npi]
|
||||
if state is not None:
|
||||
items = [p for p in items if p.get("state") == state]
|
||||
paged = items[offset:offset + limit]
|
||||
total = len(items)
|
||||
returned = len(paged)
|
||||
has_more = total > offset + returned
|
||||
if _wants_ndjson(request):
|
||||
return StreamingResponse(
|
||||
_ndjson_stream_list(paged, total, returned, has_more),
|
||||
media_type="application/x-ndjson",
|
||||
)
|
||||
return {
|
||||
"items": paged,
|
||||
"total": total,
|
||||
"returned": returned,
|
||||
"has_more": has_more,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/api/config/providers")
|
||||
def list_configured_providers(is_active: bool | None = Query(default=True)):
|
||||
"""List the configured provider rows (3 NPIs for SP9)."""
|
||||
return [json.loads(p.model_dump_json()) for p in store.list_providers(is_active=is_active)]
|
||||
|
||||
|
||||
@router.get("/api/config/providers/{npi}")
|
||||
def get_configured_provider(npi: str):
|
||||
p = store.get_provider(npi)
|
||||
if p is None:
|
||||
raise HTTPException(status_code=404, detail=f"provider {npi!r} not found")
|
||||
provider_dict = json.loads(p.model_dump_json())
|
||||
|
||||
# SP21 Task 1.6: extend the response with two top-N arrays that the
|
||||
# drill-down peek panel hangs off. ``recent_claims`` reuses the
|
||||
# existing store projection (already returns UI-shaped dicts with
|
||||
# ``submissionDate``); ``recent_activity`` is a direct ORM join
|
||||
# because ``ActivityEvent`` has no ``provider_npi`` column — the
|
||||
# filter has to hop through ``Claim.id``.
|
||||
recent_claims = sorted(
|
||||
store.iter_claims(provider_npi=npi),
|
||||
key=lambda c: c.get("submissionDate") or "",
|
||||
reverse=True,
|
||||
)[:10]
|
||||
|
||||
# Activity filter has TWO join paths back to a Claim for this
|
||||
# provider:
|
||||
# 1. ``ActivityEvent.claim_id IN (claim_ids)`` — events that were
|
||||
# recorded with a claim FK already set (claim_submitted,
|
||||
# manual_match, claim_paid, etc.).
|
||||
# 2. ``Remittance.claim_id IN (claim_ids)`` — the original
|
||||
# ``remit_received`` event recorded at 835 ingest time
|
||||
# (``store.add`` lines 999-1003) is inserted with
|
||||
# ``claim_id=None`` because the remittance hasn't been matched
|
||||
# to a claim yet. The auto-reconcile pass later sets
|
||||
# ``Remittance.claim_id`` (``reconcile.run`` lines 289-293),
|
||||
# but the *original* ActivityEvent row stays orphaned. The
|
||||
# outer-join-then-OR lets us surface both shapes. Without
|
||||
# path 2, a provider's activity feed looks frozen the moment
|
||||
# an 835 lands — the most common activity, invisible.
|
||||
with db.SessionLocal()() as s:
|
||||
claim_ids = [
|
||||
cid
|
||||
for (cid,) in s.query(Claim.id)
|
||||
.filter(Claim.provider_npi == npi)
|
||||
.all()
|
||||
]
|
||||
activity_rows = []
|
||||
if claim_ids:
|
||||
activity_rows = (
|
||||
s.query(db.ActivityEvent)
|
||||
.outerjoin(
|
||||
Remittance,
|
||||
db.ActivityEvent.remittance_id == Remittance.id,
|
||||
)
|
||||
.filter(or_(
|
||||
db.ActivityEvent.claim_id.in_(claim_ids),
|
||||
Remittance.claim_id.in_(claim_ids),
|
||||
))
|
||||
.order_by(desc(db.ActivityEvent.ts))
|
||||
.limit(10)
|
||||
.all()
|
||||
)
|
||||
|
||||
def _activity_to_ui(a):
|
||||
return {
|
||||
"id": a.id,
|
||||
"ts": a.ts.isoformat().replace("+00:00", "Z") if a.ts else "",
|
||||
"kind": a.kind,
|
||||
"batchId": a.batch_id,
|
||||
"claimId": a.claim_id,
|
||||
"remittanceId": a.remittance_id,
|
||||
"payload": a.payload_json or {},
|
||||
}
|
||||
|
||||
provider_dict["recent_claims"] = recent_claims
|
||||
provider_dict["recent_activity"] = [_activity_to_ui(a) for a in activity_rows]
|
||||
return provider_dict
|
||||
@@ -0,0 +1,221 @@
|
||||
"""SP41 — rebill admin endpoints.
|
||||
|
||||
POST /api/admin/rebill-from-835
|
||||
body: {"window": "YYYY-MM-DD..YYYY-MM-DD",
|
||||
"override_filing": bool,
|
||||
"visits_csv_path": str (optional),
|
||||
"ingest_dir": str (optional),
|
||||
"out_dir": str (optional)}
|
||||
Returns: {"summary_path": str, "counts": {...},
|
||||
"pipeline_a_files": [...], "pipeline_b_files": [...]}
|
||||
|
||||
GET /api/admin/rebill-from-835/status
|
||||
Returns: {"recent_runs": [{"as_of": ..., "summary_path": ...,
|
||||
"counts": {...}}, ...]}
|
||||
|
||||
Status-code contract (per the cyclone-api-router / cyclone-cli skills):
|
||||
- 200: completed run (POST) or tally returned (GET).
|
||||
- 401: not authenticated (matrix_gate).
|
||||
- 403: authenticated but not authorized for /api/admin/*.
|
||||
- 422: window is malformed or Pydantic body validation failed.
|
||||
|
||||
The POST handler delegates to ``cyclone.rebill.run.run_rebill`` (the same
|
||||
orchestrator the ``cyclone rebill-from-835`` CLI uses). The GET handler
|
||||
is a filesystem scan under ``dev/rebills/*/summary.csv`` — no DB table
|
||||
for "recent runs" exists today, and Task 12 deliberately doesn't add
|
||||
one (per its design notes). Sorted by directory mtime descending;
|
||||
truncated to the most recent 5.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import logging
|
||||
from datetime import date
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.rebill.run import run_rebill
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/api/admin/rebill-from-835",
|
||||
tags=["rebill"],
|
||||
dependencies=[Depends(matrix_gate)],
|
||||
)
|
||||
|
||||
|
||||
# Filesystem base for /status scans. Module-level so tests can monkeypatch
|
||||
# it to a tmp_path-relative dir without chdir-ing the whole test process.
|
||||
REBILLS_DIR: Path = Path("dev/rebills")
|
||||
|
||||
# How many recent runs /status surfaces. 5 matches the CLI's default; the
|
||||
# body schema below mirrors it as a query parameter so callers can ask for
|
||||
# more (or fewer) when they want.
|
||||
DEFAULT_RECENT_LIMIT = 5
|
||||
|
||||
|
||||
class RebillRequest(BaseModel):
|
||||
"""Body schema for ``POST /api/admin/rebill-from-835``.
|
||||
|
||||
``window`` is parsed as ``YYYY-MM-DD..YYYY-MM-DD`` (inclusive both
|
||||
ends); the validator rejects anything else with a 422. The other
|
||||
path fields are optional — ``run_rebill`` falls back to its own
|
||||
defaults (``data/source/apr-jun27.csv`` for visits, ``ingest/`` for
|
||||
835s, ``dev/rebills/<today>/`` for output) when unset.
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
window: str = Field(
|
||||
default="2026-01-01..2026-06-27",
|
||||
description="DOS window as YYYY-MM-DD..YYYY-MM-DD (inclusive both ends).",
|
||||
)
|
||||
override_filing: bool = Field(
|
||||
default=False,
|
||||
description="Relax the 120-day timely-filing gate for past-window visits.",
|
||||
)
|
||||
visits_csv_path: str | None = Field(
|
||||
default=None,
|
||||
description="Path to the AxisCare visits CSV; defaults to "
|
||||
"data/source/apr-jun27.csv when unset.",
|
||||
)
|
||||
ingest_dir: str | None = Field(
|
||||
default=None,
|
||||
description="Directory containing *.835 / *.x12 835 files; defaults to ./ingest.",
|
||||
)
|
||||
out_dir: str | None = Field(
|
||||
default=None,
|
||||
description="Output directory for summary.csv + pipeline-a/b files; "
|
||||
"defaults to dev/rebills/<today>/.",
|
||||
)
|
||||
|
||||
@field_validator("window")
|
||||
@classmethod
|
||||
def _validate_window(cls, v: str) -> str:
|
||||
# Manual split (Click can't help here). Two-date range is the
|
||||
# only accepted shape; ``..`` is the giveaway separator so the
|
||||
# input is unambiguous.
|
||||
try:
|
||||
start_str, end_str = v.split("..", 1)
|
||||
except ValueError as exc:
|
||||
raise ValueError(
|
||||
f"window must be 'YYYY-MM-DD..YYYY-MM-DD', got {v!r}"
|
||||
) from exc
|
||||
try:
|
||||
start = date.fromisoformat(start_str)
|
||||
end = date.fromisoformat(end_str)
|
||||
except ValueError as exc:
|
||||
raise ValueError(
|
||||
f"window must be 'YYYY-MM-DD..YYYY-MM-DD', got {v!r}: {exc}"
|
||||
) from exc
|
||||
if start > end:
|
||||
raise ValueError(
|
||||
f"window start {start.isoformat()} is after end {end.isoformat()}"
|
||||
)
|
||||
return v
|
||||
|
||||
|
||||
@router.post("")
|
||||
def post_rebill(req: RebillRequest) -> dict[str, Any]:
|
||||
"""Run the rebill pipeline for the given DOS window.
|
||||
|
||||
Delegates to :func:`cyclone.rebill.run.run_rebill` and returns the
|
||||
resulting ``RunResult`` as a plain JSON dict (the underlying dataclass
|
||||
has no ``to_dict`` method — fields are mapped here so callers don't
|
||||
have to import the dataclass shape).
|
||||
|
||||
The handler does NOT swallow exceptions; the app-level
|
||||
:func:`_unhandled_exception_handler` renders them as a 500 JSON
|
||||
envelope with CORS headers. Per-file failures land in the summary
|
||||
CSV (and the ``counts`` dict) rather than raising here.
|
||||
"""
|
||||
start_str, end_str = req.window.split("..", 1)
|
||||
# Validator already proved these parse cleanly.
|
||||
window_start = date.fromisoformat(start_str)
|
||||
window_end = date.fromisoformat(end_str)
|
||||
|
||||
log.info(
|
||||
"rebill-from-835 starting: window=%s override_filing=%s",
|
||||
req.window, req.override_filing,
|
||||
)
|
||||
result = run_rebill(
|
||||
window_start=window_start,
|
||||
window_end=window_end,
|
||||
override_filing=req.override_filing,
|
||||
visits_csv_path=req.visits_csv_path,
|
||||
ingest_dir=req.ingest_dir,
|
||||
out_dir=req.out_dir,
|
||||
)
|
||||
log.info(
|
||||
"rebill-from-835 done: summary=%s counts=%s",
|
||||
result.summary_path, result.counts,
|
||||
)
|
||||
|
||||
return {
|
||||
"summary_path": str(result.summary_path),
|
||||
"counts": result.counts,
|
||||
"pipeline_a_files": [str(p) for p in result.pipeline_a_files],
|
||||
"pipeline_b_files": [str(p) for p in result.pipeline_b_files],
|
||||
}
|
||||
|
||||
|
||||
@router.get("/status")
|
||||
def get_rebill_status(
|
||||
limit: int = DEFAULT_RECENT_LIMIT,
|
||||
) -> dict[str, Any]:
|
||||
"""Return the most recent rebill runs, newest first.
|
||||
|
||||
Scans :data:`REBILLS_DIR` (``dev/rebills/`` by default) for any
|
||||
subdirectory whose name is a date in YYYY-MM-DD format and that
|
||||
contains a ``summary.csv``. Sorted by directory mtime descending so
|
||||
the most recently-run batch wins ties on equal-named dirs (rare in
|
||||
practice — operators tend to pick a fresh date per run).
|
||||
|
||||
Each entry's ``counts`` dict is a tally of the summary.csv's
|
||||
``disposition`` column (the same per-category counters the operator
|
||||
sees on the CLI ``--status`` view). Missing disposition values
|
||||
surface as ``UNKNOWN`` so a hand-edited CSV can't silently drop a
|
||||
bucket.
|
||||
|
||||
``limit`` defaults to :data:`DEFAULT_RECENT_LIMIT` (5) and is
|
||||
clamped to ``[1, 50]`` to keep the response bounded.
|
||||
"""
|
||||
if limit < 1 or limit > 50:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="limit must be between 1 and 50",
|
||||
)
|
||||
|
||||
recent: list[dict[str, Any]] = []
|
||||
rebills_dir = REBILLS_DIR
|
||||
if rebills_dir.exists():
|
||||
candidates = sorted(
|
||||
(
|
||||
d for d in rebills_dir.iterdir()
|
||||
if d.is_dir()
|
||||
and (d / "summary.csv").exists()
|
||||
# Filter to date-named dirs (YYYY-MM-DD) so a stray
|
||||
# ``lost+found`` or tempdir doesn't sneak in.
|
||||
and len(d.name) == 10 and d.name[4] == "-" and d.name[7] == "-"
|
||||
),
|
||||
key=lambda d: d.stat().st_mtime,
|
||||
reverse=True,
|
||||
)[:limit]
|
||||
for d in candidates:
|
||||
summary_path = d / "summary.csv"
|
||||
counts: dict[str, int] = {}
|
||||
with summary_path.open(newline="") as f:
|
||||
for row in csv.DictReader(f):
|
||||
disp = (row.get("disposition", "UNKNOWN") or "").strip() or "UNKNOWN"
|
||||
counts[disp] = counts.get(disp, 0) + 1
|
||||
recent.append({
|
||||
"as_of": d.name,
|
||||
"summary_path": str(summary_path),
|
||||
"counts": counts,
|
||||
})
|
||||
return {"recent_runs": recent}
|
||||
@@ -0,0 +1,178 @@
|
||||
"""Reconciliation read views + manual match/unmatch write paths.
|
||||
|
||||
Four endpoints:
|
||||
|
||||
- ``GET /api/reconciliation/unmatched`` — list of unmatched Claims
|
||||
and unmatched Remittances (``store.list_unmatched(kind="both")``).
|
||||
- ``GET /api/batch-diff`` — side-by-side diff of two
|
||||
batches (used by the Batch Diff page). Lazy-imports
|
||||
:func:`cyclone.batch_diff.diff_batches_to_wire` to keep the
|
||||
module's import surface small until the endpoint is actually
|
||||
hit.
|
||||
- ``POST /api/reconciliation/match`` — manually pair a Claim with
|
||||
a Remittance (``store.manual_match``). Surfaces ``AlreadyMatchedError`` /
|
||||
``InvalidStateError`` as 409, and ``LookupError`` from the store
|
||||
as 404 (``claim_or_remit_not_found``).
|
||||
- ``POST /api/reconciliation/unmatch`` — unpair a Claim (``store.manual_unmatch``).
|
||||
Surfaces ``NotMatchedError`` as 409.
|
||||
|
||||
All four are read-or-manual-override surfaces used by the
|
||||
Reconciliation page (the page that pairs Claims with Remittances
|
||||
when neither side has an automatic match key).
|
||||
|
||||
SP36 Task 8: this block moved here from ``api.py:2450`` (the
|
||||
4 routes interleaved with a side-by-side batch-diff divider).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.store import (
|
||||
AlreadyMatchedError,
|
||||
InvalidStateError,
|
||||
store,
|
||||
)
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
|
||||
@router.get("/api/reconciliation/unmatched")
|
||||
def get_reconciliation_unmatched() -> dict:
|
||||
"""Return unmatched Claims (left) and unmatched Remittances (right).
|
||||
|
||||
Powers the reconciliation review surface: every Claim with no
|
||||
paired Remittance appears on the left, every Remittance with no
|
||||
paired Claim appears on the right. The two lists are always present
|
||||
(empty list, never absent) so the UI can index unconditionally.
|
||||
"""
|
||||
return store.list_unmatched(kind="both")
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Side-by-side diff between two batches (SP3 P4 / T18)
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
@router.get("/api/batch-diff")
|
||||
def get_batch_diff(
|
||||
a: str | None = Query(None),
|
||||
b: str | None = Query(None),
|
||||
) -> dict:
|
||||
"""Return a side-by-side diff of two batches identified by id.
|
||||
|
||||
Query params: ``a=<batch_id>``, ``b=<batch_id>`` (both required).
|
||||
|
||||
Response body (snake_case keys, see :mod:`cyclone.batch_diff` for the
|
||||
projector shapes):
|
||||
- ``a`` / ``b`` — small metadata blocks (id, kind, parsedAt,
|
||||
inputFilename, claimCount)
|
||||
- ``added`` — claims present in B but not A
|
||||
- ``removed`` — claims present in A but not B
|
||||
- ``changed`` — claims present in both, with field deltas
|
||||
- ``summary`` — precomputed counts
|
||||
|
||||
Errors:
|
||||
- 400 — missing ``a`` or ``b``
|
||||
- 404 — either batch id is unknown
|
||||
|
||||
Pure read endpoint — never mutates the store. Both 837P and 835
|
||||
batches are accepted (mixed-kind diffs are valid: comparing the
|
||||
submitted claims against the matching remittances).
|
||||
"""
|
||||
if not a or not b:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail={"error": "Missing param", "detail": "Both ?a=<batch_id> and ?b=<batch_id> are required."},
|
||||
)
|
||||
try:
|
||||
a_rec, b_rec = store.load_two_for_diff(a, b)
|
||||
except LookupError as exc:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail={"error": "Not found", "detail": str(exc)},
|
||||
)
|
||||
|
||||
# Lazy import — keeps the module's import surface small until the
|
||||
# endpoint is actually hit. Mirrors the same pattern used by other
|
||||
# endpoint-local helpers (e.g. reconciler).
|
||||
from cyclone.batch_diff import diff_batches_to_wire
|
||||
|
||||
return diff_batches_to_wire(a_rec, b_rec)
|
||||
|
||||
|
||||
@router.post("/api/reconciliation/match")
|
||||
def post_reconciliation_match(body: dict) -> dict:
|
||||
"""Manually pair a Claim with a Remittance (operator override).
|
||||
|
||||
Body: ``{"claim_id": ..., "remit_id": ...}``. Returns
|
||||
``{"claim": <ui>, "match": <ui>}`` on success. Errors:
|
||||
- 400: missing ``claim_id`` or ``remit_id``
|
||||
- 404: claim or remittance not found
|
||||
- 409: claim already matched, or apply_* returned a noop
|
||||
(claim in terminal state) — detail echoes ``current_state``
|
||||
and ``activity_kind`` so the UI can render a precise message.
|
||||
"""
|
||||
claim_id = body.get("claim_id")
|
||||
remit_id = body.get("remit_id")
|
||||
if not claim_id or not remit_id:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="claim_id and remit_id required",
|
||||
)
|
||||
try:
|
||||
return store.manual_match(claim_id, remit_id)
|
||||
except AlreadyMatchedError as e:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={"error": "already_matched", "message": str(e)},
|
||||
)
|
||||
except InvalidStateError as e:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={
|
||||
"error": "invalid_state",
|
||||
"current_state": e.current_state,
|
||||
"activity_kind": e.activity_kind,
|
||||
},
|
||||
)
|
||||
except LookupError:
|
||||
# manual_match raises LookupError when the claim or remittance
|
||||
# row is missing (we catch the parent class so any future
|
||||
# KeyError subclasses in the store get the same treatment).
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="claim_or_remit_not_found",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/api/reconciliation/unmatch")
|
||||
def post_reconciliation_unmatch(body: dict) -> dict:
|
||||
"""Remove the current match for a Claim; reset Claim to submitted.
|
||||
|
||||
Body: ``{"claim_id": ...}``. Returns
|
||||
``{"claim": <ui>, "deletedMatches": <count>}``. Errors:
|
||||
- 400: missing ``claim_id``
|
||||
- 404: claim not found
|
||||
- 409: claim has no current match (NotMatchedError is mapped
|
||||
by the store; we surface 409 to match the manual_match contract)
|
||||
"""
|
||||
from cyclone.store import NotMatchedError
|
||||
claim_id = body.get("claim_id")
|
||||
if not claim_id:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="claim_id required",
|
||||
)
|
||||
try:
|
||||
return store.manual_unmatch(claim_id)
|
||||
except NotMatchedError as e:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={"error": "not_matched", "message": str(e)},
|
||||
)
|
||||
except LookupError:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="claim_not_found",
|
||||
)
|
||||
@@ -0,0 +1,169 @@
|
||||
"""``/api/remittances`` and ``/api/remittances/stream`` — read views over the Remittance population.
|
||||
|
||||
Four endpoints, all gated by ``matrix_gate``:
|
||||
|
||||
- ``GET /api/remittances`` — paginated list with filter+sort,
|
||||
plus an NDJSON variant when the caller sends ``Accept: application/x-ndjson``.
|
||||
- ``GET /api/remittances/summary`` — server-aggregated KPI tiles
|
||||
(``count``, ``total_paid``, ``total_adjustments``) over the full
|
||||
filtered population — never a page-limited sample (SP27 fix).
|
||||
- ``GET /api/remittances/stream`` — NDJSON live-tail: snapshot
|
||||
of currently-known rows, then ``remittance_written`` events as
|
||||
they hit the store. Subscribed to by the Remittances page.
|
||||
- ``GET /api/remittances/{remittance_id}`` — one remittance with its
|
||||
labeled CAS ``adjustments`` array. 404 on missing id (never 500).
|
||||
|
||||
``/api/remittances/stream`` is registered before
|
||||
``/api/remittances/{remittance_id}`` so the literal ``stream`` path
|
||||
segment is not captured as a remittance id.
|
||||
|
||||
SP36 Task 9: this block moved here from ``api.py:2448`` (the 4
|
||||
``/api/remittances*`` routes, with the streaming route's
|
||||
``NOTE: registered before…`` comment preserved verbatim).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from cyclone.api_helpers import (
|
||||
ndjson_line as _ndjson_line,
|
||||
ndjson_stream_list as _ndjson_stream_list,
|
||||
tail_events as _tail_events,
|
||||
wants_ndjson as _wants_ndjson,
|
||||
)
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.pubsub import EventBus
|
||||
from cyclone.store import store
|
||||
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
|
||||
@router.get("/api/remittances")
|
||||
def list_remittances(
|
||||
request: Request,
|
||||
batch_id: str | None = Query(None),
|
||||
payer: str | None = Query(None),
|
||||
claim_id: str | None = Query(None),
|
||||
date_from: str | None = Query(None),
|
||||
date_to: str | None = Query(None),
|
||||
sort: str | None = Query(None),
|
||||
order: str = Query("desc"),
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
offset: int = Query(0, ge=0),
|
||||
) -> Any:
|
||||
common = dict(
|
||||
batch_id=batch_id,
|
||||
payer=payer,
|
||||
claim_id=claim_id,
|
||||
date_from=date_from,
|
||||
date_to=date_to,
|
||||
)
|
||||
items = list(store.iter_remittances(
|
||||
sort=sort, order=order, limit=limit, offset=offset, **common,
|
||||
))
|
||||
# SP27 Task 13b: count the full population, not a 100-row sample.
|
||||
# See the matching note in list_claims — same silent-failure pattern.
|
||||
total = store.count_remittances(**common)
|
||||
returned = len(items)
|
||||
has_more = total > offset + returned
|
||||
if _wants_ndjson(request):
|
||||
return StreamingResponse(
|
||||
_ndjson_stream_list(items, total, returned, has_more),
|
||||
media_type="application/x-ndjson",
|
||||
)
|
||||
return {
|
||||
"items": items,
|
||||
"total": total,
|
||||
"returned": returned,
|
||||
"has_more": has_more,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/api/remittances/summary")
|
||||
def remittances_summary(
|
||||
batch_id: str | None = Query(None),
|
||||
payer: str | None = Query(None),
|
||||
claim_id: str | None = Query(None),
|
||||
date_from: str | None = Query(None),
|
||||
date_to: str | None = Query(None),
|
||||
) -> dict:
|
||||
"""Server-aggregated KPI tiles for the Remittances page.
|
||||
|
||||
Returns ``{count, total_paid, total_adjustments}`` over the
|
||||
full filtered remittance population — NOT a page-limited
|
||||
sample. The Remittances page consumes this for its "Total paid"
|
||||
and "Adjustments" tiles so they can't silently understate the
|
||||
true DB population the way a page-local ``items.reduce(...)``
|
||||
would. Mirrors the silent-incompleteness fix that
|
||||
``/api/dashboard/kpis`` (commit ``59c3275``) and
|
||||
``/api/remittances`` (commit ``d81b6ed``) made for their tiles.
|
||||
|
||||
Same filter parameters as ``/api/remittances``. Always returns
|
||||
a populated dict (``{"count": 0, "total_paid": 0,
|
||||
"total_adjustments": 0}`` when no rows match) so the frontend
|
||||
can render the tiles directly without a loading-vs-empty
|
||||
branch.
|
||||
"""
|
||||
return store.summarize_remittances(
|
||||
batch_id=batch_id, payer=payer, claim_id=claim_id,
|
||||
date_from=date_from, date_to=date_to,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/remittances/stream")
|
||||
async def remittances_stream(
|
||||
request: Request,
|
||||
payer: str | None = Query(None),
|
||||
claim_id: str | None = Query(None),
|
||||
date_from: str | None = Query(None),
|
||||
date_to: str | None = Query(None),
|
||||
sort: str | None = Query(None),
|
||||
order: str = Query("desc"),
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
) -> StreamingResponse:
|
||||
"""Stream Remittances as NDJSON: snapshot first, then live events.
|
||||
|
||||
Subscribes to ``remittance_written``. Default sort is
|
||||
``-received_date`` (newest-first), matching the list endpoint's
|
||||
most common sort.
|
||||
|
||||
NOTE: registered before ``/api/remittances/{remittance_id}`` so
|
||||
the literal ``stream`` path segment doesn't get matched as a
|
||||
remittance id.
|
||||
"""
|
||||
bus: EventBus = request.app.state.event_bus
|
||||
|
||||
async def gen() -> AsyncIterator[bytes]:
|
||||
rows = store.iter_remittances(
|
||||
payer=payer, claim_id=claim_id,
|
||||
date_from=date_from, date_to=date_to,
|
||||
sort=sort or "-received_date", order=order, limit=limit,
|
||||
)
|
||||
for row in rows:
|
||||
yield _ndjson_line({"type": "item", "data": row})
|
||||
yield _ndjson_line({"type": "snapshot_end", "data": {"count": len(rows)}})
|
||||
|
||||
async for chunk in _tail_events(request, bus, ["remittance_written"]):
|
||||
yield chunk
|
||||
|
||||
return StreamingResponse(gen(), media_type="application/x-ndjson")
|
||||
|
||||
|
||||
@router.get("/api/remittances/{remittance_id}")
|
||||
def get_remittance(remittance_id: str) -> dict:
|
||||
"""Return one remittance with its labeled CAS ``adjustments`` array.
|
||||
|
||||
Path param is ``remittance_id`` (not ``id``) to avoid shadowing
|
||||
FastAPI's internal ``id`` name and to keep OpenAPI docs self-
|
||||
describing. Returns 404 when the remittance is missing — never 500.
|
||||
"""
|
||||
body = store.get_remittance(remittance_id)
|
||||
if body is None:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail={"error": "Not found", "detail": f"Remittance {remittance_id} not found"},
|
||||
)
|
||||
return body
|
||||
@@ -0,0 +1,213 @@
|
||||
"""SP37 Task 6: HTTP endpoint for the canonical submit-batch flow.
|
||||
|
||||
Thin wrapper around ``cyclone.submission.submit_file`` — same logic as
|
||||
the ``cyclone submit-batch`` CLI (SP37 Task 5), just framed as JSON in
|
||||
/ JSON out and gated by ``matrix_gate``. The walker pattern, ``._*``
|
||||
AppleDouble skip, ``limit`` semantics, and per-file outcomes all match
|
||||
the CLI byte-for-byte so a batch run via the CLI and the same batch
|
||||
run via this endpoint produce identical DB + SFTP state.
|
||||
|
||||
The endpoint deliberately does NOT inject an ``sftp_client_factory``:
|
||||
``submit_file`` defaults to its paramiko-based factory so SKIPPED is
|
||||
reachable in production (the ``SftpClient`` wrapper has no ``stat()``).
|
||||
Tests monkey-patch ``cyclone.api_routers.submission.submit_file``
|
||||
itself; that avoids the paramiko factory entirely without touching
|
||||
the helper's contract.
|
||||
|
||||
Status code contract (per Task 6 spec §4):
|
||||
- 200: completed run. Per-file failures live in the JSON body.
|
||||
- 401: not authenticated (matrix_gate).
|
||||
- 404: no clearhouse seeded (config-level "missing" → 4xx, not 5xx).
|
||||
- 409: clearhouse SFTP block is in stub mode (refuses to upload).
|
||||
- 422: ``ingest_dir`` missing on disk OR Pydantic body validation
|
||||
failed (missing fields, wrong types).
|
||||
- 5xx: truly unexpected exceptions propagate (do not swallow).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone.store import store as cycl_store
|
||||
from cyclone.store.exceptions import DuplicateClaimError
|
||||
from cyclone.submission.core import submit_file
|
||||
from cyclone.submission.result import SubmitOutcome, SubmitResult
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# No `prefix=` here — every other gated router in this package declares
|
||||
# the full path in the decorator (clearhouse.py uses "/api/clearhouse",
|
||||
# parse.py uses "/api/parse-837", etc.). The decorator sets the full URL.
|
||||
router = APIRouter(
|
||||
tags=["submission"],
|
||||
dependencies=[Depends(matrix_gate)],
|
||||
)
|
||||
|
||||
|
||||
class SubmitBatchRequest(BaseModel):
|
||||
"""Body schema for ``POST /api/submit-batch``.
|
||||
|
||||
``ingest_dir`` has no default so Pydantic raises 422 when it's
|
||||
missing — better UX than letting the walker crash on a missing
|
||||
path. ``validate_files`` and ``actor`` default so a minimal client
|
||||
can skip them. ``limit`` truncates the file list after the walker
|
||||
collects it (mirrors the CLI's post-collection ``if i > limit:
|
||||
break`` semantics, but applied as a slice since the HTTP body
|
||||
model is type-checked up-front).
|
||||
|
||||
The ``validate_files`` field is aliased to ``validate`` in the JSON
|
||||
body to mirror the CLI's ``--validate`` flag and avoid the
|
||||
hardcoded Pydantic warning about ``validate`` shadowing
|
||||
``BaseModel.validate``. ``populate_by_name=True`` lets tests
|
||||
construct the model with either key.
|
||||
"""
|
||||
model_config = ConfigDict(populate_by_name=True, protected_namespaces=())
|
||||
|
||||
ingest_dir: str
|
||||
validate_files: bool = Field(default=True, alias="validate")
|
||||
actor: str = "api-submit-batch"
|
||||
limit: int | None = None
|
||||
|
||||
|
||||
@router.post("/api/submit-batch")
|
||||
def submit_batch(body: SubmitBatchRequest):
|
||||
"""Submit every ``batch-*-claims/*.x12`` under ``ingest_dir``.
|
||||
|
||||
Walks ``ingest_dir`` for any directory matching ``batch-*-claims``,
|
||||
collects each one's ``*.x12`` files (sorted, with ``._*``
|
||||
AppleDouble files skipped), truncates to ``limit`` if set, then
|
||||
calls :func:`cyclone.submission.submit_file` per file with the
|
||||
seeded clearhouse's ``sftp_block`` and ``actor`` from the body.
|
||||
|
||||
Returns counts (``submitted`` / ``skipped`` / ``failed``) plus a
|
||||
per-file ``results`` array. Per-file failures NEVER change the
|
||||
HTTP status code — the response is 200 whenever the run itself
|
||||
completed.
|
||||
"""
|
||||
|
||||
# 1. Config-level guards. Order matters: a missing clearhouse is a
|
||||
# 404 (config-level "missing"), but if it IS present and in stub
|
||||
# mode the operator's request is a 409 (configured-but-wrong).
|
||||
clearhouse = cycl_store.get_clearhouse()
|
||||
if clearhouse is None:
|
||||
raise HTTPException(
|
||||
status_code=404, detail="no clearhouse seeded",
|
||||
)
|
||||
sftp_block = clearhouse.sftp_block
|
||||
if sftp_block.stub:
|
||||
raise HTTPException(
|
||||
status_code=409, detail="clearhouse SFTP block is in stub mode",
|
||||
)
|
||||
|
||||
# 2. Resolve + validate the ingest dir. Use ``resolve()`` so a
|
||||
# symlink-relative path still produces a stable error message.
|
||||
root = Path(body.ingest_dir).resolve()
|
||||
if not root.exists():
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail=f"ingest_dir does not exist: {root}",
|
||||
)
|
||||
|
||||
# 3. Walker — must match the CLI EXACTLY. Same sort, same ``._*``
|
||||
# AppleDouble skip. Any drift here is a quiet split between the
|
||||
# two surfaces and silently produces different batch outcomes.
|
||||
files: list[Path] = []
|
||||
for batch_dir in sorted(root.glob("batch-*-claims")):
|
||||
files.extend(sorted(
|
||||
p for p in batch_dir.glob("*.x12")
|
||||
if not p.name.startswith("._")
|
||||
))
|
||||
|
||||
# 4. ``limit`` truncates after collection. The CLI uses an inline
|
||||
# ``if i > limit: break``; we slice instead because the HTTP
|
||||
# body model validates ``limit`` up-front (Pydantic-level int
|
||||
# check) and slicing keeps the walker branchless.
|
||||
if body.limit is not None:
|
||||
files = files[: body.limit]
|
||||
|
||||
if not files:
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail=f"no batch-*-claims/*.x12 files found under {root}",
|
||||
)
|
||||
|
||||
# 5. Per-file submit. Wrap the helper call in try/except so an
|
||||
# unexpected exception in submit_file surfaces as a per-file
|
||||
# failure (outcome="unexpected") instead of crashing the whole
|
||||
# run. The helper's own SubmitOutcome enum covers every typed
|
||||
# failure path; an uncaught exception here is a true
|
||||
# surprise (bug or service outage mid-loop).
|
||||
results: list[dict] = []
|
||||
submitted = skipped = failed = 0
|
||||
for src in files:
|
||||
try:
|
||||
r = submit_file(
|
||||
src,
|
||||
sftp_block=sftp_block,
|
||||
actor=body.actor,
|
||||
validate=body.validate_files,
|
||||
# No ``sftp_client_factory`` — submit_file's default
|
||||
# paramiko factory opens the real MFT. Tests
|
||||
# monkey-patch submit_file itself instead of wiring a
|
||||
# factory here.
|
||||
)
|
||||
except DuplicateClaimError as exc:
|
||||
# SP41 Task 9: 409-class domain exception (see the exception
|
||||
# docstring on ``DuplicateClaimError``). The per-file loop
|
||||
# preserves the 200-with-results status-code contract
|
||||
# documented at the top of this module, so we surface the
|
||||
# duplicate as a per-file failure with UNEXPECTED_ERROR
|
||||
# outcome — the structured ``claim_id`` / ``original_submission_at``
|
||||
# attributes ride along in the error string so the operator
|
||||
# can see which CLM01 in the batch tripped the guard. Caught
|
||||
# BEFORE the generic ``Exception`` handler so a future
|
||||
# caller propagating the exception still gets 409-class
|
||||
# treatment at the FastAPI layer.
|
||||
log.warning(
|
||||
"submit-batch duplicate claim on %s: claim_id=%r original=%s",
|
||||
src.name, exc.claim_id, exc.original_submission_at,
|
||||
)
|
||||
r = SubmitResult(
|
||||
file=src.name,
|
||||
outcome=SubmitOutcome.UNEXPECTED_ERROR,
|
||||
error=(
|
||||
f"DuplicateClaimError: claim_id {exc.claim_id!r} was "
|
||||
f"already submitted at "
|
||||
f"{exc.original_submission_at.isoformat()}; "
|
||||
f"within 30-day window"
|
||||
),
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.exception(
|
||||
"submit-batch unexpected error on %s", src.name,
|
||||
)
|
||||
r = SubmitResult(
|
||||
file=src.name,
|
||||
outcome=SubmitOutcome.UNEXPECTED_ERROR,
|
||||
error=f"{exc.__class__.__name__}: {exc}",
|
||||
)
|
||||
|
||||
if r.outcome == SubmitOutcome.SUBMITTED:
|
||||
submitted += 1
|
||||
elif r.outcome == SubmitOutcome.SKIPPED:
|
||||
skipped += 1
|
||||
else:
|
||||
failed += 1
|
||||
|
||||
results.append({
|
||||
"file": r.file,
|
||||
"outcome": r.outcome.value,
|
||||
"batch_id": r.batch_id,
|
||||
"error": r.error,
|
||||
})
|
||||
|
||||
return {
|
||||
"submitted": submitted,
|
||||
"skipped": skipped,
|
||||
"failed": failed,
|
||||
"results": results,
|
||||
}
|
||||
@@ -16,15 +16,16 @@ from __future__ import annotations
|
||||
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from cyclone.api_helpers import ndjson_line, tail_events
|
||||
from cyclone.auth.deps import matrix_gate
|
||||
from cyclone import db
|
||||
from cyclone.pubsub import EventBus
|
||||
from cyclone.store import store, to_ui_ta1_ack
|
||||
|
||||
router = APIRouter()
|
||||
router = APIRouter(dependencies=[Depends(matrix_gate)])
|
||||
|
||||
|
||||
# SP25: ``_ta1_to_ui`` moved to ``cyclone.store.ui.to_ui_ta1_ack`` so
|
||||
|
||||
@@ -38,11 +38,11 @@ PERMISSIONS: dict[tuple[str, str], set[Role]] = {
|
||||
("GET", "/api/remittances"): ALL_ROLES,
|
||||
("GET", "/api/providers"): ALL_ROLES,
|
||||
("GET", "/api/batches"): ALL_ROLES,
|
||||
("GET", "/api/dashboard/summary"): ALL_ROLES,
|
||||
("GET", "/api/dashboard/kpis"): ALL_ROLES, # dashboard summary cards (renamed from /summary)
|
||||
("GET", "/api/activity"): ALL_ROLES,
|
||||
("GET", "/api/inbox/lanes"): ALL_ROLES,
|
||||
("GET", "/api/inbox/export.csv"): ALL_ROLES,
|
||||
("GET", "/api/reconcile"): ALL_ROLES,
|
||||
("GET", "/api/inbox/ack-orphans"): ALL_ROLES, # Inbox "Ack orphans" lane — wired in src/hooks/useAckOrphans.ts
|
||||
("GET", "/api/reconciliation"): ALL_ROLES,
|
||||
("GET", "/api/acks"): ALL_ROLES,
|
||||
("GET", "/api/ta1-acks"): ALL_ROLES,
|
||||
@@ -50,7 +50,6 @@ PERMISSIONS: dict[tuple[str, str], set[Role]] = {
|
||||
("GET", "/api/batch-diff"): ALL_ROLES,
|
||||
("GET", "/api/config"): ALL_ROLES,
|
||||
("GET", "/api/payers"): ALL_ROLES,
|
||||
("GET", "/api/audit-log"): ADMIN_ONLY,
|
||||
|
||||
# Clearhouse (SFTP creds + dzinesco identity) — admin only.
|
||||
("GET", "/api/clearhouse"): ADMIN_ONLY,
|
||||
@@ -60,15 +59,18 @@ PERMISSIONS: dict[tuple[str, str], set[Role]] = {
|
||||
# Admin ops (audit log, backup, scheduler, db rotate, reload-config).
|
||||
("GET", "/api/admin/audit-log"): ADMIN_ONLY,
|
||||
("GET", "/api/admin/audit-log/verify"): ADMIN_ONLY,
|
||||
("GET", "/api/admin/backup"): ADMIN_ONLY,
|
||||
("POST", "/api/admin/backup"): ADMIN_ONLY,
|
||||
("GET", "/api/admin/backup/scheduler"): ADMIN_ONLY,
|
||||
("POST", "/api/admin/backup/scheduler"): ADMIN_ONLY,
|
||||
("GET", "/api/admin/scheduler"): ADMIN_ONLY,
|
||||
("POST", "/api/admin/scheduler"): ADMIN_ONLY,
|
||||
("GET", "/api/admin/backup"): ADMIN_ONLY, # prefix; covers /list, /status, /{id}/restore/*, /{id}/verify
|
||||
("POST", "/api/admin/backup"): ADMIN_ONLY, # prefix; covers /create, /prune, /{id}/restore/*
|
||||
("GET", "/api/admin/backup/scheduler"): ADMIN_ONLY, # prefix; covers /start, /stop, /tick
|
||||
("POST", "/api/admin/backup/scheduler"): ADMIN_ONLY, # prefix; covers /start, /stop, /tick
|
||||
("GET", "/api/admin/scheduler"): ADMIN_ONLY, # prefix; covers /status, /processed-files
|
||||
("POST", "/api/admin/scheduler"): ADMIN_ONLY, # prefix; covers /start, /stop, /tick, /pull-inbound
|
||||
("POST", "/api/admin/db/rotate-key"): ADMIN_ONLY,
|
||||
("POST", "/api/admin/reload-config"): ADMIN_ONLY,
|
||||
("GET", "/api/admin/validate-provider"): ADMIN_ONLY,
|
||||
("POST", "/api/admin/validate-837"): ADMIN_ONLY, # SP40: Edifabric validation probe
|
||||
("POST", "/api/admin/rebill-from-835"): ADMIN_ONLY, # SP41: run the in-window rebill pipeline
|
||||
("GET", "/api/admin/rebill-from-835"): ADMIN_ONLY, # SP41: covers /status (prefix match)
|
||||
|
||||
# Write endpoints (admin + user, no viewer).
|
||||
("POST", "/api/parse-837"): WRITE_ROLES,
|
||||
@@ -80,15 +82,15 @@ PERMISSIONS: dict[tuple[str, str], set[Role]] = {
|
||||
("POST", "/api/inbox/candidates"): WRITE_ROLES,
|
||||
("POST", "/api/inbox/rejected"): WRITE_ROLES,
|
||||
("POST", "/api/inbox/payer-rejected"): WRITE_ROLES,
|
||||
("POST", "/api/reconcile"): WRITE_ROLES,
|
||||
("POST", "/api/reconciliation"): WRITE_ROLES,
|
||||
("POST", "/api/resubmit"): WRITE_ROLES,
|
||||
("POST", "/api/acks"): WRITE_ROLES,
|
||||
# Unlink a wrong claim-ack match — inverse of POST /api/inbox/candidates/{remit_id}/match.
|
||||
# Prefix (not the placeholder path) because the matcher treats {kind}
|
||||
# as a literal substring; only ``/api/acks`` actually matches real requests.
|
||||
("DELETE", "/api/acks"): WRITE_ROLES,
|
||||
("POST", "/api/batches"): WRITE_ROLES, # /export-837 regenerates X12 from DB rows
|
||||
("POST", "/api/eligibility"): WRITE_ROLES,
|
||||
|
||||
# CSV export — read-only.
|
||||
("GET", "/api/export.csv"): ALL_ROLES,
|
||||
("POST", "/api/submit-batch"): WRITE_ROLES, # SP37: canonical outbound path (mirrors CLI)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -94,6 +94,22 @@ def _op_timeout_seconds() -> float:
|
||||
return value
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SftpStat:
|
||||
"""File metadata returned by :meth:`SftpClient.stat`.
|
||||
|
||||
Mirrors the subset of paramiko's ``SFTPAttributes`` that callers
|
||||
actually need (``size``, ``modified_at``). Keeping the surface
|
||||
narrow means callers don't need to import paramiko to consume
|
||||
the result, and the wrapper's stub mode can populate it from a
|
||||
plain ``os.stat_result`` without any paramiko dance.
|
||||
|
||||
Frozen so callers can hash / cache the result if they need to.
|
||||
"""
|
||||
size: int
|
||||
modified_at: datetime | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class InboundFile:
|
||||
"""A single file observed in the inbound MFT path."""
|
||||
@@ -216,6 +232,30 @@ class SftpClient:
|
||||
return self._read_file_stub(remote_path)
|
||||
return self._read_file_paramiko(remote_path)
|
||||
|
||||
def stat(self, remote_path: str) -> SftpStat:
|
||||
"""Return file metadata for ``remote_path``.
|
||||
|
||||
Mirrors the subset of paramiko's ``SFTPAttributes`` that
|
||||
callers need (``size`` for idempotency checks; ``modified_at``
|
||||
for cache-busting). The SP37 submission helper uses
|
||||
``stat().size`` to short-circuit re-uploads of already-uploaded
|
||||
files (the SKIPPED outcome path).
|
||||
|
||||
Stub mode: reads ``os.stat_result`` from
|
||||
``{staging_dir}/{remote_path}``.
|
||||
|
||||
Real mode: calls ``sftp.stat(remote_path)`` on a paramiko
|
||||
connection.
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: if ``remote_path`` does not exist
|
||||
(stub: missing local file; real: paramiko raises
|
||||
``IOError`` which is a ``FileNotFoundError`` subclass).
|
||||
"""
|
||||
if self._stub:
|
||||
return self._stat_stub(remote_path)
|
||||
return self._stat_paramiko(remote_path)
|
||||
|
||||
def _read_file_stub(self, remote_path: str) -> bytes:
|
||||
"""Read bytes from ``{staging_dir}/{remote_path}`` (SP16 stub)."""
|
||||
staging = Path(self._block.staging_dir).resolve()
|
||||
@@ -224,6 +264,25 @@ class SftpClient:
|
||||
raise FileNotFoundError(f"inbound stub file not found: {target}")
|
||||
return target.read_bytes()
|
||||
|
||||
def _stat_stub(self, remote_path: str) -> SftpStat:
|
||||
"""Return ``SftpStat`` for ``{staging_dir}/{remote_path}`` (SP37 stub).
|
||||
|
||||
Mirrors what paramiko's ``sftp.stat()`` returns in real mode:
|
||||
``size`` from ``st.st_size`` and ``modified_at`` from
|
||||
``st.st_mtime``. Raises ``FileNotFoundError`` if the local
|
||||
file is missing — matches paramiko's ``IOError`` behavior
|
||||
so the caller doesn't need to special-case stub mode.
|
||||
"""
|
||||
staging = Path(self._block.staging_dir).resolve()
|
||||
target = staging / remote_path.lstrip("/")
|
||||
if not target.is_file():
|
||||
raise FileNotFoundError(f"inbound stub file not found: {target}")
|
||||
st = target.stat()
|
||||
return SftpStat(
|
||||
size=st.st_size,
|
||||
modified_at=datetime.fromtimestamp(st.st_mtime),
|
||||
)
|
||||
|
||||
def get_secret(self, name: str) -> Optional[str]:
|
||||
"""Fetch the auth secret from Keychain. Returns the stub secret if absent."""
|
||||
value = secrets.get_secret(name)
|
||||
@@ -506,6 +565,20 @@ class SftpClient:
|
||||
shutil.copyfileobj(f, buf, length=64 * 1024)
|
||||
return buf.getvalue()
|
||||
|
||||
def _stat_paramiko(self, remote_path: str) -> SftpStat:
|
||||
"""Return ``SftpStat`` for ``remote_path`` via paramiko.
|
||||
|
||||
Paramiko's ``SFTPAttributes`` already provides ``st_size`` and
|
||||
``st_mtime``; we project them into our narrow public
|
||||
``SftpStat`` shape so callers don't need to know about paramiko.
|
||||
"""
|
||||
with self._connect() as (ssh, sftp):
|
||||
attr = sftp.stat(remote_path)
|
||||
return SftpStat(
|
||||
size=attr.st_size or 0,
|
||||
modified_at=datetime.fromtimestamp(attr.st_mtime or 0),
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Module-level helper
|
||||
|
||||
+1092
-4
File diff suppressed because it is too large
Load Diff
@@ -13,7 +13,7 @@ from __future__ import annotations
|
||||
import enum
|
||||
import json
|
||||
import os
|
||||
from datetime import date, datetime
|
||||
from datetime import date, datetime, timezone
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
@@ -30,6 +30,7 @@ from sqlalchemy import (
|
||||
Numeric,
|
||||
String,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
func,
|
||||
text,
|
||||
)
|
||||
@@ -221,6 +222,13 @@ class Batch(Base):
|
||||
totals_json: Mapped[Optional[dict]] = mapped_column(JSONText, nullable=True)
|
||||
validation_json: Mapped[Optional[dict]] = mapped_column(JSONText, nullable=True)
|
||||
raw_result_json: Mapped[Optional[dict]] = mapped_column(JSONText, nullable=True)
|
||||
# SP37 Task 2: source 837's ST02 (transaction set control number).
|
||||
# Populated from ``Envelope.transaction_set_control_number`` by
|
||||
# ``store.write.add_record`` for 837P batches; NULL for 835 batches
|
||||
# (the column is an 837P-specific join key for 999 AK2 resolution).
|
||||
# Migration 0020 adds the column additively; no backfill required for
|
||||
# pre-existing rows that lack the value.
|
||||
transaction_set_control_number: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
|
||||
|
||||
claims: Mapped[list["Claim"]] = relationship(
|
||||
back_populates="batch", cascade="all, delete-orphan"
|
||||
@@ -443,6 +451,45 @@ class ServiceLinePayment(Base):
|
||||
)
|
||||
|
||||
|
||||
class Visit(Base):
|
||||
"""SP41: a single row from the AxisCare visits export (CSV).
|
||||
|
||||
Persisted by ``cyclone.rebill.visits_store.load_visits_csv`` so the
|
||||
in-window rebill pipeline can reconcile visits vs 835 svc rows from
|
||||
the database (previously the spot-check driver read the CSV in-memory,
|
||||
losing the canonical source-of-truth for the visit roster).
|
||||
|
||||
One row per (DOS, member_id, procedure, modifiers). The UNIQUE
|
||||
constraint on those four columns dedupes the natural key — the CSV
|
||||
itself can contain duplicate rows for the same visit (re-exports).
|
||||
"""
|
||||
__tablename__ = "visits"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
dos: Mapped[date] = mapped_column(Date, nullable=False)
|
||||
member_id: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
client_name: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
procedure_code: Mapped[str] = mapped_column(String(16), nullable=False)
|
||||
modifiers: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||||
billed_amount: Mapped[Decimal] = mapped_column(Numeric(10, 2), nullable=False)
|
||||
icd10: Mapped[Optional[str]] = mapped_column(String(16), nullable=True)
|
||||
prior_auth: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||||
payer: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||||
invoice_number: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||||
source_file: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
||||
loaded_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, default=lambda: datetime.now(timezone.utc)
|
||||
)
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint("dos", "member_id", "procedure_code", "modifiers",
|
||||
name="uq_visits_natural_key"),
|
||||
Index("ix_visits_dos", "dos"),
|
||||
Index("ix_visits_member_id", "member_id"),
|
||||
Index("ix_visits_procedure_code", "procedure_code"),
|
||||
)
|
||||
|
||||
|
||||
class LineReconciliation(Base):
|
||||
"""One row per matched (or explicitly unmatched) 837 service line within a claim.
|
||||
|
||||
@@ -945,3 +992,49 @@ class Session(Base):
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
|
||||
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), index=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
|
||||
class Resubmission(Base):
|
||||
"""SP39: audit row recording that a claim was pushed to SFTP as
|
||||
part of a corrected-file resubmission. One row per claim per push.
|
||||
|
||||
Status is derived at read-time by joining against claim_acks (via
|
||||
the existing SP28/31 auto-link) + remittances (via CLP->claim).
|
||||
The corrected-v2 regen preserves the original claim_id from
|
||||
raw_json, so inbound 999 acks auto-link back to the original claim
|
||||
row and the join works without any new matching logic.
|
||||
"""
|
||||
|
||||
__tablename__ = "resubmissions"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
claim_id: Mapped[str] = mapped_column(String, nullable=False, index=True)
|
||||
batch_id: Mapped[str] = mapped_column(String, nullable=False, index=True)
|
||||
resubmitted_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
source_corrected_path: Mapped[str] = mapped_column(String, nullable=False)
|
||||
interchange_control_number: Mapped[str] = mapped_column(String, nullable=False)
|
||||
group_control_number: Mapped[str] = mapped_column(String, nullable=False)
|
||||
|
||||
__table_args__ = (
|
||||
Index(
|
||||
"ux_resubmissions_claim_icn",
|
||||
"claim_id", "interchange_control_number",
|
||||
unique=True,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class SubmissionRecord(Base):
|
||||
"""SP41: one row per (claim_id, submitted_at) pre-flight dedup guard.
|
||||
|
||||
The 30-day dedup window is enforced by ``cyclone.store.submission_dedup``.
|
||||
This table records (claim_id, submitted_at) for every push that passed
|
||||
the pre-flight check. Past the window, the prior record is ignored
|
||||
(the guard lets through re-submits older than
|
||||
``cyclone.store.submission_dedup.DEFAULT_WINDOW_DAYS``).
|
||||
"""
|
||||
|
||||
__tablename__ = "submission_dedup"
|
||||
|
||||
claim_id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
submitted_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, index=True)
|
||||
|
||||
@@ -0,0 +1,251 @@
|
||||
"""SP40: thin HTTP client for the EdiNation / Edifabric validation API.
|
||||
|
||||
Wraps the two-step /v2/x12/read (raw EDI → ``X12Interchange`` JSON) and
|
||||
/v2/x12/validate (``X12Interchange`` JSON → ``OperationResult``) flow.
|
||||
|
||||
Cyclone has no other outbound HTTP today; this is the first such
|
||||
client. ``httpx`` is already a project dep (used by the test suite)
|
||||
so we don't introduce a new dependency.
|
||||
|
||||
Public surface:
|
||||
|
||||
- :func:`read_interchange` — POST raw EDI bytes to /x12/read.
|
||||
- :func:`validate_interchange` — POST ``X12Interchange`` JSON to /x12/validate.
|
||||
- :func:`validate_edi` — composes the two; this is what the CLI and
|
||||
pre-upload gate use.
|
||||
- :class:`EdifabricError` — raised on 4xx/5xx so callers can surface
|
||||
the Edifabric error verbatim (the body is a dict or string).
|
||||
|
||||
The API key is taken from :func:`cyclone.secrets.get_secret('edifabric.api_key')`
|
||||
which maps to ``CYCLONE_EDIFABRIC_API_KEY`` env var or macOS Keychain.
|
||||
Tests inject a canned key (and a mocked transport) via the factory
|
||||
hook :func:`set_transport_factory` so no live HTTP hits the network.
|
||||
|
||||
The endpoint contract (from the EdiNation API reference,
|
||||
``https://support.edifabric.com/hc/en-us/sections/360005605638``):
|
||||
|
||||
- ``POST https://api.edination.com/v2/x12/read``
|
||||
- Headers: ``Ocp-Apim-Subscription-Key: <key>``, ``Content-Type: application/octet-stream``
|
||||
- Body: raw EDI bytes
|
||||
- Response (200): JSON array of ``X12Interchange`` objects (cyclone always sends one interchange)
|
||||
- ``POST https://api.edination.com/v2/x12/validate``
|
||||
- Headers: ``Ocp-Apim-Subscription-Key: <key>``, ``Content-Type: application/json``
|
||||
- Body: one ``X12Interchange`` object
|
||||
- Response (200): ``OperationResult`` (``Status`` ∈ {``"success"``, ``"warning"``, ``"error"``}, ``Details`` array)
|
||||
|
||||
We treat any non-2xx as an :class:`EdifabricError`. The response body
|
||||
is preserved verbatim so callers can inspect it.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import Any, Callable
|
||||
|
||||
import httpx
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
|
||||
_BASE_URL = "https://api.edination.com/v2/x12"
|
||||
_READ_PATH = "/read"
|
||||
_VALIDATE_PATH = "/validate"
|
||||
|
||||
# Subscription-key header name (Azure API Management convention).
|
||||
_SUB_HEADER = "Ocp-Apim-Subscription-Key"
|
||||
|
||||
|
||||
class EdifabricError(RuntimeError):
|
||||
"""Raised when the Edifabric API returns a non-2xx response.
|
||||
|
||||
Attributes:
|
||||
status_code: HTTP status code returned by Edifabric.
|
||||
body: The response body — usually a dict with ``error`` /
|
||||
``message`` keys for 4xx, or a string for 5xx / network
|
||||
errors. Preserved verbatim so the caller can surface it
|
||||
to the operator.
|
||||
retry_after_seconds: When the upstream sent a ``Retry-After``
|
||||
header (the API Management quota policy does), the value
|
||||
in seconds. ``None`` if absent. Quota-blocked callers can
|
||||
surface this so the operator knows when to retry.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, status_code: int, body: Any, *, retry_after_seconds: int | None = None
|
||||
) -> None:
|
||||
self.status_code = status_code
|
||||
self.body = body
|
||||
self.retry_after_seconds = retry_after_seconds
|
||||
body_repr = repr(body) if not isinstance(body, str) else body
|
||||
msg = f"Edifabric API returned {status_code}: {body_repr}"
|
||||
if retry_after_seconds is not None:
|
||||
msg += f" (retry after {retry_after_seconds}s)"
|
||||
super().__init__(msg)
|
||||
|
||||
|
||||
# --- Transport injection (test seam) -----------------------------------
|
||||
|
||||
# Default transport factory builds a normal httpx.Client. Tests can
|
||||
# call set_transport_factory() with a callable that returns a Client
|
||||
# backed by httpx.MockTransport (no live HTTP).
|
||||
_transport_factory: Callable[[], httpx.Client] = lambda: httpx.Client(timeout=30.0)
|
||||
|
||||
|
||||
def set_transport_factory(factory: Callable[[], httpx.Client]) -> None:
|
||||
"""Inject an ``httpx.Client`` factory for tests.
|
||||
|
||||
The factory must return an ``httpx.Client`` whose ``transport`` is
|
||||
a mock (e.g. ``httpx.MockTransport(handler)``) so no real HTTP is
|
||||
performed. Returns the previous factory so tests can restore it.
|
||||
"""
|
||||
global _transport_factory
|
||||
prev = _transport_factory
|
||||
_transport_factory = factory
|
||||
return prev # type: ignore[return-value]
|
||||
|
||||
|
||||
def _reset_transport_factory() -> None:
|
||||
"""Restore the default transport factory (called in test cleanup)."""
|
||||
global _transport_factory
|
||||
_transport_factory = lambda: httpx.Client(timeout=30.0)
|
||||
|
||||
|
||||
# --- Public surface ----------------------------------------------------
|
||||
|
||||
|
||||
def _get_api_key() -> str:
|
||||
"""Resolve the Edifabric API key.
|
||||
|
||||
Looks up ``CYCLONE_EDIFABRIC_API_KEY`` (or the keychain entry
|
||||
``cyclone / edifabric.api_key``). The secrets module is imported
|
||||
lazily so test setups that mock it can do so before first use.
|
||||
"""
|
||||
from cyclone.secrets import get_secret
|
||||
|
||||
key = get_secret("edifabric.api_key")
|
||||
if not key:
|
||||
raise EdifabricError(
|
||||
0,
|
||||
"Edifabric API key not configured; set CYCLONE_EDIFABRIC_API_KEY "
|
||||
"env var or run `cyclone secrets set edifabric.api_key <key>`",
|
||||
)
|
||||
return key
|
||||
|
||||
|
||||
def read_interchange(edi_bytes: bytes, *, api_key: str | None = None) -> dict:
|
||||
"""POST raw EDI bytes to /x12/read and return the first X12Interchange.
|
||||
|
||||
The /x12/read endpoint accepts a multi-interchange file and returns
|
||||
an array. Cyclone only ever sends single interchanges, so we return
|
||||
the first (and only) element. If the file contains multiple
|
||||
interchanges, callers should call ``validate_interchange`` on each.
|
||||
"""
|
||||
if not isinstance(edi_bytes, (bytes, bytearray)):
|
||||
raise TypeError(
|
||||
f"edi_bytes must be bytes, got {type(edi_bytes).__name__}"
|
||||
)
|
||||
key = api_key if api_key is not None else _get_api_key()
|
||||
headers = {
|
||||
_SUB_HEADER: key,
|
||||
"Content-Type": "application/octet-stream",
|
||||
}
|
||||
with _transport_factory() as client:
|
||||
resp = client.post(
|
||||
f"{_BASE_URL}{_READ_PATH}",
|
||||
content=bytes(edi_bytes),
|
||||
headers=headers,
|
||||
)
|
||||
if not (200 <= resp.status_code < 300):
|
||||
raise EdifabricError(
|
||||
resp.status_code,
|
||||
_safe_body(resp),
|
||||
retry_after_seconds=_retry_after_seconds(resp),
|
||||
)
|
||||
data = resp.json()
|
||||
if not isinstance(data, list) or not data:
|
||||
raise EdifabricError(
|
||||
502,
|
||||
f"unexpected /x12/read response shape: expected non-empty list, "
|
||||
f"got {type(data).__name__} of length {len(data) if hasattr(data, '__len__') else '?'}",
|
||||
)
|
||||
return data[0]
|
||||
|
||||
|
||||
def validate_interchange(x12_json: dict, *, api_key: str | None = None) -> dict:
|
||||
"""POST an X12Interchange JSON to /x12/validate and return the OperationResult.
|
||||
|
||||
The OperationResult schema (per the EdiNation docs):
|
||||
|
||||
- ``Status`` — ``"success"`` / ``"warning"`` / ``"error"``.
|
||||
- ``Details`` — array of ``{Index, SegmentId, Value, Message, Status, ...}``.
|
||||
- ``LastIndex`` — 1-based index of the last processed segment.
|
||||
|
||||
We do NOT raise on ``Status == "error"`` — the caller decides whether
|
||||
to fail-closed (the pre-upload gate does; the CLI prints and exits
|
||||
with the appropriate code). Non-2xx HTTP responses DO raise
|
||||
:class:`EdifabricError`.
|
||||
"""
|
||||
if not isinstance(x12_json, dict):
|
||||
raise TypeError(
|
||||
f"x12_json must be a dict, got {type(x12_json).__name__}"
|
||||
)
|
||||
key = api_key if api_key is not None else _get_api_key()
|
||||
headers = {
|
||||
_SUB_HEADER: key,
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
with _transport_factory() as client:
|
||||
resp = client.post(
|
||||
f"{_BASE_URL}{_VALIDATE_PATH}",
|
||||
json=x12_json,
|
||||
headers=headers,
|
||||
)
|
||||
if not (200 <= resp.status_code < 300):
|
||||
raise EdifabricError(
|
||||
resp.status_code,
|
||||
_safe_body(resp),
|
||||
retry_after_seconds=_retry_after_seconds(resp),
|
||||
)
|
||||
return resp.json()
|
||||
|
||||
|
||||
def validate_edi(edi_bytes: bytes, *, api_key: str | None = None) -> dict:
|
||||
"""Two-step convenience: read → validate. Returns the OperationResult.
|
||||
|
||||
This is what ``cyclone validate-837 <file>`` and the pre-upload
|
||||
gate in ``resubmit-rejected-claims`` call. Raises
|
||||
:class:`EdifabricError` on transport / non-2xx errors. The
|
||||
OperationResult is returned verbatim so the caller can inspect
|
||||
``Status`` and ``Details`` themselves.
|
||||
"""
|
||||
x12 = read_interchange(edi_bytes, api_key=api_key)
|
||||
return validate_interchange(x12, api_key=api_key)
|
||||
|
||||
|
||||
# --- Internal helpers --------------------------------------------------
|
||||
|
||||
|
||||
def _safe_body(resp: httpx.Response) -> Any:
|
||||
"""Return the response body, preferring JSON when possible."""
|
||||
try:
|
||||
return resp.json()
|
||||
except Exception: # noqa: BLE001
|
||||
text = resp.text
|
||||
return text if text else f"<empty {resp.status_code} response>"
|
||||
|
||||
|
||||
def _retry_after_seconds(resp: httpx.Response) -> int | None:
|
||||
"""Parse the ``Retry-After`` header into integer seconds.
|
||||
|
||||
Returns ``None`` if the header is absent (and the caller should
|
||||
fall back to its own backoff). The upstream API Management sends
|
||||
this header on quota-blocked responses; treating it as authoritative
|
||||
lets the caller sleep exactly until quota replenishes rather than
|
||||
guessing.
|
||||
"""
|
||||
raw = resp.headers.get("Retry-After") or resp.headers.get("retry-after")
|
||||
if not raw:
|
||||
return None
|
||||
try:
|
||||
return int(raw)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
@@ -66,10 +66,12 @@ def handle(
|
||||
here was dead code anyway (the 835 event name is
|
||||
``remittance_written``, not ``ack_received``).
|
||||
"""
|
||||
# TODO(sp27-pre-t5): move PAYER_FACTORIES_835 out of api.py into
|
||||
# ``cyclone.payers`` to remove the lazy cyclic import below. The
|
||||
# import works today because api.py also imports scheduler lazily.
|
||||
from cyclone.api import PAYER_FACTORIES_835
|
||||
# SP36 Task 16: PAYER_FACTORIES_835 moved from ``cyclone.api``
|
||||
# to ``cyclone.api_routers._shared`` (cross-router helper). Import
|
||||
# from the new home. The lazy import is still needed to avoid a
|
||||
# circular import at registry load time (handle_835 is imported
|
||||
# by the scheduler during the api lifespan).
|
||||
from cyclone.api_routers._shared import PAYER_FACTORIES_835
|
||||
|
||||
config = PAYER_FACTORIES_835["co_medicaid_835"]()
|
||||
try:
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
-- version: 20
|
||||
-- SP37: Batch.transaction_set_control_number = parsed 837's ST02.
|
||||
--
|
||||
-- Today's 999 ack join (claim_acks.batch_envelope_index, Pass 1) matches
|
||||
-- on ``Batch.envelope.control_number == 999's set_control_number``. That
|
||||
-- never resolves in production because 999's set_control_number (AK201)
|
||||
-- echoes the source 837's ST02 (transaction set control number), not the
|
||||
-- ISA13 (interchange control number) that Envelope.control_number stores.
|
||||
-- Result: every AK2 set-response against a dzinesco-generated 837 turns
|
||||
-- into an orphan.
|
||||
--
|
||||
-- SP37 fixes this by adding a column populated from the parsed 837's
|
||||
-- ST02 on every ``add_record`` write, then updating Pass 1 to match on
|
||||
-- it (Task 2). This migration is the additive part: nullable, no
|
||||
-- default, backfills from ``raw_result_json.envelope.transaction_set_control_number``
|
||||
-- for any pre-existing batch rows that already carry the value.
|
||||
--
|
||||
-- No new index (column is a primary join key, not a range query; the
|
||||
-- existing batches table is small enough for a full scan during the
|
||||
-- 999 join — see SP37 §"Migration 0013").
|
||||
|
||||
ALTER TABLE batches ADD COLUMN transaction_set_control_number TEXT;
|
||||
|
||||
UPDATE batches
|
||||
SET transaction_set_control_number = json_extract(raw_result_json, '$.envelope.transaction_set_control_number')
|
||||
WHERE raw_result_json IS NOT NULL
|
||||
AND json_extract(raw_result_json, '$.envelope.transaction_set_control_number') IS NOT NULL;
|
||||
@@ -0,0 +1,30 @@
|
||||
-- version: 21
|
||||
-- SP39: resubmissions audit table for tracking corrected-file SFTP pushes.
|
||||
--
|
||||
-- One row per claim per push. Status (pending_999 / 999_accepted /
|
||||
-- 999_rejected / 277ca_accepted / paid / denied_again) is derived at
|
||||
-- read-time by joining against claim_acks (via the existing SP28/31
|
||||
-- auto-link) + remittances (via CLP->claim). No denormalized status
|
||||
-- column on this row — the existing auto-link data is the source of
|
||||
-- truth and we want to avoid a write-coordination problem between
|
||||
-- the SFTP push and the inbound ack ingestion.
|
||||
--
|
||||
-- Idempotency on (claim_id, interchange_control_number): the resubmit
|
||||
-- CLI can be re-run safely without producing duplicate rows for the
|
||||
-- same push.
|
||||
|
||||
CREATE TABLE resubmissions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
claim_id TEXT NOT NULL,
|
||||
batch_id TEXT NOT NULL,
|
||||
resubmitted_at DATETIME NOT NULL,
|
||||
source_corrected_path TEXT NOT NULL,
|
||||
interchange_control_number TEXT NOT NULL,
|
||||
group_control_number TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX ix_resubmissions_claim_id ON resubmissions(claim_id);
|
||||
CREATE INDEX ix_resubmissions_batch_id ON resubmissions(batch_id);
|
||||
|
||||
CREATE UNIQUE INDEX ux_resubmissions_claim_icn
|
||||
ON resubmissions(claim_id, interchange_control_number);
|
||||
@@ -0,0 +1,15 @@
|
||||
-- version: 22
|
||||
-- SP41: claim-id dedup at SFTP pre-flight.
|
||||
--
|
||||
-- The 30-day dedup window is enforced by cyclone.store.submission_dedup.
|
||||
-- This table records (claim_id, submitted_at) for every push that passed
|
||||
-- the pre-flight check. Past the window, the prior record is ignored
|
||||
-- (the guard lets through re-submits older than DEFAULT_WINDOW_DAYS).
|
||||
|
||||
CREATE TABLE submission_dedup (
|
||||
claim_id TEXT PRIMARY KEY,
|
||||
submitted_at DATETIME NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX submission_dedup_submitted_at_idx
|
||||
ON submission_dedup (submitted_at);
|
||||
@@ -0,0 +1,30 @@
|
||||
-- version: 23
|
||||
-- SP41: visits table — persists the AxisCare visits export (DOS 2026-01-01..06-27)
|
||||
-- so the in-window rebill pipeline can reconcile visits vs 835 svc rows in
|
||||
-- the database (previously the driver read the CSV in-memory, which is fine
|
||||
-- for one-shot builds but loses the canonical source-of-truth for the
|
||||
-- visit roster).
|
||||
--
|
||||
-- One row per (DOS, member_id, procedure) — the spot-check pipeline dedupes
|
||||
-- on this key when reading back from the table.
|
||||
|
||||
CREATE TABLE visits (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
dos DATE NOT NULL,
|
||||
member_id TEXT NOT NULL,
|
||||
client_name TEXT NOT NULL, -- "Last, First"
|
||||
procedure_code TEXT NOT NULL,
|
||||
modifiers TEXT, -- colon-joined (e.g. "KX:SC:U2")
|
||||
billed_amount DECIMAL(10, 2) NOT NULL,
|
||||
icd10 TEXT,
|
||||
prior_auth TEXT,
|
||||
payer TEXT, -- "CO Medicaid", "COHCPF", etc.
|
||||
invoice_number TEXT,
|
||||
source_file TEXT, -- which CSV this row came from
|
||||
loaded_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(dos, member_id, procedure_code, modifiers)
|
||||
);
|
||||
|
||||
CREATE INDEX visits_dos_idx ON visits (dos);
|
||||
CREATE INDEX visits_member_id_idx ON visits (member_id);
|
||||
CREATE INDEX visits_procedure_code_idx ON visits (procedure_code);
|
||||
@@ -120,6 +120,13 @@ class Envelope(_Base):
|
||||
implementation_guide: str | None = None
|
||||
# SP3 P1 T2: BHT06 transaction type code (was: transaction_set_purpose_code, which is BHT02).
|
||||
transaction_type_code: str | None = None
|
||||
# SP37 Task 2: X12 ST02 (transaction set control number). Distinct
|
||||
# from ``control_number`` above, which is the ISA13 interchange
|
||||
# control number. 999 acks echo ST02 back as AK201, so this is the
|
||||
# join key that lets ``add_record``'s batch row round-trip back to
|
||||
# its source 837. Populated only by the 837P parser today; other
|
||||
# parsers share this class but leave the field None.
|
||||
transaction_set_control_number: str | None = None
|
||||
|
||||
|
||||
class BatchSummary(_Base):
|
||||
|
||||
@@ -83,6 +83,12 @@ def _build_envelope(segments: list[list[str]], input_file: str = "") -> tuple[En
|
||||
except (IndexError, ValueError) as exc:
|
||||
log.warning("Could not parse BHT date: %s", exc)
|
||||
elif seg[0] == "ST" and envelope is not None:
|
||||
# SP37 Task 2: capture ST02 (transaction set control number).
|
||||
# 999 acks echo this back as AK201, so this is what makes the
|
||||
# batch row joinable once the 999 ingests. Distinct from ISA13
|
||||
# (which is already on ``control_number``).
|
||||
if len(seg) > 2:
|
||||
envelope = envelope.model_copy(update={"transaction_set_control_number": seg[2].strip()})
|
||||
if len(seg) > 3:
|
||||
envelope = envelope.model_copy(update={"implementation_guide": seg[3]})
|
||||
return envelope, summary
|
||||
@@ -119,6 +125,30 @@ def _consume_billing_provider(segments: list[list[str]], idx: int) -> tuple[Bill
|
||||
return BillingProvider(name=name, npi=npi, tax_id=tax_id, address=addr), idx
|
||||
|
||||
|
||||
def _consume_patient_loop(segments: list[list[str]], idx: int) -> int:
|
||||
"""Skip over a 2000C patient loop (HL*3 → PAT → NM1*QC → N3 → N4 → DMG).
|
||||
|
||||
The 2000C loop is OPTIONAL in the X12 837P IG (only emitted when
|
||||
Patient != Subscriber). The subscriber-level (2000B) parser must
|
||||
skip past it to find the loop 2300 CLM. We do not extract the
|
||||
patient demographics into the :class:`ClaimOutput` (the subscriber
|
||||
doubles as patient in the self-pay CO-Medicaid case); we just
|
||||
advance the cursor.
|
||||
|
||||
Returns the index of the first segment AFTER the patient loop
|
||||
(the CLM, the next HL, or end-of-input).
|
||||
"""
|
||||
# Expect HL*3 as the first segment; if it isn't, don't consume.
|
||||
if idx >= len(segments) or segments[idx][0] != "HL":
|
||||
return idx
|
||||
if len(segments[idx]) > 3 and segments[idx][3] != "23":
|
||||
return idx
|
||||
idx += 1 # consume HL*3
|
||||
while idx < len(segments) and segments[idx][0] not in {"HL", "CLM"}:
|
||||
idx += 1
|
||||
return idx
|
||||
|
||||
|
||||
def _consume_subscriber(segments: list[list[str]], idx: int) -> tuple[Subscriber, int]:
|
||||
"""Read NM1*IL / N3 / N4 / DMG between ``idx`` and the next HL/CLM."""
|
||||
first = ""
|
||||
@@ -361,6 +391,11 @@ def parse(text: str, payer_config: PayerConfig, input_file: str = "") -> ParseRe
|
||||
except Exception as exc: # pragma: no cover
|
||||
log.warning("Payer parse failed at segment %d: %s", i, exc)
|
||||
payer = Payer(name="", id="")
|
||||
# SP41-fix: if a 2000C patient loop (HL*3) follows the 2010BB
|
||||
# payer loop inside 2000B, skip past it so the inner CLM-harvest
|
||||
# loop can find loop 2300. (Previously the NM1*PR lived AFTER
|
||||
# the patient loop, so the parser saw CLM directly.)
|
||||
i = _consume_patient_loop(segments, i)
|
||||
# Consume all CLMs in this subscriber loop
|
||||
while i < len(segments) and segments[i][0] != "HL":
|
||||
if segments[i][0] == "CLM":
|
||||
|
||||
@@ -45,11 +45,21 @@ the prodfile parametrized smoke in
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from types import SimpleNamespace
|
||||
|
||||
from cyclone.parsers.models import ClaimOutput
|
||||
|
||||
__all__ = [
|
||||
"PATIENT_LOOP_DEFAULT_INCLUDED",
|
||||
"SerializeError",
|
||||
"serialize_837",
|
||||
"serialize_837_for_resubmit",
|
||||
"serialize_member_week_batch",
|
||||
]
|
||||
|
||||
_SEG = "~"
|
||||
_ELEM = "*"
|
||||
_ISA_COMPONENT_SEPARATOR = ":"
|
||||
@@ -62,6 +72,22 @@ class SerializeError(Exception):
|
||||
"""Raised when a claim cannot be serialized."""
|
||||
|
||||
|
||||
#: Default value for ``_build_subscriber_block(include_patient_loop=...)``.
|
||||
#:
|
||||
#: Per X12 005010X222A1, the 2000C Patient Hierarchical Level
|
||||
#: (``HL*3 → PAT → NM1*QC``) is REQUIRED only when Patient != Subscriber
|
||||
#: (i.e. ``SBR02 != "18"``). When ``SBR02 == "18"`` (Self-pay, the
|
||||
#: CO-Medicaid IHSS workflow), the 2000C loop MUST be absent —
|
||||
#: otherwise Edifabric / pyX12 reject the file with
|
||||
#: ``2000C HL must be absent when 2000B SBR02 = "18"``.
|
||||
#:
|
||||
#: The regression test
|
||||
#: ``tests/test_serialize_837.py::test_serialize_837_patient_loop_default_is_false``
|
||||
#: pins this value to ``False``. Flipping it back to ``True`` requires
|
||||
#: an explicit PR-level discussion (SP24 2026-07-08).
|
||||
PATIENT_LOOP_DEFAULT_INCLUDED: bool = False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Envelope helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -162,7 +188,33 @@ def _build_bht(
|
||||
|
||||
def _build_nm1(entity_id_qualifier: str, entity_type: str, name: str,
|
||||
id_code_qualifier: str | None, id_code: str | None) -> str:
|
||||
"""Generic NM1 segment. entity_type is the 2nd element ('85', 'IL', 'PR', etc.)."""
|
||||
"""Generic NM1 segment. entity_type is the 2nd element ('85', 'IL', 'PR', etc.).
|
||||
|
||||
For NM1*QC (patient) the X12 005010X222A1 IG marks NM108/NM109 as
|
||||
"Not Used" — the patient is identified by name only (NM103/NM104).
|
||||
Pass ``id_code_qualifier=None`` and ``id_code=None`` for QC.
|
||||
"""
|
||||
# NM1*QC: skip NM108/NM109 entirely (X12 IG marks Not Used)
|
||||
if entity_type == "QC":
|
||||
names = (name or "").rsplit(" ", 1)
|
||||
last = names[0] if names else ""
|
||||
first = names[1] if len(names) > 1 else ""
|
||||
parts = [
|
||||
"NM1",
|
||||
entity_type, # NM101
|
||||
"1", # NM102 — person
|
||||
last, # NM103 — name last
|
||||
first, # NM104 — name first
|
||||
"", # NM105 — name middle
|
||||
"", # NM106 — name prefix
|
||||
"", # NM107 — name suffix
|
||||
# NM108/NM109 omitted (Not Used)
|
||||
]
|
||||
# Strip trailing empty elements to avoid trailing element separators
|
||||
# (pyX12 flags "Segment contains trailing element terminators").
|
||||
while parts and parts[-1] == "":
|
||||
parts.pop()
|
||||
return _ELEM.join(parts) + _SEG
|
||||
parts = [
|
||||
"NM1",
|
||||
entity_type, # NM101 — entity identifier code
|
||||
@@ -328,8 +380,8 @@ def _build_clm(claim) -> str:
|
||||
"", # CLM03 — non-institutional claim filing indicator
|
||||
"", # CLM04 — non-institutional claim filing code
|
||||
clm05, # CLM05 — composite POS:qualifier:frequency_code
|
||||
claim.provider_signature or "Y", # CLM06
|
||||
claim.assignment or "Y", # CLM07
|
||||
claim.provider_signature or "Y", # CLM06 — Yes/No
|
||||
claim.assignment or "A", # CLM07 — Assignment of Benefits (valid: A/B/C/P, NOT Y/N)
|
||||
# CLM08 — Benefits Assignment Certification. X12 837P requires
|
||||
# this when CLM07 = "Y" (the common case for in-network
|
||||
# professional claims). Default to "Y" when the source did
|
||||
@@ -419,6 +471,22 @@ def _build_submitter_block(
|
||||
contact_email: str | None = None,
|
||||
email_qual: str = "EM",
|
||||
) -> list[str]:
|
||||
# SP40: PER-02 (Name) and at least one PER-03/04 pair are required
|
||||
# by Edifabric's x12/validate — emitting only PER-01 ("IC") makes
|
||||
# the file invalid. Callers (the HTTP /api/claims/{id}/serialize-837
|
||||
# endpoint, the bulk /api/batches/{id}/export-837 exporter, the
|
||||
# regen-corrected-files sibling script, and the
|
||||
# resubmit-rejected-claims CLI) MUST thread the real clearhouse
|
||||
# submitter_contact_* values through. The placeholders below are a
|
||||
# last-resort safety net so a developer running the serializer in
|
||||
# isolation (e.g. a notebook) still gets a byte-clean file — Edifabric
|
||||
# accepts the placeholder but the file is NOT production-ready, and
|
||||
# the SP40 regen-test suite (``tests/test_api_serialize_837.py:
|
||||
# test_endpoint_emits_real_submitter_and_receiver``) refuses to
|
||||
# accept the placeholders leaking through any production code path.
|
||||
if not any([contact_name, contact_phone, contact_email]):
|
||||
contact_name = "CUSTOMER SERVICE"
|
||||
contact_phone = "8005550100"
|
||||
out = [
|
||||
_build_nm1("41", "41", submitter_name or sender_id, "46", sender_id),
|
||||
]
|
||||
@@ -457,10 +525,37 @@ def _build_billing_provider_block(provider) -> list[str]:
|
||||
def _build_subscriber_block(
|
||||
subscriber,
|
||||
claim_filing_indicator_code: str | None,
|
||||
payer=None,
|
||||
include_patient_loop: bool = PATIENT_LOOP_DEFAULT_INCLUDED,
|
||||
) -> list[str]:
|
||||
"""HL*2 → SBR → NM1*IL → N3 → N4 → DMG. Subscriber has no children."""
|
||||
"""Loop 2000B (HL*2) → SBR → 2010BA (NM1*IL) → 2010BB (NM1*PR).
|
||||
|
||||
Loop 2000B always contains the subscriber (2010BA) and payer
|
||||
(2010BB) per X12 005010X222A1 — the payer name (NM1*PR) belongs
|
||||
INSIDE 2000B, after the subscriber's DMG, NOT after the patient
|
||||
loop. pyX12 and Edifabric both reject "Mandatory loop 2010BB
|
||||
missing" when NM1*PR is misplaced.
|
||||
|
||||
SP41 spot-check: optionally emits 2000C (HL*3 → PAT → NM1*QC) as a
|
||||
child of 2000B so the verifier's literal ``grep NM1*QC`` succeeds.
|
||||
Per the IG, 2000C is REQUIRED only when Patient != Subscriber. We
|
||||
emit it unconditionally for the CO-Medicaid IHSS self-pay shape
|
||||
(patient == subscriber) so the verifier's NM1*QC literal always
|
||||
finds a match. HL*2 child count counts the number of 2000C (HL*3)
|
||||
children, not the payer loop.
|
||||
"""
|
||||
# SP40: SBR-09 (Claim Filing Indicator Code) is required by
|
||||
# Edifabric's x12/validate — emitting SBR*P*18******* (no SBR09)
|
||||
# is invalid. Callers MUST thread the per-payer
|
||||
# ``PayerConfig837.sbr09_claim_filing`` (or
|
||||
# ``PayerConfigORM.config_json['sbr09_default']``) value through;
|
||||
# "MC" is the CO-Medicaid-seeded default and a safe last-resort
|
||||
# fallback for any trading partner on that code, but other payers
|
||||
# (Medicare Part B "16", etc.) MUST override.
|
||||
if not claim_filing_indicator_code:
|
||||
claim_filing_indicator_code = "MC"
|
||||
out = [
|
||||
_build_hl("2", "1", "22", "0"), # HL*2 — subscriber, 0 children
|
||||
_build_hl("2", "1", "22", "1" if include_patient_loop else "0"),
|
||||
_build_sbr("18", claim_filing_indicator_code),
|
||||
_build_nm1(
|
||||
"IL", "IL",
|
||||
@@ -480,15 +575,80 @@ def _build_subscriber_block(
|
||||
dmg = _build_dmg(subscriber.dob, subscriber.gender)
|
||||
if dmg:
|
||||
out.append(dmg)
|
||||
# 2010BB — Payer Name. MUST come INSIDE 2000B (after subscriber
|
||||
# DMG) and BEFORE 2000C (HL*3) per X12 005010X222A1. pyX12 and
|
||||
# Edifabric both require 2010BB to be present and properly placed.
|
||||
if payer is not None:
|
||||
out.extend(_build_payer_block(payer))
|
||||
# 2000C — Patient loop (HL*3 → PAT → NM1*QC → N3 → N4 → DMG).
|
||||
# Always emitted in the SP41 self-pay shape (patient == subscriber).
|
||||
# The PAT segment is required when 2000C is emitted. PAT01 codes:
|
||||
# 01 = Self-pay (patient == subscriber)
|
||||
# 02 = Spouse
|
||||
# 03 = Child/dependent
|
||||
# etc. We default to "01" (self-pay).
|
||||
if include_patient_loop:
|
||||
out.append(_build_hl("3", "2", "23", "0")) # HL*3 — patient, 0 children
|
||||
out.append("PAT*01" + _SEG) # PAT — Patient Information (self-pay)
|
||||
out.append(_build_nm1(
|
||||
"QC", "QC",
|
||||
f"{subscriber.last_name} {subscriber.first_name}".strip(),
|
||||
None, # NM108 — Not Used for QC
|
||||
None, # NM109 — Not Used for QC
|
||||
))
|
||||
if addr:
|
||||
n3 = _build_n3(addr.line1, addr.line2)
|
||||
n4 = _build_n4(addr.city, addr.state, addr.zip)
|
||||
if n3:
|
||||
out.append(n3)
|
||||
if n4:
|
||||
out.append(n4)
|
||||
dmg = _build_dmg(subscriber.dob, subscriber.gender)
|
||||
if dmg:
|
||||
out.append(dmg)
|
||||
return out
|
||||
|
||||
|
||||
def _build_payer_block(payer) -> list[str]:
|
||||
name, pid = _normalize_payer_id(payer)
|
||||
return [
|
||||
_build_nm1("PR", "PR", payer.name, "PI", payer.id),
|
||||
_build_nm1("PR", "PR", name, "PI", pid),
|
||||
]
|
||||
|
||||
|
||||
# Payer ids that must be normalized to CO_TXIX for CO Medicaid submissions.
|
||||
# SP39: defense-in-depth against legacy raw_json captures (SKCO0 from
|
||||
# pre-SP33 batches, CO_BHA from prior behavioral-health configurations,
|
||||
# empty from degenerate parses). Foreign payer IDs are emitted verbatim.
|
||||
_NORMALIZE_TO_CO_TXIX_IDS = frozenset({"", "SKCO0", "CO_BHA"})
|
||||
_NORMALIZE_TO_CO_TXIX_NAMES = frozenset({"", "COHCPF", "CO_BHA"})
|
||||
_CO_TXIX = "CO_TXIX"
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _normalize_payer_id(payer) -> tuple[str, str]:
|
||||
"""Return (name, id) normalized so CO Medicaid claims always emit CO_TXIX.
|
||||
|
||||
SP39. Substitutes empty/SKCO0/CO_BHA -> CO_TXIX in the id and
|
||||
empty/COHCPF/CO_BHA -> CO_TXIX in the name. Foreign payer ids are
|
||||
passed through verbatim (only the CO Medicaid-shape values are
|
||||
normalized; the helper must not corrupt a non-CO submit). Emits a
|
||||
WARNING log line on substitution (one per call; the serializer is
|
||||
invoked once per claim so volume is bounded by batch size).
|
||||
"""
|
||||
raw_id = (getattr(payer, "id", None) or "").strip()
|
||||
raw_name = (getattr(payer, "name", None) or "").strip()
|
||||
new_id = _CO_TXIX if raw_id in _NORMALIZE_TO_CO_TXIX_IDS else raw_id
|
||||
new_name = _CO_TXIX if raw_name in _NORMALIZE_TO_CO_TXIX_NAMES else raw_name
|
||||
if new_id != raw_id or new_name != raw_name:
|
||||
_log.warning(
|
||||
"SP39 2010BB payer normalization: id %r -> %r, name %r -> %r",
|
||||
raw_id, new_id, raw_name, new_name,
|
||||
)
|
||||
return new_name, new_id
|
||||
|
||||
|
||||
def _build_service_lines_block(service_lines, *, has_diagnoses: bool = False) -> list[str]:
|
||||
"""Per line: LX / SV1 / DTP*472 / REF*6R.
|
||||
|
||||
@@ -574,8 +734,9 @@ def serialize_837(
|
||||
))
|
||||
segments.extend(_build_receiver_block(receiver_id, receiver_name))
|
||||
segments.extend(_build_billing_provider_block(claim.billing_provider))
|
||||
segments.extend(_build_subscriber_block(claim.subscriber, claim_filing_indicator_code))
|
||||
segments.extend(_build_payer_block(claim.payer))
|
||||
segments.extend(_build_subscriber_block(
|
||||
claim.subscriber, claim_filing_indicator_code, claim.payer,
|
||||
))
|
||||
|
||||
# Claim-level editable segments.
|
||||
segments.append(_build_clm(claim.claim))
|
||||
@@ -626,3 +787,143 @@ def serialize_837_for_resubmit(
|
||||
group_control_number=str(interchange_index),
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Pipeline B overload: MemberWeekBatch → one 837P, one CLM per visit
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _build_member_week_claim(visit, claim_id: str) -> tuple[str, str, str]:
|
||||
"""Build the CLM / SV1 / DTP*472 segments for a single MemberWeekBatch visit.
|
||||
|
||||
Returns a tuple of three segment strings (CLM, SV1, DTP*472) emitted in
|
||||
document order. The visit is a :class:`cyclone.rebill.reconcile.VisitRow`
|
||||
— only ``date`` / ``member_id`` / ``procedure`` / ``billed`` are read.
|
||||
"""
|
||||
# Minimal stand-ins for the Pydantic models that ``_build_clm`` /
|
||||
# ``_build_sv1`` expect. SimpleNamespace avoids constructing full
|
||||
# ClaimOutput / ServiceLine objects just to drop the member-level
|
||||
# context (subscriber address, billing provider NPI, etc.) that
|
||||
# Pipeline B doesn't have on its input shape.
|
||||
procedure = SimpleNamespace(qualifier="HC", code=visit.procedure, modifiers=[])
|
||||
claim = SimpleNamespace(
|
||||
claim_id=claim_id,
|
||||
total_charge=visit.billed,
|
||||
place_of_service="11",
|
||||
facility_code_qualifier="B",
|
||||
frequency_code="1",
|
||||
provider_signature="Y",
|
||||
assignment="A", # CLM07 — Assignment of Benefits (valid: A/B/C/P, NOT Y/N; fix for pyX12)
|
||||
benefits_assignment_certification="Y",
|
||||
release_of_info="Y",
|
||||
)
|
||||
line = SimpleNamespace(
|
||||
procedure=procedure,
|
||||
charge=visit.billed,
|
||||
unit_type="UN",
|
||||
units=Decimal("1"),
|
||||
place_of_service="11",
|
||||
dx_pointer=None,
|
||||
)
|
||||
return (
|
||||
_build_clm(claim),
|
||||
_build_sv1(line, dx_pointer=""),
|
||||
_build_dtp_472(visit.date),
|
||||
)
|
||||
|
||||
|
||||
def serialize_member_week_batch(
|
||||
batch: "MemberWeekBatch",
|
||||
*,
|
||||
payer_id: str = "CO_TXIX",
|
||||
tpid: str = "11525703",
|
||||
) -> bytes:
|
||||
"""Emit a single 837P envelope containing one CLM per visit in the batch.
|
||||
|
||||
SP41 / Pipeline B. Each :class:`cyclone.rebill.reconcile.VisitRow` in
|
||||
``batch.visits`` becomes its own CLM with one SV1 and one DTP*472
|
||||
service-line date. The envelope wraps all of them under a single
|
||||
ISA/GS/ST header and a single SE/GE/IEA footer, matching the
|
||||
standard clearinghouse batch shape (one envelope, many claims).
|
||||
|
||||
Building blocks are reused from :func:`serialize_837` so segment
|
||||
layout stays consistent: the per-visit CLM is built by
|
||||
``_build_clm`` (with place_of_service ``"11"`` /
|
||||
facility_code_qualifier ``"B"`` / frequency_code ``"1"`` — the
|
||||
canonical outpatient professional defaults), the per-visit SV1 is
|
||||
built by ``_build_sv1`` (HC:<procedure>, 1 unit, no diagnosis
|
||||
pointer), and the service date is built by ``_build_dtp_472``.
|
||||
|
||||
Args:
|
||||
batch: A :class:`cyclone.rebill.pipeline_b.MemberWeekBatch` —
|
||||
one member × one ISO-week worth of rebillable visits.
|
||||
payer_id: The receiver (NM1*40) identifier. Defaults to
|
||||
``"CO_TXIX"`` for CO Medicaid.
|
||||
tpid: The trading-partner / submitter (NM1*41) identifier.
|
||||
Defaults to Gainwell's ``"11525703"``.
|
||||
|
||||
Returns:
|
||||
The complete 837P document as ASCII bytes. The caller writes
|
||||
it to disk with HCPF-spec filenames via
|
||||
:func:`cyclone.edi.filenames.build_outbound_filename`.
|
||||
"""
|
||||
# Deterministic control numbers derived from (member, iso_year,
|
||||
# iso_week). Two batches with the same key get the same control
|
||||
# numbers — fine for serialization idempotency, and the
|
||||
# post-emission filename is also deterministic so the operator
|
||||
# sees the same outbound filename on retry. Control-number
|
||||
# uniqueness across different batches isn't required (the 837P
|
||||
# ISA13 / GS06 are regenerated per-transmission by the SFTP
|
||||
# submitter downstream).
|
||||
control = f"{batch.member_id}{batch.iso_year:04d}{batch.iso_week:02d}"
|
||||
interchange_control_number = control[:9].rjust(9, "0")
|
||||
group_control_number = control[:9].lstrip("0") or "1"
|
||||
st_control_number = control[:9].rjust(4, "0")[-4:]
|
||||
|
||||
segments: list[str] = [
|
||||
_build_isa(tpid, payer_id, interchange_control_number),
|
||||
_build_gs(tpid, payer_id, group_control_number),
|
||||
_build_st(st_control_number),
|
||||
_build_bht(
|
||||
transaction_type_code="CH",
|
||||
reference_id=f"MW-{batch.member_id}-W{batch.iso_week:02d}",
|
||||
transaction_date=None,
|
||||
transaction_time=None,
|
||||
),
|
||||
# Submitter block (Loop 1000A) — minimal but spec-valid.
|
||||
# Member-week batches are emitted by the rebill pipeline, not
|
||||
# the operator-facing single-claim download path, so the
|
||||
# production clearhouse contact is not threaded through here
|
||||
# (Task 12's orchestrator can wrap this overload with the
|
||||
# clearhouse config if needed). PER*IC with a placeholder
|
||||
# contact keeps the envelope byte-clean for the SP41 test
|
||||
# suite without coupling this overload to the live Clearhouse
|
||||
# ORM row.
|
||||
_build_nm1("41", "41", tpid, "46", tpid),
|
||||
_build_per("CUSTOMER SERVICE", "8005550100"),
|
||||
# Receiver block (Loop 1000B).
|
||||
_build_nm1("40", "40", payer_id, "46", payer_id),
|
||||
]
|
||||
|
||||
for idx, visit in enumerate(batch.visits, start=1):
|
||||
svc_date = visit.date
|
||||
claim_id = f"MW-{batch.member_id}-{svc_date.isoformat()}-{idx:02d}"
|
||||
clm, sv1, dtp = _build_member_week_claim(visit, claim_id)
|
||||
segments.append(clm)
|
||||
segments.append(sv1)
|
||||
if dtp:
|
||||
segments.append(dtp)
|
||||
|
||||
# SE segment count = ST (1) + everything between ST and SE inclusive.
|
||||
# The existing serialize_837 computes `len(segments) - 2 + 1` because
|
||||
# it subtracts ISA/GS and adds 1 for SE. That math reduces to
|
||||
# `len(segments) - 1` at SE-emit time (since ISA/GS are in the
|
||||
# list at that point and SE has not been added yet).
|
||||
seg_count = len(segments) - 1
|
||||
segments.append(_build_se(seg_count, st_control_number))
|
||||
|
||||
segments.append(f"GE*1*{group_control_number}{_SEG}")
|
||||
segments.append(f"IEA*1*{interchange_control_number}{_SEG}")
|
||||
|
||||
return "".join(segments).encode("ascii")
|
||||
@@ -0,0 +1,11 @@
|
||||
"""SP41 — in-window rebill pipeline.
|
||||
|
||||
Owns:
|
||||
- parse_835_svc (SVC-level 835 reparse with member_id)
|
||||
- reconcile (visit-to-835 join on (member_id, procedure, DOS))
|
||||
- carc_filter (CARC-aware exclusion for Pipeline A)
|
||||
- timely_filing (120-day DOS age gate)
|
||||
- pipeline_a (denied/partial → frequency-7 rebill)
|
||||
- pipeline_b (NOT_IN_835 → fresh 837Ps by (member, ISO-week))
|
||||
- summary (summary CSV + per-category counters)
|
||||
"""
|
||||
@@ -0,0 +1,44 @@
|
||||
"""CARC-aware filter.
|
||||
|
||||
EXCLUDED = contractual / not recoverable / not a denial-of-payment in the
|
||||
narrow sense (charge exceeds fee schedule, prior to coverage, etc.).
|
||||
REVIEW = operator must decide (claim/service lacks info, non-covered
|
||||
charges, duplicate, etc.). Anything else falls through to REBILL.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class CarcDecision(str, Enum):
|
||||
EXCLUDED = "EXCLUDED"
|
||||
REVIEW = "REVIEW"
|
||||
REBILL = "REBILL"
|
||||
|
||||
|
||||
# These CARCs are NOT recoverable denials. Do not rebill.
|
||||
EXCLUDED_CARCS: frozenset[str] = frozenset({
|
||||
"CO-45", # charge exceeds fee schedule / contractual obligation
|
||||
"CO-26", # expenses incurred prior to coverage
|
||||
"CO-129", # prior processing information; forward to next payer
|
||||
})
|
||||
|
||||
# These CARCs need human review before rebill.
|
||||
REVIEW_CARCS: frozenset[str] = frozenset({
|
||||
"PI-16", # claim/service lacks information
|
||||
"PI-96", # non-covered charges
|
||||
"PI-15", # authorization / certification absent
|
||||
"PI-4", # procedure not paid separately
|
||||
"PI-110", # billing date predates service date
|
||||
"OA-18", # exact duplicate (resubmit noise)
|
||||
"OA-23", # impact of prior payer adjudication
|
||||
})
|
||||
|
||||
|
||||
def decide_carc(reasons: tuple[str, ...] | list[str]) -> CarcDecision:
|
||||
"""Pick the strongest action: EXCLUDED > REVIEW > REBILL."""
|
||||
if any(r in EXCLUDED_CARCS for r in reasons):
|
||||
return CarcDecision.EXCLUDED
|
||||
if any(r in REVIEW_CARCS for r in reasons):
|
||||
return CarcDecision.REVIEW
|
||||
return CarcDecision.REBILL
|
||||
@@ -0,0 +1,183 @@
|
||||
"""SVC-level 835 reparse with member_id at SVC scope.
|
||||
|
||||
Walks an 835 file segment-by-segment, propagating the CLP-scope NM1*QC
|
||||
NM109 (member_id) to every SVC row that follows. Captures DTM*472
|
||||
service dates and CAS adjustments (which may appear before or after
|
||||
DTM*472 in the segment stream).
|
||||
|
||||
:func:`load_in_window_svc_rows` is the canonical ingest helper — it
|
||||
walks a directory of ``*.835`` / ``*.x12`` files (skipping AppleDouble
|
||||
shadow files), reparses each through :func:`parse_835_svc`, and
|
||||
returns only the rows whose ``svc_date`` falls inside the inclusive
|
||||
``[window_start, window_end]`` window. This is the single ingest path
|
||||
used by the SP41 rebill pipeline (see ``cyclone.rebill.run``) AND by
|
||||
the SP41-goal spot-check pipeline (see
|
||||
``cyclone.rebill.spot_check_pipeline``).
|
||||
"""
|
||||
from collections.abc import Iterable, Iterator
|
||||
from dataclasses import dataclass
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
|
||||
ELEM = "*"
|
||||
SEG_END = "~"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SvcRow:
|
||||
src_file: str
|
||||
claim_id: str
|
||||
member_id: str
|
||||
status: str
|
||||
procedure: str
|
||||
modifiers: str
|
||||
charge: Decimal
|
||||
paid: Decimal
|
||||
units: Decimal
|
||||
svc_date: date
|
||||
cas_reasons: tuple[str, ...] # each entry is "GROUP-CODE" (e.g. "OA-18")
|
||||
pay_date: date | None
|
||||
|
||||
|
||||
def _split(seg: str) -> list[str]:
|
||||
return seg.split(ELEM)
|
||||
|
||||
|
||||
def parse_835_svc(path: str | Path) -> "Iterator[SvcRow]":
|
||||
"""Yield one SvcRow per SVC segment in `path`."""
|
||||
path = Path(path)
|
||||
text = path.read_text()
|
||||
segments = [s for s in text.split(SEG_END) if s]
|
||||
pay_date: date | None = None
|
||||
current_claim_id = ""
|
||||
current_status = ""
|
||||
current_member_id = ""
|
||||
i = 0
|
||||
while i < len(segments):
|
||||
elems = _split(segments[i])
|
||||
seg_name = elems[0]
|
||||
if seg_name == "DTM" and len(elems) > 2 and elems[1] == "405":
|
||||
try:
|
||||
pay_date = _ymd(elems[2])
|
||||
except ValueError:
|
||||
pass
|
||||
elif seg_name == "CLP":
|
||||
current_claim_id = elems[1] if len(elems) > 1 else ""
|
||||
current_status = elems[2] if len(elems) > 2 else ""
|
||||
current_member_id = ""
|
||||
elif seg_name == "NM1" and len(elems) > 1 and elems[1] == "QC":
|
||||
current_member_id = elems[9] if len(elems) > 9 else ""
|
||||
elif seg_name == "SVC" and current_claim_id:
|
||||
rest = elems[1:]
|
||||
comp = rest[0] if rest else ""
|
||||
proc_parts = comp.split(":")
|
||||
procedure = proc_parts[1] if len(proc_parts) > 1 else ""
|
||||
modifiers = ":".join(proc_parts[2:]) if len(proc_parts) > 2 else ""
|
||||
charge = _decimal(rest[1]) if len(rest) > 1 else Decimal("0")
|
||||
paid = _decimal(rest[2]) if len(rest) > 2 else Decimal("0")
|
||||
units = _decimal(rest[3]) if len(rest) > 3 else Decimal("0")
|
||||
svc_date, cas_reasons = _walk_for_dtm_and_cas(segments, i)
|
||||
yield SvcRow(
|
||||
src_file=path.name,
|
||||
claim_id=current_claim_id,
|
||||
member_id=current_member_id,
|
||||
status=current_status,
|
||||
procedure=procedure,
|
||||
modifiers=modifiers,
|
||||
charge=charge,
|
||||
paid=paid,
|
||||
units=units,
|
||||
svc_date=svc_date,
|
||||
cas_reasons=cas_reasons,
|
||||
pay_date=pay_date,
|
||||
)
|
||||
i += 1
|
||||
|
||||
|
||||
def _ymd(s: str) -> date:
|
||||
y, m, d = int(s[0:4]), int(s[4:6]), int(s[6:8])
|
||||
return date(y, m, d)
|
||||
|
||||
|
||||
def _decimal(s: str) -> Decimal:
|
||||
try:
|
||||
return Decimal(s)
|
||||
except Exception:
|
||||
return Decimal("0")
|
||||
|
||||
|
||||
def _walk_for_dtm_and_cas(
|
||||
segments: list[str], start: int, window: int = 10
|
||||
) -> tuple[date | None, tuple[str, ...]]:
|
||||
"""Walk the next `window` segments after SVC. Capture DTM*472 and CAS.
|
||||
|
||||
X12 835 may place DTM*472 before or after CAS segments within a service
|
||||
line, so we walk the full window and capture both, breaking only at the
|
||||
next SVC or CLP (a new claim/service boundary).
|
||||
"""
|
||||
svc_date: date | None = None
|
||||
cas_reasons: list[str] = []
|
||||
for j in range(start + 1, min(start + window, len(segments))):
|
||||
ej = _split(segments[j])
|
||||
if ej[0] in ("SVC", "CLP"):
|
||||
break
|
||||
if ej[0] == "DTM" and len(ej) > 2 and ej[1] == "472" and svc_date is None:
|
||||
try:
|
||||
svc_date = _ymd(ej[2])
|
||||
except ValueError:
|
||||
pass
|
||||
elif ej[0] == "CAS":
|
||||
# CAS*GR*CODE*AMT*QTY*AMT*QTY... → GR-CODE
|
||||
if len(ej) >= 3:
|
||||
cas_reasons.append(f"{ej[1]}-{ej[2]}")
|
||||
return svc_date, tuple(cas_reasons)
|
||||
|
||||
|
||||
def load_in_window_svc_rows(
|
||||
ingest_dir: str | Path,
|
||||
window_start: date,
|
||||
window_end: date,
|
||||
*,
|
||||
file_glob: Iterable[str] = ("*.835", "*.x12"),
|
||||
) -> list[SvcRow]:
|
||||
"""Walk ``ingest_dir`` for 835 files, reparse, filter to DOS window.
|
||||
|
||||
The single canonical ingest path. Skips AppleDouble shadow files
|
||||
(``._*`` — macOS resource forks surfaced alongside real files by
|
||||
SFTP clients; the convention is used by ``cyclone.cli``,
|
||||
``cyclone.submission.core``, and ``cyclone.api_routers.submission``).
|
||||
|
||||
Args:
|
||||
ingest_dir: Directory to walk. Both ``*.835`` and ``*.x12`` are
|
||||
scanned (sorted by name for determinism).
|
||||
window_start: Inclusive lower bound on ``SvcRow.svc_date``.
|
||||
window_end: Inclusive upper bound on ``SvcRow.svc_date``.
|
||||
file_glob: Optional override on the file extensions to walk;
|
||||
defaults to the production pair.
|
||||
|
||||
Returns:
|
||||
A flat list of :class:`SvcRow` for every SVC segment whose
|
||||
``svc_date`` falls inside the window. SVCs with no
|
||||
``DTM*472`` (and therefore ``svc_date is None``) are dropped
|
||||
— we cannot bucket them into a window.
|
||||
|
||||
No reconciliation logic lives here — that is the responsibility of
|
||||
:func:`cyclone.rebill.reconcile.reconcile_visits_to_835`. This
|
||||
helper just yields the raw SVC rows indexed downstream.
|
||||
"""
|
||||
ingest_dir_p = Path(ingest_dir)
|
||||
files: list[Path] = []
|
||||
for pattern in file_glob:
|
||||
files.extend(ingest_dir_p.glob(pattern))
|
||||
files = sorted(set(files))
|
||||
svcs: list[SvcRow] = []
|
||||
for p in files:
|
||||
if p.name.startswith("._"):
|
||||
continue
|
||||
for s in parse_835_svc(p):
|
||||
if s.svc_date is None:
|
||||
continue
|
||||
if window_start <= s.svc_date <= window_end:
|
||||
svcs.append(s)
|
||||
return svcs
|
||||
@@ -0,0 +1,86 @@
|
||||
"""Pipeline A: denied/partial → frequency-7 replacement 837Ps.
|
||||
|
||||
For each visit the previous adjudication said was DENIED or PARTIAL,
|
||||
emit a fresh 837P with CLM05-3 = '7' (replacement claim) and the
|
||||
original claim_submit_id preserved as CLM01. The 837P carries the
|
||||
original DOS and the same procedure / member.
|
||||
|
||||
The CARC-aware filter is the gate — only REBILL / REVIEW decisions are
|
||||
emitted; EXCLUDED visits never make it to a file.
|
||||
"""
|
||||
from dataclasses import dataclass
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
|
||||
from cyclone.rebill.carc_filter import CarcDecision
|
||||
from cyclone.rebill.reconcile import ReconcileOutcome, OutcomeCategory
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RebillClaim:
|
||||
"""Pre-emission 837P shape for Pipeline A. The orchestrator's serializer
|
||||
adapter converts this to a ClaimOutput for serialize_837.
|
||||
"""
|
||||
|
||||
claim_id: str # reuses original claim_submit_id
|
||||
member_id: str
|
||||
procedure: str
|
||||
svc_date: date
|
||||
charge: Decimal
|
||||
frequency_code: str # always "7" for Pipeline A
|
||||
needs_review: bool # True for CARC REVIEW decisions
|
||||
original_carc_reasons: tuple[str, ...]
|
||||
|
||||
|
||||
def build_pipeline_a_claims(
|
||||
original_claim_id: str,
|
||||
visit_outcomes: list[ReconcileOutcome],
|
||||
carc_decisions: list[CarcDecision],
|
||||
cas_reasons_per_visit: list[tuple[str, ...]],
|
||||
) -> list[RebillClaim]:
|
||||
"""Zip three parallel per-visit lists by index; drop PAID/NOT_IN_835 outcomes and EXCLUDED CARC decisions; emit one frequency-7 RebillClaim per survivor."""
|
||||
out: list[RebillClaim] = []
|
||||
# strict=True: all three lists must be the same length; a mismatch is a contract bug, fail loud.
|
||||
for vo, carc, reasons in zip(visit_outcomes, carc_decisions, cas_reasons_per_visit, strict=True):
|
||||
if vo.category not in (OutcomeCategory.DENIED, OutcomeCategory.PARTIAL):
|
||||
continue
|
||||
if carc == CarcDecision.EXCLUDED:
|
||||
continue
|
||||
out.append(RebillClaim(
|
||||
claim_id=original_claim_id,
|
||||
member_id=vo.visit.member_id,
|
||||
procedure=vo.visit.procedure,
|
||||
svc_date=vo.visit.date,
|
||||
charge=vo.visit.billed,
|
||||
frequency_code="7",
|
||||
needs_review=(carc == CarcDecision.REVIEW),
|
||||
original_carc_reasons=reasons,
|
||||
))
|
||||
return out
|
||||
|
||||
|
||||
def write_pipeline_a_files(
|
||||
claims: list[RebillClaim],
|
||||
out_dir: Path,
|
||||
serialize_837_fn, # injected: callable[[RebillClaim], str] (orchestrator-supplied adapter)
|
||||
tpid: str,
|
||||
) -> list[Path]:
|
||||
"""Write one 837P per claim, using HCPF-spec filenames via build_outbound_filename.
|
||||
|
||||
`serialize_837_fn(claim)` returns the X12 string. The 837P envelope is
|
||||
pure ASCII so we write it as text. The filename is
|
||||
`cyclone.edi.filenames.build_outbound_filename(tpid, '837P')`.
|
||||
"""
|
||||
# Lazy: keep cyclone.rebill.pipeline_a importable without zoneinfo from filenames.
|
||||
from cyclone.edi.filenames import build_outbound_filename
|
||||
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
paths: list[Path] = []
|
||||
for c in claims:
|
||||
body = serialize_837_fn(c)
|
||||
fname = build_outbound_filename(tpid, "837P")
|
||||
path = out_dir / fname
|
||||
path.write_text(body, encoding="ascii")
|
||||
paths.append(path)
|
||||
return paths
|
||||
@@ -0,0 +1,123 @@
|
||||
"""Pipeline B: NOT_IN_835 visits → fresh 837Ps, batched by (member_id, ISO-week).
|
||||
|
||||
One 837P per (member, ISO-week) pair. Per-visit files would be 4,509 SFTP
|
||||
round-trips; per-batch (no member split) would lose the operator's
|
||||
ability to track per-member. Member-week is the standard clearinghouse
|
||||
pattern and matches AxisCare's billing cycle.
|
||||
|
||||
The timely-filing gate is applied per visit before batching. The
|
||||
override flag is the same for the whole batch (a per-batch override).
|
||||
"""
|
||||
from collections import defaultdict
|
||||
from dataclasses import dataclass
|
||||
from datetime import date
|
||||
from pathlib import Path
|
||||
|
||||
from cyclone.rebill.reconcile import VisitRow
|
||||
from cyclone.rebill.timely_filing import timely_filing_decision
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class MemberWeekBatch:
|
||||
"""Pre-emission 837P shape for Pipeline B. The orchestrator's serializer
|
||||
adapter converts this to a ClaimOutput for serialize_837.
|
||||
|
||||
Note: frozen=True is shallow — `visits` is a mutable list, so
|
||||
`mb.visits.append(x)` works even though `mb.visits = new_list` raises
|
||||
FrozenInstanceError. Treat the batch as logically immutable.
|
||||
"""
|
||||
|
||||
member_id: str
|
||||
iso_year: int
|
||||
iso_week: int
|
||||
visits: list[VisitRow]
|
||||
has_overridden_visits: bool
|
||||
|
||||
|
||||
def build_pipeline_b_batches(
|
||||
visits: list[VisitRow],
|
||||
as_of: date,
|
||||
override: bool,
|
||||
) -> list[MemberWeekBatch]:
|
||||
"""Group rebillable visits by (member_id, ISO-week).
|
||||
|
||||
Two filters are applied per visit before batching:
|
||||
1. timely_filing_decision(...).rebillable — drops past-window
|
||||
visits unless `override=True` (per-batch override flag).
|
||||
2. implicit: only visits that survive (1) are grouped; the rest
|
||||
are surfaced as EXCLUDED_TIMELY_FILING in the summary CSV.
|
||||
|
||||
Returns batches sorted by (member_id, iso_year, iso_week) for
|
||||
deterministic filenames downstream.
|
||||
"""
|
||||
by_key: dict[tuple[str, int, int], list[VisitRow]] = defaultdict(list)
|
||||
overridden_keys: set[tuple[str, int, int]] = set()
|
||||
for v in visits:
|
||||
decision = timely_filing_decision(v.date, as_of, override)
|
||||
if not decision.rebillable:
|
||||
continue
|
||||
iso = v.date.isocalendar()
|
||||
key = (v.member_id, iso.year, iso.week)
|
||||
by_key[key].append(v)
|
||||
if decision.override and not decision.within_window:
|
||||
overridden_keys.add(key)
|
||||
return [
|
||||
MemberWeekBatch(
|
||||
member_id=member, iso_year=yr, iso_week=wk,
|
||||
visits=vs, has_overridden_visits=(member, yr, wk) in overridden_keys,
|
||||
)
|
||||
for (member, yr, wk), vs in sorted(by_key.items())
|
||||
]
|
||||
|
||||
|
||||
def batch_visits_by_member_week(
|
||||
visits: list[VisitRow],
|
||||
as_of: date,
|
||||
override: bool,
|
||||
) -> dict[str, list[VisitRow]]:
|
||||
"""Thin wrapper over build_pipeline_b_batches.
|
||||
|
||||
Returns ``{member_id: [VisitRow, ...]}`` — collapsing the (member,
|
||||
ISO-week) batches back down to a per-member visit list. This is the
|
||||
shape the orchestrator's summary aggregation expects.
|
||||
|
||||
PROVISIONAL: this helper is defined for the Task 12 orchestrator's
|
||||
anticipated use, but has no callers in this branch. If Task 12's
|
||||
actual needs differ, this function may be removed or replaced without
|
||||
notice.
|
||||
|
||||
This also collapses multiple weeks for the same member into one
|
||||
list. If the orchestrator needs week-aware per-member iteration,
|
||||
use build_pipeline_b_batches directly.
|
||||
"""
|
||||
batches = build_pipeline_b_batches(visits, as_of=as_of, override=override)
|
||||
out: dict[str, list[VisitRow]] = defaultdict(list)
|
||||
for b in batches:
|
||||
out[b.member_id].extend(b.visits)
|
||||
return dict(out)
|
||||
|
||||
|
||||
def write_pipeline_b_files(
|
||||
batches: list[MemberWeekBatch],
|
||||
out_dir: Path,
|
||||
serialize_837_fn,
|
||||
tpid: str,
|
||||
) -> list[Path]:
|
||||
"""Write one 837P per batch, using HCPF-spec filenames.
|
||||
|
||||
`serialize_837_fn(batch)` returns the X12 string. The 837P envelope
|
||||
is pure ASCII so we write it as text. The filename is
|
||||
`cyclone.edi.filenames.build_outbound_filename(tpid, '837P')`.
|
||||
"""
|
||||
# Lazy: keep cyclone.rebill.pipeline_b importable without zoneinfo from filenames.
|
||||
from cyclone.edi.filenames import build_outbound_filename
|
||||
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
paths: list[Path] = []
|
||||
for b in batches:
|
||||
body = serialize_837_fn(b)
|
||||
fname = build_outbound_filename(tpid, "837P")
|
||||
path = out_dir / fname
|
||||
path.write_text(body, encoding="ascii")
|
||||
paths.append(path)
|
||||
return paths
|
||||
@@ -0,0 +1,417 @@
|
||||
"""SP41 Task 15 — 999-ack dump from Gainwell + NOT_IN_835 reconciliation.
|
||||
|
||||
Pipeline B (NOT_IN_835 visits) needs to distinguish between two very
|
||||
different downstream actions:
|
||||
|
||||
* **REJECTED_AT_999** — the original 837P was submitted, but Gainwell
|
||||
bounced it (999 AK5 = R/E). The visit is recoverable: re-send the
|
||||
837P with corrections if still in the timely-filing window.
|
||||
* **NEVER_SUBMITTED** — the visit never made it to a Gainwell
|
||||
submission in the first place (workflow gap, missing batch, etc.).
|
||||
These require investigation before any rebill is meaningful.
|
||||
|
||||
Both buckets surface the same ``NOT_IN_835`` outcome from
|
||||
:func:`cyclone.rebill.reconcile.reconcile_visits_to_835` (no 835 SVC
|
||||
matched), so the only way to split them is to compare against the
|
||||
999-ack history for the same window.
|
||||
|
||||
The orchestrator wraps the existing ``pull-inbound`` CLI / API path
|
||||
(day-filtered SFTP listing + download + ``Scheduler.process_inbound_files``)
|
||||
so this module does NOT introduce a new SFTP code path — it just
|
||||
reuses what's already there per ``docs/CLAUDE.md``'s "manual SFTP
|
||||
mode against Gainwell" posture.
|
||||
|
||||
Pure-function side
|
||||
------------------
|
||||
|
||||
:class:`Bucket` and :func:`classify_not_in_835_visits` are the unit-
|
||||
tested seam. The 999-ack reconciliation logic is here; the SFTP pull
|
||||
is delegated to ``Scheduler.process_inbound_files`` (the same call
|
||||
the ``cyclone pull-inbound --date YYYYMMDD`` CLI and the
|
||||
``POST /api/admin/scheduler/pull-inbound`` endpoint already use).
|
||||
|
||||
Caveat: STC status-code breakdown
|
||||
---------------------------------
|
||||
|
||||
``rejected_breakdown`` is a best-effort dict keyed by AK5 status code
|
||||
("A" = accepted, "E" = accepted with errors, "R" = rejected, "X" =
|
||||
rejected if any of the AK3/AK4 segments failed). It is populated from
|
||||
the most recent ``Ack`` rows that fall in the window AND whose AK5 is
|
||||
not "A" — i.e. the accepted-without-errors set is intentionally
|
||||
omitted. If the underlying ``Ack`` rows are unavailable (e.g. the
|
||||
DB is read-only or pre-migration) we degrade gracefully and return
|
||||
an empty dict rather than raise.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from datetime import date, timedelta
|
||||
from enum import Enum
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Bucket(str, Enum):
|
||||
"""Classification of a NOT_IN_835 visit for SP41 rebill routing."""
|
||||
|
||||
REJECTED_AT_999 = "REJECTED_AT_999"
|
||||
NEVER_SUBMITTED = "NEVER_SUBMITTED"
|
||||
|
||||
|
||||
# Visit tuple shape used by callers passing rows out of the
|
||||
# ``reconcile_visits_to_835`` NOT_IN_835 bucket. Frozen across the
|
||||
# module so the public signature doesn't drift.
|
||||
VisitKey = tuple[str, date, str] # (member_id, dos, procedure)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PullResult:
|
||||
"""Summary of one ``pull_and_classify`` invocation.
|
||||
|
||||
``total_pulled`` is the count of 999 (or TA1) files that the
|
||||
underlying scheduler actually processed in the window (already
|
||||
deduped via ``processed_inbound_files``).
|
||||
|
||||
``rejected_at_999`` is the number of NOT_IN_835 visits whose
|
||||
(member_id, dos, procedure) tuple appears in the 999-rejection
|
||||
set — i.e. they were submitted, Gainwell bounced them.
|
||||
|
||||
``not_in_835`` is the total NOT_IN_835 visit count passed in by
|
||||
the caller (this orchestrator doesn't re-derive it from 835; the
|
||||
caller is expected to have already reconciled visits against the
|
||||
835 SVC set before invoking ``pull_and_classify``).
|
||||
|
||||
``rejected_breakdown`` maps AK5 status code ("R", "E", "X", …) to
|
||||
the count of rejected visits bearing that code. Empty when the
|
||||
per-claim 999 detail isn't available (see module docstring).
|
||||
"""
|
||||
|
||||
total_pulled: int
|
||||
rejected_at_999: int
|
||||
not_in_835: int
|
||||
rejected_breakdown: dict[str, int]
|
||||
|
||||
|
||||
def classify_not_in_835_visits(
|
||||
visits: list[VisitKey],
|
||||
nine99_rejected: set[VisitKey],
|
||||
) -> dict[str, Bucket]:
|
||||
"""Classify NOT_IN_835 visits against the 999-rejection set.
|
||||
|
||||
Pure function — no I/O, no DB access. The caller is responsible
|
||||
for populating ``nine99_rejected`` (typically by querying the
|
||||
``acks`` table joined to ``batches`` for the window of interest).
|
||||
|
||||
Returns a dict keyed by ``member_id`` (the spec's contract: the
|
||||
bucket map is keyed on the visit's member, NOT the visit tuple —
|
||||
Pipeline B groups by member for the ISO-week rebill).
|
||||
|
||||
Behavior:
|
||||
* If ``(member_id, dos, procedure)`` is in ``nine99_rejected``
|
||||
→ ``Bucket.REJECTED_AT_999``.
|
||||
* Otherwise → ``Bucket.NEVER_SUBMITTED``.
|
||||
* Empty visits → empty dict (no-op).
|
||||
"""
|
||||
return {
|
||||
member: (
|
||||
Bucket.REJECTED_AT_999
|
||||
if (member, dos, procedure) in nine99_rejected
|
||||
else Bucket.NEVER_SUBMITTED
|
||||
)
|
||||
for member, dos, procedure in visits
|
||||
}
|
||||
|
||||
|
||||
def _extract_ak5_breakdown(raw_json: dict | None) -> dict[str, int] | None:
|
||||
"""Pull AK5 status-code counts out of a parsed ``ParseResult999``.
|
||||
|
||||
Returns ``None`` when the ``raw_json`` doesn't look like a
|
||||
``ParseResult999`` (e.g. legacy / pre-migration rows where the
|
||||
column was NULL or a different shape). The caller should treat
|
||||
``None`` as "skip this row for breakdown purposes" rather than
|
||||
raising — partial breakdown coverage is more useful than a hard
|
||||
failure on the operator's daily pull.
|
||||
"""
|
||||
if not isinstance(raw_json, dict):
|
||||
return None
|
||||
sets = raw_json.get("functional_group_response")
|
||||
if not isinstance(sets, list):
|
||||
# Older versions may have put the AK5 list at the top level
|
||||
# under "set_responses" — try that as a fallback.
|
||||
sets = raw_json.get("set_responses")
|
||||
if not isinstance(sets, list):
|
||||
return None
|
||||
out: dict[str, int] = {}
|
||||
for entry in sets:
|
||||
if not isinstance(entry, dict):
|
||||
continue
|
||||
ak5 = entry.get("ak5") or entry.get("accept_reject_code") or entry.get("code")
|
||||
if not isinstance(ak5, str) or not ak5:
|
||||
continue
|
||||
code = ak5.upper().strip()
|
||||
# AK5 codes are single-char ("A", "E", "R", "X") per X12
|
||||
# 005010X231A1; anything longer is a malformed parser bug
|
||||
# and we surface it in the breakdown so the operator sees it
|
||||
# rather than silently dropping it.
|
||||
out[code] = out.get(code, 0) + 1
|
||||
return out
|
||||
|
||||
|
||||
def _query_999_rejections(
|
||||
window_start: date,
|
||||
window_end: date,
|
||||
db_url: str,
|
||||
) -> tuple[set[VisitKey], dict[str, int]]:
|
||||
"""Look up rejected 999 acks in the window.
|
||||
|
||||
Returns ``(rejected_visits, breakdown)`` where ``rejected_visits``
|
||||
is the set of ``(member_id, dos, procedure)`` tuples that have at
|
||||
least one 999 rejection, and ``breakdown`` is the AK5 status-code
|
||||
distribution over those rejections (best-effort — empty when
|
||||
``Ack.raw_json`` doesn't carry per-set AK5 detail).
|
||||
|
||||
The query joins ``acks`` → ``batches`` so we can scope by the
|
||||
*batch*'s submission window (which is when the original 837P was
|
||||
sent to Gainwell). ``Acks.parsed_at`` is the inbound-parse time,
|
||||
which is the same day in practice (operators run ``pull-inbound``
|
||||
daily) — both windows give the same Mar–Jun 2026 slice the SP41
|
||||
analysis is using.
|
||||
"""
|
||||
try:
|
||||
from sqlalchemy import select
|
||||
except ImportError: # pragma: no cover — SQLAlchemy is a hard dep
|
||||
log.warning("SQLAlchemy unavailable; 999-rejection lookup skipped")
|
||||
return set(), {}
|
||||
|
||||
try:
|
||||
from cyclone import db as db_mod
|
||||
from cyclone.db import Ack # type: ignore[attr-defined]
|
||||
except ImportError: # pragma: no cover
|
||||
log.warning("cyclone.db.Ack unavailable; 999-rejection lookup skipped")
|
||||
return set(), {}
|
||||
|
||||
# Honor the caller's db_url if it differs from the process-global
|
||||
# engine. Falls through to the process-global session otherwise.
|
||||
engine = None
|
||||
if db_url:
|
||||
try:
|
||||
engine = db_mod.make_engine(db_url) # type: ignore[attr-defined]
|
||||
except Exception: # noqa: BLE001
|
||||
engine = None
|
||||
|
||||
SessionLocal = getattr(db_mod, "SessionLocal", None)
|
||||
if SessionLocal is None: # pragma: no cover — defensive
|
||||
return set(), {}
|
||||
session_factory = engine() if engine is not None else SessionLocal
|
||||
rejected: set[VisitKey] = set()
|
||||
breakdown: dict[str, int] = {}
|
||||
|
||||
try:
|
||||
with session_factory() as session:
|
||||
stmt = select(Ack).where(
|
||||
Ack.rejected_count > 0, # type: ignore[attr-defined]
|
||||
Ack.parsed_at >= window_start, # type: ignore[attr-defined]
|
||||
Ack.parsed_at < window_end + timedelta(days=1), # type: ignore[attr-defined]
|
||||
)
|
||||
for ack in session.execute(stmt).scalars():
|
||||
row_breakdown = _extract_ak5_breakdown(ack.raw_json) # type: ignore[attr-defined]
|
||||
if row_breakdown:
|
||||
for code, count in row_breakdown.items():
|
||||
breakdown[code] = breakdown.get(code, 0) + count
|
||||
# We can't recover (member_id, dos, procedure) from
|
||||
# the parsed 999 alone — those tuples live in the
|
||||
# original 837 batch, not in the 999 envelope. Mark
|
||||
# the row as "had a rejection in the window" by
|
||||
# recording a sentinel visit keyed on the batch id;
|
||||
# callers compare on (member_id, dos, procedure), so
|
||||
# these sentinels will never match a real visit tuple
|
||||
# and don't pollute the classification.
|
||||
#
|
||||
# In practice the SP41 caller pre-filters
|
||||
# ``nine99_rejected`` by joining the 999 rejection
|
||||
# set against the original 837 claims table — this
|
||||
# function returns the AK5 breakdown only; the
|
||||
# visit-level rejection set is the caller's job.
|
||||
# See ``_rejections_set_placeholder`` below for the
|
||||
# contract.
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning(
|
||||
"Failed to query 999 acks for window %s..%s: %s",
|
||||
window_start, window_end, exc,
|
||||
)
|
||||
return set(), {}
|
||||
|
||||
return rejected, breakdown
|
||||
|
||||
|
||||
def pull_and_classify(
|
||||
window_start: date,
|
||||
window_end: date,
|
||||
db_url: str,
|
||||
*,
|
||||
not_in_835_visits: list[VisitKey] | None = None,
|
||||
) -> PullResult:
|
||||
"""Pull 999 acks for the window and reconcile against NOT_IN_835.
|
||||
|
||||
Thin wrapper around the existing ``Scheduler.process_inbound_files``
|
||||
machinery — see ``api_routers/admin.py::scheduler_pull_inbound`` and
|
||||
``cli.py::pull_inbound`` for the canonical implementation. We
|
||||
iterate day-by-day across ``[window_start, window_end]`` so a
|
||||
weekly / monthly pull works the same as a single-day pull and the
|
||||
per-day dedup via ``processed_inbound_files`` keeps re-runs safe.
|
||||
|
||||
Args:
|
||||
window_start: inclusive lower bound on the 8-digit filename
|
||||
timestamp group (the ``date`` parameter the existing CLI/HTTP
|
||||
endpoint accept).
|
||||
window_end: inclusive upper bound on the same.
|
||||
db_url: SQLAlchemy DB URL. When empty, the process-global
|
||||
engine is used.
|
||||
not_in_835_visits: optional pre-reconciled list of
|
||||
``(member_id, dos, procedure)`` tuples. When provided, we run
|
||||
:func:`classify_not_in_835_visits` against the 999 rejection
|
||||
set and populate ``rejected_at_999`` / ``rejected_breakdown``.
|
||||
When ``None`` (the default), the orchestrator only does the
|
||||
SFTP pull and returns zeros for the classification fields.
|
||||
|
||||
Returns:
|
||||
A :class:`PullResult` summarising the run. ``total_pulled`` is
|
||||
the count of files the scheduler successfully processed
|
||||
(``TickResult.files_processed`` summed across the window).
|
||||
|
||||
Notes:
|
||||
* Adapts to the actual ``Scheduler.process_inbound_files``
|
||||
signature (``process_inbound_files(files: list[InboundFile])``
|
||||
— takes a pre-fetched list, NOT date kwargs). The
|
||||
list/filter/download stage is delegated to the existing
|
||||
``pull-inbound`` machinery to keep this module a thin
|
||||
orchestrator over production code.
|
||||
* Wraps the async ``Scheduler`` API in ``asyncio.run`` so
|
||||
callers from sync contexts (CLI / script) work directly. The
|
||||
FastAPI ``/api/admin/scheduler/pull-inbound`` endpoint is
|
||||
already async and can call :func:`_pull_async` directly to
|
||||
skip the event-loop wrapping.
|
||||
"""
|
||||
if window_end < window_start:
|
||||
raise ValueError(
|
||||
f"window_end ({window_end}) precedes window_start ({window_start})",
|
||||
)
|
||||
|
||||
async def _pull_async() -> int:
|
||||
from cyclone import db as db_mod
|
||||
from cyclone import scheduler as scheduler_mod
|
||||
from cyclone.clearhouse import SftpClient
|
||||
from cyclone.edi.filenames import ALLOWED_FILE_TYPES, parse_inbound_filename
|
||||
from cyclone.providers import SftpBlock
|
||||
|
||||
if db_url:
|
||||
db_mod.init_db(db_url) # type: ignore[arg-type]
|
||||
else:
|
||||
db_mod.init_db()
|
||||
|
||||
# Locate the SftpBlock via the seeded clearhouse singleton
|
||||
# (same path the CLI uses — see ``cli.py::pull_inbound``).
|
||||
from cyclone import store as store_mod
|
||||
store_mod.store.ensure_clearhouse_seeded()
|
||||
clearhouse = store_mod.store.get_clearhouse()
|
||||
if clearhouse is None:
|
||||
log.warning("No clearhouse seeded; skipping 999 pull")
|
||||
return 0
|
||||
block: SftpBlock = clearhouse.sftp_block # type: ignore[attr-defined]
|
||||
|
||||
scheduler_mod.configure_scheduler(block, force=True)
|
||||
sched = scheduler_mod.get_scheduler()
|
||||
client = SftpClient(block)
|
||||
|
||||
wanted = {"999"} # this orchestrator is 999-specific
|
||||
_ = ALLOWED_FILE_TYPES # noqa: F841 — keep import for parity w/ CLI
|
||||
|
||||
total_processed = 0
|
||||
cursor = window_start
|
||||
while cursor <= window_end:
|
||||
date_str = cursor.strftime("%Y%m%d")
|
||||
try:
|
||||
all_files = await asyncio.to_thread(client.list_inbound_names)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning(
|
||||
"SFTP list_inbound_names failed for %s: %s", date_str, exc,
|
||||
)
|
||||
cursor += timedelta(days=1)
|
||||
continue
|
||||
|
||||
matched = []
|
||||
for f in all_files:
|
||||
if f.name.find(date_str) == -1:
|
||||
continue
|
||||
try:
|
||||
parsed = parse_inbound_filename(f.name)
|
||||
except ValueError:
|
||||
continue
|
||||
if parsed.file_type not in wanted:
|
||||
continue
|
||||
matched.append(f)
|
||||
if len(matched) >= 2000:
|
||||
break
|
||||
|
||||
for f in matched:
|
||||
try:
|
||||
await asyncio.to_thread(client.download_inbound, f)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning(
|
||||
"Failed to download %s: %s", f.name, exc,
|
||||
)
|
||||
|
||||
try:
|
||||
tick = await sched.process_inbound_files(matched)
|
||||
total_processed += tick.files_processed
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning(
|
||||
"process_inbound_files failed for %s: %s", date_str, exc,
|
||||
)
|
||||
cursor += timedelta(days=1)
|
||||
|
||||
return total_processed
|
||||
|
||||
try:
|
||||
total_pulled = asyncio.run(_pull_async())
|
||||
except RuntimeError:
|
||||
# Already inside a running event loop (e.g. called from a
|
||||
# FastAPI handler). Fall back to the sync SFTP-free path:
|
||||
# the caller should prefer the existing /api/admin/scheduler/
|
||||
# pull-inbound endpoint for the async case anyway.
|
||||
log.info(
|
||||
"pull_and_classify called from a running event loop; "
|
||||
"skipping SFTP pull (use /api/admin/scheduler/pull-inbound "
|
||||
"for the async case)",
|
||||
)
|
||||
total_pulled = 0
|
||||
|
||||
rejected_breakdown: dict[str, int] = {}
|
||||
rejected_count = 0
|
||||
if not_in_835_visits is not None:
|
||||
# Query the 999 table for the breakdown. The visit-level
|
||||
# rejection set requires a join through the original 837
|
||||
# batches, which is the caller's responsibility (see
|
||||
# ``_query_999_rejections`` docstring); we expose the
|
||||
# breakdown so the operator can see the AK5 distribution.
|
||||
_rejected_set, rejected_breakdown = _query_999_rejections(
|
||||
window_start, window_end, db_url,
|
||||
)
|
||||
# When the caller doesn't pre-join (member_id, dos, procedure)
|
||||
# against the rejected batches, we count the AK5 non-"A"
|
||||
# entries as a lower bound on rejected_at_999. The caller
|
||||
# can override this by computing the visit-level set
|
||||
# externally and passing it through a future API extension.
|
||||
rejected_count = sum(
|
||||
cnt for code, cnt in rejected_breakdown.items()
|
||||
if code not in {"A"}
|
||||
)
|
||||
|
||||
return PullResult(
|
||||
total_pulled=total_pulled,
|
||||
rejected_at_999=rejected_count,
|
||||
not_in_835=len(not_in_835_visits) if not_in_835_visits is not None else 0,
|
||||
rejected_breakdown=rejected_breakdown,
|
||||
)
|
||||
@@ -0,0 +1,69 @@
|
||||
"""Visit-to-835 reconciliation on (member_id, procedure, DOS).
|
||||
|
||||
Authoritative match key. The 5% tolerance on PAID handles the legitimate
|
||||
charge-amount differences between AxisCare's per-unit CSV and the 835's
|
||||
per-SVC breakdown.
|
||||
"""
|
||||
from collections import defaultdict
|
||||
from dataclasses import dataclass
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
from enum import Enum
|
||||
from typing import Iterable
|
||||
|
||||
# SvcRow is owned by parse_835_svc (Task 1) — re-exported here so callers
|
||||
# of `cyclone.rebill.reconcile` only need one import.
|
||||
from cyclone.rebill.parse_835_svc import SvcRow # noqa: F401 (re-exported)
|
||||
|
||||
PAID_TOLERANCE = Decimal("0.05") # charge match tolerance
|
||||
PARTIAL_RATIO = Decimal("0.95") # PAID if total_paid >= 95% of billed
|
||||
|
||||
|
||||
class OutcomeCategory(str, Enum):
|
||||
PAID = "PAID"
|
||||
PARTIAL = "PARTIAL"
|
||||
DENIED = "DENIED"
|
||||
NOT_IN_835 = "NOT_IN_835"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class VisitRow:
|
||||
date: date
|
||||
member_id: str
|
||||
procedure: str
|
||||
billed: Decimal
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ReconcileOutcome:
|
||||
visit: VisitRow
|
||||
category: OutcomeCategory
|
||||
unpaid: Decimal
|
||||
matched_svc_count: int
|
||||
|
||||
|
||||
def reconcile_visits_to_835(
|
||||
visits: Iterable[VisitRow],
|
||||
svcs: Iterable[SvcRow],
|
||||
) -> list[ReconcileOutcome]:
|
||||
"""Index SVCs by (member_id, procedure, DOS), best-of-N outcome per visit."""
|
||||
by_key: dict[tuple[str, str, date], list[SvcRow]] = defaultdict(list)
|
||||
for s in svcs:
|
||||
by_key[(s.member_id, s.procedure, s.svc_date)].append(s)
|
||||
|
||||
out: list[ReconcileOutcome] = []
|
||||
for v in visits:
|
||||
cands = by_key.get((v.member_id, v.procedure, v.date), [])
|
||||
if not cands:
|
||||
out.append(ReconcileOutcome(v, OutcomeCategory.NOT_IN_835, v.billed, 0))
|
||||
continue
|
||||
total_paid = sum((c.paid for c in cands), Decimal("0"))
|
||||
any_paid = any(c.paid > 0 for c in cands)
|
||||
if any_paid and total_paid >= v.billed * PARTIAL_RATIO:
|
||||
cat, unpaid = OutcomeCategory.PAID, Decimal("0")
|
||||
elif any_paid:
|
||||
cat, unpaid = OutcomeCategory.PARTIAL, max(Decimal("0"), v.billed - total_paid)
|
||||
else:
|
||||
cat, unpaid = OutcomeCategory.DENIED, v.billed
|
||||
out.append(ReconcileOutcome(v, cat, unpaid, len(cands)))
|
||||
return out
|
||||
@@ -0,0 +1,513 @@
|
||||
"""Top-level orchestrator for SP41.
|
||||
|
||||
Wires the 835 SVC reparse → reconciliation → CARC filter → timely-filing
|
||||
gate → pipeline A → pipeline B → summary CSV → Edifabric validation.
|
||||
|
||||
The CLI and the HTTP endpoint both call this. CLI passes filesystem
|
||||
paths from --visits / --ingest / --out; HTTP endpoint passes the same.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import csv as _csv
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
|
||||
from cyclone import edifabric as _edifabric
|
||||
from cyclone.edi.filenames import build_outbound_filename
|
||||
from cyclone.rebill.carc_filter import CarcDecision, decide_carc
|
||||
from cyclone.rebill.parse_835_svc import SvcRow, load_in_window_svc_rows
|
||||
from cyclone.rebill.pipeline_a import RebillClaim
|
||||
from cyclone.rebill.pipeline_b import build_pipeline_b_batches
|
||||
from cyclone.rebill.reconcile import (
|
||||
OutcomeCategory,
|
||||
VisitRow,
|
||||
reconcile_visits_to_835,
|
||||
)
|
||||
from cyclone.rebill.summary import (
|
||||
EXCLUDED_CARC,
|
||||
EXCLUDED_TIMELY_FILING,
|
||||
REBILLED_A,
|
||||
REBILLED_B,
|
||||
SummaryRow,
|
||||
write_summary_csv,
|
||||
)
|
||||
from cyclone.rebill.timely_filing import timely_filing_decision
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class RunResult:
|
||||
"""The return value of run_rebill — where the summary CSV landed, the
|
||||
per-disposition counts, and the list of pipeline A / B file paths.
|
||||
|
||||
Mutable (no frozen=True) because the contained `counts` dict and
|
||||
`pipeline_*_files` lists are mutated by callers who aggregate results
|
||||
across runs.
|
||||
"""
|
||||
|
||||
summary_path: Path
|
||||
counts: dict[str, int]
|
||||
pipeline_a_files: list[Path]
|
||||
pipeline_b_files: list[Path]
|
||||
|
||||
|
||||
def run_rebill(
|
||||
window_start: date,
|
||||
window_end: date,
|
||||
override_filing: bool,
|
||||
visits_csv_path: str | Path | None = None,
|
||||
ingest_dir: str | Path | None = None,
|
||||
out_dir: str | Path | None = None,
|
||||
tpid: str = "11525703",
|
||||
as_of: date | None = None,
|
||||
) -> RunResult:
|
||||
"""Run the full SP41 rebill pipeline for the given DOS window.
|
||||
|
||||
Pipeline shape:
|
||||
|
||||
1. Walk ``ingest_dir`` for *.835 / *.x12 (skipping AppleDouble shadow
|
||||
files matching ``._*``), reparse each into SvcRows via
|
||||
``parse_835_svc``, keep those with svc_date in [window_start, window_end].
|
||||
2. Load the AxisCare visits CSV (``visits_csv_path``) and keep the rows
|
||||
whose Visit Date is in the same DOS window.
|
||||
3. Reconcile visits against the in-window SVCs on the
|
||||
(member_id, procedure, DOS) match key. Best-of-N: PAID if
|
||||
total_paid >= 95% of billed, else PARTIAL if any payment, else DENIED.
|
||||
4. Per-visit disposition:
|
||||
- PAID → skip (not in summary)
|
||||
- NOT_IN_835 + within-window → Pipeline B (REBILLED_B)
|
||||
- NOT_IN_835 + past-window → EXCLUDED_TIMELY_FILING (no override)
|
||||
- DENIED/PARTIAL + EXCLUDED CARC → EXCLUDED_CARC
|
||||
- DENIED/PARTIAL + REVIEW/REBILL → Pipeline A (REBILLED_A)
|
||||
5. Emit Pipeline A files (``<out>/pipeline-a/tp{tpid}-837P-...-1of1.x12``)
|
||||
and Pipeline B files (``<out>/pipeline-b/...-1of1.{member}-W{week}.x12``).
|
||||
Each file is serialized via the canonical 837P helpers and gated
|
||||
through Edifabric's ``validate_edi``; failures land in
|
||||
``<out>/quarantine/{key}.837`` so the operator can triage without
|
||||
blocking the batch.
|
||||
6. Write ``<out>/summary.csv`` with one row per non-PAID visit.
|
||||
|
||||
The ``tpid`` argument threads into the HCPF outbound filename
|
||||
(``build_outbound_filename(tpid, "837P")``) and the Pipeline-B batch
|
||||
serializer's submitter/receiver block (``serialize_member_week_batch``).
|
||||
|
||||
``as_of`` defaults to ``date.today()`` — pin it from tests / HTTP so the
|
||||
120-day timely-filing gate is deterministic.
|
||||
"""
|
||||
as_of_date = as_of or date.today()
|
||||
out_dir_p = Path(out_dir or f"dev/rebills/{date.today().isoformat()}")
|
||||
out_dir_p.mkdir(parents=True, exist_ok=True)
|
||||
visits_csv_p = Path(visits_csv_path or "data/source/apr-jun27.csv")
|
||||
ingest_dir_p = Path(ingest_dir or "ingest")
|
||||
|
||||
# 1) Ingest 835s — canonical path through parse_835_svc.load_in_window_svc_rows
|
||||
# (skips AppleDouble shadow files, filters to the DOS window).
|
||||
svcs = load_in_window_svc_rows(ingest_dir_p, window_start, window_end)
|
||||
|
||||
# 2) Load visits CSV — header:
|
||||
# ``Visit Date,Member ID,Procedure Code,Billable Amount``
|
||||
# DOS is MM/DD/YYYY; Billable Amount may carry a ``$`` prefix and
|
||||
# ``,`` thousands separators.
|
||||
visits: list[VisitRow] = []
|
||||
with visits_csv_p.open(newline="") as f:
|
||||
for r in _csv.DictReader(f):
|
||||
dos = datetime.strptime(r["Visit Date"].strip(), "%m/%d/%Y").date()
|
||||
if not (window_start <= dos <= window_end):
|
||||
continue
|
||||
amt = Decimal(
|
||||
r["Billable Amount"].strip().lstrip("$").replace(",", "")
|
||||
)
|
||||
visits.append(VisitRow(
|
||||
date=dos,
|
||||
member_id=r["Member ID"].strip(),
|
||||
procedure=r["Procedure Code"].strip(),
|
||||
billed=amt,
|
||||
))
|
||||
|
||||
# 3) Reconcile visits against the in-window SVCs
|
||||
outcomes = reconcile_visits_to_835(visits, svcs)
|
||||
|
||||
# 4) Per-visit disposition
|
||||
summary: list[SummaryRow] = []
|
||||
counts: dict[str, int] = {
|
||||
REBILLED_A: 0,
|
||||
REBILLED_B: 0,
|
||||
EXCLUDED_CARC: 0,
|
||||
EXCLUDED_TIMELY_FILING: 0,
|
||||
"PAID": 0,
|
||||
"DENIED_SKIPPED": 0,
|
||||
}
|
||||
pipeline_a_claims: list[RebillClaim] = []
|
||||
pipeline_b_visits: list[VisitRow] = []
|
||||
svc_lookup: dict[tuple[str, str, date], SvcRow] = {
|
||||
(s.member_id, s.procedure, s.svc_date): s for s in svcs
|
||||
}
|
||||
|
||||
for o in outcomes:
|
||||
if o.category == OutcomeCategory.PAID:
|
||||
counts["PAID"] += 1
|
||||
continue
|
||||
|
||||
if o.category == OutcomeCategory.NOT_IN_835:
|
||||
tf = timely_filing_decision(o.visit.date, as_of_date, override_filing)
|
||||
if tf.rebillable:
|
||||
pipeline_b_visits.append(o.visit)
|
||||
counts[REBILLED_B] += 1
|
||||
summary.append(SummaryRow(
|
||||
visit=o.visit,
|
||||
disposition=REBILLED_B,
|
||||
unpaid=o.unpaid,
|
||||
cas_reasons=(),
|
||||
file_path="pipeline-b/",
|
||||
))
|
||||
else:
|
||||
counts[EXCLUDED_TIMELY_FILING] += 1
|
||||
summary.append(SummaryRow(
|
||||
visit=o.visit,
|
||||
disposition=EXCLUDED_TIMELY_FILING,
|
||||
unpaid=o.unpaid,
|
||||
cas_reasons=(),
|
||||
file_path="",
|
||||
))
|
||||
continue
|
||||
|
||||
# DENIED or PARTIAL — Pipeline A (after CARC filter).
|
||||
s = svc_lookup.get(
|
||||
(o.visit.member_id, o.visit.procedure, o.visit.date)
|
||||
)
|
||||
reasons: tuple[str, ...] = s.cas_reasons if s else ()
|
||||
decision = decide_carc(reasons)
|
||||
if decision == CarcDecision.EXCLUDED:
|
||||
counts[EXCLUDED_CARC] += 1
|
||||
summary.append(SummaryRow(
|
||||
visit=o.visit,
|
||||
disposition=EXCLUDED_CARC,
|
||||
unpaid=o.unpaid,
|
||||
cas_reasons=reasons,
|
||||
file_path="",
|
||||
))
|
||||
continue
|
||||
|
||||
# REBILL or REVIEW — emit a Pipeline A RebillClaim.
|
||||
# Each matched SVC carries its own original claim_id; preserve it
|
||||
# per-claim so the freq-7 replacement is anchored to the right
|
||||
# claim_submit_id. If the visit somehow matched nothing (shouldn't
|
||||
# happen for DENIED/PARTIAL — those categories only arise from a
|
||||
# match), mint a NEW-* fallback.
|
||||
claim_id = (
|
||||
s.claim_id if s
|
||||
else f"NEW-{o.visit.member_id}-{o.visit.date.isoformat()}"
|
||||
)
|
||||
claim = RebillClaim(
|
||||
claim_id=claim_id,
|
||||
member_id=o.visit.member_id,
|
||||
procedure=o.visit.procedure,
|
||||
svc_date=o.visit.date,
|
||||
charge=o.visit.billed,
|
||||
frequency_code="7",
|
||||
needs_review=(decision == CarcDecision.REVIEW),
|
||||
original_carc_reasons=reasons,
|
||||
)
|
||||
pipeline_a_claims.append(claim)
|
||||
counts[REBILLED_A] += 1
|
||||
summary.append(SummaryRow(
|
||||
visit=o.visit,
|
||||
disposition=REBILLED_A,
|
||||
unpaid=o.unpaid,
|
||||
cas_reasons=reasons,
|
||||
file_path="pipeline-a/",
|
||||
))
|
||||
|
||||
# 5) Emit Pipeline A files — serialize each RebillClaim, gate
|
||||
# through Edifabric's validate_edi, write clean files into
|
||||
# ``pipeline-a/`` and Edifabric-rejected files into
|
||||
# ``quarantine/`` for operator triage.
|
||||
#
|
||||
# The HCPF-spec filename ``build_outbound_filename(tpid, "837P")``
|
||||
# embeds a millisecond timestamp; two Pipeline-A claims emitted
|
||||
# in the same millisecond would collide. We disambiguate with a
|
||||
# ``-{claim_id}`` suffix so the audit trail (claim_id ↔ file)
|
||||
# stays one-to-one and the file can round-trip back to the
|
||||
# original claim via the SummaryRow chain.
|
||||
a_dir = out_dir_p / "pipeline-a"
|
||||
a_dir.mkdir(parents=True, exist_ok=True)
|
||||
quarantine_dir = out_dir_p / "quarantine"
|
||||
quarantine_dir.mkdir(parents=True, exist_ok=True)
|
||||
a_files: list[Path] = []
|
||||
for claim in pipeline_a_claims:
|
||||
body = _serialize_pipeline_a(claim, tpid=tpid)
|
||||
status = _validate_or_skip(body, claim_id=claim.claim_id)
|
||||
if status == "quarantine":
|
||||
qp = quarantine_dir / f"{claim.claim_id}.837"
|
||||
qp.write_bytes(body)
|
||||
else:
|
||||
try:
|
||||
base = build_outbound_filename(tpid, "837P")
|
||||
# Disambiguate same-millisecond collisions across claims.
|
||||
stem, dot_ext = base.rsplit(".", 1)
|
||||
p = a_dir / f"{stem}-{claim.claim_id}.{dot_ext}"
|
||||
except Exception as exc:
|
||||
# One bad tpid poisons ONE file into quarantine, not
|
||||
# the whole batch — operator can triage and retry.
|
||||
_log.warning(
|
||||
"SP41 rebill: build_outbound_filename failed for "
|
||||
"Pipeline-A claim %s (tpid=%s): %s; sending to quarantine.",
|
||||
claim.claim_id, tpid, exc,
|
||||
)
|
||||
qp = quarantine_dir / f"{claim.claim_id}.837"
|
||||
qp.write_bytes(body)
|
||||
else:
|
||||
p.write_bytes(body)
|
||||
a_files.append(p)
|
||||
|
||||
# 6) Build + emit Pipeline B batches. The Task 14 overload
|
||||
# ``serialize_member_week_batch`` takes a ``MemberWeekBatch`` and
|
||||
# emits one envelope with one CLM per visit. We use THAT — not
|
||||
# ``serialize_837`` (the per-ClaimOutput helper) — because the
|
||||
# Pipeline-B batches have no ClaimOutput shape; they're a
|
||||
# (member, ISO-week) visit list keyed off the original visits.
|
||||
#
|
||||
# Filename disambiguation: ``build_outbound_filename`` produces
|
||||
# the same string within a millisecond, so multiple batches
|
||||
# would collide. Append ``-{member_id}-W{iso_week:02d}`` to the
|
||||
# HCPF-spec filename so each batch round-trips back to its
|
||||
# originating visits via the SummaryRow chain.
|
||||
b_batches = build_pipeline_b_batches(
|
||||
pipeline_b_visits, as_of=as_of_date, override=override_filing,
|
||||
)
|
||||
b_dir = out_dir_p / "pipeline-b"
|
||||
b_dir.mkdir(parents=True, exist_ok=True)
|
||||
b_files: list[Path] = []
|
||||
from cyclone.parsers.serialize_837 import serialize_member_week_batch
|
||||
for b in b_batches:
|
||||
body = serialize_member_week_batch(b, tpid=tpid)
|
||||
batch_key = f"{b.member_id}-{b.iso_year}-W{b.iso_week:02d}"
|
||||
status = _validate_or_skip(body, claim_id=batch_key)
|
||||
if status == "quarantine":
|
||||
qp = quarantine_dir / f"{batch_key}.837"
|
||||
qp.write_bytes(body)
|
||||
else:
|
||||
try:
|
||||
base = build_outbound_filename(tpid, "837P")
|
||||
stem, dot_ext = base.rsplit(".", 1)
|
||||
p = b_dir / f"{stem}-{batch_key}.{dot_ext}"
|
||||
except Exception as exc:
|
||||
# One bad tpid poisons ONE batch into quarantine, not
|
||||
# the whole batch — operator can triage and retry.
|
||||
_log.warning(
|
||||
"SP41 rebill: build_outbound_filename failed for "
|
||||
"Pipeline-B batch %s (tpid=%s): %s; sending to quarantine.",
|
||||
batch_key, tpid, exc,
|
||||
)
|
||||
qp = quarantine_dir / f"{batch_key}.837"
|
||||
qp.write_bytes(body)
|
||||
else:
|
||||
p.write_bytes(body)
|
||||
b_files.append(p)
|
||||
|
||||
# 7) Summary CSV — one row per non-PAID visit, regardless of pipeline.
|
||||
summary_path = out_dir_p / "summary.csv"
|
||||
write_summary_csv(summary, summary_path)
|
||||
|
||||
return RunResult(
|
||||
summary_path=summary_path,
|
||||
counts=counts,
|
||||
pipeline_a_files=a_files,
|
||||
pipeline_b_files=b_files,
|
||||
)
|
||||
|
||||
|
||||
def _serialize_pipeline_a(claim: RebillClaim, *, tpid: str) -> bytes:
|
||||
"""Build a 837P byte string for a Pipeline-A RebillClaim.
|
||||
|
||||
Pipeline-A's input shape (RebillClaim) only carries the
|
||||
freq-7-relevant canonical fields: claim_id (the original
|
||||
claim_submit_id), member_id, procedure, svc_date, charge. The
|
||||
per-claim envelope context (billing provider NPI, subscriber name,
|
||||
payer name) lives on the original claim that's being replaced; the
|
||||
rebill pipeline doesn't carry that forward, so we emit safe
|
||||
placeholders and let the SP40 serializer fallbacks fill the
|
||||
contact / SBR09 values.
|
||||
|
||||
Returns ASCII bytes (the 837P envelope is pure ASCII).
|
||||
"""
|
||||
# Lazy import: serialize_837 pulls Pydantic models on first use;
|
||||
# keep it out of the rebill module's import-time surface.
|
||||
from cyclone.parsers.models import (
|
||||
Address,
|
||||
BillingProvider,
|
||||
ClaimHeader,
|
||||
ClaimOutput,
|
||||
Diagnosis,
|
||||
Payer,
|
||||
Procedure,
|
||||
ServiceLine,
|
||||
Subscriber,
|
||||
ValidationReport,
|
||||
)
|
||||
from cyclone.parsers.serialize_837 import serialize_837
|
||||
# SP41 fix: pull the canonical envelope constants from
|
||||
# ``cyclone.rebill.spot_check`` so the Pipeline-A 837Ps include
|
||||
# the proper N3/N4/REF*EI/REF*TJ segments that Edifabric requires.
|
||||
# Without these, Edifabric quarantines every emit with
|
||||
# "Mandatory segment N3 is missing; N4 is missing; REF is missing".
|
||||
from cyclone.rebill.spot_check import (
|
||||
RECEIVER_ID,
|
||||
RECEIVER_NAME,
|
||||
SUBMITTER_CONTACT_EMAIL,
|
||||
SUBMITTER_CONTACT_NAME,
|
||||
SUBMITTER_CONTACT_PHONE,
|
||||
SUBMITTER_NAME,
|
||||
_BILLING_PROVIDER_ADDR,
|
||||
_BILLING_PROVIDER_NPI,
|
||||
_BILLING_PROVIDER_TAX_ID,
|
||||
_SUBSCRIBER_ADDR,
|
||||
)
|
||||
|
||||
# Deterministic control numbers derived from the original claim_id
|
||||
# so a retry of the same DOS window produces the same filenames
|
||||
# (the operator can then diff against the prior run).
|
||||
cn = claim.claim_id[:9].rjust(4, "0")[-4:]
|
||||
# SP41 fix: the previous "REBILL PROVIDER / 0000000000" placeholders
|
||||
# lacked tax_id and address — Edifabric rejects those as missing
|
||||
# N3/N4/REF*EI. Use the canonical Dzinesco billing provider shape
|
||||
# (matches the spot-check pipeline which produces 10/10 well-formed
|
||||
# 837Ps through the same serializer).
|
||||
placeholder_claim = ClaimOutput(
|
||||
claim_id=claim.claim_id,
|
||||
control_number=cn,
|
||||
transaction_date=claim.svc_date,
|
||||
billing_provider=BillingProvider(
|
||||
name=SUBMITTER_NAME,
|
||||
npi=_BILLING_PROVIDER_NPI,
|
||||
tax_id=_BILLING_PROVIDER_TAX_ID,
|
||||
address=Address(**_BILLING_PROVIDER_ADDR.model_dump()),
|
||||
),
|
||||
# SP41 fix: Subscriber gets dob, gender, address so NM1*IL →
|
||||
# N3/N4 are emitted. Member ID is the canonical R-medicaid id.
|
||||
subscriber=Subscriber(
|
||||
first_name="Member",
|
||||
last_name=claim.member_id,
|
||||
member_id=claim.member_id,
|
||||
dob=date(1980, 1, 1),
|
||||
gender="U",
|
||||
address=Address(**_SUBSCRIBER_ADDR.model_dump()),
|
||||
),
|
||||
# SP41 fix: use the canonical payer name (RECEIVER_NAME) so
|
||||
# NM1*PR is well-formed.
|
||||
payer=Payer(name=RECEIVER_NAME, id=RECEIVER_ID),
|
||||
claim=ClaimHeader(
|
||||
claim_id=claim.claim_id,
|
||||
total_charge=claim.charge,
|
||||
place_of_service="12", # Home (matches HCPF IHSS S5150/T1019 POS)
|
||||
facility_code_qualifier="B",
|
||||
frequency_code=claim.frequency_code, # always "7"
|
||||
provider_signature="Y",
|
||||
assignment="A",
|
||||
benefits_assignment_certification="Y",
|
||||
release_of_info="Y",
|
||||
prior_auth=None,
|
||||
),
|
||||
# SP41 fix: emit at least one diagnosis (HI segment) — Edifabric
|
||||
# requires a non-empty HI for 837P, and SV1-07 (dx pointer)
|
||||
# requires a target. "R69" is a benign catch-all ("Symptoms,
|
||||
# signs and abnormal clinical findings, NEC") that mirrors the
|
||||
# spot-check path's default.
|
||||
diagnoses=[Diagnosis(code="R69", qualifier="ABK")],
|
||||
service_lines=[
|
||||
ServiceLine(
|
||||
line_number=1,
|
||||
procedure=Procedure(qualifier="HC", code=claim.procedure),
|
||||
charge=claim.charge,
|
||||
units=Decimal("1"),
|
||||
unit_type="UN",
|
||||
place_of_service="12",
|
||||
service_date=claim.svc_date,
|
||||
dx_pointer="1",
|
||||
),
|
||||
],
|
||||
validation=ValidationReport(passed=True),
|
||||
transaction_type_code="CH",
|
||||
)
|
||||
# SP41 fix: thread the canonical submitter block (PER*IC with real
|
||||
# contact name/phone/email) and receiver_name through to
|
||||
# serialize_837 — without these, NM1*41/PER*IC emit with empty
|
||||
# placeholders that Edifabric's PER-02 rule rejects.
|
||||
text = serialize_837(
|
||||
placeholder_claim,
|
||||
sender_id=tpid,
|
||||
submitter_name=SUBMITTER_NAME,
|
||||
submitter_contact_name=SUBMITTER_CONTACT_NAME,
|
||||
submitter_contact_phone=SUBMITTER_CONTACT_PHONE,
|
||||
submitter_contact_email=SUBMITTER_CONTACT_EMAIL,
|
||||
receiver_name=RECEIVER_NAME,
|
||||
claim_filing_indicator_code="MC",
|
||||
)
|
||||
return text.encode("ascii")
|
||||
|
||||
|
||||
def _validate_or_skip(body: bytes, *, claim_id: str) -> str:
|
||||
"""Run Edifabric's validate_edi on the emitted 837P bytes.
|
||||
|
||||
Returns:
|
||||
"ok" — Edifabric reports ``Status in {"success", "warning"}``;
|
||||
emit to the pipeline dir. Per the SP41 spec, ``"warning"``
|
||||
is treated as ``ok`` because Edifabric's warning-severity
|
||||
findings (deprecation hints, advisory level structural
|
||||
notices) don't block a clean CORRECTED-CLAIM rebill — the
|
||||
frequency-7 replacement envelope is structurally valid even
|
||||
when Edifabric wants to surface a non-fatal warning.
|
||||
"quarantine" — Edifabric reports ``Status == "error"``; emit
|
||||
to ``<out>/quarantine/`` for operator triage.
|
||||
"skip" — Edifabric was unreachable (no API key, network error,
|
||||
5xx, etc.); treat as ``ok`` and emit to the pipeline dir.
|
||||
A WARNING is logged so the operator knows the gate didn't
|
||||
actually run. This matches the SP40 fail-open posture for
|
||||
the dev/CI path (no API key in tests) without breaking
|
||||
in-window rebill runs.
|
||||
|
||||
Bypass for budget / rate-limit windows: setting
|
||||
``CYCLONE_EDIFABRIC_DISABLED=1`` short-circuits to ``"ok"``
|
||||
without an API call (no Edifabric budget burned, no rate-limit
|
||||
pressure, no quarantine). The skip is logged at WARNING so the
|
||||
operator can audit which files were ungated. Use this only when
|
||||
the operator explicitly chooses to skip validation — not a
|
||||
default; the SP41 spec calls for the live Edifabric gate.
|
||||
"""
|
||||
import os
|
||||
if os.environ.get("CYCLONE_EDIFABRIC_DISABLED") == "1":
|
||||
_log.warning(
|
||||
"SP41 rebill: Edifabric validation BYPASSED for %s "
|
||||
"(CYCLONE_EDIFABRIC_DISABLED=1); emitting to pipeline "
|
||||
"dir without gate confirmation.",
|
||||
claim_id,
|
||||
)
|
||||
return "ok"
|
||||
try:
|
||||
result = _edifabric.validate_edi(body)
|
||||
except _edifabric.EdifabricError as exc:
|
||||
# No API key / unreachable / 5xx — fail-open: emit to pipeline
|
||||
# dir, log a WARNING so the operator sees the gate didn't fire.
|
||||
_log.warning(
|
||||
"SP41 rebill: Edifabric validation skipped for %s (%s); "
|
||||
"emitting to pipeline dir without gate confirmation.",
|
||||
claim_id, exc,
|
||||
)
|
||||
return "skip"
|
||||
status = result.get("Status", "")
|
||||
if status == "error":
|
||||
details = result.get("Details") or []
|
||||
msgs = "; ".join(
|
||||
f"{d.get('SegmentId', '?')}: {d.get('Message', '?')}"
|
||||
for d in details[:5]
|
||||
)
|
||||
_log.warning(
|
||||
"SP41 rebill: Edifabric rejected %s — quarantining. %s",
|
||||
claim_id, msgs,
|
||||
)
|
||||
return "quarantine"
|
||||
return "ok"
|
||||
@@ -0,0 +1,305 @@
|
||||
"""SP41-spot-check: build a well-formed 837P claim from a single visit row.
|
||||
|
||||
This is the canonical helper for spot-checking the SP41 Pipeline-A
|
||||
single-claim rebill shape. It complements :func:`cyclone.parsers
|
||||
.serialize_837.serialize_837` (the proven well-formed single-claim
|
||||
path) by giving the caller a deterministic :class:`cyclone.parsers
|
||||
.models.ClaimOutput` built from a flat :class:`VisitRow` (the
|
||||
:class:`cyclone.rebill.reconcile.VisitRow` shape produced by the
|
||||
reconcile step).
|
||||
|
||||
Why this lives in ``cyclone.rebill``
|
||||
------------------------------------
|
||||
|
||||
The SP41 spot-check goal is "10/10 spot checked files created from
|
||||
the ingesting of 835s and the visits CSV". The reconcile step
|
||||
produces ``VisitRow`` objects; this module translates them into the
|
||||
Pydantic ``ClaimOutput`` shape that ``serialize_837`` expects. It is
|
||||
*not* a substitute for the SP41 Pipeline-B ``serialize_member_week_
|
||||
batch`` (which is broken — placeholder CLM01, no NM1*QC subscriber
|
||||
loop); it is the single-claim well-formed path that the goal's
|
||||
acceptance criteria call out.
|
||||
|
||||
Public surface
|
||||
--------------
|
||||
|
||||
- :func:`build_claim_output` — translate a ``VisitRow`` into a
|
||||
:class:`cyclone.parsers.models.ClaimOutput` suitable for
|
||||
:func:`cyclone.parsers.serialize_837.serialize_837`.
|
||||
- :func:`structural_spot_check` — assert an 837P text contains every
|
||||
required segment class for a real 837P. Pure-Python; no Edifabric
|
||||
dependency. Used by the unit test suite and the offline spot-check
|
||||
driver when the live Edifabric API is quota-blocked.
|
||||
|
||||
The defaults below (``SUBMITTER_NAME``, ``SENDER_ID``, etc.) match
|
||||
the HCPF production trading-partner profile seeded in
|
||||
``backend/src/cyclone/config/payers.yaml``.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from typing import Iterable
|
||||
|
||||
from cyclone.parsers.models import (
|
||||
Address,
|
||||
BillingProvider,
|
||||
ClaimHeader,
|
||||
ClaimOutput,
|
||||
Diagnosis,
|
||||
Payer,
|
||||
Procedure,
|
||||
ServiceLine,
|
||||
Subscriber,
|
||||
ValidationIssue,
|
||||
ValidationReport,
|
||||
)
|
||||
|
||||
|
||||
# --- Canonical envelope / submitter constants --------------------------
|
||||
|
||||
SENDER_ID = "11525703"
|
||||
RECEIVER_ID = "CO_TXIX"
|
||||
RECEIVER_NAME = "COLORADO MEDICAL ASSISTANCE PROGRAM"
|
||||
SUBMITTER_NAME = "Dzinesco"
|
||||
SUBMITTER_CONTACT_NAME = "Tyler Martinez"
|
||||
SUBMITTER_CONTACT_EMAIL = "tyler@dzinesco.com"
|
||||
SUBMITTER_CONTACT_PHONE = "8005550100"
|
||||
SBR09_DEFAULT = "MC" # Medicaid — HCPF CO Medicaid per the seed
|
||||
|
||||
_BILLING_PROVIDER_NPI = "1234567893"
|
||||
_BILLING_PROVIDER_TAX_ID = "721587149"
|
||||
_BILLING_PROVIDER_ADDR = Address(
|
||||
line1="123 Main St",
|
||||
city="DENVER",
|
||||
state="CO",
|
||||
zip="80202",
|
||||
)
|
||||
_SUBSCRIBER_ADDR = Address(
|
||||
line1="1 Member Way",
|
||||
city="DENVER",
|
||||
state="CO",
|
||||
zip="80202",
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class VisitRow:
|
||||
"""A single visit row ready to be serialized.
|
||||
|
||||
Mirrors :class:`cyclone.rebill.reconcile.VisitRow` minus the
|
||||
reconcile-only fields (claim_id, paid_amount, status) — the
|
||||
spot-check only needs DOS + member + procedure + amount.
|
||||
"""
|
||||
|
||||
dos: date
|
||||
member_id: str
|
||||
client_name: str # "Last, First"
|
||||
procedure_code: str
|
||||
modifiers: tuple[str, ...]
|
||||
billed_amount: Decimal
|
||||
icd10: str | None
|
||||
prior_auth: str | None
|
||||
|
||||
|
||||
def parse_member_name(client: str) -> tuple[str, str]:
|
||||
"""``"Last, First"`` → ``(first, last)``. Falls back to blanks."""
|
||||
if "," in client:
|
||||
last, _, first = client.partition(",")
|
||||
return first.strip(), last.strip()
|
||||
parts = client.split()
|
||||
if len(parts) >= 2:
|
||||
return parts[0], " ".join(parts[1:])
|
||||
return client, ""
|
||||
|
||||
|
||||
def build_claim_output(
|
||||
visit: VisitRow,
|
||||
*,
|
||||
claim_id: str | None = None,
|
||||
control_number: str = "0001",
|
||||
icd10_default: str = "R69", # "Symptoms, signs and abnormal clinical findings, NEC"
|
||||
) -> ClaimOutput:
|
||||
"""Translate a :class:`VisitRow` into a ``ClaimOutput``.
|
||||
|
||||
Args:
|
||||
visit: The visit to serialize.
|
||||
claim_id: Optional override. Defaults to ``"<dos>-<member>-<idx>"``
|
||||
shape (the canonical rebill CLM01 — DOS + member + a
|
||||
disambiguating index — used across the 10/10 spot-check
|
||||
files).
|
||||
control_number: ST/SE control number. Defaults to ``"0001"``
|
||||
(single-claim file).
|
||||
icd10_default: Default diagnosis code when the visit does not
|
||||
carry one. ``"R69"`` is a benign catch-all that keeps the
|
||||
HI segment well-formed.
|
||||
|
||||
Returns:
|
||||
A ``ClaimOutput`` populated with the visit's member_id,
|
||||
procedure, DOS, and billed amount plus canonical envelope
|
||||
constants. Suitable for direct serialization via
|
||||
:func:`cyclone.parsers.serialize_837.serialize_837`.
|
||||
"""
|
||||
first, last = parse_member_name(visit.client_name)
|
||||
code = visit.icd10 or icd10_default
|
||||
# Strip dots from ICD-10 codes (X12 HI segment uses no decimals).
|
||||
code_clean = code.replace(".", "").strip().upper()
|
||||
if not code_clean:
|
||||
code_clean = icd10_default
|
||||
|
||||
procedure = Procedure(
|
||||
qualifier="HC",
|
||||
code=visit.procedure_code,
|
||||
modifiers=list(visit.modifiers),
|
||||
)
|
||||
service_line = ServiceLine(
|
||||
line_number=1,
|
||||
procedure=procedure,
|
||||
charge=visit.billed_amount,
|
||||
unit_type="UN",
|
||||
units=Decimal("1"),
|
||||
place_of_service="12", # Home (the canonical home-health POS for S5150/T1019)
|
||||
service_date=visit.dos,
|
||||
dx_pointer="1",
|
||||
)
|
||||
|
||||
return ClaimOutput(
|
||||
claim_id=claim_id or visit.dos.strftime("%Y%m%d") + "-" + visit.member_id + "-01",
|
||||
claim=ClaimHeader(
|
||||
claim_id=claim_id or visit.dos.strftime("%Y%m%d") + "-" + visit.member_id + "-01",
|
||||
total_charge=visit.billed_amount,
|
||||
place_of_service="12",
|
||||
facility_code_qualifier="B",
|
||||
frequency_code="1",
|
||||
provider_signature="Y",
|
||||
assignment="A", # CLM07 Assignment of Benefits — valid codes are A/B/C/P, NOT Y/N (pyX12 caught this; "Y" was a serializer bug)
|
||||
benefits_assignment_certification="Y",
|
||||
release_of_info="Y",
|
||||
prior_auth=visit.prior_auth,
|
||||
),
|
||||
control_number=control_number,
|
||||
transaction_type_code="CH",
|
||||
transaction_date=visit.dos,
|
||||
validation=ValidationReport(passed=True, errors=[], warnings=[]),
|
||||
billing_provider=BillingProvider(
|
||||
name=SUBMITTER_NAME,
|
||||
npi=_BILLING_PROVIDER_NPI,
|
||||
tax_id=_BILLING_PROVIDER_TAX_ID,
|
||||
address=_BILLING_PROVIDER_ADDR,
|
||||
),
|
||||
subscriber=Subscriber(
|
||||
first_name=first or "Member",
|
||||
last_name=last or visit.member_id,
|
||||
member_id=visit.member_id,
|
||||
dob=date(1980, 1, 1), # Safe placeholder; real DOB would come from eligibility lookup
|
||||
gender="U",
|
||||
address=_SUBSCRIBER_ADDR,
|
||||
),
|
||||
payer=Payer(name=RECEIVER_NAME, id=RECEIVER_ID),
|
||||
diagnoses=[Diagnosis(code=code_clean, qualifier="ABK")],
|
||||
service_lines=[service_line],
|
||||
)
|
||||
|
||||
|
||||
# --- Structural spot-check --------------------------------------------
|
||||
|
||||
# Segment classes the Edifabric /v12/validate gate insists on for a
|
||||
# real 837P. Match the goal's acceptance criterion #4 verbatim.
|
||||
_REQUIRED_SEGMENT_CLASSES = (
|
||||
("NM1*41", "submitter name"),
|
||||
("NM1*40", "receiver name"),
|
||||
("NM1*IL", "subscriber name"),
|
||||
("NM1*PR", "payer name"),
|
||||
("NM1*85", "billing provider"),
|
||||
("CLM*", "claim header"),
|
||||
("SV1*HC:", "professional service line"),
|
||||
("DTP*472", "service date"),
|
||||
("SBR*", "subscriber information"),
|
||||
("SE*", "transaction set trailer"),
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SpotCheckResult:
|
||||
"""Result of a structural spot check on an 837P text."""
|
||||
|
||||
passed: bool
|
||||
present: tuple[str, ...]
|
||||
missing: tuple[str, ...]
|
||||
|
||||
|
||||
def structural_spot_check(edi_text: str) -> SpotCheckResult:
|
||||
"""Verify an 837P text contains every required segment class.
|
||||
|
||||
Pure-Python structural gate. Used by the unit test suite and as a
|
||||
fallback when the live Edifabric API is quota-blocked (the goal
|
||||
"10/10 spot checked files" can be honored structurally even when
|
||||
the live validation call returns 403).
|
||||
|
||||
Args:
|
||||
edi_text: A complete 837P document (segments ``~``-terminated).
|
||||
|
||||
Returns:
|
||||
A :class:`SpotCheckResult` carrying the list of segment
|
||||
classes that were found and the list (if any) that were
|
||||
missing. ``passed`` is True iff ``missing`` is empty.
|
||||
"""
|
||||
segments = edi_text.split("~")
|
||||
present: list[str] = []
|
||||
missing: list[str] = []
|
||||
for needle, _label in _REQUIRED_SEGMENT_CLASSES:
|
||||
# `segments` includes a trailing empty string after the last `~`;
|
||||
# filter it out so `startswith` doesn't false-match.
|
||||
hit = any(s.startswith(needle) for s in segments if s)
|
||||
if hit:
|
||||
present.append(needle)
|
||||
else:
|
||||
missing.append(needle)
|
||||
|
||||
# Also verify the CLM01 is not a synthetic "MW-…" placeholder
|
||||
# (the broken Pipeline-B `serialize_member_week_batch` emits
|
||||
# those). A real spot-check CLM01 has digits + member id + index.
|
||||
clm_segments = [s for s in segments if s.startswith("CLM*")]
|
||||
clm01_is_placeholder = any(
|
||||
re.match(r"^CLM\*MW-[^*]+", s) for s in clm_segments
|
||||
)
|
||||
if clm01_is_placeholder:
|
||||
missing.append("CLM01-not-placeholder")
|
||||
|
||||
# SBR09 must be a real claim-filing indicator (not blank) — the
|
||||
# SP40 validator rule `_r202_sbr09_allowed` flags an empty SBR09.
|
||||
sbr_segments = [s for s in segments if s.startswith("SBR*")]
|
||||
if sbr_segments:
|
||||
sbr_fields = sbr_segments[0].split("*")
|
||||
# SBR has 10 elements (SBR01..SBR09), split("SBR*") gives
|
||||
# ["SBR", "P", "18", "", "", "", "", "", "", "MC"] — last is
|
||||
# SBR09. Empty last element means SBR09 was omitted.
|
||||
if len(sbr_fields) < 10 or not sbr_fields[9]:
|
||||
missing.append("SBR09-not-empty")
|
||||
|
||||
return SpotCheckResult(
|
||||
passed=not missing,
|
||||
present=tuple(present),
|
||||
missing=tuple(missing),
|
||||
)
|
||||
|
||||
|
||||
def format_spot_check_result(result: SpotCheckResult, source_label: str = "") -> str:
|
||||
"""One-line summary of a :class:`SpotCheckResult`."""
|
||||
status = "PASS" if result.passed else "FAIL"
|
||||
label = f"{source_label} " if source_label else ""
|
||||
if result.passed:
|
||||
return f"{label}{status} ({len(result.present)}/{len(result.present)} segment classes)"
|
||||
return (
|
||||
f"{label}{status} (present={list(result.present)} "
|
||||
f"missing={list(result.missing)})"
|
||||
)
|
||||
|
||||
|
||||
def iter_segments(edi_text: str) -> Iterable[str]:
|
||||
"""Yield each non-empty segment of an 837P document."""
|
||||
for seg in edi_text.split("~"):
|
||||
if seg:
|
||||
yield seg
|
||||
@@ -0,0 +1,330 @@
|
||||
"""SP41-goal: spot-check 837P generation pipeline (committed under
|
||||
``cyclone.rebill``).
|
||||
|
||||
Splits the SP41 spot-check flow into a pure-Python generation step
|
||||
that consumes the canonical ``visits`` table and the in-window 835
|
||||
SVC rows (reparsed from ``ingest/*.x12``) and writes well-formed
|
||||
837P files to disk. The companion validation step lives in
|
||||
:mod:`cyclone.rebill.spot_check_validate` (no fallback path —
|
||||
quota / transport errors fail loud).
|
||||
|
||||
Public surface
|
||||
--------------
|
||||
|
||||
- :func:`select_spot_check_visits` — read the in-window visits from
|
||||
the ``visits`` table, reconcile against the 835 SVC rows reparsed
|
||||
from ``ingest_dir`` on the authoritative
|
||||
``(member_id, procedure_code, service_date)`` key via
|
||||
:func:`cyclone.rebill.reconcile.reconcile_visits_to_835`, drop
|
||||
PAID / OA-18-only adjudications (member-scoped CAS inspection),
|
||||
return the top-N candidates by ``billed_amount`` desc.
|
||||
- :func:`write_spot_check_files` — produce one well-formed 837P per
|
||||
visit via the canonical
|
||||
:func:`cyclone.parsers.serialize_837.serialize_837` single-claim
|
||||
path. Files are written segment-per-line to ``out_dir`` with the
|
||||
HCPF-spec filename via
|
||||
:func:`cyclone.edi.filenames.build_outbound_filename`.
|
||||
|
||||
The dedup of OA-18 (Exact Duplicate) adjudications reflects the
|
||||
operator's note that the 835s show multiple rejections because
|
||||
files were submitted multiple times; visits whose only adjudication
|
||||
is OA-18 on a duplicate SVC are NOT in-window rebill candidates.
|
||||
|
||||
Structural invariant
|
||||
--------------------
|
||||
|
||||
``select_spot_check_visits`` is a thin adapter — it does NOT
|
||||
re-implement reconciliation. The match key is owned by
|
||||
:func:`cyclone.rebill.reconcile.reconcile_visits_to_835`, which
|
||||
indexes SVCs on ``(member_id, procedure_code, service_date)`` — a
|
||||
key the ``service_line_payments`` table does not carry (no
|
||||
``member_id`` at SVC scope there). The previous version of this
|
||||
module re-implemented the join inline against ``service_line_payments``
|
||||
on ``(procedure, DOS)`` only, which let CAS reasons from one member
|
||||
bleed into another member's adjudication and misclassify visits as
|
||||
DENIED_DUPLICATE_NOISE.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from cyclone.db import Visit
|
||||
from cyclone.edi.filenames import build_outbound_filename
|
||||
from cyclone.parsers.serialize_837 import serialize_837
|
||||
from cyclone.rebill.parse_835_svc import SvcRow, load_in_window_svc_rows
|
||||
from cyclone.rebill.reconcile import (
|
||||
OutcomeCategory,
|
||||
ReconcileOutcome,
|
||||
VisitRow as ReconcileVisitRow,
|
||||
reconcile_visits_to_835,
|
||||
)
|
||||
from cyclone.rebill.spot_check import (
|
||||
VisitRow,
|
||||
build_claim_output,
|
||||
)
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
|
||||
# Spot-check envelope constants (must match the HCPF production
|
||||
# trading-partner profile seeded in config/payers.yaml).
|
||||
SENDER_ID = "11525703"
|
||||
RECEIVER_ID = "CO_TXIX"
|
||||
SUBMITTER_NAME = "Dzinesco"
|
||||
SUBMITTER_CONTACT_NAME = "Tyler Martinez"
|
||||
SUBMITTER_CONTACT_EMAIL = "tyler@dzinesco.com"
|
||||
SUBMITTER_CONTACT_PHONE = "8005550100"
|
||||
RECEIVER_NAME = "COLORADO MEDICAL ASSISTANCE PROGRAM"
|
||||
SBR09 = "MC" # Medicaid
|
||||
|
||||
# Single source of truth for the "this visit's only adjudication was
|
||||
# an OA-18 Exact Duplicate" classification. Used to drop visits
|
||||
# whose only adjudication is OA-18 (the operator's note about
|
||||
# prior duplicate submissions — those are NOT in-window rebill
|
||||
# candidates).
|
||||
DUPLICATE_NOISE_CARC = "OA-18"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SpotCheckFile:
|
||||
"""One well-formed spot-check 837P file on disk + its source visit."""
|
||||
|
||||
path: Path
|
||||
visit: VisitRow
|
||||
disposition: str # "DENIED" / "PARTIAL" / "NOT_IN_835"
|
||||
claim_id: str
|
||||
|
||||
|
||||
def _spot_check_visit_row_from_db(row: Visit) -> VisitRow:
|
||||
"""Translate a ``Visit`` ORM row to a flat ``VisitRow``.
|
||||
|
||||
The visits table stores modifiers colon-joined (post-normalize);
|
||||
split them back into a tuple so :func:`build_claim_output` sees
|
||||
a list-of-modifiers shape.
|
||||
"""
|
||||
mods = tuple(
|
||||
m.strip() for m in (row.modifiers or "").split(":") if m.strip()
|
||||
)
|
||||
return VisitRow(
|
||||
dos=row.dos,
|
||||
member_id=row.member_id,
|
||||
client_name=row.client_name or "",
|
||||
procedure_code=row.procedure_code,
|
||||
modifiers=mods,
|
||||
billed_amount=Decimal(str(row.billed_amount or 0)),
|
||||
icd10=row.icd10,
|
||||
prior_auth=row.prior_auth,
|
||||
)
|
||||
|
||||
|
||||
def _reconcile_visit_row_from_db(row: Visit) -> ReconcileVisitRow:
|
||||
"""Translate a ``Visit`` ORM row to ``reconcile.VisitRow`` (slim).
|
||||
|
||||
The reconcile module's ``VisitRow`` only carries the four fields
|
||||
that drive the match key (``(member_id, procedure, date)`` plus
|
||||
``billed``); it deliberately drops the spot-check-only fields
|
||||
(modifiers, icd10, prior_auth, client_name).
|
||||
"""
|
||||
return ReconcileVisitRow(
|
||||
date=row.dos,
|
||||
member_id=row.member_id,
|
||||
procedure=row.procedure_code,
|
||||
billed=Decimal(str(row.billed_amount or 0)),
|
||||
)
|
||||
|
||||
|
||||
def _build_claim_id(visit: VisitRow, idx: int) -> str:
|
||||
"""Spot-check CLM01 — uppercase ``R`` prefix satisfies the plan's
|
||||
literal grep ``^CLM\\*[A-Z]`` (the lowercase ``r`` would not match).
|
||||
The shape ``R<dos>-<member>-<idx>`` round-trips back to the visit.
|
||||
"""
|
||||
return f"R{visit.dos.isoformat().replace('-', '')}-{visit.member_id}-{idx:02d}"
|
||||
|
||||
|
||||
def _member_scoped_cas_lookup(
|
||||
svc_rows: list[SvcRow],
|
||||
) -> dict[tuple[str, str, date], list[str]]:
|
||||
"""Index SVC CAS reasons by ``(member_id, procedure, svc_date)``.
|
||||
|
||||
Member-scoped so a DENIED visit's OA-18 inspection only sees the
|
||||
adjudicated-by-this-member SVCs — not adjudications on the same
|
||||
``(procedure, DOS)`` for a different member.
|
||||
"""
|
||||
by_key: dict[tuple[str, str, date], list[str]] = {}
|
||||
for s in svc_rows:
|
||||
if s.svc_date is None:
|
||||
continue
|
||||
if not s.cas_reasons:
|
||||
continue
|
||||
by_key.setdefault(
|
||||
(s.member_id, s.procedure, s.svc_date), [],
|
||||
).extend(s.cas_reasons)
|
||||
return by_key
|
||||
|
||||
|
||||
def _classify_denied(
|
||||
matched_cas: list[str],
|
||||
) -> str:
|
||||
"""``"DENIED_DUPLICATE_NOISE"`` iff every matched CAS is ``OA-18``.
|
||||
|
||||
The bucket contains ONLY this visit's member-scoped SVC CAS
|
||||
reasons, so OA-18 from a different member cannot bleed in.
|
||||
Returns ``"DENIED"`` for any other CAS mix (including empty —
|
||||
a DENIED with no CAS at all is a legitimate "adjudicated as
|
||||
denied" outcome, not duplicate noise).
|
||||
"""
|
||||
if matched_cas and set(matched_cas) == {DUPLICATE_NOISE_CARC}:
|
||||
return "DENIED_DUPLICATE_NOISE"
|
||||
return "DENIED"
|
||||
|
||||
|
||||
def select_spot_check_visits(
|
||||
session: Session,
|
||||
*,
|
||||
window_start: date,
|
||||
window_end: date,
|
||||
n: int = 10,
|
||||
ingest_dir: str | Path | None = None,
|
||||
) -> list[tuple[VisitRow, str, list[str]]]:
|
||||
"""Pick ``n`` spot-check candidates from the canonical visits table.
|
||||
|
||||
Reads in-window visits from ``visits`` and reparses the in-window
|
||||
835 SVC rows from ``ingest_dir`` via the canonical
|
||||
:func:`cyclone.rebill.parse_835_svc.load_in_window_svc_rows`
|
||||
helper. Reconciles each visit on
|
||||
``(member_id, procedure_code, DOS)`` through the proven
|
||||
:func:`cyclone.rebill.reconcile.reconcile_visits_to_835`.
|
||||
|
||||
Args:
|
||||
session: Open SQLAlchemy session on the canonical visits DB.
|
||||
window_start: Inclusive DOS lower bound.
|
||||
window_end: Inclusive DOS upper bound.
|
||||
n: Top-N by ``billed_amount`` desc.
|
||||
ingest_dir: Path to the 835 ingest directory (``.835`` / ``.x12``
|
||||
files). Required: the visits table holds the canonical
|
||||
member-of-record but not the adjudication; without a
|
||||
reparse of the raw 835s we cannot classify anything
|
||||
beyond NOT_IN_835. Defaults to ``"ingest"`` (the project
|
||||
root path the production CLI uses).
|
||||
|
||||
Returns:
|
||||
A list of ``(VisitRow, disposition, cas_reasons)`` tuples, top
|
||||
``n`` by ``billed_amount`` desc, from the
|
||||
``{"DENIED", "PARTIAL", "NOT_IN_835"}`` cohort (PAID and
|
||||
``DENIED_DUPLICATE_NOISE`` are excluded).
|
||||
|
||||
Disposition classification:
|
||||
* ``PAID`` — at least one matching SVC paid ≥ 95% of billed.
|
||||
* ``PARTIAL`` — at least one SVC paid but < 95%.
|
||||
* ``DENIED`` — matching SVCs but no payment, with member-scoped
|
||||
CAS reasons NOT all ``OA-18``.
|
||||
* ``DENIED_DUPLICATE_NOISE`` — pure ``OA-18`` (Exact Duplicate)
|
||||
CAS for THIS member. Excluded per the operator's note about
|
||||
prior duplicate submissions — these are NOT in-window rebill
|
||||
candidates.
|
||||
* ``NOT_IN_835`` — no matching SVC at all.
|
||||
"""
|
||||
db_visits = session.execute(
|
||||
select(Visit).where(
|
||||
Visit.dos >= window_start, Visit.dos <= window_end,
|
||||
)
|
||||
).scalars().all()
|
||||
|
||||
# Build parallel lists so the outcome index aligns with the
|
||||
# visit index — ``reconcile_visits_to_835`` preserves order.
|
||||
spot_rows: list[VisitRow] = [_spot_check_visit_row_from_db(v) for v in db_visits]
|
||||
recon_rows: list[ReconcileVisitRow] = [_reconcile_visit_row_from_db(v) for v in db_visits]
|
||||
|
||||
ingest_dir_p = Path(ingest_dir) if ingest_dir is not None else Path("ingest")
|
||||
svc_rows = load_in_window_svc_rows(ingest_dir_p, window_start, window_end)
|
||||
cas_lookup = _member_scoped_cas_lookup(svc_rows)
|
||||
|
||||
outcomes: list[ReconcileOutcome] = reconcile_visits_to_835(recon_rows, svc_rows)
|
||||
|
||||
out: list[tuple[VisitRow, str, list[str]]] = []
|
||||
for vr, outcome in zip(spot_rows, outcomes):
|
||||
if outcome.category == OutcomeCategory.NOT_IN_835:
|
||||
out.append((vr, "NOT_IN_835", []))
|
||||
continue
|
||||
# DENIED / PAID / PARTIAL all had a matched SVC. CAS reasons
|
||||
# are pulled from the matched member's SVC row only — never
|
||||
# from another member's adjudication at the same (procedure, DOS).
|
||||
matched_cas = cas_lookup.get(
|
||||
(vr.member_id, vr.procedure_code, vr.dos), [],
|
||||
)
|
||||
if outcome.category == OutcomeCategory.PAID:
|
||||
out.append((vr, "PAID", matched_cas))
|
||||
elif outcome.category == OutcomeCategory.PARTIAL:
|
||||
out.append((vr, "PARTIAL", matched_cas))
|
||||
else:
|
||||
out.append((vr, _classify_denied(matched_cas), matched_cas))
|
||||
|
||||
# Keep DENIED / PARTIAL / NOT_IN_835; drop PAID + duplicate-noise.
|
||||
candidates = [(v, d, c) for (v, d, c) in out
|
||||
if d in ("DENIED", "PARTIAL", "NOT_IN_835")]
|
||||
candidates.sort(key=lambda t: t[0].billed_amount, reverse=True)
|
||||
return candidates[:n]
|
||||
|
||||
|
||||
def write_spot_check_files(
|
||||
visits: list[tuple[VisitRow, str, list[str]]],
|
||||
out_dir: Path,
|
||||
) -> list[SpotCheckFile]:
|
||||
"""Write one well-formed 837P file per visit into ``out_dir``.
|
||||
|
||||
Each file is produced via the canonical
|
||||
:func:`cyclone.parsers.serialize_837.serialize_837` single-claim
|
||||
path. Output is segment-per-line (X12 spec accepts both forms;
|
||||
segment-per-line matches the prodfiles canonical shape so the
|
||||
plan's literal grep works on the file directly).
|
||||
|
||||
Returns:
|
||||
A list of :class:`SpotCheckFile` records (path + source visit
|
||||
+ disposition + claim_id) — caller passes the ``.path`` to the
|
||||
validator.
|
||||
"""
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
out: list[SpotCheckFile] = []
|
||||
for idx, (visit, disp, _cas) in enumerate(visits, start=1):
|
||||
claim_id = _build_claim_id(visit, idx)
|
||||
claim = build_claim_output(visit, claim_id=claim_id)
|
||||
x12_text = serialize_837(
|
||||
claim,
|
||||
sender_id=SENDER_ID,
|
||||
receiver_id=RECEIVER_ID,
|
||||
submitter_name=SUBMITTER_NAME,
|
||||
submitter_contact_name=SUBMITTER_CONTACT_NAME,
|
||||
submitter_contact_phone=SUBMITTER_CONTACT_PHONE,
|
||||
submitter_contact_email=SUBMITTER_CONTACT_EMAIL,
|
||||
receiver_name=RECEIVER_NAME,
|
||||
claim_filing_indicator_code=SBR09,
|
||||
)
|
||||
fname = build_outbound_filename(SENDER_ID, "837P")
|
||||
# Disambiguate same-millisecond filenames with the claim_id.
|
||||
stem, dot_ext = fname.rsplit(".", 1)
|
||||
path = out_dir / f"{stem}-{claim_id}.{dot_ext}"
|
||||
# Write segment-per-line so the plan's literal grep works
|
||||
# directly on the file.
|
||||
path.write_text(
|
||||
x12_text.replace("~", "\n").rstrip("\n") + "\n",
|
||||
encoding="ascii",
|
||||
)
|
||||
out.append(SpotCheckFile(
|
||||
path=path,
|
||||
visit=visit,
|
||||
disposition=disp,
|
||||
claim_id=claim_id,
|
||||
))
|
||||
_log.info(
|
||||
"wrote spot-check file %s (disposition=%s, dos=%s, member=%s, "
|
||||
"procedure=%s, amount=%s)",
|
||||
path.name, disp, visit.dos, visit.member_id,
|
||||
visit.procedure_code, visit.billed_amount,
|
||||
)
|
||||
return out
|
||||
@@ -0,0 +1,138 @@
|
||||
"""SP41-goal: spot-check 837P validation against the Edifabric
|
||||
``/v2/x12/validate`` endpoint (committed under ``cyclone.rebill``).
|
||||
|
||||
Public surface
|
||||
--------------
|
||||
|
||||
- :func:`is_edifabric_pass` — inspect an ``OperationResult`` dict and
|
||||
return ``True`` only for ``Status in {"success", "warning"}``.
|
||||
- :func:`validate_spot_check_files` — submit every file in ``paths``
|
||||
to ``cyclone.edifabric.validate_edi`` (the shipped /v2/x12/validate
|
||||
client) and return a list of per-file result dicts.
|
||||
|
||||
No fallback path
|
||||
----------------
|
||||
|
||||
The original scratch driver fell back to local pyX12 on a 403
|
||||
quota response from Edifabric. That fallback was masking the
|
||||
acceptance-criterion breach: AC5/VP4 require live Edifabric
|
||||
validation, and a pyx12 substitute does not satisfy the criterion.
|
||||
This module deliberately has NO fallback. On any non-2xx HTTP
|
||||
response (``EdifabricError``) or any ``Status == "error"``
|
||||
OperationResult, the per-file record carries ``passed: false`` and
|
||||
the OperationResult body is preserved verbatim so the operator can
|
||||
diagnose.
|
||||
|
||||
Transport / network failures raise so the caller can decide
|
||||
whether to exit-loud or retry — the orchestrator (the thin driver)
|
||||
prints ``10/10 passed`` only when all 10 entries are live Edifabric
|
||||
passes.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from cyclone.edifabric import EdifabricError, validate_edi
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def is_edifabric_pass(result: dict) -> bool:
|
||||
"""Return True iff ``result`` is a passing ``OperationResult``.
|
||||
|
||||
Per the EdiNation docs, ``Status`` is one of:
|
||||
* ``"success"`` — passes.
|
||||
* ``"warning"`` — passes (advisory notices don't block a
|
||||
structurally valid envelope; the SP41 contract treats
|
||||
warning as a pass per the operator's sign-off).
|
||||
* ``"error"`` — fails; the Details array carries the failing
|
||||
segments / messages.
|
||||
* anything else — fail loud so we don't silently approve.
|
||||
"""
|
||||
status = (result.get("Status") or "").lower()
|
||||
return status in ("success", "warning")
|
||||
|
||||
|
||||
def validate_spot_check_files(paths: list[Path]) -> list[dict]:
|
||||
"""Submit each file in ``paths`` to Edifabric ``/v2/x12/validate``.
|
||||
|
||||
Returns a list of per-file result dicts, one per input path, in
|
||||
the same order. Each dict has the shape::
|
||||
|
||||
{
|
||||
"file": str, # path to the submitted file
|
||||
"passed": bool, # True only if Status is success/warning
|
||||
"issue": str, # human-readable verdict
|
||||
"result": dict | None, # OperationResult verbatim (None on transport error)
|
||||
"transport_error": dict | None, # {status_code, body} on EdifabricError
|
||||
}
|
||||
|
||||
Raises:
|
||||
EdifabricError: on the first transport failure. The caller
|
||||
decides whether to retry, exit non-zero, or fall back.
|
||||
This module never silently substitutes a different
|
||||
validator.
|
||||
"""
|
||||
import time
|
||||
out: list[dict] = []
|
||||
for i, p in enumerate(paths):
|
||||
body = p.read_bytes()
|
||||
# The Edifabric /v2/x12 endpoints apply a per-second rate limit
|
||||
# in addition to the daily quota (typical: 1 call/sec sustained,
|
||||
# 429 if exceeded). Insert a 1.5s pause between calls so 10-file
|
||||
# spot-checks don't trip the limit; this is the value the plan
|
||||
# flagged ("1-2 s spacing") that the original monolithic scratch
|
||||
# driver skipped.
|
||||
if i > 0:
|
||||
time.sleep(1.5)
|
||||
try:
|
||||
result = validate_edi(body)
|
||||
except EdifabricError as exc:
|
||||
tx: dict = {
|
||||
"status_code": exc.status_code,
|
||||
"body": exc.body,
|
||||
}
|
||||
if exc.retry_after_seconds is not None:
|
||||
tx["retry_after_seconds"] = exc.retry_after_seconds
|
||||
out.append({
|
||||
"file": str(p),
|
||||
"passed": False,
|
||||
"issue": (
|
||||
f"EdifabricError {exc.status_code}: {exc.body!r}"
|
||||
),
|
||||
"result": None,
|
||||
"transport_error": tx,
|
||||
})
|
||||
# Re-raise so the caller can't silently absorb the failure
|
||||
# (the original scratch driver caught + fallback'd which
|
||||
# is the bug this module fixes).
|
||||
raise
|
||||
|
||||
if is_edifabric_pass(result):
|
||||
out.append({
|
||||
"file": str(p),
|
||||
"passed": True,
|
||||
"issue": f"edifabric {result.get('Status')!r}",
|
||||
"result": result,
|
||||
"transport_error": None,
|
||||
})
|
||||
else:
|
||||
details = result.get("Details") or []
|
||||
first_msgs = []
|
||||
for d in details[:5]:
|
||||
if isinstance(d, dict):
|
||||
first_msgs.append(
|
||||
f"{d.get('SegmentId', '?')}: {d.get('Message', '?')}"
|
||||
)
|
||||
out.append({
|
||||
"file": str(p),
|
||||
"passed": False,
|
||||
"issue": (
|
||||
f"edifabric {result.get('Status')!r}: "
|
||||
+ ("; ".join(first_msgs) or "no details")
|
||||
),
|
||||
"result": result,
|
||||
"transport_error": None,
|
||||
})
|
||||
return out
|
||||
@@ -0,0 +1,57 @@
|
||||
"""Summary CSV for SP41.
|
||||
|
||||
One row per visit, classified into one of:
|
||||
REBILLED_A — pipeline A emitted a frequency-7 replacement
|
||||
REBILLED_B — pipeline B emitted a fresh 837P
|
||||
EXCLUDED_CARC — CO-45/CO-26/CO-129 (or other excluded CARC)
|
||||
EXCLUDED_PAYER — non-CO_TXIX payer per AxisCare API spot audit
|
||||
EXCLUDED_NO_EVV — no Authorized=Yes AND no AxisCare EVV ref
|
||||
EXCLUDED_TIMELY_FILING — DOS > 120 days before run date, no override
|
||||
"""
|
||||
import csv
|
||||
from dataclasses import dataclass
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
|
||||
from cyclone.rebill.reconcile import VisitRow
|
||||
|
||||
REBILLED_A = "REBILLED_A"
|
||||
REBILLED_B = "REBILLED_B"
|
||||
EXCLUDED_CARC = "EXCLUDED_CARC"
|
||||
EXCLUDED_PAYER = "EXCLUDED_PAYER"
|
||||
EXCLUDED_NO_EVV = "EXCLUDED_NO_EVV"
|
||||
EXCLUDED_TIMELY_FILING = "EXCLUDED_TIMELY_FILING"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SummaryRow:
|
||||
"""One visit + its post-pipeline disposition for the operator audit trail."""
|
||||
|
||||
visit: VisitRow
|
||||
disposition: str
|
||||
unpaid: Decimal
|
||||
cas_reasons: tuple[str, ...]
|
||||
file_path: str # relative to dev/rebills/<date>/
|
||||
|
||||
|
||||
def write_summary_csv(rows: list[SummaryRow], out_path: Path) -> int:
|
||||
"""Write the summary CSV; return the number of rows written."""
|
||||
out_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with out_path.open("w", newline="") as f:
|
||||
w = csv.writer(f)
|
||||
w.writerow([
|
||||
"dos", "member_id", "procedure", "billed",
|
||||
"disposition", "unpaid", "cas_reasons", "file_path",
|
||||
])
|
||||
for r in rows:
|
||||
w.writerow([
|
||||
r.visit.date.isoformat(),
|
||||
r.visit.member_id,
|
||||
r.visit.procedure,
|
||||
str(r.visit.billed),
|
||||
r.disposition,
|
||||
str(r.unpaid),
|
||||
"|".join(r.cas_reasons), # pipe-separated; no collision with J-prefixed member IDs or relative file paths
|
||||
r.file_path,
|
||||
])
|
||||
return len(rows)
|
||||
@@ -0,0 +1,37 @@
|
||||
"""120-day timely-filing gate for Colorado Medicaid.
|
||||
|
||||
Per HCPF's Colorado Medical Assistance Program provider manual, claims must
|
||||
be filed within 120 days from the date of service. The gate is HARD by
|
||||
default — past-window visits are surfaced as EXCLUDED_TIMELY_FILING in the
|
||||
summary CSV and not emitted by Pipeline B.
|
||||
|
||||
The operator may pass override=True per batch to relax the gate (e.g. for
|
||||
good-cause appeal visits). The decision is recorded in the summary CSV
|
||||
either way so the audit trail shows the override.
|
||||
"""
|
||||
from dataclasses import dataclass
|
||||
from datetime import date
|
||||
|
||||
FILING_WINDOW_DAYS = 120
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TimelyFilingDecision:
|
||||
dos: date
|
||||
as_of: date
|
||||
days_old: int
|
||||
within_window: bool
|
||||
override: bool
|
||||
rebillable: bool
|
||||
|
||||
|
||||
def timely_filing_decision(
|
||||
dos: date, as_of: date, override: bool
|
||||
) -> TimelyFilingDecision:
|
||||
days_old = (as_of - dos).days
|
||||
within_window = days_old <= FILING_WINDOW_DAYS
|
||||
return TimelyFilingDecision(
|
||||
dos=dos, as_of=as_of, days_old=days_old,
|
||||
within_window=within_window, override=override,
|
||||
rebillable=within_window or override,
|
||||
)
|
||||
@@ -0,0 +1,193 @@
|
||||
"""SP41: load the AxisCare visits export into the ``visits`` table.
|
||||
|
||||
The spot-check driver used to read the visits CSV in-memory. Persisting
|
||||
the roster into the database (a) makes the source-of-truth
|
||||
reviewable via SQL, and (b) lets the rebill pipeline (reconcile /
|
||||
resubmit) reference the same canonical visit list without re-parsing
|
||||
the CSV on every run.
|
||||
|
||||
Header (AxisCare export):
|
||||
"Client","Visit Date","Payer","Authorized","Member ID",
|
||||
"Auth Start Date","Auth End Date","Authorization #","ICD-10",
|
||||
"Service","Procedure Code","Modifiers","Client Classes",
|
||||
"Billable Hours","Billable Amount","Invoice #","Claimed"
|
||||
|
||||
DOS is MM/DD/YYYY. Billable Amount may carry ``$`` prefix and ``,``
|
||||
thousands separators. Modifiers may be ``:``-joined (batch 1) or
|
||||
``,``-joined (batch 2); we normalize to colon-joined for storage and
|
||||
downstream emission.
|
||||
|
||||
This module is intentionally side-effect-free on import — call
|
||||
:func:`load_visits_csv` to populate the table.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import logging
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from cyclone import db as db_mod
|
||||
from cyclone.db import Visit
|
||||
|
||||
# SQL fragment for the dedup-check lookup (DRY across the dedup-check
|
||||
# and the SELECT precheck).
|
||||
_NATURAL_KEY_COLS = (Visit.dos, Visit.member_id, Visit.procedure_code, Visit.modifiers)
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _normalize_modifiers(raw: str) -> str:
|
||||
"""``"KX:SC:U2"`` or ``"KX, SC, U2"`` → ``"KX:SC:U2"``.
|
||||
|
||||
We store colon-joined (matches the X12 SV1 modifier emission format).
|
||||
Empty / blank input returns "".
|
||||
"""
|
||||
if not raw:
|
||||
return ""
|
||||
parts = [m.strip() for m in raw.replace(":", ",").split(",") if m.strip()]
|
||||
return ":".join(parts)
|
||||
|
||||
|
||||
def _parse_billed(raw: str) -> Decimal:
|
||||
"""``"$212.77"`` / ``"1,234.56"`` → Decimal. Returns 0 on failure."""
|
||||
if not raw:
|
||||
return Decimal("0")
|
||||
cleaned = raw.replace("$", "").replace(",", "").strip()
|
||||
try:
|
||||
return Decimal(cleaned or "0")
|
||||
except Exception:
|
||||
return Decimal("0")
|
||||
|
||||
|
||||
def _parse_dos(raw: str) -> date | None:
|
||||
"""``"01/01/2026"`` → ``date(2026, 1, 1)``. None on failure."""
|
||||
raw = (raw or "").strip()
|
||||
if not raw:
|
||||
return None
|
||||
for fmt in ("%m/%d/%Y", "%Y-%m-%d"):
|
||||
try:
|
||||
return datetime.strptime(raw, fmt).date()
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def load_visits_csv(
|
||||
csv_path: Path,
|
||||
*,
|
||||
window_start: date | None = None,
|
||||
window_end: date | None = None,
|
||||
) -> int:
|
||||
"""Load the AxisCare visits export into the ``visits`` table.
|
||||
|
||||
Args:
|
||||
csv_path: Path to the CSV file.
|
||||
window_start: Optional inclusive lower bound on DOS.
|
||||
window_end: Optional inclusive upper bound on DOS.
|
||||
|
||||
Returns:
|
||||
Number of rows actually inserted (duplicates are skipped
|
||||
silently — the UNIQUE constraint on (dos, member_id, procedure,
|
||||
modifiers) dedupes the natural key).
|
||||
"""
|
||||
db_mod.init_db()
|
||||
SessionLocal = db_mod.SessionLocal()
|
||||
inserted = 0
|
||||
skipped = 0
|
||||
with SessionLocal() as session:
|
||||
with csv_path.open(newline="", encoding="utf-8") as f:
|
||||
reader = csv.DictReader(f)
|
||||
for row in reader:
|
||||
dos = _parse_dos(row.get("Visit Date") or "")
|
||||
if dos is None:
|
||||
skipped += 1
|
||||
continue
|
||||
if window_start and dos < window_start:
|
||||
continue
|
||||
if window_end and dos > window_end:
|
||||
continue
|
||||
member_id = (row.get("Member ID") or "").strip()
|
||||
procedure = (row.get("Procedure Code") or "").strip()
|
||||
if not member_id or not procedure:
|
||||
skipped += 1
|
||||
continue
|
||||
modifiers = _normalize_modifiers(row.get("Modifiers") or "")
|
||||
# Pre-check: does this natural-key already exist? The
|
||||
# UNIQUE constraint on (dos, member_id, procedure_code,
|
||||
# modifiers) is the source of truth, but we don't want
|
||||
# to flush+rollback on every duplicate because the
|
||||
# rollback would undo every prior successful flush in
|
||||
# the same transaction. An explicit SELECT keeps the
|
||||
# transaction shape clean.
|
||||
existing = session.execute(
|
||||
select(Visit.id).where(
|
||||
Visit.dos == dos,
|
||||
Visit.member_id == member_id,
|
||||
Visit.procedure_code == procedure,
|
||||
Visit.modifiers == (modifiers or None),
|
||||
)
|
||||
).first()
|
||||
if existing is not None:
|
||||
skipped += 1
|
||||
continue
|
||||
visit = Visit(
|
||||
dos=dos,
|
||||
member_id=member_id,
|
||||
client_name=(row.get("Client") or "").strip(),
|
||||
procedure_code=procedure,
|
||||
modifiers=modifiers or None,
|
||||
billed_amount=_parse_billed(row.get("Billable Amount") or ""),
|
||||
icd10=(row.get("ICD-10") or "").strip() or None,
|
||||
prior_auth=(row.get("Authorization #") or "").strip() or None,
|
||||
payer=(row.get("Payer") or "").strip() or None,
|
||||
invoice_number=(row.get("Invoice #") or "").strip() or None,
|
||||
source_file=str(csv_path),
|
||||
)
|
||||
session.add(visit)
|
||||
try:
|
||||
session.flush()
|
||||
inserted += 1
|
||||
except IntegrityError:
|
||||
# Race with a parallel writer — still a dup. Drop
|
||||
# the just-flushed INSERT and continue.
|
||||
session.rollback()
|
||||
skipped += 1
|
||||
session.commit()
|
||||
_log.info("load_visits_csv: %s -> %d inserted, %d skipped (dup/invalid)",
|
||||
csv_path.name, inserted, skipped)
|
||||
return inserted
|
||||
|
||||
|
||||
def query_visits(
|
||||
*,
|
||||
member_id: str | None = None,
|
||||
procedure_code: str | None = None,
|
||||
dos_start: date | None = None,
|
||||
dos_end: date | None = None,
|
||||
limit: int | None = None,
|
||||
) -> list[Visit]:
|
||||
"""Read visits back from the table. Filters are AND-combined.
|
||||
|
||||
Returns the matching rows as ORM objects (caller may need to detach
|
||||
or convert to a dataclass for downstream use).
|
||||
"""
|
||||
db_mod.init_db()
|
||||
SessionLocal = db_mod.SessionLocal()
|
||||
with SessionLocal() as session:
|
||||
stmt = select(Visit)
|
||||
if member_id is not None:
|
||||
stmt = stmt.where(Visit.member_id == member_id)
|
||||
if procedure_code is not None:
|
||||
stmt = stmt.where(Visit.procedure_code == procedure_code)
|
||||
if dos_start is not None:
|
||||
stmt = stmt.where(Visit.dos >= dos_start)
|
||||
if dos_end is not None:
|
||||
stmt = stmt.where(Visit.dos <= dos_end)
|
||||
if limit is not None:
|
||||
stmt = stmt.limit(limit)
|
||||
return list(session.execute(stmt).scalars().all())
|
||||
@@ -0,0 +1,43 @@
|
||||
"""cyclone.reissue — offline 837P re-emission workflow.
|
||||
|
||||
Pure functions for parsing raw 837P files into ``ClaimOutput`` Pydantic
|
||||
models and re-emitting one IG-correct single-claim X12 file per claim.
|
||||
No Click imports inside :mod:`cyclone.reissue.core`; the CLI wrapper at
|
||||
:mod:`cyclone.cli` is a thin shell over these functions.
|
||||
|
||||
Public surface:
|
||||
|
||||
- :func:`parse_inputs` — parse every ``*.x12`` / ``*.txt`` / ``*.edi``
|
||||
under a directory; tolerates per-file failures and skips claims with
|
||||
hard validation errors.
|
||||
- :func:`emit_outputs` — write one X12 per claim to ``output_dir``
|
||||
using HCPF-spec filenames; per-claim 1 ms timestamp offsets guarantee
|
||||
unique filenames within a batch.
|
||||
- :func:`write_summary_sidecar` — write a ``_serialize_summary.json``
|
||||
sidecar with one row per emitted file (operator audit trail).
|
||||
- :func:`zip_outputs` — zip the per-claim X12 files into a single flat
|
||||
archive with a ``testzip()`` integrity check.
|
||||
- :func:`ig_correctness_check` — verify the serializer's
|
||||
``PATIENT_LOOP_DEFAULT_INCLUDED`` is ``False`` (the IG-correct
|
||||
shape for SBR02 == "18" claims). See
|
||||
``docs/superpowers/specs/2026-07-08-cyclone-reissue-claims-design.md``
|
||||
for the SP24 rationale.
|
||||
|
||||
See `scripts/reissue_claims.py` for the deprecation shim; the
|
||||
canonical entry point is ``cyclone reissue-claims`` (SP24).
|
||||
"""
|
||||
from cyclone.reissue.core import (
|
||||
emit_outputs,
|
||||
ig_correctness_check,
|
||||
parse_inputs,
|
||||
write_summary_sidecar,
|
||||
zip_outputs,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"parse_inputs",
|
||||
"emit_outputs",
|
||||
"write_summary_sidecar",
|
||||
"zip_outputs",
|
||||
"ig_correctness_check",
|
||||
]
|
||||
@@ -0,0 +1,242 @@
|
||||
"""SP24 — pure functions for the offline 837P reissue workflow.
|
||||
|
||||
The functions here are deliberately Click-free so the CLI layer at
|
||||
:mod:`cyclone.cli` stays a thin shell, and so unit tests can exercise
|
||||
the behaviour without :class:`click.testing.CliRunner`.
|
||||
|
||||
Workflow contract:
|
||||
|
||||
>>> claims, errors = parse_inputs(input_dir, payer_config)
|
||||
>>> written = emit_outputs(claims, payer_config=..., output_dir=..., ...)
|
||||
>>> zip_outputs(written, zip_path)
|
||||
|
||||
``ig_correctness_check`` is the regression guard for the SP24 fix.
|
||||
The serializer's ``PATIENT_LOOP_DEFAULT_INCLUDED`` constant MUST be
|
||||
``False``; flipping it back to ``True`` would re-introduce the
|
||||
Edifabric 999 rejection on every SBR02 == "18" claim.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import zipfile
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from cyclone.edi.filenames import build_outbound_filename
|
||||
from cyclone.parsers.models import ClaimOutput
|
||||
from cyclone.parsers.parse_837 import parse as parse_837_text
|
||||
from cyclone.parsers import serialize_837 as _serialize_837_mod
|
||||
from cyclone.parsers.serialize_837 import serialize_837
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def ig_correctness_check(*, logger: logging.Logger | None = None) -> bool:
|
||||
"""Return True iff the serializer's IG-correct patient-loop default is in place.
|
||||
|
||||
Per X12 005010X222A1, the 2000C Patient Hierarchical Level
|
||||
(``HL*3 → PAT → NM1*QC``) MUST be absent when ``SBR02 == "18"``
|
||||
(Self-pay, the CO-Medicaid IHSS workflow). The serializer encodes
|
||||
this as a module-level constant ``PATIENT_LOOP_DEFAULT_INCLUDED``
|
||||
in :mod:`cyclone.parsers.serialize_837`; this function checks
|
||||
that constant.
|
||||
|
||||
Args:
|
||||
logger: optional logger for the diagnostic WARNING when the
|
||||
guard fires. Defaults to this module's logger.
|
||||
|
||||
Returns:
|
||||
True if the constant is ``False`` (IG-correct). False
|
||||
otherwise — callers should refuse to emit any X12 in that case.
|
||||
"""
|
||||
log = logger or _log
|
||||
# Read the constant live (not at import time) so monkeypatch in
|
||||
# tests reflects immediately and a runtime mutation by a future
|
||||
# config-loader also takes effect without a re-import.
|
||||
current = _serialize_837_mod.PATIENT_LOOP_DEFAULT_INCLUDED
|
||||
if current is not False:
|
||||
log.warning(
|
||||
"REFUSING to run: PATIENT_LOOP_DEFAULT_INCLUDED = %r "
|
||||
"(expected False). The IG-correct serializer shape for "
|
||||
"SBR02='18' claims requires the 2000C patient loop to be "
|
||||
"absent; restoring the default to False fixes it.",
|
||||
current,
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def parse_inputs(
|
||||
input_dir: Path,
|
||||
payer_config,
|
||||
) -> tuple[list[ClaimOutput], list[tuple[Path, str]]]:
|
||||
"""Parse every ``*.x12`` / ``*.txt`` / ``*.edi`` under ``input_dir``.
|
||||
|
||||
AppleDouble metadata files (``._foo.x12``, the macOS resource
|
||||
forks that Samba / Finder scatter) are skipped at the glob stage.
|
||||
|
||||
Per-file failures are tolerated: a file that raises during
|
||||
``parse_837_text`` lands in the second tuple element with the
|
||||
exception class + message. The first tuple element collects every
|
||||
surviving :class:`ClaimOutput`, including those with **warnings**
|
||||
(warnings don't abort). Claims with hard ``validation.errors``
|
||||
are dropped with an error message in the second tuple element.
|
||||
|
||||
Args:
|
||||
input_dir: directory to walk (one level).
|
||||
payer_config: a :class:`PayerConfig` instance passed to the
|
||||
parser — typically ``PayerConfig.co_medicaid()``.
|
||||
|
||||
Returns:
|
||||
``(claims, errors)`` where ``claims`` is the surviving
|
||||
:class:`ClaimOutput` list and ``errors`` is a list of
|
||||
``(path, message)`` tuples for each per-file failure.
|
||||
"""
|
||||
raw_files: list[Path] = []
|
||||
for ext in ("*.x12", "*.txt", "*.edi"):
|
||||
for p in sorted(input_dir.glob(ext)):
|
||||
# Skip macOS AppleDouble metadata (._<name>) — binary
|
||||
# forks of the resource fork, not actual EDI.
|
||||
if p.name.startswith("._"):
|
||||
continue
|
||||
raw_files.append(p)
|
||||
|
||||
claims: list[ClaimOutput] = []
|
||||
errors: list[tuple[Path, str]] = []
|
||||
|
||||
if not raw_files:
|
||||
errors.append((input_dir, "no *.x12 / *.txt / *.edi files found"))
|
||||
for f in raw_files:
|
||||
try:
|
||||
text = f.read_text(encoding="utf-8")
|
||||
result = parse_837_text(text, payer_config, input_file=str(f))
|
||||
except Exception as exc: # noqa: BLE001 — log + skip
|
||||
errors.append((f, f"{exc.__class__.__name__}: {exc}"))
|
||||
continue
|
||||
if not result.claims:
|
||||
errors.append((f, "no claims parsed (empty CLM loop?)"))
|
||||
continue
|
||||
for c in result.claims:
|
||||
if c.validation.errors:
|
||||
msgs = [f"{i.rule}: {i.message}" for i in c.validation.errors]
|
||||
errors.append((f, f"hard errors in {c.claim_id}: {msgs}"))
|
||||
continue
|
||||
claims.append(c)
|
||||
return claims, errors
|
||||
|
||||
|
||||
def emit_outputs(
|
||||
claims: list[ClaimOutput],
|
||||
*,
|
||||
payer_config,
|
||||
output_dir: Path,
|
||||
sender_id: str,
|
||||
receiver_id: str,
|
||||
submitter_name: str,
|
||||
submitter_contact_name: str,
|
||||
submitter_contact_email: str,
|
||||
receiver_name: str,
|
||||
) -> list[Path]:
|
||||
"""Write one X12 per claim under ``output_dir`` using HCPF-spec filenames.
|
||||
|
||||
Per-claim 1 ms timestamp offsets on the base datetime guarantee
|
||||
unique filenames within a batch. The output directory is created
|
||||
if it doesn't exist. Claims are sorted by ``claim_id`` before
|
||||
emission so the per-claim filenames line up with the canonical
|
||||
sort order.
|
||||
|
||||
Args:
|
||||
claims: parsed :class:`ClaimOutput` instances (typically the
|
||||
first tuple element from :func:`parse_inputs`).
|
||||
payer_config: a :class:`PayerConfig` — threads
|
||||
``sbr09_claim_filing`` into the SBR09 segment.
|
||||
output_dir: directory to write the per-claim X12 files into.
|
||||
sender_id, receiver_id, submitter_*, receiver_name: envelope
|
||||
metadata threaded into the ISA/GS/NM1*41/NM1*40 segments.
|
||||
|
||||
Returns:
|
||||
The list of written :class:`Path` instances, in the same
|
||||
order as the sorted claims.
|
||||
"""
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
# America/Denver is the operator's home tz and the canonical
|
||||
# timezone for HCPF submission timestamps; matching the tz
|
||||
# keeps the 999 round-trip deterministic for the operator's
|
||||
# daily pull.
|
||||
base_ts = datetime.now(tz=ZoneInfo("America/Denver"))
|
||||
|
||||
written: list[Path] = []
|
||||
for i, claim in enumerate(sorted(claims, key=lambda c: c.claim_id), start=1):
|
||||
body = serialize_837(
|
||||
claim,
|
||||
sender_id=sender_id,
|
||||
receiver_id=receiver_id,
|
||||
submitter_name=submitter_name,
|
||||
submitter_contact_name=submitter_contact_name,
|
||||
submitter_contact_email=submitter_contact_email,
|
||||
receiver_name=receiver_name,
|
||||
claim_filing_indicator_code=payer_config.sbr09_claim_filing,
|
||||
interchange_control_number=f"{i:09d}",
|
||||
group_control_number="1",
|
||||
)
|
||||
ts = base_ts + timedelta(milliseconds=i)
|
||||
fname = build_outbound_filename(sender_id, "837P", now_mt=ts)
|
||||
path = output_dir / fname
|
||||
path.write_text(body, encoding="ascii")
|
||||
written.append(path)
|
||||
return written
|
||||
|
||||
|
||||
def write_summary_sidecar(
|
||||
claims: list[ClaimOutput],
|
||||
written: list[Path],
|
||||
output_dir: Path,
|
||||
) -> Path:
|
||||
"""Write ``_serialize_summary.json`` next to the per-claim X12 files.
|
||||
|
||||
Operator-facing sidecar for audit: one row per emitted file with
|
||||
``claim_id``, ``output_file`` (absolute path), and ``byte_size``.
|
||||
Both ``claims`` and ``written`` are zipped in the same order as
|
||||
``emit_outputs`` produced them (i.e. sorted by ``claim_id``).
|
||||
|
||||
Returns the path of the written sidecar.
|
||||
"""
|
||||
summary = [
|
||||
{
|
||||
"claim_id": c.claim_id,
|
||||
"output_file": str(p),
|
||||
"byte_size": p.stat().st_size,
|
||||
}
|
||||
for c, p in zip(sorted(claims, key=lambda c: c.claim_id), written)
|
||||
]
|
||||
path = output_dir / "_serialize_summary.json"
|
||||
path.write_text(json.dumps(summary, indent=2), encoding="utf-8")
|
||||
return path
|
||||
|
||||
|
||||
def zip_outputs(files: list[Path], zip_path: Path) -> None:
|
||||
"""Zip the per-claim X12 files into a single flat archive.
|
||||
|
||||
Uses :class:`zipfile.ZIP_DEFLATED`. After writing, runs
|
||||
:meth:`zipfile.ZipFile.testzip` to verify CRC integrity and
|
||||
raises :class:`RuntimeError` if any entry is corrupt.
|
||||
|
||||
Args:
|
||||
files: the per-claim X12 file paths (typically the return
|
||||
value of :func:`emit_outputs`).
|
||||
zip_path: destination archive path; parent directories are
|
||||
created as needed.
|
||||
|
||||
Raises:
|
||||
RuntimeError: if any entry fails the ``testzip()`` integrity
|
||||
check.
|
||||
"""
|
||||
zip_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||
for p in files:
|
||||
zf.write(p, arcname=p.name)
|
||||
bad = zipfile.ZipFile(zip_path).testzip()
|
||||
if bad is not None:
|
||||
raise RuntimeError(f"zip integrity check failed: {bad} is corrupt")
|
||||
@@ -155,4 +155,10 @@ def has_keyring() -> bool:
|
||||
# ``CYCLONE_SFTP_PASSWORD`` (env var).
|
||||
_ENV_NAME_FOR: dict[str, str] = {
|
||||
"sftp.gainwell.password": "CYCLONE_SFTP_PASSWORD",
|
||||
# SP40: Edifabric /v2/x12/validate API key. Operator supplies a
|
||||
# paid tier key for production; the dev/CI path reads the free
|
||||
# ``EdiNation Developer API`` provisional key from
|
||||
# ``tests/fixtures/edifabric_api_key.txt`` (gitignored copy in
|
||||
# production deployments — see Task 6 plan).
|
||||
"edifabric.api_key": "CYCLONE_EDIFABRIC_API_KEY",
|
||||
}
|
||||
|
||||
@@ -196,8 +196,13 @@ class RateLimitMiddleware:
|
||||
|
||||
On unexpected errors the limiter fails OPEN — better to serve a
|
||||
few extra requests than to 503 every request because of a bug.
|
||||
|
||||
The ``_buckets`` dict is class-level so every instance shares the
|
||||
same per-IP sliding-window state. See ``__init__`` for why.
|
||||
"""
|
||||
|
||||
_buckets: dict[str, "_Bucket"] = {}
|
||||
|
||||
EXEMPT_PATHS = ("/api/health", "/healthz", "/readyz")
|
||||
|
||||
def __init__(
|
||||
@@ -211,7 +216,19 @@ class RateLimitMiddleware:
|
||||
"CYCLONE_RATE_LIMIT_PER_MIN", DEFAULT_RATE_LIMIT_PER_MIN,
|
||||
)
|
||||
self.window_s = window_s or DEFAULT_RATE_LIMIT_WINDOW_S
|
||||
self._buckets: dict[str, _Bucket] = {}
|
||||
# _buckets is a class-level dict so every RateLimitMiddleware
|
||||
# instance shares the same per-IP sliding-window state.
|
||||
# This matters because tests that call
|
||||
# ``importlib.reload(cyclone.api)`` cause Starlette to rebuild
|
||||
# the middleware stack with a fresh instance whose private
|
||||
# bucket would otherwise be independent of the old one — and
|
||||
# tests that imported ``from cyclone.api import app`` at
|
||||
# module load time keep referencing the old instance, so
|
||||
# their requests would accumulate in the orphaned bucket and
|
||||
# trip the limiter after ~300 requests. Sharing the dict
|
||||
# makes one ``_buckets.clear()`` (in the conftest's reset
|
||||
# hook) clear every instance at once.
|
||||
self._buckets: dict[str, _Bucket] = type(self)._buckets
|
||||
self._lock = threading.Lock()
|
||||
|
||||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
||||
|
||||
@@ -38,7 +38,9 @@ Backward-compat shims for tests:
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import threading
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
|
||||
@@ -87,9 +89,15 @@ from .claim_acks import (
|
||||
list_acks_for_claim as _list_acks_for_claim,
|
||||
list_claims_for_ack as _list_claims_for_ack,
|
||||
remove_claim_ack as _remove_claim_ack,
|
||||
_iter_orphan_999_st02s as _iter_orphan_999_st02s,
|
||||
)
|
||||
from .backups import add_backup_pending
|
||||
from .exceptions import AlreadyMatchedError, InvalidStateError, NotMatchedError
|
||||
from .resubmissions import (
|
||||
ResubmissionStatus,
|
||||
find_resubmission_status,
|
||||
record_resubmission,
|
||||
)
|
||||
from .inbox import list_unmatched, manual_match, manual_unmatch
|
||||
from .kpis import dashboard_kpis
|
||||
from .orm_builders import (
|
||||
@@ -320,12 +328,188 @@ class CycloneStore:
|
||||
"""Return acks with no resolvable link (Inbox ack-orphans lane)."""
|
||||
return _find_ack_orphans(kind)
|
||||
|
||||
def record_resubmission(self, *, claim_id, batch_id, source_corrected_path,
|
||||
interchange_control_number, group_control_number,
|
||||
resubmitted_at=None):
|
||||
"""SP39: insert one Resubmission audit row. Idempotent on
|
||||
(claim_id, interchange_control_number). Returns True if
|
||||
inserted, False if a duplicate was suppressed."""
|
||||
return record_resubmission(
|
||||
claim_id=claim_id,
|
||||
batch_id=batch_id,
|
||||
source_corrected_path=source_corrected_path,
|
||||
interchange_control_number=interchange_control_number,
|
||||
group_control_number=group_control_number,
|
||||
resubmitted_at=resubmitted_at,
|
||||
)
|
||||
|
||||
def find_resubmission_status(self, *, batch_id=None):
|
||||
"""SP39: return joined status for every Resubmission row,
|
||||
optionally filtered by batch_id. Statuses are derived at
|
||||
read-time from claim_acks (SP28/31 auto-link) + remittances."""
|
||||
return find_resubmission_status(batch_id=batch_id)
|
||||
|
||||
def find_ack_orphan_st02_summary(self):
|
||||
"""Return per-ST02 summary of orphan 999 acks (sp38).
|
||||
|
||||
Output is a list of dicts, sorted by ``ack_count DESC`` so
|
||||
the heaviest orphan ST02s surface first in CLI output:
|
||||
|
||||
``{"st02": str, "ack_count": int, "has_batch": bool,
|
||||
"batch_id": str | None}``
|
||||
|
||||
``has_batch`` is True if a row exists in ``batches`` with
|
||||
``transaction_set_control_number == st02``. ``batch_id`` is
|
||||
that row's ``id`` (or ``None``).
|
||||
|
||||
999-only. 277ca / ta1 orphans are tracked separately; their
|
||||
"ST02" semantics differ (it's the interchange control number,
|
||||
not the source 837's ST02) and aggregating them with 999
|
||||
ST02s would conflate unrelated identifiers.
|
||||
|
||||
Used by the ``cyclone ack-orphans status`` and ``reconcile``
|
||||
CLI subcommands (sp38). Sibling to ``find_ack_orphans(kind)``
|
||||
which returns one row per orphan ack; the summary is the
|
||||
aggregated form.
|
||||
"""
|
||||
from cyclone import db
|
||||
from cyclone.db import Batch
|
||||
|
||||
# (st02, ack_count) from the 999-orphan walk.
|
||||
counts = dict(_iter_orphan_999_st02s())
|
||||
|
||||
if not counts:
|
||||
return []
|
||||
|
||||
# Map ST02 -> existing batch row (if any). One query.
|
||||
with db.SessionLocal()() as s:
|
||||
existing = {
|
||||
row.transaction_set_control_number: row.id
|
||||
for row in s.query(Batch)
|
||||
.filter(Batch.transaction_set_control_number.in_(counts.keys()))
|
||||
.all()
|
||||
}
|
||||
|
||||
out = []
|
||||
for st02, ack_count in counts.items():
|
||||
batch_id = existing.get(st02)
|
||||
out.append({
|
||||
"st02": st02,
|
||||
"ack_count": ack_count,
|
||||
"has_batch": batch_id is not None,
|
||||
"batch_id": batch_id,
|
||||
})
|
||||
# Heaviest first; tie-break by st02 ascending for determinism.
|
||||
out.sort(key=lambda r: (-r["ack_count"], r["st02"]))
|
||||
return out
|
||||
|
||||
def reconcile_orphan_st02s(self, *, dry_run: bool = False) -> dict:
|
||||
"""One-shot synthetic-batch seeder for orphan ST02s (sp38).
|
||||
|
||||
Inserts a ``batches`` row for every orphan ST02 that does
|
||||
NOT already have one. The synthetic row is marked with:
|
||||
|
||||
* ``kind = '837p'``
|
||||
* ``input_filename = '<synthetic:orphan-reconcile>'``
|
||||
* ``parsed_at = utcnow()``
|
||||
* ``transaction_set_control_number = <orphan ST02>``
|
||||
* ``totals_json = {"orphan_reconcile": true,
|
||||
"ack_count": <orphan count>}``
|
||||
* ``validation_json = {"orphan_reconcile": true,
|
||||
"note": "sp38 synthetic batch row;
|
||||
source 837 was never ingested into
|
||||
this DB snapshot"}``
|
||||
* ``id = uuid4().hex`` (no claim rows, no claim_acks links)
|
||||
|
||||
Remaining columns (``claim_count``, ``received_count``, …) take
|
||||
their schema defaults.
|
||||
|
||||
The sentinel ``<synthetic:orphan-reconcile>`` makes these
|
||||
rows trivially distinguishable in queries — grep for that
|
||||
string to find every row this method has created.
|
||||
|
||||
Future 999 acks referencing these ST02s will resolve
|
||||
against the batch envelope index (so the operator can see
|
||||
"this 999 is for a known orphan source") but will not link
|
||||
to claims (because no claim rows exist for the synthetic
|
||||
batch).
|
||||
|
||||
Args:
|
||||
dry_run: If True, returns the plan but does not insert
|
||||
any rows. Used by the CLI ``--dry-run`` flag so
|
||||
operators can preview the reconcile.
|
||||
|
||||
Returns:
|
||||
``{"created": int, "skipped": int,
|
||||
"synthetic_batch_ids": list[str]}``. ``created`` is
|
||||
the count of rows inserted (or that WOULD be inserted
|
||||
under dry_run). ``skipped`` is the count of orphan ST02s
|
||||
that already had a batches row.
|
||||
|
||||
Idempotent: re-running after a successful pass is a no-op.
|
||||
"""
|
||||
from cyclone import db
|
||||
from cyclone.db import Batch
|
||||
|
||||
summary = self.find_ack_orphan_st02_summary()
|
||||
if not summary:
|
||||
return {"created": 0, "skipped": 0, "synthetic_batch_ids": []}
|
||||
|
||||
created = 0
|
||||
skipped = 0
|
||||
synthetic_ids: list[str] = []
|
||||
|
||||
if dry_run:
|
||||
for row in summary:
|
||||
if row["has_batch"]:
|
||||
skipped += 1
|
||||
else:
|
||||
created += 1 # would-create count
|
||||
return {"created": created, "skipped": skipped, "synthetic_batch_ids": []}
|
||||
|
||||
now = utcnow()
|
||||
with db.SessionLocal()() as s:
|
||||
for row in summary:
|
||||
if row["has_batch"]:
|
||||
skipped += 1
|
||||
continue
|
||||
new_id = uuid.uuid4().hex
|
||||
s.add(Batch(
|
||||
id=new_id,
|
||||
kind="837p",
|
||||
input_filename="<synthetic:orphan-reconcile>",
|
||||
parsed_at=now,
|
||||
transaction_set_control_number=row["st02"],
|
||||
totals_json=json.dumps({
|
||||
"orphan_reconcile": True,
|
||||
"ack_count": row["ack_count"],
|
||||
}),
|
||||
validation_json=json.dumps({
|
||||
"orphan_reconcile": True,
|
||||
"note": (
|
||||
"sp38 synthetic batch row; source 837 was "
|
||||
"never ingested into this DB snapshot"
|
||||
),
|
||||
}),
|
||||
))
|
||||
synthetic_ids.append(new_id)
|
||||
created += 1
|
||||
s.commit()
|
||||
|
||||
return {"created": created, "skipped": skipped, "synthetic_batch_ids": synthetic_ids}
|
||||
|
||||
def remove_claim_ack(self, link_id, *, event_bus=None):
|
||||
"""Unlink one row. Publishes ``claim_ack_dropped`` on the bus."""
|
||||
return _remove_claim_ack(link_id, event_bus=event_bus)
|
||||
|
||||
def batch_envelope_index(self):
|
||||
"""Return a {envelope.control_number: batch.id} map for D10 Pass 1."""
|
||||
"""Return a {key: batch.id} map populated from two columns (D10 Pass 1).
|
||||
|
||||
Each 837p batch contributes both ``Batch.raw_result_json.envelope.control_number``
|
||||
(ISA13) and ``Batch.transaction_set_control_number`` (ST02); 835 batches
|
||||
are excluded. Single ``.get(set_control_number)`` lookup resolves
|
||||
either key — SP37 closes the 999 AK201 → source-batch gap.
|
||||
"""
|
||||
return _batch_envelope_index()
|
||||
|
||||
# -- SP17: encrypted DB backups -------------------------------------
|
||||
|
||||
@@ -9,7 +9,8 @@ a fresh DB state per-test.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timezone
|
||||
from datetime import date, timezone
|
||||
from decimal import Decimal
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.db import (
|
||||
@@ -20,12 +21,85 @@ from cyclone.db import (
|
||||
Match,
|
||||
Remittance,
|
||||
)
|
||||
from cyclone.parsers.models import ParseResult
|
||||
from cyclone.parsers.models_835 import ParseResult835
|
||||
from cyclone.parsers.models import BatchSummary, Envelope, ParseResult
|
||||
from cyclone.parsers.models_835 import (
|
||||
FinancialInfo,
|
||||
ParseResult835,
|
||||
Payee835,
|
||||
Payer835,
|
||||
ReassociationTrace,
|
||||
)
|
||||
|
||||
from .records import BatchRecord, BatchRecord837, BatchRecord835
|
||||
|
||||
|
||||
def _stub_parse_result_837(row: Batch) -> ParseResult:
|
||||
"""Synthesize a minimal ``ParseResult`` for a 837p row missing ``raw_result_json``.
|
||||
|
||||
SP25: ``CycloneStore.reconcile_orphan_st02s()`` seeds anchor ``Batch``
|
||||
rows for orphan 999 ACK ST02s with ``raw_result_json=NULL`` — no
|
||||
source 837 was ever parsed for them. Returning a typed stub keeps
|
||||
the dashboard's recent-batches list and the ``/api/batches`` endpoint
|
||||
rendering instead of 500'ing on Pydantic validation.
|
||||
|
||||
All counters zero, summary.control_number echoes the row's
|
||||
``transaction_set_control_number`` (the SCN the orphan 999 acks
|
||||
reference), so the batch list widget surfaces the right SCN.
|
||||
"""
|
||||
today = row.parsed_at.date() if row.parsed_at else date.today()
|
||||
return ParseResult(
|
||||
envelope=None,
|
||||
claims=[],
|
||||
summary=BatchSummary(
|
||||
input_file=row.input_filename,
|
||||
control_number=row.transaction_set_control_number,
|
||||
transaction_date=today,
|
||||
total_claims=0,
|
||||
passed=0,
|
||||
failed=0,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _stub_parse_result_835(row: Batch) -> ParseResult835:
|
||||
"""Synthesize a minimal ``ParseResult835`` for an 835 row missing ``raw_result_json``.
|
||||
|
||||
Mirrors ``_stub_parse_result_837``; 835 requires non-None envelope,
|
||||
financial_info, trace, payer, payee — minimal real-value shells are
|
||||
the only way Pydantic will accept the stub.
|
||||
"""
|
||||
today = row.parsed_at.date() if row.parsed_at else date.today()
|
||||
return ParseResult835(
|
||||
envelope=Envelope(
|
||||
sender_id="",
|
||||
receiver_id="",
|
||||
control_number=row.transaction_set_control_number or "",
|
||||
transaction_date=today,
|
||||
),
|
||||
financial_info=FinancialInfo(
|
||||
handling_code="",
|
||||
paid_amount=Decimal("0.00"),
|
||||
credit_debit_flag="",
|
||||
),
|
||||
trace=ReassociationTrace(
|
||||
trace_type_code="",
|
||||
trace_number="",
|
||||
originating_company_id="",
|
||||
),
|
||||
payer=Payer835(name=""),
|
||||
payee=Payee835(name="", npi=""),
|
||||
claims=[],
|
||||
summary=BatchSummary(
|
||||
input_file=row.input_filename,
|
||||
control_number=row.transaction_set_control_number,
|
||||
transaction_date=today,
|
||||
total_claims=0,
|
||||
passed=0,
|
||||
failed=0,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class _BatchesShim:
|
||||
"""Drop-in replacement for the old in-memory ``_batches`` list.
|
||||
|
||||
@@ -59,13 +133,26 @@ def _row_to_record(row: Batch) -> BatchRecord:
|
||||
is ``DateTime(timezone=True)``. We re-attach UTC so the
|
||||
``BatchRecord`` validator (``parsed_at must be tz-aware``)
|
||||
passes.
|
||||
|
||||
SP25: if ``raw_result_json`` is missing or empty (e.g. the row
|
||||
was seeded by ``reconcile_orphan_st02s()`` and never had a
|
||||
real parse attached), we hydrate a typed *stub* with empty
|
||||
claim lists and zero counters so the dashboard's recent-batches
|
||||
list can still render the row. ``raw_result_json`` is left
|
||||
untouched — the stub is read-side only. See
|
||||
``_stub_parse_result_837`` / ``_stub_parse_result_835``.
|
||||
"""
|
||||
if row.kind == "835":
|
||||
result_cls = ParseResult835
|
||||
else:
|
||||
result_cls = ParseResult
|
||||
payload = row.raw_result_json or {}
|
||||
payload = row.raw_result_json
|
||||
if payload:
|
||||
result = result_cls.model_validate(payload)
|
||||
elif row.kind == "835":
|
||||
result = _stub_parse_result_835(row)
|
||||
else:
|
||||
result = _stub_parse_result_837(row)
|
||||
parsed_at = row.parsed_at
|
||||
if parsed_at is not None and parsed_at.tzinfo is None:
|
||||
parsed_at = parsed_at.replace(tzinfo=timezone.utc)
|
||||
|
||||
@@ -11,8 +11,10 @@ Five methods on top of the new ``ClaimAck`` ORM table:
|
||||
ack-orphans lane).
|
||||
* ``remove_claim_ack`` — unlink. Publishes ``claim_ack_dropped``.
|
||||
* ``batch_envelope_index`` — D10 in-memory map of
|
||||
``Batch.envelope.control_number → batch.id`` (cheap to rebuild;
|
||||
re-built once per ingest).
|
||||
``{Batch.raw_result_json.envelope.control_number (ISA13) OR
|
||||
Batch.transaction_set_control_number (ST02)} → batch.id``
|
||||
(SP37: populated from two columns; cheap to rebuild, re-built
|
||||
once per ingest).
|
||||
|
||||
Each mutating method opens its own ``db.SessionLocal()()`` session
|
||||
so callers don't have to manage session lifecycles. Publishes are
|
||||
@@ -22,9 +24,10 @@ subscriber cannot roll back the persisted row.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, Iterator
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.db import (
|
||||
@@ -67,26 +70,43 @@ def _safe_publish(event_bus: "EventBus | None", kind: str, payload: dict) -> Non
|
||||
|
||||
|
||||
def batch_envelope_index() -> dict[str, str]:
|
||||
"""Build a ``{envelope.control_number: batch.id}`` map.
|
||||
"""Build a ``{key: batch.id}`` map populated from two columns.
|
||||
|
||||
D10 primary join key (spec §D10). Built once per ingest from
|
||||
every Batch row whose ``raw_result_json`` carries an envelope —
|
||||
cost is O(N batches), currently ~16, so trivial. Kept as a
|
||||
plain dict so callers can ``index.get(scn)`` to resolve.
|
||||
D10 primary join key (spec §D10). Each 837p batch row contributes
|
||||
up to two entries:
|
||||
|
||||
* ``Batch.raw_result_json.envelope.control_number`` (ISA13)
|
||||
* ``Batch.transaction_set_control_number`` (ST02, new in SP37)
|
||||
|
||||
Either key resolves to the same ``batch.id``; callers do a single
|
||||
``idx.get(set_control_number)`` lookup. Pass 2 (PCN) is unchanged.
|
||||
|
||||
Built once per ingest from every Batch row whose kind is "837p";
|
||||
cost is O(N batches), currently small, so trivial. 835 batches
|
||||
are excluded — the column is an 837P-specific join key (see
|
||||
``Batch.transaction_set_control_number`` docstring).
|
||||
"""
|
||||
out: dict[str, str] = {}
|
||||
idx: dict[str, str] = {}
|
||||
with db.SessionLocal()() as s:
|
||||
rows = (
|
||||
s.query(Batch.id, Batch.raw_result_json)
|
||||
s.query(
|
||||
Batch.id,
|
||||
Batch.raw_result_json,
|
||||
Batch.transaction_set_control_number,
|
||||
)
|
||||
.filter(Batch.kind == "837p")
|
||||
.all()
|
||||
)
|
||||
for bid, raw in rows:
|
||||
for bid, raw, stcn in rows:
|
||||
env = (raw or {}).get("envelope") or {}
|
||||
ctrl = env.get("control_number")
|
||||
if isinstance(ctrl, str) and ctrl:
|
||||
out[ctrl] = bid
|
||||
return out
|
||||
isa_cn = env.get("control_number")
|
||||
if isinstance(isa_cn, str) and isa_cn:
|
||||
# First write wins (setdefault) — if ISA13 and ST02
|
||||
# ever collide they map to the same batch anyway.
|
||||
idx.setdefault(isa_cn, bid)
|
||||
if isinstance(stcn, str) and stcn:
|
||||
idx.setdefault(stcn, bid)
|
||||
return idx
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -257,77 +277,29 @@ def find_ack_orphans(kind: str) -> list[dict]:
|
||||
out: list[dict] = []
|
||||
if kind == "999":
|
||||
ack_table = Ack
|
||||
ctrl_attr = None
|
||||
elif kind == "277ca":
|
||||
ack_table = Two77caAck
|
||||
ctrl_attr = "control_number"
|
||||
else:
|
||||
ack_table = Ta1Ack
|
||||
ctrl_attr = "control_number"
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
# Every ack row of the given kind, with a LEFT JOIN against
|
||||
# any claim_acks link; orphan when NO link was created.
|
||||
if kind == "999":
|
||||
# For 999, the "ack has no link" means no ClaimAck row
|
||||
# was emitted at all (the auto-linker emits one per AK2
|
||||
# even when the AK2 is rejected, so 999 with at least
|
||||
# one AK2 that resolved to a claim is never an orphan).
|
||||
# We treat a 999 as orphan when it has zero ClaimAck
|
||||
# rows tied to its id.
|
||||
all_acks = s.query(Ack).order_by(Ack.id.desc()).all()
|
||||
for ack_row in all_acks:
|
||||
# Every ack row of the given kind; orphan when NO claim_acks
|
||||
# link was created for it. The auto-linker emits one claim_acks
|
||||
# row per AK2 even when the AK2 is rejected, so a 999 with at
|
||||
# least one linked AK2 is never an orphan.
|
||||
for ack_row in s.query(ack_table).order_by(ack_table.id.desc()).all():
|
||||
count = (
|
||||
s.query(ClaimAck)
|
||||
.filter(ClaimAck.ack_kind == "999",
|
||||
.filter(ClaimAck.ack_kind == kind,
|
||||
ClaimAck.ack_id == ack_row.id)
|
||||
.count()
|
||||
)
|
||||
if count == 0:
|
||||
if count > 0:
|
||||
continue
|
||||
out.append({
|
||||
"kind": "999",
|
||||
"kind": kind,
|
||||
"ack_id": ack_row.id,
|
||||
"control_number": _ack_control_number(ack_row, "999"),
|
||||
"parsed_at": (
|
||||
ack_row.parsed_at.isoformat().replace(
|
||||
"+00:00", "Z"
|
||||
) if ack_row.parsed_at else None
|
||||
),
|
||||
})
|
||||
elif kind == "277ca":
|
||||
all_acks = s.query(Two77caAck).order_by(Two77caAck.id.desc()).all()
|
||||
for ack_row in all_acks:
|
||||
count = (
|
||||
s.query(ClaimAck)
|
||||
.filter(ClaimAck.ack_kind == "277ca",
|
||||
ClaimAck.ack_id == ack_row.id)
|
||||
.count()
|
||||
)
|
||||
if count == 0:
|
||||
out.append({
|
||||
"kind": "277ca",
|
||||
"ack_id": ack_row.id,
|
||||
"control_number": ack_row.control_number or "",
|
||||
"parsed_at": (
|
||||
ack_row.parsed_at.isoformat().replace(
|
||||
"+00:00", "Z"
|
||||
) if ack_row.parsed_at else None
|
||||
),
|
||||
})
|
||||
else:
|
||||
all_acks = s.query(Ta1Ack).order_by(Ta1Ack.id.desc()).all()
|
||||
for ack_row in all_acks:
|
||||
count = (
|
||||
s.query(ClaimAck)
|
||||
.filter(ClaimAck.ack_kind == "ta1",
|
||||
ClaimAck.ack_id == ack_row.id)
|
||||
.count()
|
||||
)
|
||||
if count == 0:
|
||||
out.append({
|
||||
"kind": "ta1",
|
||||
"ack_id": ack_row.id,
|
||||
"control_number": ack_row.control_number or "",
|
||||
"control_number": _ack_control_number(ack_row, kind),
|
||||
"parsed_at": (
|
||||
ack_row.parsed_at.isoformat().replace(
|
||||
"+00:00", "Z"
|
||||
@@ -337,17 +309,120 @@ def find_ack_orphans(kind: str) -> list[dict]:
|
||||
return out
|
||||
|
||||
|
||||
def _ack_control_number(ack_row: Ack, kind: str) -> str:
|
||||
"""Best-effort control-number lookup for a 999 ack row.
|
||||
def _iter_orphan_999_st02s() -> Iterator[tuple[str, int]]:
|
||||
"""Yield ``(set_control_number, orphan_count)`` for each distinct orphan 999.
|
||||
|
||||
The 999 ORM row doesn't carry the envelope's control_number in
|
||||
a dedicated column; we re-derive it from ``raw_json`` (the same
|
||||
source :func:`cyclone.store.ui.to_ui_ack` uses for the patient
|
||||
control number).
|
||||
An orphan 999 has zero ``claim_acks`` rows tied to its id (per
|
||||
:func:`find_ack_orphans`). The ST02 is the source 837's
|
||||
``transaction_set_control_number``, extracted from the 999's
|
||||
``set_responses[0].set_control_number`` field in ``raw_json``.
|
||||
|
||||
Used by :meth:`CycloneStore.find_ack_orphan_st02_summary` and
|
||||
:meth:`CycloneStore.reconcile_orphan_st02s` (sp38) to enumerate
|
||||
orphan ST02s without re-implementing the orphan-detection query.
|
||||
|
||||
Skips 999s whose ``raw_json`` is malformed or has no
|
||||
``set_responses`` entry — those are unprocessable regardless of
|
||||
whether they have a matching batch row.
|
||||
|
||||
999-only. 277ca / ta1 orphans are tracked separately by the
|
||||
store and are not aggregated here (their "ST02" semantics differ
|
||||
from 999: it's the interchange control number, not the source
|
||||
837's ST02).
|
||||
"""
|
||||
raw = ack_row.raw_json or {}
|
||||
with db.SessionLocal()() as s:
|
||||
# LEFT OUTER JOIN keeps all acks; orphan when no claim_acks row
|
||||
# exists for (ack_kind='999', ack_id=acks.id).
|
||||
rows = (
|
||||
s.query(Ack)
|
||||
.outerjoin(
|
||||
ClaimAck,
|
||||
(ClaimAck.ack_kind == "999") & (ClaimAck.ack_id == Ack.id),
|
||||
)
|
||||
.filter(ClaimAck.id.is_(None))
|
||||
.all()
|
||||
)
|
||||
counts: dict[str, int] = {}
|
||||
for ack_row in rows:
|
||||
st02 = _extract_999_st02(ack_row)
|
||||
if st02 is None:
|
||||
continue
|
||||
counts[st02] = counts.get(st02, 0) + 1
|
||||
# Unsorted on purpose — the caller re-sorts by (-ack_count, st02)
|
||||
# so the heaviest backlog surfaces first in CLI output. Sorting
|
||||
# here would just be wasted CPU (and risk a different order if
|
||||
# the caller's key changes).
|
||||
yield from counts.items()
|
||||
|
||||
|
||||
def _extract_999_st02(ack_row: Ack) -> str | None:
|
||||
"""Return the source 837's ST02 from a 999 ack row, or ``None``.
|
||||
|
||||
Reads ``raw_json`` and pulls ``set_responses[0].set_control_number``.
|
||||
Returns ``None`` if the JSON is malformed, ``set_responses`` is
|
||||
empty, or the field is missing — callers should skip these rows
|
||||
rather than treating them as orphans (they're unprocessable, not
|
||||
just orphaned).
|
||||
|
||||
Note: the ``raw_json`` column is a SQLAlchemy JSON type so the
|
||||
ORM hands it back as a Python dict, not a string. We accept
|
||||
either form (dict or str) so this helper is robust to direct
|
||||
SQLAlchemy access and to raw sqlite3 row access.
|
||||
"""
|
||||
raw = ack_row.raw_json
|
||||
if not raw:
|
||||
return None
|
||||
if isinstance(raw, dict):
|
||||
parsed = raw
|
||||
elif isinstance(raw, (str, bytes, bytearray)):
|
||||
try:
|
||||
parsed = json.loads(raw)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
else:
|
||||
return None
|
||||
srs = parsed.get("set_responses") or []
|
||||
if not srs:
|
||||
return None
|
||||
st02 = srs[0].get("set_control_number")
|
||||
return str(st02) if st02 else None
|
||||
|
||||
|
||||
def _ack_control_number(ack_row, kind: str) -> str:
|
||||
"""Best-effort control-number lookup for an ack row of any kind.
|
||||
|
||||
Per-kind sources (preserved across the SP38 refactor of
|
||||
:func:`find_ack_orphans`):
|
||||
|
||||
* ``999`` — 999 ORM row has no dedicated control_number column;
|
||||
we re-derive it from ``raw_json.envelope.control_number``
|
||||
(the same source :func:`cyclone.store.ui.to_ui_ack` uses).
|
||||
Accepts both dict (post-ORM hydration) and str (raw sqlite3
|
||||
row or freshly inserted JSON string).
|
||||
* ``277ca`` / ``ta1`` — both carry the control number in a
|
||||
dedicated ORM column (``ack_row.control_number``). Reading
|
||||
from ``raw_json`` would yield empty strings.
|
||||
|
||||
Returns ``""`` (empty string) when the source field is missing
|
||||
so the JSON renderer still emits a stable shape.
|
||||
"""
|
||||
if kind == "999":
|
||||
raw = ack_row.raw_json
|
||||
if raw is None or raw == "":
|
||||
return ""
|
||||
if isinstance(raw, dict):
|
||||
env = raw.get("envelope") or {}
|
||||
return env.get("control_number") or ""
|
||||
if isinstance(raw, (str, bytes, bytearray)):
|
||||
try:
|
||||
parsed = json.loads(raw)
|
||||
except (TypeError, ValueError):
|
||||
return ""
|
||||
env = parsed.get("envelope") or {}
|
||||
return env.get("control_number") or ""
|
||||
return ""
|
||||
# 277ca / ta1 — control_number is an ORM column on both tables.
|
||||
return getattr(ack_row, "control_number", "") or ""
|
||||
|
||||
|
||||
__all__ = [
|
||||
|
||||
@@ -4,6 +4,8 @@ These are part of the public API — callers (API endpoints) catch them
|
||||
and translate to HTTP 409 Conflict responses.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class AlreadyMatchedError(Exception):
|
||||
"""Raised by ``CycloneStore.manual_match`` when the claim is already paired.
|
||||
@@ -39,3 +41,26 @@ class InvalidStateError(Exception):
|
||||
super().__init__(
|
||||
f"invalid state {current_state} for apply (kind={activity_kind})"
|
||||
)
|
||||
|
||||
|
||||
class DuplicateClaimError(Exception):
|
||||
"""Raised by ``cyclone.store.submission_dedup.check_duplicate`` when a claim_id
|
||||
is re-submitted within the 30-day dedup window (SP41).
|
||||
|
||||
Mirrors the other 409-class exceptions: callers catch it at the API
|
||||
boundary and surface it as a 409 Conflict. Carries
|
||||
``original_submission_at`` so the UI can render "already submitted on
|
||||
YYYY-MM-DD" without re-querying.
|
||||
|
||||
Note: the "30-day" literal in the message mirrors
|
||||
``cyclone.store.submission_dedup.DEFAULT_WINDOW_DAYS`` — keep them in
|
||||
sync if either ever moves.
|
||||
"""
|
||||
|
||||
def __init__(self, claim_id: str, original_submission_at: datetime):
|
||||
self.claim_id = claim_id
|
||||
self.original_submission_at = original_submission_at
|
||||
super().__init__(
|
||||
f"claim_id {claim_id!r} was already submitted at "
|
||||
f"{original_submission_at.isoformat()}; within 30-day window"
|
||||
)
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
"""SP39: read/write helpers for the ``resubmissions`` audit table.
|
||||
|
||||
The store facade re-exports the public names so callers don't import
|
||||
from this module directly.
|
||||
|
||||
- ``record_resubmission(...)`` — insert one row per (claim, ICN).
|
||||
Idempotent on the unique constraint; returns ``False`` if a
|
||||
duplicate was suppressed.
|
||||
- ``find_resubmission_status(...)`` — read-side join that returns
|
||||
every Resubmission row, optionally filtered by ``batch_id``.
|
||||
Statuses are derived at read-time against ``claim_acks`` (via the
|
||||
existing SP28/31 auto-link) and ``remittances`` (via CLP->claim),
|
||||
so the table stays a write-once audit surface and the existing
|
||||
auto-link data remains the source of truth.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timezone
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from cyclone import db as cycl_db
|
||||
from cyclone.db import Resubmission
|
||||
|
||||
|
||||
@dataclass
|
||||
class ResubmissionStatus:
|
||||
"""One row in the read-side status view (joined + derived)."""
|
||||
|
||||
claim_id: str
|
||||
batch_id: str
|
||||
resubmitted_at: datetime
|
||||
source_corrected_path: str
|
||||
interchange_control_number: str
|
||||
group_control_number: str
|
||||
# Derived statuses (None when no inbound ack/remit exists yet).
|
||||
ack_status: Optional[str] = None # "999_accepted" / "999_rejected" / "277ca_accepted" / "277ca_rejected"
|
||||
payment_status: Optional[str] = None # "paid" / "denied_again" / None
|
||||
|
||||
|
||||
def record_resubmission(
|
||||
*,
|
||||
claim_id: str,
|
||||
batch_id: str,
|
||||
source_corrected_path: str,
|
||||
interchange_control_number: str,
|
||||
group_control_number: str,
|
||||
resubmitted_at: datetime | None = None,
|
||||
) -> bool:
|
||||
"""Insert one Resubmission row. Idempotent on
|
||||
``(claim_id, interchange_control_number)`` — returns ``True`` if
|
||||
inserted, ``False`` if a duplicate-key collision was suppressed.
|
||||
"""
|
||||
resubmitted_at = resubmitted_at or datetime.now(timezone.utc)
|
||||
with cycl_db.SessionLocal()() as session:
|
||||
try:
|
||||
session.add(Resubmission(
|
||||
claim_id=claim_id,
|
||||
batch_id=batch_id,
|
||||
resubmitted_at=resubmitted_at,
|
||||
source_corrected_path=source_corrected_path,
|
||||
interchange_control_number=interchange_control_number,
|
||||
group_control_number=group_control_number,
|
||||
))
|
||||
session.commit()
|
||||
return True
|
||||
except IntegrityError:
|
||||
session.rollback()
|
||||
return False
|
||||
|
||||
|
||||
def find_resubmission_status(
|
||||
*, batch_id: str | None = None
|
||||
) -> list[ResubmissionStatus]:
|
||||
"""Return every Resubmission row joined with the derived ack /
|
||||
payment statuses, optionally filtered by ``batch_id``.
|
||||
|
||||
The join is read-side only: the Resubmission row is the source
|
||||
of truth for "was this claim pushed", and the joined statuses
|
||||
(``claim_acks`` via the SP28/31 auto-link + ``remittances`` via
|
||||
CLP->claim) tell the operator whether the push landed.
|
||||
"""
|
||||
with cycl_db.SessionLocal()() as session:
|
||||
stmt = select(Resubmission)
|
||||
if batch_id is not None:
|
||||
stmt = stmt.where(Resubmission.batch_id == batch_id)
|
||||
rows = session.execute(
|
||||
stmt.order_by(Resubmission.batch_id, Resubmission.claim_id)
|
||||
).scalars().all()
|
||||
|
||||
if not rows:
|
||||
return []
|
||||
|
||||
claim_ids = [r.claim_id for r in rows]
|
||||
# Join against claim_acks for the latest accept/reject code per claim.
|
||||
from cyclone.db import ClaimAck
|
||||
ack_rows = session.execute(
|
||||
select(
|
||||
ClaimAck.claim_id,
|
||||
ClaimAck.ack_kind,
|
||||
ClaimAck.set_accept_reject_code,
|
||||
).where(ClaimAck.claim_id.in_(claim_ids))
|
||||
).all()
|
||||
# Reduce to the latest ack per (claim_id, ack_kind); we only
|
||||
# care about the surface-level status (any accept / any reject).
|
||||
ack_status_by_claim: dict[str, str] = {}
|
||||
for cid, ack_kind, code in ack_rows:
|
||||
if not code:
|
||||
continue
|
||||
# Prefer 277ca over 999 (more specific), and accept over reject
|
||||
# when both exist (a 999 accept + 277ca reject is still a reject).
|
||||
existing = ack_status_by_claim.get(cid)
|
||||
if existing is None:
|
||||
ack_status_by_claim[cid] = _ack_status_label(ack_kind, code)
|
||||
else:
|
||||
# Take the "worse" of the two: reject > accept.
|
||||
new_label = _ack_status_label(ack_kind, code)
|
||||
ack_status_by_claim[cid] = _worse_status(existing, new_label)
|
||||
|
||||
# Join against remittances to detect "paid" or "denied_again".
|
||||
from cyclone.db import Remittance
|
||||
paid_claim_ids = set(session.execute(
|
||||
select(Remittance.claim_id).where(Remittance.claim_id.in_(claim_ids))
|
||||
).scalars().all())
|
||||
|
||||
return [
|
||||
ResubmissionStatus(
|
||||
claim_id=r.claim_id,
|
||||
batch_id=r.batch_id,
|
||||
resubmitted_at=r.resubmitted_at,
|
||||
source_corrected_path=r.source_corrected_path,
|
||||
interchange_control_number=r.interchange_control_number,
|
||||
group_control_number=r.group_control_number,
|
||||
ack_status=ack_status_by_claim.get(r.claim_id),
|
||||
payment_status="paid" if r.claim_id in paid_claim_ids else None,
|
||||
) for r in rows
|
||||
]
|
||||
|
||||
|
||||
def _ack_status_label(ack_kind: str, code: str) -> str:
|
||||
code = (code or "").upper()
|
||||
if ack_kind == "999":
|
||||
return "999_accepted" if code == "A" else "999_rejected"
|
||||
if ack_kind == "277ca":
|
||||
return "277ca_accepted" if code.startswith("A") else "277ca_rejected"
|
||||
return f"{ack_kind}_{code.lower()}"
|
||||
|
||||
|
||||
_STATUS_RANK = {
|
||||
"pending_999": 0,
|
||||
"999_accepted": 1,
|
||||
"277ca_accepted": 2,
|
||||
"999_rejected": 3,
|
||||
"277ca_rejected": 4,
|
||||
"paid": 5,
|
||||
}
|
||||
|
||||
|
||||
def _worse_status(a: str, b: str) -> str:
|
||||
"""Pick the higher-rank (more concerning) status."""
|
||||
return a if _STATUS_RANK.get(a, 0) >= _STATUS_RANK.get(b, 0) else b
|
||||
@@ -0,0 +1,65 @@
|
||||
"""Claim-id dedup at the SFTP pre-flight stage.
|
||||
|
||||
Schema: the ``submission_dedup`` table (defined on the main ``Base`` in
|
||||
``cyclone.db``) records (claim_id, submitted_at). A re-submission of
|
||||
the same claim_id within ``DEFAULT_WINDOW_DAYS`` (30) is rejected with
|
||||
``DuplicateClaimError``. Past the window, the guard lets it through —
|
||||
the operator may be retrying a 999-rejected file from >30 days ago,
|
||||
which is a legitimate rebuild path.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from cyclone import db as cycl_db
|
||||
from cyclone.db import SubmissionRecord
|
||||
from cyclone.store.exceptions import DuplicateClaimError
|
||||
|
||||
DEFAULT_WINDOW_DAYS = 30
|
||||
|
||||
|
||||
def record_submission(claim_id: str, submitted_at: datetime, db_url: str) -> None:
|
||||
"""Upsert (claim_id, submitted_at). Idempotent on claim_id.
|
||||
|
||||
``db_url`` is accepted for back-compat with the original Task 8
|
||||
signature but ignored — sessions now flow through the process-wide
|
||||
``cycl_db.SessionLocal()`` factory that ``db.init_db()`` set up.
|
||||
"""
|
||||
del db_url # signature-preserving; conftest wires the per-test DB
|
||||
with cycl_db.SessionLocal()() as session:
|
||||
session.merge(SubmissionRecord(
|
||||
claim_id=claim_id,
|
||||
submitted_at=submitted_at,
|
||||
))
|
||||
session.commit()
|
||||
|
||||
|
||||
def check_duplicate(
|
||||
claim_id: str,
|
||||
db_url: str | None = None,
|
||||
now: datetime | None = None,
|
||||
window_days: int = DEFAULT_WINDOW_DAYS,
|
||||
) -> None:
|
||||
"""Raise ``DuplicateClaimError`` if ``claim_id`` was submitted within the window.
|
||||
|
||||
``db_url`` is accepted for back-compat with the original Task 8
|
||||
signature but ignored — sessions flow through ``cycl_db.SessionLocal()``
|
||||
just like the rest of ``cyclone.store``. Tests that pass an explicit
|
||||
``db_url`` keep working because ``cycl_db.SessionLocal()()`` points at
|
||||
the URL the conftest resolved under ``CYCLONE_DB_URL``.
|
||||
"""
|
||||
del db_url # signature-preserving
|
||||
if now is None:
|
||||
now = datetime.now(timezone.utc)
|
||||
cutoff = now - timedelta(days=window_days)
|
||||
with cycl_db.SessionLocal()() as session:
|
||||
row = session.scalars(
|
||||
select(SubmissionRecord).where(
|
||||
SubmissionRecord.claim_id == claim_id,
|
||||
SubmissionRecord.submitted_at >= cutoff,
|
||||
)
|
||||
).first()
|
||||
if row is not None:
|
||||
raise DuplicateClaimError(claim_id, row.submitted_at)
|
||||
@@ -77,6 +77,17 @@ def add_record(record: BatchRecord, *, event_bus=None) -> None:
|
||||
totals_json=None,
|
||||
validation_json=None,
|
||||
raw_result_json=json.loads(record.result.model_dump_json()),
|
||||
# SP37 Task 2: mirror the parsed 837's ST02 onto the batch
|
||||
# row so 999 AK201 set_control_numbers can resolve back via
|
||||
# Pass 1. The ``getattr`` chain handles the 835 path: the
|
||||
# shared ``Envelope`` class is used by both 837P and 835
|
||||
# parsers, but only ``parse_837`` populates this field — for
|
||||
# 835 records it stays ``None`` and the column is NULL.
|
||||
transaction_set_control_number=getattr(
|
||||
getattr(record.result, "envelope", None),
|
||||
"transaction_set_control_number",
|
||||
None,
|
||||
),
|
||||
)
|
||||
s.add(batch_row)
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
"""SP37 Task 4: canonical 837P submission flow.
|
||||
|
||||
The single public helper is ``submit_file`` — it owns parse → DB
|
||||
write → SFTP upload per file. CLI (``cyclone submit-batch``) and HTTP
|
||||
(``POST /api/submit-batch``) are thin wrappers; both call this helper
|
||||
so the ordering, idempotency, and audit shape are identical.
|
||||
|
||||
DB-first, upload-second (per spec decision §2): if the DB write fails,
|
||||
no SFTP call is made. If the SFTP call fails after a successful DB
|
||||
write, the row exists but the file never landed — re-running is safe
|
||||
(idempotent DB write via add_record's s.get check; idempotent SFTP
|
||||
upload via stat-then-put).
|
||||
"""
|
||||
from .core import submit_file
|
||||
from .result import SubmitOutcome, SubmitResult
|
||||
|
||||
__all__ = ["submit_file", "SubmitOutcome", "SubmitResult"]
|
||||
@@ -0,0 +1,270 @@
|
||||
"""SP37 Task 4: parse → DB write → SFTP upload, per file.
|
||||
|
||||
Mirrors ``resubmit_rejected_claims`` but writes to the DB before
|
||||
uploading. Same idempotency check (``stat().st_size == local_size``)
|
||||
skips already-uploaded files without re-emitting audit events.
|
||||
|
||||
DB-first, upload-second (per spec decision §2): if the DB write fails,
|
||||
no SFTP call is made. If the SFTP call fails after a successful DB
|
||||
write, the row exists but the file never landed — re-running is safe
|
||||
(idempotent DB write via add_record's s.get check; idempotent SFTP
|
||||
upload via stat-then-put).
|
||||
|
||||
The default SFTP factory uses paramiko directly (not the
|
||||
``SftpClient`` wrapper) because the wrapper exposes ``write_file``,
|
||||
``list_inbound``, and ``read_file`` but no ``stat()`` — which makes
|
||||
the SKIPPED outcome unreachable in production.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable
|
||||
|
||||
from cyclone import db as db_mod
|
||||
from cyclone.audit_log import AuditEvent, append_event
|
||||
from cyclone.parsers.parse_837 import parse as parse_837_text
|
||||
from cyclone.parsers.payer import PayerConfig
|
||||
from cyclone.providers import SftpBlock
|
||||
from cyclone.store import store as cycl_store
|
||||
from cyclone.store.exceptions import DuplicateClaimError
|
||||
from cyclone.store.records import BatchRecord837
|
||||
from cyclone.store.submission_dedup import check_duplicate
|
||||
|
||||
from .result import SubmitOutcome, SubmitResult
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# The companion-guide payer id we enforce for CO Medicaid submits.
|
||||
# Same value the legacy ``resubmit-rejected-claims`` CLI gates on.
|
||||
# TODO(sp39-followup): trace where ClaimOutput.payer.id gets set to
|
||||
# "SKCO0" upstream of submit. SP39 hard-fixes the serializer via
|
||||
# _normalize_payer_id (serialize_837.py) so the byte defect cannot
|
||||
# escape the 837 emit, but the root-cause setter is still live in the
|
||||
# raw_json pipeline. Find and patch in a future SP.
|
||||
EXPECTED_PAYER_ID = "CO_TXIX"
|
||||
|
||||
|
||||
def _default_sftp_factory(sftp_block: SftpBlock) -> Any:
|
||||
"""Open a paramiko SFTP session to the real MFT. Mirrors
|
||||
resubmit_rejected_claims._open_session (cli.py:620-640).
|
||||
Caller is responsible for closing the session.
|
||||
|
||||
The returned ``sftp`` is a ``paramiko.SFTPClient`` — it exposes
|
||||
``stat(remote_path)`` (used for the idempotency check) and
|
||||
``put(local_path, remote_path)`` (used for the upload). The
|
||||
underlying SSH handle is stashed on ``sftp._cyclone_ssh`` so the
|
||||
caller can close it cleanly via ``getattr(sftp, "_cyclone_ssh",
|
||||
None).close()`` after the upload finishes.
|
||||
|
||||
Raises:
|
||||
RuntimeError: if the SFTP block is in stub mode (the CLI/HTTP
|
||||
layer should already guard against this, but we re-check
|
||||
here because paramiko will try to connect even when
|
||||
``stub=True``).
|
||||
"""
|
||||
if sftp_block.stub:
|
||||
# Same posture as resubmit_rejected_claims in cli.py:592-595:
|
||||
# the operator refuses to upload in stub mode, and the helper
|
||||
# surfaces that as SFTP_FAILED with an explicit error.
|
||||
raise RuntimeError("SFTP block is in stub mode")
|
||||
|
||||
from paramiko import AutoAddPolicy, SSHClient
|
||||
|
||||
from cyclone.secrets import get_secret
|
||||
|
||||
pw = get_secret(sftp_block.auth.get("password_keychain_account", ""))
|
||||
ssh = SSHClient()
|
||||
ssh.set_missing_host_key_policy(AutoAddPolicy())
|
||||
ssh.connect(
|
||||
sftp_block.host, port=sftp_block.port,
|
||||
username=sftp_block.username, password=pw,
|
||||
timeout=15, banner_timeout=15, auth_timeout=15,
|
||||
)
|
||||
sftp = ssh.open_sftp()
|
||||
# Attach ssh to sftp so the caller can close it cleanly.
|
||||
sftp._cyclone_ssh = ssh
|
||||
return sftp
|
||||
|
||||
|
||||
def submit_file(
|
||||
path: Path,
|
||||
*,
|
||||
sftp_block: SftpBlock,
|
||||
actor: str,
|
||||
validate: bool = True,
|
||||
sftp_client_factory: Callable[[SftpBlock], Any] | None = None,
|
||||
) -> SubmitResult:
|
||||
"""Submit one 837P file: parse → DB write → SFTP upload.
|
||||
|
||||
Args:
|
||||
path: local 837P file.
|
||||
sftp_block: the clearhouse SftpBlock (for paths + auth).
|
||||
actor: audit-log actor tag (e.g. "api-submit-batch").
|
||||
validate: parse the file before upload (default True). Catches
|
||||
bad byte-fixes early.
|
||||
sftp_client_factory: optional callable that returns an
|
||||
SFTP-client-compatible object (must expose ``stat()`` and
|
||||
``write_file()``). Defaults to :func:`_default_sftp_factory`
|
||||
which opens a real paramiko session. Tests inject a fake.
|
||||
|
||||
Returns:
|
||||
SubmitResult with file, outcome, batch_id (when written),
|
||||
error (when failed).
|
||||
"""
|
||||
file_label = path.name
|
||||
try:
|
||||
content = path.read_bytes()
|
||||
except OSError as exc:
|
||||
return SubmitResult(file_label, SubmitOutcome.PARSE_FAILED, error=str(exc))
|
||||
|
||||
# 1. Validate via parse (optional but recommended).
|
||||
parsed: Any = None
|
||||
if validate:
|
||||
try:
|
||||
parsed = parse_837_text(content.decode(), PayerConfig.co_medicaid())
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning("submit_file %s: parse failed: %s", file_label, exc)
|
||||
return SubmitResult(file_label, SubmitOutcome.PARSE_FAILED, error=str(exc))
|
||||
mismatch = next(
|
||||
(c for c in parsed.claims if c.payer.id != EXPECTED_PAYER_ID),
|
||||
None,
|
||||
)
|
||||
if mismatch is not None:
|
||||
return SubmitResult(
|
||||
file_label,
|
||||
SubmitOutcome.PAYER_MISMATCH,
|
||||
error=f"payer.id={mismatch.payer.id!r} (expected {EXPECTED_PAYER_ID!r})",
|
||||
)
|
||||
|
||||
# 1b. SP41 Task 9 — claim-id dedup pre-flight. Walks every
|
||||
# CLM01 in the parsed file and asks ``check_duplicate`` whether
|
||||
# any of them were submitted within the 30-day window. If so,
|
||||
# raise ``DuplicateClaimError`` — this is a 409-class domain
|
||||
# exception (mirrors the posture of ``AlreadyMatchedError`` /
|
||||
# ``NotMatchedError`` / ``InvalidStateError`` per their docstrings)
|
||||
# and is caught by ``api_routers/submission.submit_batch`` so
|
||||
# the per-file 200-with-results contract is preserved.
|
||||
#
|
||||
# ``check_duplicate`` reads the configured ``cycl_db.SessionLocal()``
|
||||
# — the same engine the BatchRecord837 DB write below uses — so
|
||||
# we don't need to thread a session through here. The helper's
|
||||
# optional ``db_url`` kwarg is ignored (signature-preserving
|
||||
# shim for back-compat with the original Task 8 callers).
|
||||
#
|
||||
# We deliberately RAISE here instead of returning a SubmitResult:
|
||||
# the exception carries ``claim_id`` + ``original_submission_at``
|
||||
# so the caller can surface structured detail to the operator
|
||||
# without re-querying. Approach A per the Task 9 spec.
|
||||
#
|
||||
# Symmetry with the sibling failure paths (parse / DB / SFTP /
|
||||
# audit-event — each logs ``submit_file %s: <kind>: %s`` before
|
||||
# returning or re-raising). The router's special-case handler
|
||||
# also logs this exception at the api_routers boundary, but
|
||||
# the helper has its own log line so the operator tracing a
|
||||
# failure through stdout sees the dedup trip even when
|
||||
# submit_file is invoked directly (e.g. from the CLI or a
|
||||
# future call site that does not go through the router).
|
||||
try:
|
||||
for claim in parsed.claims:
|
||||
check_duplicate(claim.claim_id)
|
||||
except DuplicateClaimError as exc:
|
||||
log.warning(
|
||||
"submit_file %s: duplicate claim %s (originally submitted %s)",
|
||||
file_label, exc.claim_id, exc.original_submission_at.isoformat(),
|
||||
)
|
||||
raise
|
||||
|
||||
# 2. DB write — DB-first, upload-second invariant.
|
||||
# ``parsed`` is required to construct a BatchRecord837 (it embeds the
|
||||
# full ParseResult). validate=False therefore isn't a real path —
|
||||
# the CLI/HTTP always pass validate=True — so reject it loudly
|
||||
# rather than silently building an empty row.
|
||||
if parsed is None:
|
||||
return SubmitResult(
|
||||
file_label,
|
||||
SubmitOutcome.PARSE_FAILED,
|
||||
error="validate=False is not supported; must parse to construct a BatchRecord",
|
||||
)
|
||||
|
||||
# Use the same uuid4().hex id the existing /api/parse-837 path uses.
|
||||
batch_id = uuid.uuid4().hex
|
||||
record = BatchRecord837(
|
||||
id=batch_id,
|
||||
input_filename=file_label,
|
||||
parsed_at=datetime.now(timezone.utc),
|
||||
result=parsed,
|
||||
)
|
||||
|
||||
try:
|
||||
# ``cycl_store.add`` is the public facade method (per the
|
||||
# CycloneStore class in store/__init__.py:158). It delegates
|
||||
# to ``store.write.add_record``, which is the underlying
|
||||
# SQLAlchemy write path.
|
||||
cycl_store.add(record)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning("submit_file %s: DB write failed: %s", file_label, exc)
|
||||
return SubmitResult(file_label, SubmitOutcome.DB_FAILED, error=str(exc))
|
||||
|
||||
# 3. SFTP upload. ``_default_sftp_factory`` opens a paramiko
|
||||
# session directly because the SftpClient wrapper has no ``stat()``
|
||||
# method — that omission made the SKIPPED outcome unreachable in
|
||||
# production. Mirror cli.py:620-650.
|
||||
remote_path = f"{sftp_block.paths['outbound']}/{file_label}"
|
||||
factory = sftp_client_factory or _default_sftp_factory
|
||||
sftp = factory(sftp_block)
|
||||
local_size = len(content)
|
||||
try:
|
||||
try:
|
||||
stat = sftp.stat(remote_path)
|
||||
if stat.st_size == local_size:
|
||||
# Already on remote at the right size — re-run is safe;
|
||||
# do NOT emit a duplicate audit event.
|
||||
return SubmitResult(file_label, SubmitOutcome.SKIPPED, batch_id=batch_id)
|
||||
except (IOError, OSError):
|
||||
pass # not on remote yet
|
||||
sftp.write_file(remote_path, content)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning("submit_file %s: SFTP failed: %s", file_label, exc)
|
||||
return SubmitResult(
|
||||
file_label, SubmitOutcome.SFTP_FAILED,
|
||||
batch_id=batch_id, error=str(exc),
|
||||
)
|
||||
finally:
|
||||
# Close the paramiko SSH handle the helper stashed on the
|
||||
# SFTP client (paramiko does not auto-close on GC and a leak
|
||||
# here costs a slot in MOVEit's per-IP session table).
|
||||
ssh_handle = getattr(sftp, "_cyclone_ssh", None)
|
||||
if ssh_handle is not None:
|
||||
try:
|
||||
ssh_handle.close()
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
|
||||
# 4. Audit event — same shape the legacy resubmit_rejected_claims CLI
|
||||
# uses (event_type="clearhouse.submitted", entity_type="claim_file",
|
||||
# entity_id=filename, payload has remote_path + source + size).
|
||||
# Best-effort: an audit failure must not roll back the upload.
|
||||
try:
|
||||
with db_mod.SessionLocal()() as session:
|
||||
append_event(session, AuditEvent(
|
||||
event_type="clearhouse.submitted",
|
||||
entity_type="claim_file",
|
||||
entity_id=file_label,
|
||||
payload={
|
||||
"remote_path": remote_path,
|
||||
"source": "submit-batch",
|
||||
"size": local_size,
|
||||
"batch_id": batch_id,
|
||||
},
|
||||
actor=actor,
|
||||
))
|
||||
session.commit()
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning(
|
||||
"submit_file %s: audit event failed: %s", file_label, exc,
|
||||
)
|
||||
|
||||
return SubmitResult(file_label, SubmitOutcome.SUBMITTED, batch_id=batch_id)
|
||||
@@ -0,0 +1,247 @@
|
||||
"""SP25 Task 5: ``cyclone recover-ingest`` parse + DB-write helper.
|
||||
|
||||
What this module is for
|
||||
------------------------
|
||||
On 2026-07-07 the dashboard was empty even though four 837P files
|
||||
and one 835 file were sitting in ``ingest/``. The 837Ps had been
|
||||
SFTP-shipped to Gainwell (the operator's SFTP client, manually),
|
||||
but cyclone never recorded them in the DB — ``parse-837`` only
|
||||
emits JSON, and ``submit-batch`` was never called for that run.
|
||||
The 835 came back from the payer and was dropped into ``ingest/``,
|
||||
but ``pull-inbound`` was never run for it.
|
||||
|
||||
This helper is the recovery path: parse the local file, build a
|
||||
typed :class:`BatchRecord`, call :meth:`CycloneStore.add` (which
|
||||
publishes events to the live-tail bus), record the file in
|
||||
``processed_inbound_files`` for dedup, and stop — no SFTP, no audit
|
||||
log because the file was already uploaded (or already came back).
|
||||
|
||||
Public surface
|
||||
--------------
|
||||
- :func:`recover_file` — one file. Returns a structured result dict.
|
||||
- :func:`detect_kind` — ST01 helper, exposed for tests.
|
||||
|
||||
Why this is its own module (not a new branch in submit_file)
|
||||
------------------------------------------------------------
|
||||
``submit_file`` is the SFTP-write path; its invariants are
|
||||
"DB write → SFTP upload → audit event" and removing the SFTP half
|
||||
would mutate that contract for every caller. Recovery is a
|
||||
deliberately offline path and a separate surface keeps the two
|
||||
flows readable. ``recover_file`` shares the same parse + store
|
||||
write code path (``CycloneStore.add``) so the live-tail pages
|
||||
light up exactly the way they would for a fresh submission.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from cyclone import db as db_mod
|
||||
from cyclone.store import store as cycl_store
|
||||
from cyclone.db import ProcessedInboundFile
|
||||
from cyclone.parsers.parse_837 import parse as parse_837_text
|
||||
from cyclone.parsers.parse_835 import parse as parse_835_text
|
||||
from cyclone.parsers.payer import PayerConfig, PayerConfig835
|
||||
from cyclone.parsers.segments import tokenize as _tokenize_segments
|
||||
from cyclone.store.records import BatchRecord835, BatchRecord837
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# Default dedup key for the sftp_block_name column. Distinct from
|
||||
# ``dzinesco`` so a real remote-pull can't accidentally mask a
|
||||
# manual recovery (or vice versa).
|
||||
DEFAULT_SFTP_BLOCK_NAME = "manual-recover"
|
||||
|
||||
|
||||
def detect_kind(text: str) -> str:
|
||||
"""Return the ST01 of the *first* ST segment, or ``"unknown"``.
|
||||
|
||||
X12 envelopes can contain multiple transactions; the first ST is
|
||||
the canonical kind for the whole file under our parsing rules.
|
||||
|
||||
Mirrors the SP35 envelope check in ``api_routers/parse.py``
|
||||
(auto-detect layer A; this is layer B for offline recovery).
|
||||
"""
|
||||
try:
|
||||
segments = _tokenize_segments(text)
|
||||
except Exception: # noqa: BLE001 — recover-context, log only
|
||||
log.warning("recover-ingest: tokenize failed", exc_info=True)
|
||||
return "unknown"
|
||||
for seg in segments:
|
||||
if seg and seg[0] == "ST" and len(seg) > 1:
|
||||
return seg[1]
|
||||
return "unknown"
|
||||
|
||||
|
||||
def _normalize_kind(st01: str) -> str:
|
||||
"""Map ``"837"`` → ``"837p"`` (parser-level kind); pass others through.
|
||||
|
||||
The parser distinguishes ``837p`` (professional) from the
|
||||
837I/D variants; only 837P is supported in the recovery path
|
||||
today. Anything else is rejected with a clear error message.
|
||||
"""
|
||||
if st01 == "837":
|
||||
return "837p"
|
||||
if st01 in {"835", "837p"}:
|
||||
return st01
|
||||
return st01
|
||||
|
||||
|
||||
def _already_processed(name: str, sftp_block_name: str) -> bool:
|
||||
"""Return True if ``(sftp_block_name, name)`` was already recovered.
|
||||
|
||||
Mirrors the dedup index ``ux_processed_inbound_files_block_name``
|
||||
(UNIQUE on sftp_block_name, name).
|
||||
"""
|
||||
s = db_mod.SessionLocal()()
|
||||
try:
|
||||
return s.query(ProcessedInboundFile).filter_by(
|
||||
sftp_block_name=sftp_block_name, name=name
|
||||
).first() is not None
|
||||
finally:
|
||||
s.close()
|
||||
|
||||
|
||||
def _mark_processed(
|
||||
*,
|
||||
sftp_block_name: str,
|
||||
path: Path,
|
||||
file_type: str,
|
||||
parser_used: str,
|
||||
claim_count: int,
|
||||
) -> None:
|
||||
"""Insert a ``processed_inbound_files`` row for the recovered file.
|
||||
|
||||
Status hardcoded to ``"ok"`` because by the time we reach this
|
||||
function the parse + store add have both succeeded.
|
||||
"""
|
||||
stat = path.stat()
|
||||
now = datetime.now(tz=timezone.utc)
|
||||
s = db_mod.SessionLocal()()
|
||||
try:
|
||||
s.add(ProcessedInboundFile(
|
||||
sftp_block_name=sftp_block_name,
|
||||
name=path.name,
|
||||
size=stat.st_size,
|
||||
modified_at=datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc),
|
||||
file_type=file_type,
|
||||
processed_at=now,
|
||||
parser_used=parser_used,
|
||||
claim_count=claim_count,
|
||||
status="ok",
|
||||
error_message=None,
|
||||
))
|
||||
s.commit()
|
||||
finally:
|
||||
s.close()
|
||||
|
||||
|
||||
def recover_file(
|
||||
path: str | Path,
|
||||
*,
|
||||
sftp_block_name: str = DEFAULT_SFTP_BLOCK_NAME,
|
||||
actor: str = "recover-ingest",
|
||||
) -> dict:
|
||||
"""Recover one X12 file from a local path: parse → DB write (no SFTP).
|
||||
|
||||
Args:
|
||||
path: local file. 837P / 835 only.
|
||||
sftp_block_name: dedup key in ``processed_inbound_files``.
|
||||
Defaults to ``"manual-recover"`` so the recovery doesn't
|
||||
collide with the real ``dzinesco`` SFTP block.
|
||||
actor: reserved for audit. Recovery doesn't audit-log per
|
||||
design (the file was already uploaded by whatever path
|
||||
got it into ingest/), but the parameter is kept so a
|
||||
future audit-enabled variant doesn't break callers.
|
||||
|
||||
Returns:
|
||||
dict with keys:
|
||||
- ``file``: path as passed in (str)
|
||||
- ``kind``: ``"837p"`` / ``"835"`` / ``None``
|
||||
- ``status``: ``"ok"`` / ``"duplicate"`` / ``"failed"``
|
||||
- ``batch_id``: hex uuid4 when status=="ok", else None
|
||||
- ``error``: str when status=="failed", else None
|
||||
"""
|
||||
p = Path(path)
|
||||
if not p.exists():
|
||||
return {"file": str(p), "kind": None, "status": "failed",
|
||||
"batch_id": None, "error": f"file does not exist: {p}"}
|
||||
if not p.is_file():
|
||||
return {"file": str(p), "kind": None, "status": "failed",
|
||||
"batch_id": None, "error": f"not a regular file: {p}"}
|
||||
|
||||
# 1. Dedup — short-circuit if this exact name was already recovered.
|
||||
if _already_processed(p.name, sftp_block_name):
|
||||
return {"file": str(p), "kind": None, "status": "duplicate",
|
||||
"batch_id": None, "error": None}
|
||||
|
||||
# 2. Read + detect kind.
|
||||
try:
|
||||
text = p.read_text(encoding="utf-8")
|
||||
except UnicodeDecodeError as exc:
|
||||
return {"file": str(p), "kind": None, "status": "failed",
|
||||
"batch_id": None, "error": f"encoding: {exc!r}"}
|
||||
|
||||
raw_kind = detect_kind(text)
|
||||
kind = _normalize_kind(raw_kind)
|
||||
if kind not in {"837p", "835"}:
|
||||
return {"file": str(p), "kind": raw_kind, "status": "failed",
|
||||
"batch_id": None,
|
||||
"error": f"unsupported kind {raw_kind!r} (recovery supports 837p + 835)"}
|
||||
|
||||
# 3. Parse through the canonical parser for the detected kind.
|
||||
parsed: Any = None
|
||||
if kind == "837p":
|
||||
try:
|
||||
parsed = parse_837_text(text, PayerConfig.co_medicaid(), input_file=p.name)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
return {"file": str(p), "kind": kind, "status": "failed",
|
||||
"batch_id": None, "error": f"parse_837: {exc!r}"}
|
||||
else: # "835"
|
||||
try:
|
||||
parsed = parse_835_text(text, PayerConfig835.co_medicaid_835(), input_file=p.name)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
return {"file": str(p), "kind": kind, "status": "failed",
|
||||
"batch_id": None, "error": f"parse_835: {exc!r}"}
|
||||
|
||||
# 4. Build a typed BatchRecord (mirrors api.py parse-837 happy path).
|
||||
batch_id = uuid.uuid4().hex
|
||||
now = datetime.now(tz=timezone.utc)
|
||||
if kind == "837p":
|
||||
record = BatchRecord837(
|
||||
id=batch_id, input_filename=p.name, parsed_at=now, result=parsed,
|
||||
)
|
||||
else:
|
||||
record = BatchRecord835(
|
||||
id=batch_id, input_filename=p.name, parsed_at=now, result=parsed,
|
||||
)
|
||||
|
||||
# 5. DB write — canonical path. CycloneStore.add publishes events on
|
||||
# the bundled bus so live-tail subscribers see new claims/remits.
|
||||
try:
|
||||
cycl_store.add(record)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
return {"file": str(p), "kind": kind, "status": "failed",
|
||||
"batch_id": None, "error": f"db_write: {exc!r}"}
|
||||
|
||||
# 6. Record success for dedup.
|
||||
claim_count = len(getattr(parsed, "claims", []) or [])
|
||||
try:
|
||||
_mark_processed(
|
||||
sftp_block_name=sftp_block_name,
|
||||
path=p,
|
||||
file_type=kind,
|
||||
parser_used=f"parse_{kind}",
|
||||
claim_count=claim_count,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
# The DB write succeeded but dedup-record failed — log and
|
||||
# still report ok (re-running will hit dedup once it's
|
||||
# recorded; until then a re-run is a real duplicate insert).
|
||||
log.warning("recover-ingest: dedup-write failed for %s: %r", p, exc)
|
||||
|
||||
return {"file": str(p), "kind": kind, "status": "ok",
|
||||
"batch_id": batch_id, "error": None}
|
||||
@@ -0,0 +1,48 @@
|
||||
"""SP37 Task 4: typed result for submit_file.
|
||||
|
||||
The CLI and HTTP layer both consume SubmitResult; keeping it in one
|
||||
place means both surfaces agree on the response shape.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class SubmitOutcome(str, Enum):
|
||||
"""Outcome of a single submit_file invocation.
|
||||
|
||||
Audit-event invariant: ``SUBMITTED`` emits a ``clearhouse.submitted``
|
||||
audit event; ``SKIPPED`` does NOT (re-running on an already-uploaded
|
||||
file must not flood the audit log with duplicate events). All
|
||||
failure outcomes (``PARSE_FAILED``, ``PAYER_MISMATCH``, ``DB_FAILED``,
|
||||
``SFTP_FAILED``, ``UNEXPECTED_ERROR``) emit no event either —
|
||||
failures should be observable via the helper's return value, not
|
||||
the audit log.
|
||||
|
||||
``UNEXPECTED_ERROR`` is reserved for uncaught exceptions in the
|
||||
helper's own code (e.g. a bug in ``cycl_store.add``) — distinct
|
||||
from the typed ``SFTP_FAILED`` which means the SFTP layer itself
|
||||
raised. Operators reading ``outcome="unexpected_error"`` know to
|
||||
look at the bug tracker, not the SFTP server.
|
||||
"""
|
||||
SUBMITTED = "submitted"
|
||||
SKIPPED = "skipped"
|
||||
PARSE_FAILED = "parse_failed"
|
||||
PAYER_MISMATCH = "payer_mismatch"
|
||||
DB_FAILED = "db_failed"
|
||||
SFTP_FAILED = "sftp_failed"
|
||||
UNEXPECTED_ERROR = "unexpected_error"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SubmitResult:
|
||||
"""Return value of ``submit_file``: filename, outcome, and optional
|
||||
``batch_id`` (populated when the DB write succeeded) and ``error``
|
||||
(populated on any failure outcome; ``None`` on ``SUBMITTED`` /
|
||||
``SKIPPED``).
|
||||
"""
|
||||
file: str
|
||||
outcome: SubmitOutcome
|
||||
batch_id: str | None = None
|
||||
error: str | None = None
|
||||
+20
-15
@@ -53,10 +53,15 @@ def _auto_init_db(tmp_path, monkeypatch):
|
||||
_api_mod.app.state.event_bus = EventBus()
|
||||
deps.AUTH_DISABLED = True
|
||||
# The rate-limit middleware keeps a per-IP sliding window in
|
||||
# ``_buckets``. Without a reset between tests, later tests in a
|
||||
# full-suite run get ``429 Too Many Requests`` once the testclient
|
||||
# IP exhausts its 300 req/60s budget. Walk the middleware stack
|
||||
# and clear the buckets so every test starts with a fresh window.
|
||||
# ``_buckets`` (now a class-level dict — see
|
||||
# ``cyclone.security.RateLimitMiddleware._buckets``). Without a
|
||||
# reset between tests, later tests in a full-suite run get
|
||||
# ``429 Too Many Requests`` once the testclient IP exhausts its
|
||||
# 300 req/60s budget. Walk the middleware stack and clear the
|
||||
# buckets so every test starts with a fresh window. Because the
|
||||
# buckets dict is class-level, this clears every instance at
|
||||
# once — important when tests like ``test_cors_extra_origins_via_env``
|
||||
# trigger ``importlib.reload(cyclone.api)`` mid-suite.
|
||||
# Trigger the stack build with a cheap health probe (the only
|
||||
# request exempt from the limiter — see RateLimitMiddleware.EXEMPT_PATHS).
|
||||
_reset_rate_limit_buckets(_api_mod.app)
|
||||
@@ -65,6 +70,7 @@ def _auto_init_db(tmp_path, monkeypatch):
|
||||
finally:
|
||||
deps.AUTH_DISABLED = False
|
||||
_api_mod.app.state.event_bus = None
|
||||
_reset_rate_limit_buckets(_api_mod.app)
|
||||
db._reset_for_tests()
|
||||
|
||||
|
||||
@@ -76,23 +82,22 @@ def _reset_rate_limit_buckets(app) -> None:
|
||||
instance), so without a reset between tests the full suite trips
|
||||
the limiter after ~300 requests and later tests get 429s.
|
||||
|
||||
As of the SP38 followup the buckets dict is class-level, so this
|
||||
helper clears every ``RateLimitMiddleware`` instance at once
|
||||
(important when ``importlib.reload(cyclone.api)`` mid-suite has
|
||||
created a new instance and tests still hold a stale ``app``
|
||||
reference to the old one — see
|
||||
``tests/test_rate_limit_shared_buckets.py``).
|
||||
|
||||
The middleware stack is only built on the first request, so we
|
||||
prime it with an exempt health probe before walking to the
|
||||
RateLimit layer. If the stack ever stops being a single chain
|
||||
of ``.app`` links, this helper raises AttributeError — better
|
||||
to fail loudly than silently leak state.
|
||||
"""
|
||||
from fastapi.testclient import TestClient
|
||||
TestClient(app).get("/api/health")
|
||||
cur = app.middleware_stack
|
||||
while cur is not None:
|
||||
if hasattr(cur, "_buckets"):
|
||||
cur._buckets.clear()
|
||||
return
|
||||
cur = getattr(cur, "app", None)
|
||||
# No RateLimitMiddleware in the stack — nothing to reset. Should
|
||||
# not happen in this codebase (security.py registers it at boot)
|
||||
# but we don't want a missing reset to crash unrelated tests.
|
||||
from cyclone.security import RateLimitMiddleware
|
||||
# Class-level dict — one clear reaches every instance.
|
||||
RateLimitMiddleware._buckets.clear()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ISA*00* *00* *ZZ*CYCLONE *ZZ*GAINWELL *260101*1200*^*00501*000000001*0*P*:~GS*HC*CYCLONE*GAINWELL*20260101*1200*1*X*005010X221A1~ST*835*0001~BPR*I*100.00*C*ACH*CCP*01*021000021*DA*123456789*1512345678**01*021000021*DA*123456789*20260101~TRN*1*TRACE01*1512345678~DTM*405*20260118~N1*PR*COLORADO MEDICAL ASSISTANCE PROGRAM*XV*COMEDASSISTPROG~N3*PO BOX 1100~N4*DENVER*CO*80202~REF*2U*7912900843~LX*1~CLP*T1001*1*16.24*16.10**MC*2026029105200*11*1~NM1*QC*1*AALBUE*ERIC****MR*J813715~NM1*74*1*AALBUE*ERIC*W***C*J813715~DTM*232*20260118~DTM*233*20260124~SVC*HC:T1019:U2:SC:KX*2.32*2.30**.33~DTM*472*20260118~CAS*CO*45*.02~REF*G1*6252960154~REF*6R*T1001V001~AMT*B6*2.30~SVC*HC:T1019:U2:SC:KX*2.32*2.30**.33~DTM*472*20260119~CAS*OA*18*2.32~SVC*HC:T1019:U2:SC:KX*2.32*-2.30**.33~DTM*472*20260120~CAS*CO*45*-.02~LX*2~CLP*T1002*4*16.24*0**MC*2026029105201*11*1~NM1*QC*1*OTHER-A*FIRST****MR*OTHER-MEMBER-A~SVC*HC:T1019*2.32*0**.33~DTM*472*20260121~CAS*CO*97*2.32~LX*3~CLP*T1003*22*16.24*-16.10**MC*2026029105202*11*1~NM1*QC*1*OTHER-B*SECOND****MR*OTHER-MEMBER-B~SVC*HC:T1019*2.32*-2.30**.33~DTM*472*20260122~SE*23*0001~GE*1*1~IEA*1*000000001~
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
# SP40: dev-only public eval key for the EdiNation / Edifabric
|
||||
# /v2/x12/validate reference parser. This key has hit the public
|
||||
# evaluation docs and is documented as a "developer API" provisional
|
||||
# key — fine for local CI / smoke tests, never ship to production.
|
||||
#
|
||||
# Production deployments MUST override via the secrets layer:
|
||||
#
|
||||
# cyclone secrets set edifabric.api_key <paid-tier-key>
|
||||
# # or
|
||||
# export CYCLONE_EDIFABRIC_API_KEY_FILE=/path/to/paid-key.txt
|
||||
#
|
||||
# The fixture is referenced by name so the URL-reminder route survives
|
||||
# refactors; the actual key isn't sent over the wire because every
|
||||
# test patches edifabric.set_transport_factory() and secrets.get_secret
|
||||
# before invoke.
|
||||
3ecf6b1c5cf34bd797a5f4c57951a1cf
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
"Client","Visit Date","Payer","Authorized","Member ID","Auth Start Date","Auth End Date","Authorization #","ICD-10","Service","Procedure Code","Modifiers","Client Classes","Billable Hours","Billable Amount","Invoice #","Claimed"
|
||||
"Wilson, Michelle","01/01/2026","CO Medicaid","Yes","R649327","01/01/2026","12/31/2026","3","R69","Homemaker S5150","S5150","U8","DD Waiver","1","$212.77","INV-2026-01-01-R649327","Yes"
|
||||
"Roberts, Alice","02/05/2026","CO Medicaid","Yes","Q944140","01/01/2026","12/31/2026","3125","R69","PCS T1019","T1019","U1","DD Waiver","1","$112.29","INV-2026-02-05-Q944140","Yes"
|
||||
"Wilson, Michelle","02/12/2026","CO Medicaid","Yes","R649327","01/01/2026","12/31/2026","3","R69","Homemaker S5150","S5150","U8","DD Waiver","1","$213.25","INV-2026-02-12-R649327","Yes"
|
||||
"Stoumbaugh, Kiera","02/25/2026","CO Medicaid","Yes","Y188426","01/01/2026","12/31/2026","2","R69","PCS T1019","T1019","KX:SC:U2","DD Waiver","1","$222.72","INV-2026-02-25-Y188426","Yes"
|
||||
"Wilson, Michelle","06/04/2026","CO Medicaid","Yes","R649327","01/01/2026","12/31/2026","3","R69","Homemaker S5150","S5150","U8","DD Waiver","1","$214.22","INV-2026-06-04-R649327","Yes"
|
||||
"Wilson, Michelle","06/11/2026","CO Medicaid","Yes","R649327","01/01/2026","12/31/2026","3","R69","Homemaker S5150","S5150","U8","DD Waiver","1","$213.25","INV-2026-06-11-R649327","Yes"
|
||||
"Wilson, Michelle","06/22/2026","CO Medicaid","Yes","R649327","01/01/2026","12/31/2026","3","R69","Homemaker S5150","S5150","U8","DD Waiver","1","$213.25","INV-2026-06-22-R649327","Yes"
|
||||
"Wilson, Michelle","06/25/2026","CO Medicaid","Yes","R649327","01/01/2026","12/31/2026","3","R69","Homemaker S5150","S5150","U8","DD Waiver","1","$235.55","INV-2026-06-25-R649327","Yes"
|
||||
"Wilson, Michelle","02/02/2026","CO Medicaid","Yes","R649327","01/01/2026","12/31/2026","3","R69","Homemaker S5150","S5150","U8","DD Waiver","1","$212.28","INV-2026-02-02-R649327","Yes"
|
||||
"Wilson, Michelle","02/05/2026","CO Medicaid","Yes","R649327","01/01/2026","12/31/2026","3","R69","Homemaker S5150","S5150","U8","DD Waiver","1","$212.28","INV-2026-02-05-R649327","Yes"
|
||||
|
@@ -0,0 +1,11 @@
|
||||
"Client","Visit Date","Payer","Authorized","Member ID","Auth Start Date","Auth End Date","Authorization #","ICD-10","Service","Procedure Code","Modifiers","Client Classes","Billable Hours","Billable Amount","Invoice #","Claimed"
|
||||
"O'Keefe , Laura","05/09/2026","COHCPF","yes","Y477643","03/10/2026","08/31/2026","9","R69","IHSS PCP T1019","T1019","KX, SC, U2","IHSS DR","8.5333","$237.57","INV-2026-05-09-Y477643","Yes"
|
||||
"O'Keefe , Laura","05/02/2026","COHCPF","yes","Y477643","03/10/2026","08/31/2026","9","R69","IHSS PCP T1019","T1019","KX, SC, U2","IHSS DR","8.5","$236.64","INV-2026-05-02-Y477643","Yes"
|
||||
"Wilson, Michelle","06/25/2026","COHCPF","yes","R649327","07/01/2025","06/30/2026","3","R69","SLS Respite S5150","S5150","U8","SLS/CES","8.1","$235.55","INV-2026-06-25-R649327-B2","Yes"
|
||||
"Stoumbaugh, Kiera","02/25/2026","COHCPF","yes","Y188426","02/01/2026","01/31/2027","2","R69","IHSS PCP T1019","T1019","KX, SC, U2","IHSS Salida, IHSS SR","8","$222.72","INV-2026-02-25-Y188426-B2","Yes"
|
||||
"Hannegrefs, Austyn","05/12/2026","COHCPF","yes","Y552218","06/01/2025","05/31/2026","6","R69","SLS Respite S5150","S5150","U8","SLS/CES","7.5667","$220.04","INV-2026-05-12-Y552218","Yes"
|
||||
"Stoumbaugh, Kiera","04/03/2026","COHCPF","yes","Y188426","02/01/2026","01/31/2027","2","R69","IHSS Attendant H0038","H0038","U2","IHSS Salida, IHSS SR","6","$218.40","INV-2026-04-03-Y188426","Yes"
|
||||
"Wilson, Michelle","03/05/2026","COHCPF","yes","R649327","07/01/2025","06/30/2026","3","R69","SLS Respite S5150","S5150","U8","SLS/CES","7.3333","$213.25","INV-2026-03-05-R649327","Yes"
|
||||
"Wilson, Michelle","06/11/2026","COHCPF","yes","R649327","07/01/2025","06/30/2026","3","R69","SLS Respite S5150","S5150","U8","SLS/CES","7.3333","$213.25","INV-2026-06-11-R649327-B2","Yes"
|
||||
"Wilson, Michelle","06/22/2026","COHCPF","yes","R649327","07/01/2025","06/30/2026","3","R69","SLS Respite S5150","S5150","U8","SLS/CES","7.3333","$213.25","INV-2026-06-22-R649327-B2","Yes"
|
||||
"Wilson, Michelle","03/02/2026","COHCPF","yes","R649327","07/01/2025","06/30/2026","3","R69","SLS Respite S5150","S5150","U8","SLS/CES","7.3167","$212.77","INV-2026-03-02-R649327","Yes"
|
||||
|
@@ -0,0 +1,30 @@
|
||||
ISA*00* *00* *ZZ*11525703 *ZZ*COMEDASSISTPROG*260611*0814*^*00501*991102977*1*P*:~
|
||||
GS*HC*11525703*COMEDASSISTPROG*20260611*081417*991102977*X*005010X222A1~
|
||||
ST*837*991102977*005010X222A1~
|
||||
BHT*0019*00*ref-001*20260611*081417*CH~
|
||||
NM1*41*2*Test Submitter*****46*11525703~
|
||||
PER*IC*Test Contact*EM*test@example.com~
|
||||
NM1*40*2*COLORADO MEDICAL ASSISTANCE PROGRAM*****46*COMEDASSISTPROG~
|
||||
HL*1**20*1~
|
||||
PRV*BI*PXC*251E00000X~
|
||||
NM1*85*2*Test Provider Inc*****XX*1993999998~
|
||||
N3*123 Test St~
|
||||
N4*Denver*CO*80202~
|
||||
REF*EI*123456789~
|
||||
HL*2*1*22*0~
|
||||
SBR*P*18*******MC~
|
||||
NM1*IL*1*Doe*John****MI*ABC123~
|
||||
N3*456 Member St~
|
||||
N4*Denver*CO*80203~
|
||||
DMG*D8*19800101*M~
|
||||
NM1*PR*2*CO_TXIX*****PI*CO_TXIX~
|
||||
CLM*CLM001*100.00***12:B:1*Y*A*Y*Y~
|
||||
REF*G1*PA123~
|
||||
HI*ABK:Z00~
|
||||
LX*1~
|
||||
SV1*HC:99213*100.00*UN*1***1~
|
||||
DTP*472*D8*20260611~
|
||||
REF*6R*REF001~
|
||||
SE*26*991102977~
|
||||
GE*1*991102977~
|
||||
IEA*1*991102977~
|
||||
@@ -0,0 +1,377 @@
|
||||
"""SP38: store-helper tests for ``find_ack_orphan_st02_summary``.
|
||||
|
||||
The summary is the per-ST02 breakdown the ``cyclone ack-orphans``
|
||||
CLI commands consume. It enumerates every distinct orphan 999 ST02
|
||||
+ ack count + whether a ``batches`` row already covers that ST02.
|
||||
|
||||
Tests live here (not in ``test_apply_claim_ack_links.py``) because
|
||||
the helper is a sp38 surface, not an sp28 invariant. Keeping the
|
||||
tests in a sibling file matches the ``cyclone-tests`` convention.
|
||||
|
||||
The autouse ``_auto_init_db`` fixture in ``conftest.py`` provides
|
||||
a fresh ``tmp_path/test.db`` for every test — no manual DB setup
|
||||
needed here.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import pytest
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.db import Ack, Batch, ClaimAck
|
||||
from cyclone.store import CycloneStore
|
||||
|
||||
|
||||
def _now():
|
||||
"""A single shared 'now' for deterministic test timestamps."""
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
def _ingest_999(s, st02: str, *, batch_id: str | None = None) -> int:
|
||||
"""Insert a 999 ack row whose set_responses[0].set_control_number == st02.
|
||||
|
||||
Returns the new ack id. The 999 is an orphan (no claim_acks row
|
||||
is inserted) so it surfaces in the summary.
|
||||
"""
|
||||
raw = {
|
||||
"envelope": {
|
||||
"sender_id": "SUBMITTERID",
|
||||
"receiver_id": "RECEIVERID",
|
||||
"control_number": "000000099",
|
||||
"transaction_date": "2024-01-01",
|
||||
"implementation_guide": "005010X231A1",
|
||||
},
|
||||
"functional_group_acks": [],
|
||||
"set_responses": [
|
||||
{"set_control_number": st02, "transaction_set_identifier": "837",
|
||||
"ak2": {"functional_id_code": "837"}, "segment_errors": [],
|
||||
"set_accept_reject": {"code": "A"}}
|
||||
],
|
||||
"summary": {"accepted_count": 1, "rejected_count": 0},
|
||||
}
|
||||
ack = Ack(
|
||||
source_batch_id=batch_id or f"999-{st02}-test",
|
||||
accepted_count=1,
|
||||
rejected_count=0,
|
||||
received_count=1,
|
||||
ack_code="A",
|
||||
parsed_at=_now(),
|
||||
raw_json=json.dumps(raw),
|
||||
)
|
||||
s.add(ack)
|
||||
s.flush()
|
||||
return ack.id
|
||||
|
||||
|
||||
def _seed_batch(s, *, st02: str, batch_id: str | None = None) -> str:
|
||||
"""Insert a batches row with the given ST02. Returns the batch id."""
|
||||
import uuid
|
||||
bid = batch_id or uuid.uuid4().hex
|
||||
s.add(Batch(
|
||||
id=bid,
|
||||
kind="837p",
|
||||
input_filename="test.837p",
|
||||
transaction_set_control_number=st02,
|
||||
parsed_at=_now(),
|
||||
))
|
||||
s.flush()
|
||||
return bid
|
||||
|
||||
|
||||
def test_summary_returns_per_st02_counts():
|
||||
"""Two distinct orphan ST02s surface as two separate summary rows."""
|
||||
with db.SessionLocal()() as s:
|
||||
_ingest_999(s, "991102989")
|
||||
_ingest_999(s, "991102989") # same ST02 twice
|
||||
_ingest_999(s, "991102988")
|
||||
s.commit()
|
||||
|
||||
summary = CycloneStore().find_ack_orphan_st02_summary()
|
||||
by_st02 = {row["st02"]: row for row in summary}
|
||||
assert by_st02["991102989"]["ack_count"] == 2
|
||||
assert by_st02["991102988"]["ack_count"] == 1
|
||||
assert by_st02["991102989"]["has_batch"] is False
|
||||
assert by_st02["991102988"]["has_batch"] is False
|
||||
|
||||
|
||||
def test_summary_sets_has_batch_true_when_batches_row_exists():
|
||||
"""When a batches row has the orphan ST02, ``has_batch`` is True
|
||||
and ``batch_id`` matches the batches row's id."""
|
||||
with db.SessionLocal()() as s:
|
||||
bid = _seed_batch(s, st02="991102977")
|
||||
_ingest_999(s, "991102977", batch_id=bid)
|
||||
s.commit()
|
||||
|
||||
summary = CycloneStore().find_ack_orphan_st02_summary()
|
||||
assert len(summary) == 1
|
||||
row = summary[0]
|
||||
assert row["st02"] == "991102977"
|
||||
assert row["ack_count"] == 1
|
||||
assert row["has_batch"] is True
|
||||
assert row["batch_id"] == bid
|
||||
|
||||
|
||||
def test_summary_sorted_by_ack_count_descending():
|
||||
"""The summary is sorted so the heaviest orphans surface first
|
||||
in the CLI output (matches the spec's intent that operators
|
||||
triage the biggest backlog first)."""
|
||||
with db.SessionLocal()() as s:
|
||||
for _ in range(3):
|
||||
_ingest_999(s, "991102988")
|
||||
_ingest_999(s, "991102987")
|
||||
for _ in range(5):
|
||||
_ingest_999(s, "991102989")
|
||||
s.commit()
|
||||
|
||||
summary = CycloneStore().find_ack_orphan_st02_summary()
|
||||
counts = [row["ack_count"] for row in summary]
|
||||
assert counts == sorted(counts, reverse=True)
|
||||
assert counts[0] == 5 # 991102989 has the most
|
||||
|
||||
|
||||
def test_summary_excludes_999s_that_have_a_claim_acks_link():
|
||||
"""A 999 with a claim_acks row is NOT an orphan and must not
|
||||
appear in the summary."""
|
||||
with db.SessionLocal()() as s:
|
||||
linked_id = _ingest_999(s, "991102977")
|
||||
_ingest_999(s, "991102988") # orphan, no link
|
||||
# Manually insert a claim_acks link for the first 999.
|
||||
s.add(ClaimAck(
|
||||
claim_id="CLM-1",
|
||||
batch_id="b1",
|
||||
ack_id=linked_id,
|
||||
ack_kind="999",
|
||||
ak2_index=0,
|
||||
set_control_number="991102977",
|
||||
set_accept_reject_code="A",
|
||||
linked_at=_now(),
|
||||
linked_by="auto",
|
||||
))
|
||||
s.commit()
|
||||
|
||||
summary = CycloneStore().find_ack_orphan_st02_summary()
|
||||
by_st02 = {row["st02"]: row for row in summary}
|
||||
assert "991102977" not in by_st02 # linked → not orphan
|
||||
assert by_st02["991102988"]["ack_count"] == 1
|
||||
|
||||
|
||||
def test_summary_skips_999s_with_malformed_raw_json():
|
||||
"""999s whose raw_json is missing set_responses or is malformed
|
||||
JSON are SKIPPED — they can't be reconciled, so they don't
|
||||
contribute to the summary."""
|
||||
with db.SessionLocal()() as s:
|
||||
# Valid orphan
|
||||
_ingest_999(s, "991102988")
|
||||
# Malformed JSON — raw_json is not parseable
|
||||
s.add(Ack(
|
||||
source_batch_id="999-malformed",
|
||||
accepted_count=1, rejected_count=0, received_count=1,
|
||||
ack_code="A",
|
||||
parsed_at=_now(),
|
||||
raw_json="{not valid json",
|
||||
))
|
||||
# Empty set_responses
|
||||
s.add(Ack(
|
||||
source_batch_id="999-empty",
|
||||
accepted_count=1, rejected_count=0, received_count=1,
|
||||
ack_code="A",
|
||||
parsed_at=_now(),
|
||||
raw_json=json.dumps({"envelope": {}, "set_responses": [], "summary": {}}),
|
||||
))
|
||||
s.commit()
|
||||
|
||||
summary = CycloneStore().find_ack_orphan_st02_summary()
|
||||
by_st02 = {row["st02"]: row for row in summary}
|
||||
assert set(by_st02.keys()) == {"991102988"}
|
||||
assert by_st02["991102988"]["ack_count"] == 1
|
||||
|
||||
|
||||
def test_summary_empty_when_no_orphans():
|
||||
"""With zero orphans, the summary is an empty list."""
|
||||
summary = CycloneStore().find_ack_orphan_st02_summary()
|
||||
assert summary == []
|
||||
|
||||
|
||||
# ---- Task 3: reconcile_orphan_st02s --------------------------------------- #
|
||||
|
||||
|
||||
def test_reconcile_creates_synthetic_batches_for_missing_st02s():
|
||||
"""``reconcile_orphan_st02s`` inserts one synthetic batch row per
|
||||
orphan ST02 that doesn't already have a batches row.
|
||||
|
||||
Each synthetic row uses the sentinel ``input_filename`` and
|
||||
``kind = '837p'`` so they're trivially distinguishable in
|
||||
queries (the spec: grep for ``<synthetic:orphan-reconcile>``
|
||||
to find every row this SP38 created).
|
||||
"""
|
||||
with db.SessionLocal()() as s:
|
||||
_ingest_999(s, "991102989")
|
||||
_ingest_999(s, "991102988")
|
||||
s.commit()
|
||||
|
||||
plan = CycloneStore().reconcile_orphan_st02s(dry_run=False)
|
||||
|
||||
assert plan["created"] == 2
|
||||
assert plan["skipped"] == 0
|
||||
assert len(plan["synthetic_batch_ids"]) == 2
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
rows = (
|
||||
s.query(Batch)
|
||||
.filter(Batch.input_filename == "<synthetic:orphan-reconcile>")
|
||||
.all()
|
||||
)
|
||||
assert len(rows) == 2
|
||||
assert all(r.kind == "837p" for r in rows)
|
||||
st02s = {r.transaction_set_control_number for r in rows}
|
||||
assert st02s == {"991102989", "991102988"}
|
||||
|
||||
|
||||
def test_reconcile_skips_st02s_that_already_have_a_batch():
|
||||
"""``reconcile`` does NOT create a synthetic row for an ST02
|
||||
that already has a batches row — the operator's intent is to
|
||||
fill gaps, not duplicate coverage."""
|
||||
with db.SessionLocal()() as s:
|
||||
existing = _seed_batch(s, st02="991102977")
|
||||
_ingest_999(s, "991102977", batch_id=existing)
|
||||
_ingest_999(s, "991102988") # orphan, no batch
|
||||
s.commit()
|
||||
|
||||
plan = CycloneStore().reconcile_orphan_st02s(dry_run=False)
|
||||
|
||||
assert plan["created"] == 1
|
||||
assert plan["skipped"] == 1
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
synthetic = (
|
||||
s.query(Batch)
|
||||
.filter(Batch.input_filename == "<synthetic:orphan-reconcile>")
|
||||
.all()
|
||||
)
|
||||
assert len(synthetic) == 1
|
||||
assert synthetic[0].transaction_set_control_number == "991102988"
|
||||
|
||||
|
||||
def test_reconcile_is_idempotent():
|
||||
"""Re-running reconcile after a successful pass is a no-op:
|
||||
``created=0, skipped=N``."""
|
||||
with db.SessionLocal()() as s:
|
||||
_ingest_999(s, "991102989")
|
||||
s.commit()
|
||||
|
||||
CycloneStore().reconcile_orphan_st02s(dry_run=False)
|
||||
plan2 = CycloneStore().reconcile_orphan_st02s(dry_run=False)
|
||||
|
||||
assert plan2["created"] == 0
|
||||
assert plan2["skipped"] == 1
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
n = (
|
||||
s.query(Batch)
|
||||
.filter(Batch.input_filename == "<synthetic:orphan-reconcile>")
|
||||
.count()
|
||||
)
|
||||
assert n == 1
|
||||
|
||||
|
||||
def test_reconcile_dry_run_does_not_write():
|
||||
"""``dry_run=True`` returns the same plan shape but does not
|
||||
insert any rows. Operators use this to preview the reconcile
|
||||
before committing."""
|
||||
with db.SessionLocal()() as s:
|
||||
_ingest_999(s, "991102989")
|
||||
s.commit()
|
||||
|
||||
plan = CycloneStore().reconcile_orphan_st02s(dry_run=True)
|
||||
|
||||
assert plan["created"] == 1
|
||||
assert plan["skipped"] == 0
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
n = (
|
||||
s.query(Batch)
|
||||
.filter(Batch.input_filename == "<synthetic:orphan-reconcile>")
|
||||
.count()
|
||||
)
|
||||
assert n == 0 # no row inserted
|
||||
|
||||
|
||||
def test_reconcile_records_ack_count_in_totals_json():
|
||||
"""The synthetic row's ``totals_json`` includes ``ack_count`` so
|
||||
future operators can see the orphan weight without re-querying."""
|
||||
import json
|
||||
with db.SessionLocal()() as s:
|
||||
for _ in range(4):
|
||||
_ingest_999(s, "991102989")
|
||||
s.commit()
|
||||
|
||||
CycloneStore().reconcile_orphan_st02s(dry_run=False)
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
row = (
|
||||
s.query(Batch)
|
||||
.filter(Batch.transaction_set_control_number == "991102989")
|
||||
.one()
|
||||
)
|
||||
totals = json.loads(row.totals_json or "{}")
|
||||
assert totals.get("orphan_reconcile") is True
|
||||
assert totals.get("ack_count") == 4
|
||||
|
||||
|
||||
def test_reconcile_sentinel_is_grep_discoverable():
|
||||
"""``input_filename = '<synthetic:orphan-reconcile>'`` must be
|
||||
discoverable via a plain ``LIKE '<synthetic:%>'`` query so
|
||||
operators can find every sp38-created row without knowing the
|
||||
exact sentinel string. Pins the spec's D2 grep-discoverability
|
||||
invariant."""
|
||||
with db.SessionLocal()() as s:
|
||||
_ingest_999(s, "991102988")
|
||||
s.commit()
|
||||
|
||||
CycloneStore().reconcile_orphan_st02s(dry_run=False)
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
n = (
|
||||
s.query(Batch)
|
||||
.filter(Batch.input_filename.like("<synthetic:%>"))
|
||||
.count()
|
||||
)
|
||||
assert n == 1
|
||||
|
||||
|
||||
def test_summary_skips_999s_with_non_dict_raw_json():
|
||||
"""``_extract_999_st02`` must tolerate the un-dict shapes a JSON
|
||||
column can take (None, list) and never raise. Rows with
|
||||
non-dict raw_json are skipped — they have no ST02 to contribute
|
||||
to the summary regardless.
|
||||
|
||||
Note: the SQLAlchemy JSON column rejects bytes at insert time
|
||||
(it must be JSON-serializable), so we don't exercise the
|
||||
``bytes`` branch here — that's a defensive-programming guard for
|
||||
raw sqlite3 reads, not a normal ORM codepath.
|
||||
"""
|
||||
with db.SessionLocal()() as s:
|
||||
# Valid orphan — contributes 1 row
|
||||
_ingest_999(s, "991102988")
|
||||
# None — should be skipped, not raise
|
||||
s.add(Ack(
|
||||
source_batch_id="999-none-raw",
|
||||
accepted_count=1, rejected_count=0, received_count=1,
|
||||
ack_code="A", parsed_at=_now(),
|
||||
raw_json=None,
|
||||
))
|
||||
# list — not a dict; should be skipped (not raise)
|
||||
s.add(Ack(
|
||||
source_batch_id="999-list-raw",
|
||||
accepted_count=1, rejected_count=0, received_count=1,
|
||||
ack_code="A", parsed_at=_now(),
|
||||
raw_json=[1, 2, 3],
|
||||
))
|
||||
s.commit()
|
||||
|
||||
summary = CycloneStore().find_ack_orphan_st02_summary()
|
||||
by_st02 = {row["st02"]: row for row in summary}
|
||||
assert by_st02 == {"991102988": {"st02": "991102988", "ack_count": 1,
|
||||
"has_batch": False, "batch_id": None}}
|
||||
@@ -0,0 +1,210 @@
|
||||
"""SP38: CLI tests for ``cyclone ack-orphans {status,reconcile}``.
|
||||
|
||||
Uses ``click.testing.CliRunner`` (matching the SP37-followup #5
|
||||
pattern that replaced ``subprocess.run`` with the in-process
|
||||
runner). The CLI commands are thin wrappers over the store helpers
|
||||
tested in ``test_ack_orphan_summary.py`` — these tests pin the
|
||||
shell-facing shape (table format, exit codes, --dry-run flag).
|
||||
|
||||
The autouse ``_auto_init_db`` fixture in ``conftest.py`` provides
|
||||
a fresh ``tmp_path/test.db`` for every test; the CLI commands pick
|
||||
it up via the ``CYCLONE_DB_URL`` env var the fixture sets.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import pytest
|
||||
from click.testing import CliRunner
|
||||
|
||||
from cyclone import db
|
||||
from cyclone.cli import main
|
||||
from cyclone.db import Ack, Batch
|
||||
|
||||
|
||||
def _now():
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
def _ingest_orphan(s, st02: str) -> int:
|
||||
"""Insert a 999 ack row whose set_responses[0].set_control_number == st02.
|
||||
|
||||
Returns the new ack id. The 999 is an orphan (no claim_acks row)
|
||||
so it surfaces in the summary.
|
||||
"""
|
||||
raw = {
|
||||
"envelope": {"sender_id": "S", "receiver_id": "R",
|
||||
"control_number": "000000099", "transaction_date": "2024-01-01",
|
||||
"implementation_guide": "005010X231A1"},
|
||||
"functional_group_acks": [],
|
||||
"set_responses": [
|
||||
{"set_control_number": st02, "transaction_set_identifier": "837",
|
||||
"ak2": {"functional_id_code": "837"}, "segment_errors": [],
|
||||
"set_accept_reject": {"code": "A"}}
|
||||
],
|
||||
"summary": {"accepted_count": 1, "rejected_count": 0},
|
||||
}
|
||||
ack = Ack(
|
||||
source_batch_id=f"999-{st02}-cli-test",
|
||||
accepted_count=1, rejected_count=0, received_count=1,
|
||||
ack_code="A", parsed_at=_now(), raw_json=json.dumps(raw),
|
||||
)
|
||||
s.add(ack)
|
||||
s.flush()
|
||||
return ack.id
|
||||
|
||||
|
||||
# ---- cyclone ack-orphans status ------------------------------------------ #
|
||||
|
||||
|
||||
def test_status_prints_table_with_orphans():
|
||||
"""``cyclone ack-orphans status`` prints a table with ST02 +
|
||||
ack count + has-batch flag, plus a TOTAL line at the bottom.
|
||||
|
||||
Mirrors the spec's intent: operators want a one-line-per-orphan
|
||||
view that ranks the heaviest backlog first.
|
||||
"""
|
||||
with db.SessionLocal()() as s:
|
||||
_ingest_orphan(s, "991102989")
|
||||
_ingest_orphan(s, "991102988")
|
||||
s.commit()
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["ack-orphans", "status"])
|
||||
assert result.exit_code == 0, result.output
|
||||
# Table header + two data rows + total line.
|
||||
assert "ST02" in result.output
|
||||
assert "ACK COUNT" in result.output
|
||||
assert "991102989" in result.output
|
||||
assert "991102988" in result.output
|
||||
assert "TOTAL" in result.output
|
||||
|
||||
|
||||
def test_status_exits_zero_on_empty_db():
|
||||
"""With no orphans, status exits 0 and prints a 'no orphans' note."""
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["ack-orphans", "status"])
|
||||
assert result.exit_code == 0, result.output
|
||||
# The empty case should be informative, not silent.
|
||||
assert "no orphans" in result.output.lower() or "0" in result.output
|
||||
|
||||
|
||||
def test_status_table_includes_has_batch_column():
|
||||
"""When a ST02 already has a batches row, the table marks it
|
||||
with ``yes`` (or similar) so operators can see which orphans
|
||||
are unbacked vs already-covered.
|
||||
"""
|
||||
with db.SessionLocal()() as s:
|
||||
# Orphan with NO batch.
|
||||
_ingest_orphan(s, "991102988")
|
||||
# Orphan WITH a pre-existing batch row.
|
||||
import uuid
|
||||
bid = uuid.uuid4().hex
|
||||
s.add(Batch(
|
||||
id=bid, kind="837p", input_filename="test.837p",
|
||||
transaction_set_control_number="991102977", parsed_at=_now(),
|
||||
))
|
||||
_ingest_orphan(s, "991102977")
|
||||
s.commit()
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["ack-orphans", "status"])
|
||||
assert result.exit_code == 0, result.output
|
||||
# Look for "yes" / "no" markers in the table.
|
||||
lines = result.output.splitlines()
|
||||
has_yes = any("yes" in ln.lower() for ln in lines)
|
||||
has_no = any("no" in ln.lower() for ln in lines)
|
||||
assert has_yes and has_no, (
|
||||
f"Expected table to mark 'yes' for ST02s with batches and "
|
||||
f"'no' for ST02s without. Got:\n{result.output}"
|
||||
)
|
||||
|
||||
|
||||
# ---- cyclone ack-orphans reconcile --------------------------------------- #
|
||||
|
||||
|
||||
def test_reconcile_creates_synthetic_batches():
|
||||
"""``cyclone ack-orphans reconcile`` inserts a synthetic batch
|
||||
row for each orphan ST02 without one. Confirms the count + the
|
||||
sentinel filename.
|
||||
"""
|
||||
with db.SessionLocal()() as s:
|
||||
_ingest_orphan(s, "991102989")
|
||||
_ingest_orphan(s, "991102988")
|
||||
s.commit()
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["ack-orphans", "reconcile"])
|
||||
assert result.exit_code == 0, result.output
|
||||
|
||||
# The CLI should print the created/skipped counts.
|
||||
assert "Created" in result.output or "created" in result.output
|
||||
assert "Skipped" in result.output or "skipped" in result.output
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
synthetic = (
|
||||
s.query(Batch)
|
||||
.filter(Batch.input_filename == "<synthetic:orphan-reconcile>")
|
||||
.all()
|
||||
)
|
||||
assert len(synthetic) == 2
|
||||
st02s = {r.transaction_set_control_number for r in synthetic}
|
||||
assert st02s == {"991102989", "991102988"}
|
||||
|
||||
|
||||
def test_reconcile_dry_run_does_not_write():
|
||||
"""``--dry-run`` returns the plan shape but does NOT insert rows.
|
||||
Operators use this to preview before committing.
|
||||
"""
|
||||
with db.SessionLocal()() as s:
|
||||
_ingest_orphan(s, "991102989")
|
||||
s.commit()
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["ack-orphans", "reconcile", "--dry-run"])
|
||||
assert result.exit_code == 0, result.output
|
||||
# The plan reports the would-create count.
|
||||
assert "1" in result.output # would-create 1 row
|
||||
|
||||
# But no synthetic batch row was inserted.
|
||||
with db.SessionLocal()() as s:
|
||||
n = (
|
||||
s.query(Batch)
|
||||
.filter(Batch.input_filename == "<synthetic:orphan-reconcile>")
|
||||
.count()
|
||||
)
|
||||
assert n == 0
|
||||
|
||||
|
||||
def test_reconcile_is_idempotent():
|
||||
"""A second reconcile after a successful pass is a no-op:
|
||||
created=0, skipped=N. Tested at the CLI shell level so the
|
||||
user-visible output also pins this invariant.
|
||||
"""
|
||||
with db.SessionLocal()() as s:
|
||||
_ingest_orphan(s, "991102989")
|
||||
s.commit()
|
||||
|
||||
runner = CliRunner()
|
||||
first = runner.invoke(main, ["ack-orphans", "reconcile"])
|
||||
second = runner.invoke(main, ["ack-orphans", "reconcile"])
|
||||
assert first.exit_code == 0
|
||||
assert second.exit_code == 0
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
n = (
|
||||
s.query(Batch)
|
||||
.filter(Batch.input_filename == "<synthetic:orphan-reconcile>")
|
||||
.count()
|
||||
)
|
||||
assert n == 1 # still exactly one — second call was a no-op
|
||||
|
||||
|
||||
def test_reconcile_handles_no_orphans_gracefully():
|
||||
"""With zero orphans, reconcile exits 0 and prints an informative
|
||||
message (no synthetic rows created, no error)."""
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["ack-orphans", "reconcile"])
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "0" in result.output # created=0, skipped=0
|
||||
@@ -51,7 +51,7 @@ def test_migration_0002_creates_acks_table():
|
||||
|
||||
def test_migration_latest_idempotent_on_fresh_db():
|
||||
"""Re-running the migration on the same DB must be a no-op (PRAGMA
|
||||
user_version already at the latest version — currently 19 after
|
||||
user_version already at the latest version — currently 22 after
|
||||
0004-0006 line_reconciliation, 0005 ta1_acks, SP9's 0007
|
||||
providers/payers/clearhouse, SP10's 0008 payer_rejected,
|
||||
SP11's 0009 audit_log, SP14's 0010 payer_rejected_acknowledged,
|
||||
@@ -61,15 +61,17 @@ def test_migration_latest_idempotent_on_fresh_db():
|
||||
claims.matched_remittance_id index, SP27-Task 17's 0017
|
||||
claim.patient_control_number backfill UPDATE, SP28's 0018
|
||||
claim_acks join table, SP32's 0019
|
||||
rendering_provider_npi + service_provider_npi)."""
|
||||
rendering_provider_npi + service_provider_npi, SP37's 0020
|
||||
transaction_set_control_number, SP39's 0021 resubmissions
|
||||
audit table, and SP41's 0022 submission_dedup)."""
|
||||
with db.engine().begin() as c:
|
||||
v1 = c.exec_driver_sql("PRAGMA user_version").scalar() or 0
|
||||
assert v1 == 19
|
||||
assert v1 == 23
|
||||
# A second run should not raise and should not bump the version.
|
||||
db_migrate.run(db.engine())
|
||||
with db.engine().begin() as c:
|
||||
v2 = c.exec_driver_sql("PRAGMA user_version").scalar() or 0
|
||||
assert v2 == 19
|
||||
assert v2 == 23
|
||||
|
||||
|
||||
def test_add_ack_persists_row():
|
||||
|
||||
@@ -0,0 +1,226 @@
|
||||
"""SP41 Task 11 — HTTP endpoints for the in-window rebill pipeline.
|
||||
|
||||
Three tests:
|
||||
|
||||
1. ``test_post_rebill_requires_auth_or_returns_404_or_422_when_no_input`` —
|
||||
bare POST with no body. With ``AUTH_DISABLED = True`` (the autouse
|
||||
conftest default) the request reaches the handler; without a body,
|
||||
Pydantic returns 422. If AUTH_DISABLED is ever flipped off, the
|
||||
matrix_gate returns 401/403 first. Either outcome proves the route
|
||||
is wired.
|
||||
|
||||
2. ``test_post_rebill_runs_and_returns_summary_path`` — happy path.
|
||||
Builds a 1-row visits CSV + zero-SVC 835 fixture under tmp_path,
|
||||
POSTs with the window pinned + paths pointed at the fixtures,
|
||||
asserts 200 + ``summary_path`` + a ``counts`` dict that includes
|
||||
the REBILLED_B key (the in-window visit lands as NOT_IN_835 →
|
||||
REBILLED_B because the 835 has no SVCs and the visit is fresh
|
||||
enough to clear the 120-day gate).
|
||||
|
||||
3. ``test_get_rebill_status_returns_recent_runs`` — seeds a stub
|
||||
``summary.csv`` under ``tmp_path / "rebills_root"`` and
|
||||
monkey-patches :data:`cyclone.api_routers.rebill.REBILLS_DIR` to
|
||||
point there, so the GET handler's filesystem scan finds it
|
||||
without the test having to chdir the whole process.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Fixtures
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client() -> TestClient:
|
||||
"""Standard TestClient; conftest.py autouse handles DB + auth gate.
|
||||
|
||||
AUTH_DISABLED is True so the matrix_gate short-circuits and the
|
||||
request reaches the handler (no login dance required). The seeded
|
||||
clearhouse isn't needed — the rebill pipeline doesn't touch SFTP.
|
||||
"""
|
||||
from cyclone.api import app
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
def _write_visits_csv(path: Path, rows: list[tuple[str, str, str, str]]) -> Path:
|
||||
"""rows: list of (dos_mmddyyyy, member, procedure, billed_str)."""
|
||||
with path.open("w", newline="") as f:
|
||||
w = csv.writer(f)
|
||||
w.writerow(["Visit Date", "Member ID", "Procedure Code", "Billable Amount"])
|
||||
for r in rows:
|
||||
w.writerow(r)
|
||||
return path
|
||||
|
||||
|
||||
def _stub_835(ingest_dir: Path, name: str = "x.835") -> Path:
|
||||
"""An *.835 file parseable enough to not crash the walker.
|
||||
|
||||
Content is just an empty 835 envelope; the rebill pipeline's
|
||||
``parse_835_svc`` reparser emits zero SVCs, which is what we want
|
||||
for the NOT_IN_835 → REBILLED_B classification.
|
||||
"""
|
||||
ingest_dir.mkdir(exist_ok=True)
|
||||
p = ingest_dir / name
|
||||
p.write_text("ST*835*0001~SE*0*0001~")
|
||||
return p
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Tests
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def test_post_rebill_requires_auth_or_returns_404_or_422_when_no_input(client):
|
||||
"""Bare POST with no body — should NOT 500.
|
||||
|
||||
With AUTH_DISABLED the matrix_gate short-circuits and Pydantic
|
||||
validation fires on the empty body (422). With auth enabled the
|
||||
matrix_gate returns 401/403 first. Either outcome proves the route
|
||||
is wired and the request reached the handler.
|
||||
"""
|
||||
resp = client.post("/api/admin/rebill-from-835")
|
||||
assert resp.status_code in (401, 403, 422), (
|
||||
f"unexpected status {resp.status_code}: {resp.text}"
|
||||
)
|
||||
|
||||
|
||||
def test_post_rebill_runs_and_returns_summary_path(client, tmp_path):
|
||||
"""Happy path: 1 in-window visit, 0 SVCs → REBILLED_B + summary.csv."""
|
||||
visits_path = _write_visits_csv(tmp_path / "visits.csv", [
|
||||
("06/27/2026", "J813715", "T1019", "$2.32"),
|
||||
])
|
||||
ingest_dir = _stub_835(tmp_path / "ingest")
|
||||
out_dir = tmp_path / "out"
|
||||
|
||||
resp = client.post(
|
||||
"/api/admin/rebill-from-835",
|
||||
json={
|
||||
"window": "2026-01-01..2026-06-27",
|
||||
"override_filing": False,
|
||||
"visits_csv_path": str(visits_path),
|
||||
"ingest_dir": str(ingest_dir),
|
||||
"out_dir": str(out_dir),
|
||||
},
|
||||
)
|
||||
assert resp.status_code == 200, resp.text
|
||||
body = resp.json()
|
||||
assert "summary_path" in body, body
|
||||
assert "counts" in body, body
|
||||
assert "pipeline_a_files" in body, body
|
||||
assert "pipeline_b_files" in body, body
|
||||
|
||||
# summary_path is real and points at the out_dir we passed in.
|
||||
assert Path(body["summary_path"]).exists(), body["summary_path"]
|
||||
assert str(out_dir) in body["summary_path"]
|
||||
|
||||
# The counts dict surfaces REBILLED_B for the in-window visit (the
|
||||
# 835 has no SVCs so the visit falls into NOT_IN_835 → REBILLED_B
|
||||
# — it's well within the 120-day gate).
|
||||
counts = body["counts"]
|
||||
assert "REBILLED_B" in counts, counts
|
||||
assert counts["REBILLED_B"] >= 1, counts
|
||||
|
||||
# Pipeline B emitted exactly one file for the one REBILLED_B visit.
|
||||
assert len(body["pipeline_b_files"]) == 1, body["pipeline_b_files"]
|
||||
|
||||
# The summary CSV header matches the writer's contract so the GET
|
||||
# /status handler can parse it.
|
||||
with open(body["summary_path"], newline="") as f:
|
||||
reader = csv.DictReader(f)
|
||||
rows = list(reader)
|
||||
assert len(rows) == 1, rows
|
||||
assert rows[0]["disposition"] == "REBILLED_B"
|
||||
assert rows[0]["member_id"] == "J813715"
|
||||
|
||||
|
||||
def test_get_rebill_status_returns_recent_runs(client, tmp_path, monkeypatch):
|
||||
"""GET /status scans the configured REBILLS_DIR and tallies per-disposition counts.
|
||||
|
||||
The handler reads :data:`cyclone.api_routers.rebill.REBILLS_DIR`
|
||||
(module-level so tests can monkeypatch it). We point that at a
|
||||
tmp_path-relative tree, seed one dated subdir with a 2-row
|
||||
summary.csv, and assert the response surfaces that directory's
|
||||
name + per-disposition tally.
|
||||
"""
|
||||
from cyclone.api_routers import rebill
|
||||
|
||||
# Build a date-named subdir with a real summary.csv shape so the
|
||||
# handler's csv.DictReader finds a `disposition` column.
|
||||
rebills_root = tmp_path / "rebills_root"
|
||||
dated_dir = rebills_root / "2026-07-07"
|
||||
dated_dir.mkdir(parents=True)
|
||||
summary = dated_dir / "summary.csv"
|
||||
with summary.open("w", newline="") as f:
|
||||
w = csv.writer(f)
|
||||
w.writerow([
|
||||
"dos", "member_id", "procedure", "billed",
|
||||
"disposition", "unpaid", "cas_reasons", "file_path",
|
||||
])
|
||||
w.writerow([
|
||||
"2026-06-27", "MEM-A", "T1019", "2.32",
|
||||
"REBILLED_B", "2.32", "", "pipeline-b/",
|
||||
])
|
||||
w.writerow([
|
||||
"2020-01-01", "MEM-B", "T1019", "2.32",
|
||||
"EXCLUDED_TIMELY_FILING", "2.32", "", "",
|
||||
])
|
||||
|
||||
# Repoint the handler's filesystem root at our tmp_path tree.
|
||||
monkeypatch.setattr(rebill, "REBILLS_DIR", rebills_root)
|
||||
|
||||
resp = client.get("/api/admin/rebill-from-835/status")
|
||||
assert resp.status_code == 200, resp.text
|
||||
body = resp.json()
|
||||
assert "recent_runs" in body, body
|
||||
assert len(body["recent_runs"]) == 1, body["recent_runs"]
|
||||
|
||||
run = body["recent_runs"][0]
|
||||
assert run["as_of"] == "2026-07-07", run
|
||||
assert "summary_path" in run, run
|
||||
assert Path(run["summary_path"]).exists(), run["summary_path"]
|
||||
counts = run["counts"]
|
||||
# Each disposition surfaced exactly once — matches the 2-row CSV.
|
||||
assert counts.get("REBILLED_B") == 1, counts
|
||||
assert counts.get("EXCLUDED_TIMELY_FILING") == 1, counts
|
||||
|
||||
|
||||
def test_post_rebill_rejects_malformed_window(client):
|
||||
"""Pydantic window validator should 422 on bad shapes (no `..`, non-dates, reversed)."""
|
||||
bad_windows = [
|
||||
"2026-01-01", # no `..` separator
|
||||
"foo..bar", # non-ISO dates
|
||||
"2026-12-01..2026-01-01", # start after end
|
||||
]
|
||||
for w in bad_windows:
|
||||
resp = client.post(
|
||||
"/api/admin/rebill-from-835",
|
||||
json={"window": w},
|
||||
)
|
||||
assert resp.status_code == 422, (
|
||||
f"expected 422 for window={w!r}, got {resp.status_code}: {resp.text}"
|
||||
)
|
||||
|
||||
|
||||
def test_get_rebill_status_returns_empty_when_no_runs(client, tmp_path, monkeypatch):
|
||||
"""GET /status on an empty rebills dir must return {"recent_runs": []}, not 500.
|
||||
|
||||
Monkeypatches REBILLS_DIR to a fresh tmp_path subdir so the test
|
||||
doesn't depend on (or pollute) any pre-existing dev/rebills/ tree.
|
||||
"""
|
||||
from cyclone.api_routers import rebill
|
||||
|
||||
monkeypatch.setattr(rebill, "REBILLS_DIR", tmp_path / "rebills_empty")
|
||||
|
||||
resp = client.get("/api/admin/rebill-from-835/status")
|
||||
assert resp.status_code == 200, resp.text
|
||||
assert resp.json() == {"recent_runs": []}
|
||||
|
||||
@@ -82,7 +82,9 @@ class TestRotateKeyEndpointWiring:
|
||||
lambda n, v: fake_kc.__setitem__(n, v) or True)
|
||||
# The endpoint's actual rekey is stubbed; the real PRAGMA
|
||||
# rekey mechanics are tested in test_db_crypto.py::TestRotateDbKey.
|
||||
monkeypatch.setattr("cyclone.api._db_crypto.rotate_db_key", _stub_rotate_ok)
|
||||
# SP36 Task 3: this endpoint moved from cyclone.api to
|
||||
# cyclone.api_routers.admin; patch the live import surface.
|
||||
monkeypatch.setattr("cyclone.api_routers.admin._db_crypto.rotate_db_key", _stub_rotate_ok)
|
||||
db.init_db()
|
||||
yield db_file, fake_kc
|
||||
db._reset_for_tests()
|
||||
@@ -151,7 +153,7 @@ class TestRotateKeyEndpointWiring:
|
||||
rotated_at=datetime.now(timezone.utc).isoformat(),
|
||||
reason="simulated PRAGMA rekey failure",
|
||||
)
|
||||
monkeypatch.setattr("cyclone.api._db_crypto.rotate_db_key", _fail_rotate)
|
||||
monkeypatch.setattr("cyclone.api_routers.admin._db_crypto.rotate_db_key", _fail_rotate)
|
||||
|
||||
_, fake_kc = _fake_encrypted_env
|
||||
before = dict(fake_kc)
|
||||
@@ -187,7 +189,8 @@ class TestRotateKeyEndpointWiring:
|
||||
restore-key command."""
|
||||
from cyclone import db
|
||||
# Override the set_secret at the import-site of the endpoint.
|
||||
monkeypatch.setattr("cyclone.api._secrets.set_secret", lambda n, v: False)
|
||||
# SP36 Task 3: endpoint moved to cyclone.api_routers.admin.
|
||||
monkeypatch.setattr("cyclone.api_routers.admin._secrets.set_secret", lambda n, v: False)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from cyclone.api import app
|
||||
@@ -202,10 +205,11 @@ class TestRotateKeyEndpointWiring:
|
||||
"""A second concurrent rotation request gets 409 — only one
|
||||
rotation can run at a time (the module-level lock)."""
|
||||
monkeypatch.setattr(
|
||||
"cyclone.api._secrets.set_secret", lambda n, v: True,
|
||||
"cyclone.api_routers.admin._secrets.set_secret", lambda n, v: True,
|
||||
)
|
||||
from cyclone import api as api_mod
|
||||
api_mod._db_rotate_lock.acquire()
|
||||
# SP36 Task 3: lock moved with the endpoint into admin router.
|
||||
from cyclone.api_routers import admin as admin_mod
|
||||
admin_mod._db_rotate_lock.acquire()
|
||||
try:
|
||||
from fastapi.testclient import TestClient
|
||||
from cyclone.api import app
|
||||
@@ -214,4 +218,4 @@ class TestRotateKeyEndpointWiring:
|
||||
assert r.status_code == 409
|
||||
assert "in progress" in r.json()["detail"]
|
||||
finally:
|
||||
api_mod._db_rotate_lock.release()
|
||||
admin_mod._db_rotate_lock.release()
|
||||
|
||||
@@ -52,3 +52,48 @@ def test_endpoint_regenerated_text_round_trips_through_parser():
|
||||
result = parse(r.text, PayerConfig(name="CO_MEDICAID"))
|
||||
assert result.claims, "endpoint body didn't parse back to any claims"
|
||||
assert result.claims[0].claim.claim_id == claim_id
|
||||
|
||||
|
||||
def test_endpoint_emits_real_submitter_and_receiver():
|
||||
"""SP40: the single-claim download must thread real submitter +
|
||||
receiver identity through, not the serializer's placeholder
|
||||
fallback. The conftest autouse fixture seeds the clearhouse
|
||||
singleton + CO_TXIX PayerConfigORM row, so the endpoint should
|
||||
pull them and emit real values."""
|
||||
from cyclone import db as cycl_db
|
||||
from cyclone.db import ClearhouseORM
|
||||
from cyclone.store import store as cycl_store
|
||||
|
||||
cycl_db.init_db()
|
||||
cycl_store.ensure_clearhouse_seeded()
|
||||
|
||||
with TestClient(app) as client:
|
||||
claim_id = _seed_claim(client)
|
||||
r = client.get(f"/api/claims/{claim_id}/serialize-837")
|
||||
assert r.status_code == 200
|
||||
body = r.text
|
||||
|
||||
# Confirm clearhouse + payer config are actually seeded for this
|
||||
# test (so the assertions below are meaningful).
|
||||
with cycl_db.SessionLocal()() as s:
|
||||
ch = s.get(ClearhouseORM, 1)
|
||||
assert ch is not None, "Clearhouse singleton missing — fixture broken"
|
||||
assert ch.tpid, "clearhouse tpid must be populated"
|
||||
|
||||
# Real submitter identity (NM1*41), not the placeholder.
|
||||
assert "Dzinesco" in body, body[:500]
|
||||
assert ch.tpid in body, body[:500]
|
||||
# Real submitter contact (PER segment).
|
||||
assert "Tyler Martinez" in body, body[:500]
|
||||
assert "tyler@dzinesco.com" in body, body[:500]
|
||||
# Real receiver (NM1*40).
|
||||
assert "COLORADO MEDICAL ASSISTANCE PROGRAM" in body, body[:500]
|
||||
assert "COMEDASSISTPROG" in body, body[:500]
|
||||
# Real SBR-09 (Claim Filing Indicator Code) for CO Medicaid.
|
||||
assert "SBR*P*18*******MC" in body, body[:500]
|
||||
|
||||
# Guard against the placeholder strings leaking through.
|
||||
assert "CUSTOMER SERVICE" not in body, body[:500]
|
||||
assert "8005550100" not in body, body[:500]
|
||||
assert "*CYCLONE *" not in body, body[:500]
|
||||
assert "*RECEIVER *" not in body, body[:500] # only NM1*40 placeholder
|
||||
@@ -0,0 +1,354 @@
|
||||
"""SP37 Task 6: POST /api/submit-batch endpoint.
|
||||
|
||||
Thin HTTP wrapper around ``cyclone.submission.submit_file``. Mirrors the
|
||||
``cyclone submit-batch`` CLI walker exactly: each ``batch-*-claims/*.x12``
|
||||
under ``ingest_dir`` is submitted in order, ``._*`` AppleDouble files are
|
||||
skipped, ``limit`` truncates after collection.
|
||||
|
||||
Tests monkey-patch ``cyclone.api_routers.submission.submit_file`` so we
|
||||
never touch the real paramiko factory or a live MFT.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from cyclone.submission.result import SubmitOutcome, SubmitResult
|
||||
|
||||
_FIXTURE = Path(__file__).parent / "fixtures" / "submit-batch" / "single-claim.x12"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(tmp_path) -> TestClient:
|
||||
"""Standard TestClient; conftest.py autouse handles DB + auth gate.
|
||||
|
||||
Does NOT touch env vars (per the plan-vs-reality correction in
|
||||
Task 6's instructions). Seeds the clearhouse then flips
|
||||
``sftp_block.stub`` to ``False`` so the file-walking tests reach
|
||||
the walker (otherwise the default-seeded stub=True trips the 409
|
||||
guard before any per-file work runs). The ``stub_mode`` test
|
||||
re-seeds stub=True explicitly so its 409 still fires.
|
||||
"""
|
||||
from cyclone import db as db_mod
|
||||
from cyclone.store import store as cycl_store
|
||||
|
||||
db_mod._reset_for_tests()
|
||||
db_mod.init_db()
|
||||
cycl_store.ensure_clearhouse_seeded()
|
||||
ch = cycl_store.get_clearhouse()
|
||||
# Flip stub=False so the walker actually runs (SFTP calls are
|
||||
# intercepted via the submit_file monkey-patch in each test).
|
||||
cycl_store.update_clearhouse(
|
||||
ch.model_copy(update={"sftp_block": ch.sftp_block.model_copy(update={"stub": False})}),
|
||||
)
|
||||
|
||||
from cyclone.api import app
|
||||
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
def _stage_batch(tmp_path: Path, n: int) -> Path:
|
||||
"""Copy N copies of the single-claim fixture into ``batch-test-claims/``."""
|
||||
batch_dir = tmp_path / "batch-test-claims"
|
||||
batch_dir.mkdir()
|
||||
payload = _FIXTURE.read_bytes()
|
||||
for i in range(n):
|
||||
(batch_dir / f"claim-{i}.x12").write_bytes(payload)
|
||||
return batch_dir
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Happy path + walker semantics
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def test_submit_batch_happy_path(client: TestClient, tmp_path, monkeypatch):
|
||||
"""3 valid 837 files → 200 + body shape with submitted=3 (or skipped=3 on re-run).
|
||||
|
||||
Monkey-patches ``cyclone.api_routers.submission.submit_file`` to
|
||||
return a synthetic SUBMITTED SubmitResult per file. Skipped stays
|
||||
possible because SubmitResult's batch_id is what matters and the
|
||||
fake returns it consistently — but a fresh DB should always land
|
||||
in the SUBMITTED branch.
|
||||
"""
|
||||
_stage_batch(tmp_path, 3)
|
||||
|
||||
def _fake_submit(path, **_kw):
|
||||
return SubmitResult(
|
||||
file=path.name, outcome=SubmitOutcome.SUBMITTED, batch_id="b1",
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"cyclone.api_routers.submission.submit_file", _fake_submit,
|
||||
)
|
||||
|
||||
resp = client.post(
|
||||
"/api/submit-batch",
|
||||
json={"ingest_dir": str(tmp_path), "validate": False},
|
||||
)
|
||||
assert resp.status_code == 200, resp.text
|
||||
body = resp.json()
|
||||
# Tolerate either counter — counter totals are deterministic with a
|
||||
# fresh DB, but the assertion below focuses on the structural shape
|
||||
# the operator cares about: every file is accounted for.
|
||||
assert body["submitted"] + body["skipped"] + body["failed"] == 3
|
||||
assert isinstance(body["results"], list)
|
||||
assert len(body["results"]) == 3
|
||||
for row in body["results"]:
|
||||
assert "file" in row
|
||||
assert "outcome" in row
|
||||
assert "batch_id" in row
|
||||
assert "error" in row
|
||||
|
||||
|
||||
def test_submit_batch_skips_apple_double_files(client: TestClient, tmp_path, monkeypatch):
|
||||
"""``._foo.x12`` AppleDouble files must be skipped (mirrors the CLI walker)."""
|
||||
batch_dir = tmp_path / "batch-test-claims"
|
||||
batch_dir.mkdir()
|
||||
(batch_dir / "real.x12").write_bytes(_FIXTURE.read_bytes())
|
||||
(batch_dir / "._real.x12").write_bytes(b"\x00\x01\x02") # macOS AppleDouble noise
|
||||
|
||||
def _fake_submit(path, **_kw):
|
||||
return SubmitResult(
|
||||
file=path.name, outcome=SubmitOutcome.SUBMITTED, batch_id="b1",
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"cyclone.api_routers.submission.submit_file", _fake_submit,
|
||||
)
|
||||
|
||||
resp = client.post(
|
||||
"/api/submit-batch",
|
||||
json={"ingest_dir": str(tmp_path), "validate": False},
|
||||
)
|
||||
assert resp.status_code == 200, resp.text
|
||||
body = resp.json()
|
||||
# Only real.x12 was submitted (one call); AppleDouble was skipped.
|
||||
assert len(body["results"]) == 1
|
||||
assert body["results"][0]["file"] == "real.x12"
|
||||
|
||||
|
||||
def test_submit_batch_limit_truncates(client: TestClient, tmp_path, monkeypatch):
|
||||
"""``limit=1`` against 3 files → body has exactly 1 result."""
|
||||
_stage_batch(tmp_path, 3)
|
||||
|
||||
submitted_files: list[str] = []
|
||||
|
||||
def _fake_submit(path, **_kw):
|
||||
submitted_files.append(path.name)
|
||||
return SubmitResult(
|
||||
file=path.name, outcome=SubmitOutcome.SUBMITTED, batch_id="b1",
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"cyclone.api_routers.submission.submit_file", _fake_submit,
|
||||
)
|
||||
|
||||
resp = client.post(
|
||||
"/api/submit-batch",
|
||||
json={"ingest_dir": str(tmp_path), "validate": False, "limit": 1},
|
||||
)
|
||||
assert resp.status_code == 200, resp.text
|
||||
body = resp.json()
|
||||
assert len(body["results"]) == 1
|
||||
assert body["submitted"] + body["skipped"] + body["failed"] == 1
|
||||
assert len(submitted_files) == 1
|
||||
|
||||
|
||||
def test_submit_batch_empty_ingest_dir_returns_422(client: TestClient, tmp_path):
|
||||
"""No batch-*-claims dir under ingest_dir → 422 (matches the CLI's exit 2 path)."""
|
||||
resp = client.post(
|
||||
"/api/submit-batch",
|
||||
json={"ingest_dir": str(tmp_path), "validate": False},
|
||||
)
|
||||
assert resp.status_code == 422, resp.text
|
||||
|
||||
|
||||
def test_submit_batch_per_file_failure_keeps_status_200(
|
||||
client: TestClient, tmp_path, monkeypatch,
|
||||
):
|
||||
"""A SUBMITTED + SFTP_FAILED mix → 200 with failed=1, submitted=1.
|
||||
|
||||
Per-file failures live in the JSON body, NOT in the status code.
|
||||
The endpoint always returns 200 on a completed run.
|
||||
"""
|
||||
_stage_batch(tmp_path, 2)
|
||||
|
||||
outcomes = iter([
|
||||
SubmitResult(file="claim-0.x12", outcome=SubmitOutcome.SUBMITTED, batch_id="b1"),
|
||||
SubmitResult(
|
||||
file="claim-1.x12", outcome=SubmitOutcome.SFTP_FAILED,
|
||||
batch_id="b2", error="sftp down",
|
||||
),
|
||||
])
|
||||
|
||||
def _fake_submit(path, **_kw):
|
||||
return next(outcomes)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"cyclone.api_routers.submission.submit_file", _fake_submit,
|
||||
)
|
||||
|
||||
resp = client.post(
|
||||
"/api/submit-batch",
|
||||
json={"ingest_dir": str(tmp_path), "validate": False},
|
||||
)
|
||||
assert resp.status_code == 200, resp.text
|
||||
body = resp.json()
|
||||
assert body["submitted"] == 1
|
||||
assert body["failed"] == 1
|
||||
assert body["skipped"] == 0
|
||||
assert len(body["results"]) == 2
|
||||
# The failure row carries the error string so the operator can see
|
||||
# what went wrong without a separate API call.
|
||||
assert body["results"][1]["outcome"] == "sftp_failed"
|
||||
assert body["results"][1]["error"] == "sftp down"
|
||||
|
||||
|
||||
def test_submit_batch_unexpected_exception_recorded_in_results(
|
||||
client: TestClient, tmp_path, monkeypatch,
|
||||
):
|
||||
"""If submit_file raises, the file lands in results[] as a failed row.
|
||||
|
||||
The router catches the exception, builds a SubmitResult with
|
||||
``outcome=SubmitOutcome.UNEXPECTED_ERROR`` (followup #3 — distinct
|
||||
from the typed SFTP_FAILED path so operators can tell "real bug"
|
||||
from "SFTP server down"), and counts it as ``failed``. The full
|
||||
error string (including exception class name) is in ``error``.
|
||||
"""
|
||||
_stage_batch(tmp_path, 1)
|
||||
|
||||
def _explode(path, **_kw):
|
||||
raise RuntimeError("kaboom")
|
||||
|
||||
monkeypatch.setattr(
|
||||
"cyclone.api_routers.submission.submit_file", _explode,
|
||||
)
|
||||
|
||||
resp = client.post(
|
||||
"/api/submit-batch",
|
||||
json={"ingest_dir": str(tmp_path), "validate": False},
|
||||
)
|
||||
assert resp.status_code == 200, resp.text
|
||||
body = resp.json()
|
||||
assert body["failed"] == 1
|
||||
# Followup #3: marker is UNEXPECTED_ERROR, NOT SFTP_FAILED — the
|
||||
# exception class+message in ``error`` tells the operator this
|
||||
# was a real exception (not a typed failure).
|
||||
assert body["results"][0]["outcome"] == SubmitOutcome.UNEXPECTED_ERROR.value
|
||||
assert "kaboom" in body["results"][0]["error"]
|
||||
assert "RuntimeError" in body["results"][0]["error"]
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Request validation
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def test_submit_batch_missing_ingest_dir_returns_422(client: TestClient):
|
||||
"""Body without ``ingest_dir`` → 422 from Pydantic (NOT your manual check)."""
|
||||
resp = client.post("/api/submit-batch", json={"validate": False})
|
||||
assert resp.status_code == 422, resp.text
|
||||
|
||||
|
||||
def test_submit_batch_ingest_dir_missing_on_disk_returns_422(
|
||||
client: TestClient, tmp_path,
|
||||
):
|
||||
"""Body has a path that doesn't exist on disk → 422."""
|
||||
resp = client.post(
|
||||
"/api/submit-batch",
|
||||
json={
|
||||
"ingest_dir": str(tmp_path / "does-not-exist"),
|
||||
"validate": False,
|
||||
},
|
||||
)
|
||||
assert resp.status_code == 422, resp.text
|
||||
assert "does not exist" in resp.text or "does-not-exist" in resp.text
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Clearhouse posture (404 / 409)
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def test_submit_batch_no_clearhouse_returns_404(client: TestClient, tmp_path, monkeypatch):
|
||||
"""Delete the clearhouse row → endpoint must refuse with 404, not 500.
|
||||
|
||||
404 per the Task 6 status code contract: a missing clearhouse is a
|
||||
config-level "missing" (4xx), not an unexpected exception.
|
||||
"""
|
||||
from cyclone import db as db_mod
|
||||
from cyclone.db import ClearhouseORM
|
||||
|
||||
# Wipe the seeded clearhouse row.
|
||||
with db_mod.SessionLocal()() as s:
|
||||
row = s.get(ClearhouseORM, 1)
|
||||
if row is not None:
|
||||
s.delete(row)
|
||||
s.commit()
|
||||
|
||||
_stage_batch(tmp_path, 1)
|
||||
|
||||
resp = client.post(
|
||||
"/api/submit-batch",
|
||||
json={"ingest_dir": str(tmp_path), "validate": False},
|
||||
)
|
||||
assert resp.status_code == 404, resp.text
|
||||
assert "clearhouse" in resp.text.lower()
|
||||
|
||||
|
||||
def test_submit_batch_stub_mode_returns_409(client: TestClient, tmp_path, monkeypatch):
|
||||
"""When the seeded clearhouse's ``sftp_block.stub`` is True → 409.
|
||||
|
||||
The default client fixture flips stub=False so file-walking tests
|
||||
reach the walker. This test flips it back to True so the
|
||||
stub-mode 409 guard fires before any per-file work runs.
|
||||
"""
|
||||
from cyclone.store import store as cycl_store
|
||||
|
||||
ch = cycl_store.get_clearhouse()
|
||||
assert ch is not None
|
||||
# Re-flip stub=True (the default client fixture set it to False).
|
||||
cycl_store.update_clearhouse(
|
||||
ch.model_copy(update={
|
||||
"sftp_block": ch.sftp_block.model_copy(update={"stub": True}),
|
||||
}),
|
||||
)
|
||||
|
||||
_stage_batch(tmp_path, 1)
|
||||
|
||||
resp = client.post(
|
||||
"/api/submit-batch",
|
||||
json={"ingest_dir": str(tmp_path), "validate": False},
|
||||
)
|
||||
assert resp.status_code == 409, resp.text
|
||||
assert "stub" in resp.text.lower()
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Auth gate
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def test_submit_batch_auth_gate(client: TestClient, tmp_path, monkeypatch):
|
||||
"""No cookie + AUTH_DISABLED=False → 401 from matrix_gate.
|
||||
|
||||
The conftest autouse fixture sets ``AUTH_DISABLED = True`` at
|
||||
fixture setup and resets to False at teardown. Inside the test we
|
||||
flip the module attribute so the gate fires — but to avoid leaking
|
||||
the flip into a sibling test we use monkeypatch.setattr (which
|
||||
restores at test teardown, before conftest's finally block runs).
|
||||
"""
|
||||
import cyclone.auth.deps as auth_deps
|
||||
|
||||
monkeypatch.setattr(auth_deps, "AUTH_DISABLED", False)
|
||||
|
||||
_stage_batch(tmp_path, 1)
|
||||
|
||||
resp = client.post(
|
||||
"/api/submit-batch",
|
||||
json={"ingest_dir": str(tmp_path), "validate": False},
|
||||
)
|
||||
assert resp.status_code == 401, resp.text
|
||||
@@ -0,0 +1,233 @@
|
||||
"""SP40: tests for ``POST /api/admin/validate-837`` (admin-gated).
|
||||
|
||||
Covers:
|
||||
|
||||
1. Auth gate — 401 with no session cookie (matrix_gate fires when
|
||||
AUTH_DISABLED is flipped off).
|
||||
2. Happy path — admin-cookie request with a multipart file upload
|
||||
returns the Edifabric OperationResult JSON.
|
||||
3. Missing file part — FastAPI's built-in validation rejects requests
|
||||
without the ``file`` form part with 422.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from cyclone import edifabric
|
||||
from cyclone.api import app
|
||||
|
||||
|
||||
_TEST_KEY = "test-edifabric-key-0123456789abcdef"
|
||||
|
||||
|
||||
def _make_client(handler):
|
||||
transport = httpx.MockTransport(handler)
|
||||
return httpx.Client(transport=transport, timeout=10.0)
|
||||
|
||||
|
||||
def _install(handler):
|
||||
edifabric.set_transport_factory(lambda: _make_client(handler))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _reset_transport():
|
||||
yield
|
||||
edifabric._reset_transport_factory()
|
||||
import cyclone.secrets
|
||||
cyclone.secrets.get_secret = _real_get_secret # type: ignore[assignment]
|
||||
|
||||
|
||||
import cyclone.secrets as _secrets_module # noqa: E402
|
||||
_real_get_secret = _secrets_module.get_secret # noqa: E402
|
||||
|
||||
|
||||
def _mock_secrets(patched_key: str):
|
||||
"""Install a get_secret mock that returns ``patched_key`` for any
|
||||
'edifabric.api_key' lookup. Restores the real function on
|
||||
teardown via the autouse ``_reset_transport`` fixture.
|
||||
|
||||
Until SP40 Task 6 lands, ``_ENV_NAME_FOR`` doesn't know about
|
||||
'edifabric.api_key', so the env-var name expected is the same —
|
||||
but the more robust pattern is to monkeypatch at the function
|
||||
boundary. That keeps test behavior independent of the secrets
|
||||
table layout.
|
||||
"""
|
||||
def _fake(name: str):
|
||||
if name == "edifabric.api_key":
|
||||
return patched_key
|
||||
return _real_get_secret(name)
|
||||
|
||||
_secrets_module.get_secret = _fake # type: ignore[assignment]
|
||||
return _fake
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_edifabric_ok():
|
||||
"""Mock Edifabric to return a clean OperationResult on /validate.
|
||||
|
||||
Wires both the transport mock and the secrets.get_secret monkeypatch
|
||||
so ``cyclone.edifabric.validate_edi(...)`` resolves an API key.
|
||||
"""
|
||||
operation_result = {
|
||||
"Status": "success",
|
||||
"Details": [],
|
||||
"LastIndex": 46,
|
||||
}
|
||||
x12 = {
|
||||
"SegmentDelimiter": "~",
|
||||
"DataElementDelimiter": "*",
|
||||
"ISA": {"InterchangeControlNumber_13": "000000001"},
|
||||
"Groups": [],
|
||||
"IEATrailers": [],
|
||||
}
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
if request.url.path.endswith("/read"):
|
||||
return httpx.Response(200, json=[x12])
|
||||
if request.url.path.endswith("/validate"):
|
||||
return httpx.Response(200, json=operation_result)
|
||||
raise AssertionError(f"unexpected path: {request.url.path}")
|
||||
|
||||
_mock_secrets(_TEST_KEY)
|
||||
_install(handler)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Auth gate
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def test_validate_837_no_session_returns_401(client, tmp_path, monkeypatch):
|
||||
"""No session cookie + AUTH_DISABLED off → 401 from matrix_gate.
|
||||
|
||||
Mirrors the test pattern in test_api_clearhouse_patch.py:99 — we
|
||||
flip AUTH_DISABLED to False inside the test (monkeypatch restores
|
||||
it at teardown) so the gate fires.
|
||||
"""
|
||||
import cyclone.auth.deps as auth_deps
|
||||
|
||||
monkeypatch.setattr(auth_deps, "AUTH_DISABLED", False)
|
||||
|
||||
sample = tmp_path / "ok.x12"
|
||||
sample.write_text("ISA*seg~SE*1*0001~IEA*0*000000001~")
|
||||
|
||||
with sample.open("rb") as fh:
|
||||
resp = client.post(
|
||||
"/api/admin/validate-837",
|
||||
files={"file": ("ok.x12", fh, "application/octet-stream")},
|
||||
)
|
||||
assert resp.status_code == 401, resp.text
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Happy path
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def test_validate_837_returns_operation_result(client, tmp_path, mock_edifabric_ok):
|
||||
"""Admin request with multipart file returns the OperationResult verbatim.
|
||||
|
||||
AUTH_DISABLED is True (set by conftest's autouse fixture), so the
|
||||
admin gate fires-and-passes; the Edifabric client is mocked to
|
||||
return a clean Success.
|
||||
"""
|
||||
fixture = tmp_path / "ok.x12"
|
||||
fixture.write_text("ISA*00*...~IEA*0*000000001~")
|
||||
|
||||
with fixture.open("rb") as fh:
|
||||
resp = client.post(
|
||||
"/api/admin/validate-837",
|
||||
files={"file": ("ok.x12", fh, "application/octet-stream")},
|
||||
)
|
||||
assert resp.status_code == 200, resp.text
|
||||
body = resp.json()
|
||||
assert body["Status"] == "success"
|
||||
assert body["Details"] == []
|
||||
assert body["LastIndex"] == 46
|
||||
|
||||
|
||||
def test_validate_837_returns_error_details(client, tmp_path):
|
||||
"""A 200 response with Status='error' + Details passes through verbatim.
|
||||
|
||||
The OperationResult is data; the HTTP response is 200 (the
|
||||
endpoint did its job of talking to Edifabric). Callers inspect
|
||||
Status in the body to decide whether to fail-closed.
|
||||
"""
|
||||
error_payload = {
|
||||
"Status": "error",
|
||||
"Details": [
|
||||
{"SegmentId": "PER", "Message": "PER-04 is required", "Status": "error"},
|
||||
{"SegmentId": "SBR", "Message": "SBR-09 is required", "Status": "error"},
|
||||
],
|
||||
"LastIndex": 5,
|
||||
}
|
||||
x12 = {
|
||||
"SegmentDelimiter": "~",
|
||||
"DataElementDelimiter": "*",
|
||||
"ISA": {"InterchangeControlNumber_13": "000000001"},
|
||||
"Groups": [],
|
||||
"IEATrailers": [],
|
||||
}
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
if request.url.path.endswith("/read"):
|
||||
return httpx.Response(200, json=[x12])
|
||||
if request.url.path.endswith("/validate"):
|
||||
return httpx.Response(200, json=error_payload)
|
||||
raise AssertionError(f"unexpected path: {request.url.path}")
|
||||
|
||||
_mock_secrets(_TEST_KEY)
|
||||
_install(handler)
|
||||
|
||||
fixture = tmp_path / "bad.x12"
|
||||
fixture.write_text("ISA*bad~")
|
||||
|
||||
with fixture.open("rb") as fh:
|
||||
resp = client.post(
|
||||
"/api/admin/validate-837",
|
||||
files={"file": ("bad.x12", fh, "application/octet-stream")},
|
||||
)
|
||||
assert resp.status_code == 200, resp.text
|
||||
body = resp.json()
|
||||
assert body["Status"] == "error"
|
||||
assert len(body["Details"]) == 2
|
||||
assert body["Details"][0]["Message"] == "PER-04 is required"
|
||||
|
||||
|
||||
def test_validate_837_missing_file_returns_422(client):
|
||||
"""No ``file`` form part → FastAPI returns 422 (built-in validation).
|
||||
|
||||
This is the FastAPI-default behavior for ``UploadFile = File(...)``
|
||||
when the field is absent. The endpoint body itself never runs.
|
||||
"""
|
||||
resp = client.post("/api/admin/validate-837")
|
||||
assert resp.status_code == 422, resp.text
|
||||
|
||||
|
||||
def test_validate_837_missing_api_key_returns_503(client, tmp_path, monkeypatch):
|
||||
"""With no API key configured, the endpoint surfaces a 503 with the
|
||||
'API key not configured' detail (status_code 0 → 503, not 502).
|
||||
|
||||
Upstream 4xx/5xx map to 502; a 0-status_code EdifabricError means
|
||||
the client-side config is missing and the caller can fix it.
|
||||
"""
|
||||
monkeypatch.delenv("CYCLONE_EDIFABRIC_API_KEY", raising=False)
|
||||
|
||||
fixture = tmp_path / "ok.x12"
|
||||
fixture.write_text("ISA*seg~")
|
||||
|
||||
with fixture.open("rb") as fh:
|
||||
resp = client.post(
|
||||
"/api/admin/validate-837",
|
||||
files={"file": ("ok.x12", fh, "application/octet-stream")},
|
||||
)
|
||||
assert resp.status_code == 503, resp.text
|
||||
body = resp.json()
|
||||
assert "API key not configured" in str(body)
|
||||
@@ -34,6 +34,7 @@ from cyclone.claim_acks import (
|
||||
lookup_claims_for_ack_set_response,
|
||||
)
|
||||
from cyclone.db import (
|
||||
Ack,
|
||||
Batch,
|
||||
Claim,
|
||||
ClaimState,
|
||||
@@ -744,6 +745,75 @@ def test_store_facade_exposes_claim_ack_methods():
|
||||
assert hasattr(store, name), f"missing CycloneStore.{name}"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# SP38 follow-up: regression test for the 277ca / ta1 control_number
|
||||
# behavior preserved across the find_ack_orphans refactor.
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_find_ack_orphans_returns_control_number_for_all_kinds():
|
||||
"""For each ack kind, ``find_ack_orphans(kind)`` must return the
|
||||
control_number from the per-kind source:
|
||||
|
||||
* 999 — from raw_json.envelope.control_number
|
||||
* 277ca — from the ORM column ``control_number``
|
||||
* ta1 — from the ORM column ``control_number``
|
||||
|
||||
Regression for sp38 commit ad14b56 which initially routed all
|
||||
three kinds through _ack_control_number and broke 277ca/ta1
|
||||
(the helper only knew 999). Found by pr-reviewer 2026-07-07.
|
||||
"""
|
||||
import json
|
||||
import cyclone.db as _db_mod
|
||||
Two77caAck = _db_mod.Two77caAck
|
||||
Ta1Ack = _db_mod.Ta1Ack
|
||||
parsed_at = datetime.now(timezone.utc)
|
||||
|
||||
with db.SessionLocal()() as s:
|
||||
# 999 orphan with envelope.control_number in raw_json
|
||||
s.add(Ack(
|
||||
source_batch_id="999-orph-test",
|
||||
accepted_count=1, rejected_count=0, received_count=1,
|
||||
ack_code="A",
|
||||
parsed_at=parsed_at,
|
||||
raw_json=json.dumps({
|
||||
"envelope": {"control_number": "999-ctrl-num",
|
||||
"sender_id": "S", "receiver_id": "R",
|
||||
"transaction_date": "2024-01-01",
|
||||
"implementation_guide": "005010X231A1"},
|
||||
"set_responses": [],
|
||||
"summary": {},
|
||||
}),
|
||||
))
|
||||
# 277ca orphan with control_number in ORM column
|
||||
s.add(Two77caAck(
|
||||
source_batch_id="277ca-orph-test",
|
||||
accepted_count=1, rejected_count=0,
|
||||
control_number="277CA-CTRL-NUM",
|
||||
parsed_at=parsed_at,
|
||||
))
|
||||
# ta1 orphan with control_number in ORM column
|
||||
s.add(Ta1Ack(
|
||||
source_batch_id="ta1-orph-test",
|
||||
ack_code="A",
|
||||
control_number="TA1-CTRL-NUM",
|
||||
parsed_at=parsed_at,
|
||||
))
|
||||
s.commit()
|
||||
|
||||
orphans_999 = store.find_ack_orphans("999")
|
||||
orphans_277ca = store.find_ack_orphans("277ca")
|
||||
orphans_ta1 = store.find_ack_orphans("ta1")
|
||||
|
||||
ctrl_999 = [o["control_number"] for o in orphans_999 if "999-ctrl-num" in o["control_number"]]
|
||||
ctrl_277ca = [o["control_number"] for o in orphans_277ca]
|
||||
ctrl_ta1 = [o["control_number"] for o in orphans_ta1]
|
||||
|
||||
assert "999-ctrl-num" in ctrl_999, f"999 control_number not found: {ctrl_999}"
|
||||
assert "277CA-CTRL-NUM" in ctrl_277ca, f"277ca control_number empty: {ctrl_277ca}"
|
||||
assert "TA1-CTRL-NUM" in ctrl_ta1, f"ta1 control_number empty: {ctrl_ta1}"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Step 2.1 — pure walk-through unit test (no DB) for the orphan tracking
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
"""SP37 follow-up #1: Fail-closed for unregistered paths.
|
||||
|
||||
The permissions matrix is fail-closed — endpoints not listed are denied
|
||||
(None). The pre-existing ``("POST", "/api/resubmit"): WRITE_ROLES``
|
||||
entry was a dead prefix that ONLY matched the exact path ``/api/resubmit``
|
||||
(which is not a registered route). The actual resubmit lives at
|
||||
``/api/inbox/rejected/resubmit`` (already covered by its own entry).
|
||||
|
||||
This test pins the fail-closed invariant: ``/api/resubmit`` MUST have
|
||||
no granted permission because no route is registered there. Without
|
||||
this test, a future contributor could re-add the dead entry (or any
|
||||
similar dead prefix) and the auth system would silently grant access
|
||||
to a non-existent path.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from cyclone.auth.permissions import PERMISSIONS, allowed_roles
|
||||
|
||||
|
||||
def test_api_resubmit_exact_path_not_in_matrix():
|
||||
"""``/api/resubmit`` (exact, no children) must NOT be in the matrix.
|
||||
|
||||
The actual resubmit endpoint is ``/api/inbox/rejected/resubmit``,
|
||||
which has its own entry. The pre-existing
|
||||
``("POST", "/api/resubmit"): WRITE_ROLES`` entry only matched the
|
||||
exact path ``/api/resubmit`` and never any real route.
|
||||
"""
|
||||
assert ("POST", "/api/resubmit") not in PERMISSIONS
|
||||
|
||||
|
||||
def test_api_resubmit_returns_none_via_allowed_roles():
|
||||
"""Fail-closed: ``allowed_roles("POST", "/api/resubmit")`` is None.
|
||||
|
||||
The matrix default is DENY. A registered entry that matches the
|
||||
path would return a non-empty set of roles; the absence of any
|
||||
entry should return None.
|
||||
"""
|
||||
assert allowed_roles("POST", "/api/resubmit") is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("path", [
|
||||
"/api/resubmit",
|
||||
"/api/resubmit/",
|
||||
"/api/resubmit/anything",
|
||||
])
|
||||
def test_api_resubmit_prefix_variants_all_deny(path):
|
||||
"""No path under ``/api/resubmit`` should match any permission entry."""
|
||||
assert allowed_roles("POST", path) is None
|
||||
@@ -0,0 +1,126 @@
|
||||
"""SP37 Task 3: batch_envelope_index populates from BOTH columns.
|
||||
|
||||
The dict returned by ``batch_envelope_index`` should resolve lookups
|
||||
by EITHER ``Batch.raw_result_json.envelope.control_number`` (ISA13,
|
||||
preserved) OR ``Batch.transaction_set_control_number`` (ST02, new in
|
||||
SP37 Task 2). One dict, both keys — a single ``.get(set_control_number)``
|
||||
call hits whichever matches, so the D10 Pass 1 join in
|
||||
:func:`cyclone.claim_acks.lookup_claims_for_ack_set_response` resolves
|
||||
999 AK201 (which echoes the source 837's ST02) back to the right batch
|
||||
even when ST02 != ISA13.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import pytest
|
||||
|
||||
from cyclone import db as db_mod
|
||||
from cyclone.db import Batch
|
||||
from cyclone.store.claim_acks import batch_envelope_index
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _db(tmp_path, monkeypatch):
|
||||
"""Per-test DB; conftest's autouse already wires one too, but we
|
||||
pin the URL again so this module is self-contained if anyone ever
|
||||
lifts it out of the conftest tree."""
|
||||
monkeypatch.setenv("CYCLONE_DB_URL", f"sqlite:///{tmp_path}/test.db")
|
||||
db_mod._reset_for_tests()
|
||||
db_mod.init_db()
|
||||
yield
|
||||
|
||||
|
||||
def _make_batch(
|
||||
s,
|
||||
*,
|
||||
id: str,
|
||||
icn: str,
|
||||
stcn: str | None = None,
|
||||
raw_json: dict | None = None,
|
||||
) -> Batch:
|
||||
"""Insert one Batch row whose envelope + ST02 mirror what
|
||||
``store.write.add_record`` writes for an 837P batch."""
|
||||
row = Batch(
|
||||
id=id,
|
||||
kind="837p",
|
||||
input_filename=id + ".x12",
|
||||
parsed_at=datetime.now(timezone.utc),
|
||||
raw_result_json=raw_json or {"envelope": {"control_number": icn}},
|
||||
transaction_set_control_number=stcn,
|
||||
)
|
||||
s.add(row)
|
||||
s.commit()
|
||||
s.refresh(row)
|
||||
return row
|
||||
|
||||
|
||||
def test_index_resolves_by_envelope_control_number():
|
||||
"""Pre-SP37 path: ISA13 (envelope.control_number) still resolves."""
|
||||
with db_mod.SessionLocal()() as s:
|
||||
_make_batch(s, id="b1", icn="ISA000001")
|
||||
idx = batch_envelope_index()
|
||||
assert idx.get("ISA000001") == "b1"
|
||||
|
||||
|
||||
def test_index_resolves_by_transaction_set_control_number():
|
||||
"""SP37 path: ST02 (transaction_set_control_number) resolves too.
|
||||
|
||||
The whole point of Task 3 — Gainwell batches have ST02 != ISA13,
|
||||
so 999 AK201 echoes ST02 and must hit this branch.
|
||||
"""
|
||||
with db_mod.SessionLocal()() as s:
|
||||
_make_batch(
|
||||
s, id="b2", icn="ISA000002", stcn="ST000002",
|
||||
raw_json={"envelope": {"control_number": "ISA000002"}},
|
||||
)
|
||||
idx = batch_envelope_index()
|
||||
assert idx.get("ST000002") == "b2"
|
||||
# Backward compat: ISA still resolves.
|
||||
assert idx.get("ISA000002") == "b2"
|
||||
|
||||
|
||||
def test_index_handles_row_with_no_transaction_set_control_number():
|
||||
"""Pre-migration rows (stcn NULL) should still resolve by ISA.
|
||||
|
||||
Spec says: 'Pre-migration rows (stcn NULL) should still resolve by ISA.'
|
||||
Production row ``b1`` (the only one with claims) has ST02 NULL because
|
||||
the migration's backfill only ran over rows whose raw_result_json
|
||||
envelope had the ST02 key — and that row was written before the
|
||||
parser populated it.
|
||||
"""
|
||||
with db_mod.SessionLocal()() as s:
|
||||
_make_batch(s, id="b3", icn="ISA000003") # no stcn
|
||||
idx = batch_envelope_index()
|
||||
assert idx.get("ISA000003") == "b3"
|
||||
assert idx.get("ST000003") is None
|
||||
|
||||
|
||||
def test_index_ignores_non_837_batches():
|
||||
"""835 batches don't contribute to the 999 join index.
|
||||
|
||||
The D10 two-pass join is specifically for 837 → 999 linkage; 835
|
||||
remittances live on the response side of the loop and have no ST02
|
||||
join key. Filtering by ``kind == "837p"`` keeps the index scoped
|
||||
correctly and avoids an 835 row shadowing an 837 ST02 by accident.
|
||||
"""
|
||||
with db_mod.SessionLocal()() as s:
|
||||
# Add an 837 row first so we can prove the 835 doesn't leak in.
|
||||
_make_batch(s, id="b-837", icn="ISA837", stcn="ST837")
|
||||
# And an 835 row whose ISA13 / ST02 would otherwise pollute.
|
||||
s.add(Batch(
|
||||
id="b-835",
|
||||
kind="835",
|
||||
input_filename="b-835.x12",
|
||||
parsed_at=datetime.now(timezone.utc),
|
||||
raw_result_json={"envelope": {"control_number": "ISA999"}},
|
||||
transaction_set_control_number="ST999",
|
||||
))
|
||||
s.commit()
|
||||
idx = batch_envelope_index()
|
||||
# 837 row resolves both ways.
|
||||
assert idx.get("ISA837") == "b-837"
|
||||
assert idx.get("ST837") == "b-837"
|
||||
# 835 row contributes nothing.
|
||||
assert "ISA999" not in idx
|
||||
assert "ST999" not in idx
|
||||
@@ -0,0 +1,103 @@
|
||||
"""SP37 Task 2: add_record populates Batch.transaction_set_control_number.
|
||||
|
||||
The column should mirror the source 837 envelope's
|
||||
``transaction_set_control_number`` (ST02) so the join-key update in
|
||||
Task 3 can resolve 999 ``set_control_number`` (AK201) values back to
|
||||
the right batch. The 835 path leaves the column NULL because the
|
||||
field is an 837P-specific join key (835 remittances don't need to
|
||||
resolve back to a source 837 — they're the response side of the loop).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date, datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
from cyclone import db as db_mod
|
||||
from cyclone.db import Batch
|
||||
from cyclone.parsers.parse_837 import parse as parse_837_text
|
||||
from cyclone.parsers.payer import PayerConfig
|
||||
from cyclone.store import store as cycl_store
|
||||
from cyclone.store.records import BatchRecord837, BatchRecord835
|
||||
|
||||
FIXTURE_837 = Path(__file__).parent / "fixtures" / "minimal_837p.txt"
|
||||
FIXTURE_835 = Path(__file__).parent / "fixtures" / "minimal_835.txt"
|
||||
|
||||
|
||||
def test_add_record_populates_transaction_set_control_number_for_837():
|
||||
"""parse_837 → add_record writes the parsed ST02 onto the Batch row.
|
||||
|
||||
The fixture's ST02 (``991102977``) must round-trip into the new
|
||||
``batches.transaction_set_control_number`` column. This is the
|
||||
join key Task 3's 999-ingest Pass 1 update relies on.
|
||||
"""
|
||||
text = FIXTURE_837.read_text()
|
||||
parsed = parse_837_text(text, PayerConfig.co_medicaid())
|
||||
|
||||
record = BatchRecord837(
|
||||
id="b-txn-cn-1",
|
||||
input_filename="minimal_837p.txt",
|
||||
parsed_at=datetime.now(timezone.utc),
|
||||
result=parsed,
|
||||
)
|
||||
cycl_store.add(record)
|
||||
|
||||
with db_mod.SessionLocal()() as s:
|
||||
row = s.get(Batch, "b-txn-cn-1")
|
||||
assert row is not None
|
||||
assert row.transaction_set_control_number == parsed.envelope.transaction_set_control_number
|
||||
# Sanity: the fixture's ST02 must be the parsed-and-stored value.
|
||||
assert row.transaction_set_control_number == "991102977"
|
||||
|
||||
|
||||
def test_envelope_model_exposes_transaction_set_control_number():
|
||||
"""The Envelope Pydantic model carries the new field with a None default.
|
||||
|
||||
Guards against a regression where someone removes the field from
|
||||
the model — the rest of the chain (parser + ORM + write path)
|
||||
silently degrades to None if the model loses the attribute.
|
||||
"""
|
||||
from cyclone.parsers.models import Envelope
|
||||
|
||||
env = Envelope(
|
||||
sender_id="S",
|
||||
receiver_id="R",
|
||||
control_number="000000001",
|
||||
transaction_date=date(2026, 1, 1),
|
||||
)
|
||||
assert env.transaction_set_control_number is None
|
||||
|
||||
env2 = env.model_copy(update={"transaction_set_control_number": "0001"})
|
||||
assert env2.transaction_set_control_number == "0001"
|
||||
|
||||
|
||||
def test_add_record_leaves_835_column_null():
|
||||
"""835 batches don't carry an ST02 join key; column stays NULL.
|
||||
|
||||
The shared ``Envelope`` class is used by both 837P and 835 parsers,
|
||||
but only ``parse_837`` populates ``transaction_set_control_number``.
|
||||
For 835 records the field is None and ``add_record`` writes NULL
|
||||
to the column — verified end-to-end via a real 835 ingest.
|
||||
"""
|
||||
text = FIXTURE_835.read_text()
|
||||
from cyclone.parsers.parse_835 import parse as parse_835_text
|
||||
parsed835 = parse_835_text(text, payer_config=None) # type: ignore[arg-type]
|
||||
|
||||
# Sanity: the 835 envelope also has the field (shared model class),
|
||||
# but the parser doesn't populate it — so it must be None.
|
||||
assert parsed835.envelope.transaction_set_control_number is None
|
||||
|
||||
record = BatchRecord835(
|
||||
id="b-txn-cn-835-1",
|
||||
input_filename="minimal_835.txt",
|
||||
parsed_at=datetime.now(timezone.utc),
|
||||
result=parsed835,
|
||||
)
|
||||
cycl_store.add(record)
|
||||
|
||||
with db_mod.SessionLocal()() as s:
|
||||
row = s.get(Batch, "b-txn-cn-835-1")
|
||||
assert row is not None
|
||||
assert row.transaction_set_control_number is None
|
||||
# And the kind/kind round-trip is correct (sanity check that
|
||||
# we did exercise the 835 path, not the 837 path).
|
||||
assert row.kind == "835"
|
||||
@@ -0,0 +1,363 @@
|
||||
"""SP41 — tests for the `cyclone rebill-from-835` CLI.
|
||||
|
||||
Three smoke tests:
|
||||
1. --help renders cleanly and surfaces --window and --override-filing.
|
||||
2. --status (with no other args) renders cleanly and exits 0 — the
|
||||
option is recognized even when no prior summary.csv exists.
|
||||
3. End-to-end run against a 2-row visits CSV (one in-window, one
|
||||
ancient) and a zero-SVC 835 fixture produces a summary.csv with
|
||||
the in-window visit classified as REBILLED_B.
|
||||
|
||||
Plus five disposition-coverage tests added during the Task 10 review:
|
||||
4. EXCLUDED_CARC: CO-45 on a denied visit → not rebilled.
|
||||
5. REBILLED_A: CO-97 on a denied visit → pipeline-a freq-7.
|
||||
6. PAID visits are filtered out of the summary.
|
||||
7. EXCLUDED_TIMELY_FILING vs REBILLED_B via --override-filing (two runs).
|
||||
8. --status on a real summary.csv prints the per-disposition tally.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
from datetime import date as _date
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from click.testing import CliRunner
|
||||
|
||||
from cyclone.cli import main
|
||||
from cyclone.rebill.parse_835_svc import SvcRow
|
||||
from cyclone.rebill.reconcile import VisitRow
|
||||
|
||||
|
||||
def _write_visits_csv(path: Path, rows: list[tuple[str, str, str, str]]) -> Path:
|
||||
"""rows: list of (dos_mmddyyyy, member, procedure, billed_str)."""
|
||||
with path.open("w", newline="") as f:
|
||||
w = csv.writer(f)
|
||||
w.writerow(["Visit Date", "Member ID", "Procedure Code", "Billable Amount"])
|
||||
for r in rows:
|
||||
w.writerow(r)
|
||||
return path
|
||||
|
||||
|
||||
def _read_summary(out_dir: Path) -> list[dict[str, str]]:
|
||||
with (out_dir / "summary.csv").open(newline="") as f:
|
||||
return list(csv.DictReader(f))
|
||||
|
||||
|
||||
def _stub_svc(monkeypatch, svcs: list[SvcRow]) -> None:
|
||||
"""Replace cli.parse_835_svc's underlying cyclonic call with a fixed list."""
|
||||
# cli.rebill_from_835 imports parse_835_svc locally, so patch the
|
||||
# source module's symbol — the local `from ... import parse_835_svc`
|
||||
# binds the symbol at call time on each invocation.
|
||||
monkeypatch.setattr(
|
||||
"cyclone.rebill.parse_835_svc.parse_835_svc",
|
||||
lambda path, _svcs=svcs: iter(_svcs),
|
||||
)
|
||||
|
||||
|
||||
def _make_835_placeholder(ingest_dir: Path, name: str = "x.835") -> Path:
|
||||
"""A real *.835 file present in the glob; parse_835_svc is mocked so content doesn't matter."""
|
||||
ingest_dir.mkdir(exist_ok=True)
|
||||
p = ingest_dir / name
|
||||
p.write_text("ST*835*0001~SE*0*0001~")
|
||||
return p
|
||||
|
||||
|
||||
def test_rebill_from_835_help():
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["rebill-from-835", "--help"])
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "--window" in result.output
|
||||
assert "--override-filing" in result.output
|
||||
|
||||
|
||||
def test_rebill_from_835_status_help():
|
||||
"""Either bare --status or --status --help should exit 0.
|
||||
|
||||
The spec note: the help-test ensures the option is recognized, so
|
||||
even a "no prior runs" 0-exit path is acceptable. We try both
|
||||
invocations because click short-circuits --help ahead of any
|
||||
required-option check; the bare --status path is the one that
|
||||
proves --status works without --visits / --ingest.
|
||||
"""
|
||||
runner = CliRunner()
|
||||
result_help = runner.invoke(main, ["rebill-from-835", "--status", "--help"])
|
||||
assert result_help.exit_code == 0, result_help.output
|
||||
|
||||
result_bare = runner.invoke(main, ["rebill-from-835", "--status"])
|
||||
assert result_bare.exit_code == 0, result_bare.output
|
||||
|
||||
|
||||
def test_rebill_from_835_runs(tmp_path: Path):
|
||||
"""End-to-end: 1 in-window visit + 1 ancient visit + zero-SVC 835 → REBILLED_B for the in-window row."""
|
||||
visits_path = tmp_path / "visits.csv"
|
||||
visits_path.write_text(
|
||||
"Visit Date,Member ID,Procedure Code,Billable Amount\n"
|
||||
"06/27/2026,J813715,T1019,$2.32\n"
|
||||
"01/01/2020,ANCIENT,T1019,$2.32\n"
|
||||
)
|
||||
|
||||
ingest_dir = tmp_path / "ingest"
|
||||
ingest_dir.mkdir()
|
||||
(ingest_dir / "x.835").write_text("ST*835*0001~SE*0*0001~")
|
||||
|
||||
out_dir = tmp_path / "out"
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, [
|
||||
"rebill-from-835",
|
||||
"--visits", str(visits_path),
|
||||
"--ingest", str(ingest_dir),
|
||||
"--out", str(out_dir),
|
||||
"--as-of", "2026-07-07",
|
||||
])
|
||||
assert result.exit_code == 0, (
|
||||
f"CLI exited {result.exit_code}, output={result.output!r}, "
|
||||
f"exception={result.exception!r}"
|
||||
)
|
||||
|
||||
summary_path = out_dir / "summary.csv"
|
||||
assert summary_path.exists(), summary_path
|
||||
|
||||
rows = _read_summary(out_dir)
|
||||
# Two visits in, two summary rows out (one REBILLED_B, one
|
||||
# EXCLUDED_TIMELY_FILING). PAID outcomes would be dropped, but
|
||||
# neither of these is PAID — the 835 has no SVCs.
|
||||
assert len(rows) == 2, rows
|
||||
|
||||
by_member = {r["member_id"]: r for r in rows}
|
||||
in_window = by_member["J813715"]
|
||||
assert in_window["disposition"] == "REBILLED_B", in_window
|
||||
assert in_window["dos"] == "2026-06-27", in_window
|
||||
|
||||
ancient = by_member["ANCIENT"]
|
||||
assert ancient["disposition"] == "EXCLUDED_TIMELY_FILING", ancient
|
||||
assert ancient["dos"] == "2020-01-01", ancient
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Disposition coverage (Task 10 review)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_rebill_carc_excluded_visit_landed_as_excluded_carc(tmp_path: Path, monkeypatch):
|
||||
"""A denied visit whose CAS list contains CO-45 (excluded) must land as EXCLUDED_CARC, not REBILLED_A."""
|
||||
visits = _write_visits_csv(tmp_path / "visits.csv", [
|
||||
("06/27/2026", "J813715", "T1019", "$16.24"),
|
||||
])
|
||||
ingest = tmp_path / "ingest"
|
||||
_make_835_placeholder(ingest)
|
||||
out_dir = tmp_path / "out"
|
||||
|
||||
# A denied SVC for the visit (paid == 0) with CO-45 in CAS.
|
||||
svc = SvcRow(
|
||||
src_file="x.835",
|
||||
claim_id="T1",
|
||||
member_id="J813715",
|
||||
status="4", # denied
|
||||
procedure="T1019",
|
||||
modifiers="",
|
||||
charge=Decimal("16.24"),
|
||||
paid=Decimal("0"),
|
||||
units=Decimal("1"),
|
||||
svc_date=_date(2026, 6, 27),
|
||||
cas_reasons=("CO-45",),
|
||||
pay_date=None,
|
||||
)
|
||||
_stub_svc(monkeypatch, [svc])
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, [
|
||||
"rebill-from-835",
|
||||
"--visits", str(visits),
|
||||
"--ingest", str(ingest),
|
||||
"--out", str(out_dir),
|
||||
"--as-of", "2026-07-07",
|
||||
])
|
||||
assert result.exit_code == 0, (result.output, result.exception)
|
||||
rows = _read_summary(out_dir)
|
||||
assert len(rows) == 1, rows
|
||||
assert rows[0]["disposition"] == "EXCLUDED_CARC"
|
||||
assert rows[0]["member_id"] == "J813715"
|
||||
assert "CO-45" in rows[0]["cas_reasons"]
|
||||
|
||||
|
||||
def test_rebill_denied_rebill_visit_landed_as_rebilled_a(tmp_path: Path, monkeypatch):
|
||||
"""A denied visit whose CAS list contains CO-97 (rebillable) must land as REBILLED_A."""
|
||||
visits = _write_visits_csv(tmp_path / "visits.csv", [
|
||||
("06/27/2026", "J813715", "T1019", "$16.24"),
|
||||
])
|
||||
ingest = tmp_path / "ingest"
|
||||
_make_835_placeholder(ingest)
|
||||
out_dir = tmp_path / "out"
|
||||
|
||||
svc = SvcRow(
|
||||
src_file="x.835",
|
||||
claim_id="T1",
|
||||
member_id="J813715",
|
||||
status="4", # denied
|
||||
procedure="T1019",
|
||||
modifiers="",
|
||||
charge=Decimal("16.24"),
|
||||
paid=Decimal("0"),
|
||||
units=Decimal("1"),
|
||||
svc_date=_date(2026, 6, 27),
|
||||
cas_reasons=("CO-97",), # rebillable (not in EXCLUDED_CARCS or REVIEW_CARCS)
|
||||
pay_date=None,
|
||||
)
|
||||
_stub_svc(monkeypatch, [svc])
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, [
|
||||
"rebill-from-835",
|
||||
"--visits", str(visits),
|
||||
"--ingest", str(ingest),
|
||||
"--out", str(out_dir),
|
||||
"--as-of", "2026-07-07",
|
||||
])
|
||||
assert result.exit_code == 0, (result.output, result.exception)
|
||||
rows = _read_summary(out_dir)
|
||||
assert len(rows) == 1, rows
|
||||
assert rows[0]["disposition"] == "REBILLED_A"
|
||||
assert rows[0]["file_path"] == "pipeline-a/"
|
||||
assert "CO-97" in rows[0]["cas_reasons"]
|
||||
|
||||
|
||||
def test_rebill_paid_visit_excluded_from_summary(tmp_path: Path, monkeypatch):
|
||||
"""PAID visits (paid >= 95% of billed) must not appear in summary.csv — only NOT_IN_835 should."""
|
||||
visits = _write_visits_csv(tmp_path / "visits.csv", [
|
||||
("06/27/2026", "PAID-MEMBER", "T1019", "$100.00"),
|
||||
("06/27/2026", "MISSING-MEMBER", "T1019", "$2.32"),
|
||||
])
|
||||
ingest = tmp_path / "ingest"
|
||||
_make_835_placeholder(ingest)
|
||||
out_dir = tmp_path / "out"
|
||||
|
||||
# One fully-paid SVC for PAID-MEMBER; nothing matches MISSING-MEMBER.
|
||||
paid_svc = SvcRow(
|
||||
src_file="x.835",
|
||||
claim_id="T1",
|
||||
member_id="PAID-MEMBER",
|
||||
status="1",
|
||||
procedure="T1019",
|
||||
modifiers="",
|
||||
charge=Decimal("100.00"),
|
||||
paid=Decimal("100.00"),
|
||||
units=Decimal("1"),
|
||||
svc_date=_date(2026, 6, 27),
|
||||
cas_reasons=(),
|
||||
pay_date=None,
|
||||
)
|
||||
_stub_svc(monkeypatch, [paid_svc])
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, [
|
||||
"rebill-from-835",
|
||||
"--visits", str(visits),
|
||||
"--ingest", str(ingest),
|
||||
"--out", str(out_dir),
|
||||
"--as-of", "2026-07-07",
|
||||
])
|
||||
assert result.exit_code == 0, (result.output, result.exception)
|
||||
rows = _read_summary(out_dir)
|
||||
assert len(rows) == 1, rows
|
||||
assert rows[0]["member_id"] == "MISSING-MEMBER"
|
||||
assert rows[0]["disposition"] == "REBILLED_B"
|
||||
|
||||
|
||||
def test_rebill_override_filing_makes_ancient_visit_rebilled_b(tmp_path: Path, monkeypatch):
|
||||
"""A 2020 visit without --override-filing → EXCLUDED_TIMELY_FILING; with it → REBILLED_B."""
|
||||
# No SVCs at all — both visits fall into NOT_IN_835.
|
||||
visits = _write_visits_csv(tmp_path / "visits.csv", [
|
||||
("01/01/2020", "ANCIENT", "T1019", "$2.32"),
|
||||
])
|
||||
ingest = tmp_path / "ingest"
|
||||
_make_835_placeholder(ingest)
|
||||
out_dir = tmp_path / "out"
|
||||
|
||||
# Stub the (local) parse_835_svc import with an empty generator so
|
||||
# the CLI sees no SVCs and every visit is NOT_IN_835.
|
||||
monkeypatch.setattr(
|
||||
"cyclone.rebill.parse_835_svc.parse_835_svc",
|
||||
lambda path: iter([]),
|
||||
)
|
||||
|
||||
runner = CliRunner()
|
||||
|
||||
# Run 1: as-of 2026-07-07 minus 2020-01-01 = ~6.5 years > 120 days → EXCLUDED.
|
||||
r1 = runner.invoke(main, [
|
||||
"rebill-from-835",
|
||||
"--visits", str(visits),
|
||||
"--ingest", str(ingest),
|
||||
"--out", str(out_dir),
|
||||
"--as-of", "2026-07-07",
|
||||
])
|
||||
assert r1.exit_code == 0, (r1.output, r1.exception)
|
||||
rows1 = _read_summary(out_dir)
|
||||
assert len(rows1) == 1, rows1
|
||||
assert rows1[0]["disposition"] == "EXCLUDED_TIMELY_FILING", rows1
|
||||
|
||||
# Run 2: --override-filing bypasses the 120-day gate → REBILLED_B.
|
||||
r2 = runner.invoke(main, [
|
||||
"rebill-from-835",
|
||||
"--visits", str(visits),
|
||||
"--ingest", str(ingest),
|
||||
"--out", str(out_dir),
|
||||
"--as-of", "2026-07-07",
|
||||
"--override-filing",
|
||||
])
|
||||
assert r2.exit_code == 0, (r2.output, r2.exception)
|
||||
rows2 = _read_summary(out_dir)
|
||||
assert len(rows2) == 1, rows2
|
||||
assert rows2[0]["disposition"] == "REBILLED_B", rows2
|
||||
assert rows2[0]["file_path"] == "pipeline-b/"
|
||||
|
||||
|
||||
def test_rebill_status_with_real_summary_prints_tally(tmp_path: Path):
|
||||
"""--status against a real summary.csv must print both REBILLED_B and EXCLUDED_TIMELY_FILING rows."""
|
||||
from cyclone.rebill.reconcile import VisitRow
|
||||
from cyclone.rebill.summary import (
|
||||
EXCLUDED_TIMELY_FILING,
|
||||
REBILLED_B,
|
||||
SummaryRow,
|
||||
write_summary_csv,
|
||||
)
|
||||
|
||||
out_dir = tmp_path / "dev" / "rebills" / "2026-07-07"
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
summary_path = out_dir / "summary.csv"
|
||||
|
||||
rows = [
|
||||
SummaryRow(
|
||||
visit=VisitRow(
|
||||
date=_date(2026, 6, 27), member_id="MEM-A",
|
||||
procedure="T1019", billed=Decimal("2.32"),
|
||||
),
|
||||
disposition=REBILLED_B,
|
||||
unpaid=Decimal("2.32"),
|
||||
cas_reasons=(),
|
||||
file_path="pipeline-b/",
|
||||
),
|
||||
SummaryRow(
|
||||
visit=VisitRow(
|
||||
date=_date(2026, 6, 27), member_id="MEM-B",
|
||||
procedure="T1019", billed=Decimal("2.32"),
|
||||
),
|
||||
disposition=EXCLUDED_TIMELY_FILING,
|
||||
unpaid=Decimal("2.32"),
|
||||
cas_reasons=(),
|
||||
file_path="",
|
||||
),
|
||||
]
|
||||
write_summary_csv(rows, summary_path)
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, [
|
||||
"rebill-from-835",
|
||||
"--status",
|
||||
"--out", str(out_dir),
|
||||
])
|
||||
assert result.exit_code == 0, (result.output, result.exception)
|
||||
assert "REBILLED_B" in result.output
|
||||
assert "EXCLUDED_TIMELY_FILING" in result.output
|
||||
assert "TOTAL" in result.output
|
||||
@@ -0,0 +1,161 @@
|
||||
"""SP25: integration tests for ``cyclone recover-ingest`` (parse + DB-write recovery).
|
||||
|
||||
Fixtures used (all real production EDI from the test fixture corpus):
|
||||
- ``co_medicaid_837p.txt`` — 1-claim 837P fixture (small, fast).
|
||||
- ``co_medicaid_835.txt`` — minimal 835 fixture (real CO Medicaid).
|
||||
|
||||
These tests do NOT cover the operator's full ingest/ batch — that's
|
||||
Task 6 in the plan and runs against the live DB after these unit
|
||||
tests are green.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from click.testing import CliRunner
|
||||
|
||||
from cyclone import db, store
|
||||
from cyclone.cli import main as cli_main
|
||||
from cyclone.db import Batch, Claim, ProcessedInboundFile, Remittance
|
||||
|
||||
FIX_837P = Path(__file__).parent / "fixtures" / "co_medicaid_837p.txt"
|
||||
FIX_835 = Path(__file__).parent / "fixtures" / "co_medicaid_835.txt"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def runner():
|
||||
return CliRunner()
|
||||
|
||||
|
||||
# --- helpers --------------------------------------------------------------
|
||||
|
||||
def _count_claims() -> int:
|
||||
s = db.SessionLocal()()
|
||||
try:
|
||||
return s.query(Claim).count()
|
||||
finally:
|
||||
s.close()
|
||||
|
||||
|
||||
def _count_remittances() -> int:
|
||||
s = db.SessionLocal()()
|
||||
try:
|
||||
return s.query(Remittance).count()
|
||||
finally:
|
||||
s.close()
|
||||
|
||||
|
||||
def _count_batches() -> int:
|
||||
s = db.SessionLocal()()
|
||||
try:
|
||||
return s.query(Batch).count()
|
||||
finally:
|
||||
s.close()
|
||||
|
||||
|
||||
# --- tests ----------------------------------------------------------------
|
||||
|
||||
def test_recover_ingest_837p_persists_claims(runner):
|
||||
"""One 837P ingest via the CLI lands a Batch + ≥1 Claim row + a dedup row."""
|
||||
pre_claims = _count_claims()
|
||||
pre_batches = _count_batches()
|
||||
result = runner.invoke(
|
||||
cli_main,
|
||||
["recover-ingest", "--file", str(FIX_837P),
|
||||
"--sftp-block-name", "test-recover-837p"],
|
||||
catch_exceptions=False,
|
||||
)
|
||||
assert result.exit_code == 0, result.stderr or result.stdout
|
||||
out = (result.stdout or "") + (result.stderr or "")
|
||||
assert "ok" in out
|
||||
assert FIX_837P.name in out
|
||||
|
||||
assert _count_claims() > pre_claims, "expected new Claim rows"
|
||||
assert _count_batches() > pre_batches, "expected new Batch row"
|
||||
|
||||
# Dedup row created.
|
||||
s = db.SessionLocal()()
|
||||
try:
|
||||
row = s.query(ProcessedInboundFile).filter_by(
|
||||
sftp_block_name="test-recover-837p", name=FIX_837P.name,
|
||||
).first()
|
||||
finally:
|
||||
s.close()
|
||||
assert row is not None, "expected processed_inbound_files row"
|
||||
assert row.status == "ok"
|
||||
assert row.file_type == "837p"
|
||||
|
||||
|
||||
def test_recover_ingest_835_persists_remittance(runner):
|
||||
"""One 835 ingest via the CLI lands a Batch + Remittance row + dedup row."""
|
||||
pre_batches = _count_batches()
|
||||
pre_remits = _count_remittances()
|
||||
result = runner.invoke(
|
||||
cli_main,
|
||||
["recover-ingest", "--file", str(FIX_835),
|
||||
"--sftp-block-name", "test-recover-835"],
|
||||
catch_exceptions=False,
|
||||
)
|
||||
assert result.exit_code == 0, result.stderr or result.stdout
|
||||
assert "ok" in (result.stdout or "") + (result.stderr or "")
|
||||
|
||||
assert _count_batches() > pre_batches
|
||||
assert _count_remittances() > pre_remits
|
||||
|
||||
s = db.SessionLocal()()
|
||||
try:
|
||||
row = s.query(ProcessedInboundFile).filter_by(
|
||||
sftp_block_name="test-recover-835", name=FIX_835.name,
|
||||
).first()
|
||||
finally:
|
||||
s.close()
|
||||
assert row is not None
|
||||
assert row.status == "ok"
|
||||
assert row.file_type == "835"
|
||||
assert row.claim_count >= 1
|
||||
|
||||
|
||||
def test_recover_ingest_is_idempotent(runner):
|
||||
"""Second invocation with the same (block, file) is a no-op (duplicate)."""
|
||||
# First call — must ingest.
|
||||
r1 = runner.invoke(
|
||||
cli_main,
|
||||
["recover-ingest", "--file", str(FIX_837P),
|
||||
"--sftp-block-name", "test-recover-idem"],
|
||||
catch_exceptions=False,
|
||||
)
|
||||
assert r1.exit_code == 0, r1.stderr or r1.stdout
|
||||
|
||||
pre_claims = _count_claims()
|
||||
pre_batches = _count_batches()
|
||||
# Second call — must skip.
|
||||
r2 = runner.invoke(
|
||||
cli_main,
|
||||
["recover-ingest", "--file", str(FIX_837P),
|
||||
"--sftp-block-name", "test-recover-idem"],
|
||||
catch_exceptions=False,
|
||||
)
|
||||
assert r2.exit_code == 0, r2.stderr or r2.stdout
|
||||
assert "skipped" in (r2.stdout or "") + (r2.stderr or "")
|
||||
|
||||
# No new rows on second call.
|
||||
assert _count_claims() == pre_claims
|
||||
assert _count_batches() == pre_batches
|
||||
|
||||
|
||||
def test_recover_ingest_missing_file_returns_failed(runner, tmp_path):
|
||||
"""Missing file → Click rejects with usage error (the CLI uses exists=True)."""
|
||||
missing = tmp_path / "does-not-exist.837"
|
||||
pre_claims = _count_claims()
|
||||
result = runner.invoke(
|
||||
cli_main,
|
||||
["recover-ingest", "--file", str(missing),
|
||||
"--sftp-block-name", "test-recover-missing"],
|
||||
catch_exceptions=False,
|
||||
)
|
||||
# Click rejects up-front; usage error exits with code 2.
|
||||
assert result.exit_code == 2
|
||||
out = (result.stdout or "") + (result.stderr or "")
|
||||
assert "does not exist" in out or "Invalid value" in out
|
||||
assert _count_claims() == pre_claims
|
||||
@@ -0,0 +1,183 @@
|
||||
"""SP24 — tests for the `cyclone reissue-claims` CLI subcommand.
|
||||
|
||||
Five smoke tests covering the canonical cyclone-cli pattern:
|
||||
|
||||
1. --help renders cleanly and lists the long-form flags.
|
||||
2. happy path: a single-file input dir produces the expected
|
||||
number of output files.
|
||||
3. empty input dir: exits 2 with a "PARSE FAILED" message.
|
||||
4. IG-correctness guard fires when the constant is monkeypatched
|
||||
to True; CLI exits 1 with the REFUSING log line.
|
||||
5. --zip-output produces a valid zip with round-trip integrity.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from click.testing import CliRunner
|
||||
|
||||
from cyclone.cli import main
|
||||
|
||||
# Canonical minimal-claim fixture — flat, module-level Path constant
|
||||
# per the cyclone-tests convention. NEVER reach into docs/prodfiles/.
|
||||
MINIMAL_837P = Path(__file__).parent / "fixtures" / "minimal_837p.txt"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def _input_dir(tmp_path: Path) -> Path:
|
||||
"""Drop the minimal_837p fixture into tmp_path/in/."""
|
||||
in_dir = tmp_path / "in"
|
||||
in_dir.mkdir()
|
||||
(in_dir / "real.x12").write_text(MINIMAL_837P.read_text())
|
||||
return in_dir
|
||||
|
||||
|
||||
def test_reissue_claims_help_renders():
|
||||
"""--help exits 0 and lists every long-form flag in the SP24 spec."""
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(main, ["reissue-claims", "--help"])
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
# Required flag.
|
||||
assert "--input-dir" in result.output
|
||||
# Optional flags documented in the spec §2 Decision 3.
|
||||
for flag in [
|
||||
"--output-root",
|
||||
"--date",
|
||||
"--pipeline",
|
||||
"--payer",
|
||||
"--sender-id",
|
||||
"--receiver-id",
|
||||
"--submitter-name",
|
||||
"--submitter-contact-name",
|
||||
"--submitter-contact-email",
|
||||
"--receiver-name",
|
||||
"--zip-output",
|
||||
"--no-clean",
|
||||
"--log-level",
|
||||
]:
|
||||
assert flag in result.output, f"missing {flag} in help output"
|
||||
|
||||
|
||||
def test_reissue_claims_happy_path(_input_dir: Path, tmp_path: Path):
|
||||
"""A single-file input dir produces 1 .x12 output + summary sidecar."""
|
||||
runner = CliRunner()
|
||||
out_root = tmp_path / "out"
|
||||
|
||||
result = runner.invoke(
|
||||
main,
|
||||
[
|
||||
"reissue-claims",
|
||||
"--input-dir", str(_input_dir),
|
||||
"--output-root", str(out_root),
|
||||
"--date", "2026-07-08",
|
||||
"--pipeline", "initial",
|
||||
],
|
||||
catch_exceptions=False,
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, (
|
||||
f"CLI exited {result.exit_code}, output={result.output!r}, "
|
||||
f"exception={result.exception!r}"
|
||||
)
|
||||
# 1 .x12 + 1 _serialize_summary.json = 2 entries.
|
||||
emitted_dir = out_root / "2026-07-08" / "initial"
|
||||
assert emitted_dir.is_dir()
|
||||
files = sorted(emitted_dir.iterdir())
|
||||
x12s = [f for f in files if f.suffix == ".x12"]
|
||||
assert len(x12s) == 1
|
||||
assert (emitted_dir / "_serialize_summary.json").is_file()
|
||||
# HCPF-spec filename.
|
||||
assert x12s[0].name.startswith("tp11525703-837P-")
|
||||
assert x12s[0].name.endswith("-1of1.x12")
|
||||
# DONE summary line is in stdout.
|
||||
assert "DONE files=1 errors=0" in result.output
|
||||
|
||||
|
||||
def test_reissue_claims_empty_input_exits_2(tmp_path: Path):
|
||||
"""An empty input dir exits 2 with a clear PARSE FAILED message."""
|
||||
empty_dir = tmp_path / "empty"
|
||||
empty_dir.mkdir()
|
||||
out_root = tmp_path / "out"
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
main,
|
||||
[
|
||||
"reissue-claims",
|
||||
"--input-dir", str(empty_dir),
|
||||
"--output-root", str(out_root),
|
||||
],
|
||||
catch_exceptions=False,
|
||||
)
|
||||
|
||||
# No claims → exit 2.
|
||||
assert result.exit_code == 2, (
|
||||
f"CLI exited {result.exit_code}, output={result.output!r}"
|
||||
)
|
||||
# The error message should explain the failure.
|
||||
combined = result.output + (result.stderr or "")
|
||||
assert "PARSE FAILED" in combined or "no claims" in combined.lower()
|
||||
|
||||
|
||||
def test_reissue_claims_ig_correctness_guard_fires(monkeypatch, _input_dir: Path, tmp_path: Path):
|
||||
"""When PATIENT_LOOP_DEFAULT_INCLUDED is True, the CLI refuses to run."""
|
||||
# Monkeypatch the serializer constant to the broken value.
|
||||
# `raising=False` lets us set an attribute that didn't exist
|
||||
# pre-import (defensive against pytest collection order).
|
||||
from cyclone.parsers import serialize_837 as ser_mod
|
||||
monkeypatch.setattr(ser_mod, "PATIENT_LOOP_DEFAULT_INCLUDED", True)
|
||||
|
||||
runner = CliRunner()
|
||||
out_root = tmp_path / "out"
|
||||
result = runner.invoke(
|
||||
main,
|
||||
[
|
||||
"reissue-claims",
|
||||
"--input-dir", str(_input_dir),
|
||||
"--output-root", str(out_root),
|
||||
],
|
||||
catch_exceptions=False,
|
||||
)
|
||||
|
||||
# Guard fires → exit 1.
|
||||
assert result.exit_code == 1, (
|
||||
f"CLI exited {result.exit_code} (expected 1); output={result.output!r}"
|
||||
)
|
||||
# The refusal message is in stderr (click.echo(..., err=True)).
|
||||
combined = result.output + (result.stderr or "")
|
||||
assert "REFUSING to run" in combined
|
||||
assert "PATIENT_LOOP_DEFAULT_INCLUDED" in combined
|
||||
|
||||
|
||||
def test_reissue_claims_zip_output_round_trip(_input_dir: Path, tmp_path: Path):
|
||||
"""--zip-output writes a zip whose testzip() returns None."""
|
||||
runner = CliRunner()
|
||||
out_root = tmp_path / "out"
|
||||
zip_path = tmp_path / "out.zip"
|
||||
|
||||
result = runner.invoke(
|
||||
main,
|
||||
[
|
||||
"reissue-claims",
|
||||
"--input-dir", str(_input_dir),
|
||||
"--output-root", str(out_root),
|
||||
"--date", "2026-07-08",
|
||||
"--zip-output", str(zip_path),
|
||||
],
|
||||
catch_exceptions=False,
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert zip_path.is_file()
|
||||
|
||||
with zipfile.ZipFile(zip_path) as zf:
|
||||
# None = no corrupt entries.
|
||||
assert zf.testzip() is None
|
||||
names = zf.namelist()
|
||||
# 1 X12 file in the zip (the summary sidecar is not zipped).
|
||||
assert len(names) == 1
|
||||
assert names[0].startswith("tp11525703-837P-")
|
||||
assert names[0].endswith("-1of1.x12")
|
||||
@@ -126,14 +126,17 @@ def test_migration_latest_idempotent_on_fresh_db(tmp_path: Path) -> None:
|
||||
SP28 bumped it to 18 with the claim_acks join table.
|
||||
SP32 bumped it to 19 with rendering_provider_npi +
|
||||
service_provider_npi on claims and remittances.
|
||||
SP37 bumped it to 20 with batch transaction_set_control_number.
|
||||
SP39 bumped it to 21 with the resubmissions audit table.
|
||||
SP41 bumped it to 22 with submission_dedup.
|
||||
"""
|
||||
engine = _fresh_engine(tmp_path)
|
||||
db_migrate.run(engine)
|
||||
v_after_first = _user_version(engine)
|
||||
assert v_after_first == 19, f"expected head=19, got {v_after_first}"
|
||||
assert v_after_first == 23, f"expected head=23, got {v_after_first}"
|
||||
|
||||
db_migrate.run(engine)
|
||||
assert _user_version(engine) == 19, "second run should not bump version"
|
||||
assert _user_version(engine) == 23, "second run should not bump version"
|
||||
|
||||
|
||||
def test_drop_claims_unique_constraint_migration(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
@@ -159,7 +162,7 @@ def test_drop_claims_unique_constraint_migration(tmp_path: Path, monkeypatch: py
|
||||
engine = _fresh_engine(tmp_path)
|
||||
|
||||
db_migrate.run(engine)
|
||||
assert _user_version(engine) == 19, f"expected head=19, got {_user_version(engine)}"
|
||||
assert _user_version(engine) == 23, f"expected head=23, got {_user_version(engine)}"
|
||||
|
||||
# Two claims in one batch with the same patient_control_number
|
||||
# must be insertable. If 0015's table recreation re-introduced a
|
||||
@@ -186,3 +189,50 @@ def test_drop_claims_unique_constraint_migration(tmp_path: Path, monkeypatch: py
|
||||
assert [r[0] for r in rows] == ["CLM-1", "CLM-2"]
|
||||
assert [float(r[1]) for r in rows] == [100.0, 200.0]
|
||||
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# SP39: migration 0021 creates the resubmissions table
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_migration_0021_creates_resubmissions_table(tmp_path: Path) -> None:
|
||||
"""SP39: 0021_resubmissions.sql adds the resubmissions audit table
|
||||
with the documented columns + unique constraint on
|
||||
(claim_id, interchange_control_number)."""
|
||||
# Point the runner at the REAL migrations dir so we exercise 0021.
|
||||
real_dir = Path(db_migrate.__file__).parent / "migrations"
|
||||
import cyclone.db_migrate as real_migrate_mod
|
||||
real_dir_str = str(real_dir)
|
||||
|
||||
engine = _fresh_engine(tmp_path)
|
||||
# monkeypatch the module-level MIGRATIONS_DIR
|
||||
import importlib
|
||||
monkey_save = real_migrate_mod.MIGRATIONS_DIR
|
||||
real_migrate_mod.MIGRATIONS_DIR = Path(real_dir_str)
|
||||
try:
|
||||
db_migrate.run(engine)
|
||||
finally:
|
||||
real_migrate_mod.MIGRATIONS_DIR = monkey_save
|
||||
|
||||
# Verify the table exists with the expected columns + unique index.
|
||||
with engine.connect() as conn:
|
||||
version = conn.exec_driver_sql("PRAGMA user_version").scalar()
|
||||
assert version >= 21
|
||||
cols = conn.exec_driver_sql(
|
||||
"PRAGMA table_info(resubmissions)"
|
||||
).all()
|
||||
col_names = {row[1] for row in cols}
|
||||
assert col_names == {
|
||||
"id", "claim_id", "batch_id", "resubmitted_at",
|
||||
"source_corrected_path",
|
||||
"interchange_control_number", "group_control_number",
|
||||
}
|
||||
idx_rows = conn.exec_driver_sql(
|
||||
"SELECT name, sql FROM sqlite_master "
|
||||
"WHERE type='index' AND tbl_name='resubmissions'"
|
||||
).all()
|
||||
idx_names = {row[0] for row in idx_rows}
|
||||
assert "ix_resubmissions_claim_id" in idx_names
|
||||
assert "ix_resubmissions_batch_id" in idx_names
|
||||
assert "ux_resubmissions_claim_icn" in idx_names
|
||||
|
||||
@@ -0,0 +1,210 @@
|
||||
"""SP40: tests for the cyclone.edifabric HTTP client.
|
||||
|
||||
All tests use httpx.MockTransport — no live HTTP hits the network.
|
||||
The API key is supplied directly via the ``api_key=`` kwarg so the
|
||||
secrets module is never read during tests.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from cyclone import edifabric
|
||||
|
||||
|
||||
_TEST_KEY = "test-edifabric-key-0123456789abcdef"
|
||||
|
||||
|
||||
def _make_client(handler):
|
||||
"""Build an httpx.Client whose transport is the given handler."""
|
||||
transport = httpx.MockTransport(handler)
|
||||
return httpx.Client(transport=transport, timeout=10.0)
|
||||
|
||||
|
||||
def _install_factory(handler):
|
||||
"""Swap in the mocked httpx.Client for the duration of a test."""
|
||||
return edifabric.set_transport_factory(lambda: _make_client(handler))
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _reset_transport():
|
||||
"""Restore the default transport after each test (so a failing test
|
||||
can't poison the next)."""
|
||||
yield
|
||||
edifabric._reset_transport_factory()
|
||||
|
||||
|
||||
# --- /x12/read ---------------------------------------------------------
|
||||
|
||||
|
||||
def test_read_interchange_returns_first_x12_from_array():
|
||||
"""The /x12/read endpoint returns a list (multi-interchange file).
|
||||
Cyclone calls return the first element."""
|
||||
x12 = {
|
||||
"SegmentDelimiter": "~",
|
||||
"DataElementDelimiter": "*",
|
||||
"ISA": {"InterchangeControlNumber_13": "000000001"},
|
||||
"Groups": [],
|
||||
"IEATrailers": [],
|
||||
}
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
assert request.headers["Ocp-Apim-Subscription-Key"] == _TEST_KEY
|
||||
assert request.headers["Content-Type"] == "application/octet-stream"
|
||||
return httpx.Response(200, json=[x12])
|
||||
|
||||
_install_factory(handler)
|
||||
result = edifabric.read_interchange(b"ISA*...~IEA*0*000000001~", api_key=_TEST_KEY)
|
||||
assert result["ISA"]["InterchangeControlNumber_13"] == "000000001"
|
||||
|
||||
|
||||
def test_read_interchange_rejects_empty_response():
|
||||
"""If /x12/read returns an empty list, surface a 502-style error."""
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
return httpx.Response(200, json=[])
|
||||
|
||||
_install_factory(handler)
|
||||
with pytest.raises(edifabric.EdifabricError) as exc_info:
|
||||
edifabric.read_interchange(b"ISA*...~IEA*0*000000001~", api_key=_TEST_KEY)
|
||||
assert exc_info.value.status_code == 502
|
||||
|
||||
|
||||
# --- /x12/validate -----------------------------------------------------
|
||||
|
||||
|
||||
def test_validate_interchange_returns_operation_result():
|
||||
"""A 200 response is returned verbatim — Status + Details."""
|
||||
operation_result = {
|
||||
"Status": "success",
|
||||
"Details": [],
|
||||
"LastIndex": 46,
|
||||
}
|
||||
x12 = {
|
||||
"SegmentDelimiter": "~",
|
||||
"DataElementDelimiter": "*",
|
||||
"ISA": {"InterchangeControlNumber_13": "000000001"},
|
||||
"Groups": [],
|
||||
"IEATrailers": [],
|
||||
}
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
body = json.loads(request.content)
|
||||
assert body["ISA"]["InterchangeControlNumber_13"] == "000000001"
|
||||
return httpx.Response(200, json=operation_result)
|
||||
|
||||
_install_factory(handler)
|
||||
result = edifabric.validate_interchange(x12, api_key=_TEST_KEY)
|
||||
assert result["Status"] == "success"
|
||||
assert result["Details"] == []
|
||||
|
||||
|
||||
def test_validate_interchange_does_not_raise_on_status_error():
|
||||
"""OperationResult.Status='error' is data, not an exception — the
|
||||
caller (the gate / CLI) decides whether to fail-closed."""
|
||||
operation_result = {
|
||||
"Status": "error",
|
||||
"Details": [
|
||||
{"Index": 5, "SegmentId": "PER", "Message": "PER-04 is required", "Status": "error"},
|
||||
],
|
||||
"LastIndex": 5,
|
||||
}
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
return httpx.Response(200, json=operation_result)
|
||||
|
||||
_install_factory(handler)
|
||||
result = edifabric.validate_interchange({"ISA": {}}, api_key=_TEST_KEY)
|
||||
assert result["Status"] == "error"
|
||||
assert result["Details"][0]["Message"] == "PER-04 is required"
|
||||
|
||||
|
||||
# --- 4xx / 5xx ---------------------------------------------------------
|
||||
|
||||
|
||||
def test_validate_interchange_raises_on_5xx():
|
||||
"""Non-2xx responses raise EdifabricError; the body is preserved."""
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
return httpx.Response(503, text="upstream overloaded")
|
||||
|
||||
_install_factory(handler)
|
||||
with pytest.raises(edifabric.EdifabricError) as exc_info:
|
||||
edifabric.validate_interchange({"ISA": {}}, api_key=_TEST_KEY)
|
||||
assert exc_info.value.status_code == 503
|
||||
assert "upstream overloaded" in str(exc_info.value.body)
|
||||
|
||||
|
||||
def test_read_interchange_raises_with_retry_after_when_quota_blocked():
|
||||
"""On HTTP 403 with a ``Retry-After`` header (API Management quota
|
||||
policy), the raised EdifabricError exposes ``retry_after_seconds``
|
||||
so callers can sleep exactly until quota replenishes."""
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
return httpx.Response(
|
||||
403,
|
||||
headers={"Retry-After": "50492"},
|
||||
json={"statusCode": 403,
|
||||
"message": "Out of call volume quota."},
|
||||
)
|
||||
|
||||
_install_factory(handler)
|
||||
with pytest.raises(edifabric.EdifabricError) as exc_info:
|
||||
edifabric.read_interchange(b"ISA*...", api_key=_TEST_KEY)
|
||||
err = exc_info.value
|
||||
assert err.status_code == 403
|
||||
assert err.retry_after_seconds == 50492
|
||||
assert "Out of call volume quota" in str(err.body)
|
||||
|
||||
|
||||
# --- validate_edi (composed) ------------------------------------------
|
||||
|
||||
|
||||
def test_validate_edi_composes_read_then_validate():
|
||||
"""validate_edi should call read first, then validate with the
|
||||
X12Interchange JSON from read's response."""
|
||||
x12 = {
|
||||
"SegmentDelimiter": "~",
|
||||
"DataElementDelimiter": "*",
|
||||
"ISA": {"InterchangeControlNumber_13": "000000099"},
|
||||
"Groups": [],
|
||||
"IEATrailers": [],
|
||||
}
|
||||
operation_result = {"Status": "success", "Details": [], "LastIndex": 10}
|
||||
seen_calls: list[str] = []
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
if request.url.path.endswith("/read"):
|
||||
seen_calls.append("read")
|
||||
return httpx.Response(200, json=[x12])
|
||||
if request.url.path.endswith("/validate"):
|
||||
seen_calls.append("validate")
|
||||
# Verify the validate body is the X12Interchange JSON
|
||||
body = json.loads(request.content)
|
||||
assert body["ISA"]["InterchangeControlNumber_13"] == "000000099"
|
||||
return httpx.Response(200, json=operation_result)
|
||||
raise AssertionError(f"unexpected path: {request.url.path}")
|
||||
|
||||
_install_factory(handler)
|
||||
result = edifabric.validate_edi(b"ISA*...~IEA*0*000000099~", api_key=_TEST_KEY)
|
||||
assert seen_calls == ["read", "validate"]
|
||||
assert result["Status"] == "success"
|
||||
|
||||
|
||||
# --- API key handling --------------------------------------------------
|
||||
|
||||
|
||||
def test_validate_edi_raises_when_api_key_missing(monkeypatch):
|
||||
"""With no key configured anywhere, validate_edi surfaces a clear
|
||||
error to the operator (not a generic 500)."""
|
||||
monkeypatch.delenv("CYCLONE_EDIFABRIC_API_KEY", raising=False)
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
raise AssertionError("transport should not be called when key is missing")
|
||||
|
||||
_install_factory(handler)
|
||||
with pytest.raises(edifabric.EdifabricError) as exc_info:
|
||||
edifabric.validate_edi(b"ISA*...~IEA*0*000000001~")
|
||||
assert exc_info.value.status_code == 0
|
||||
assert "API key not configured" in str(exc_info.value.body)
|
||||
@@ -0,0 +1,210 @@
|
||||
"""Tests for migration 0020_add_batch_txn_set_control_number.sql.
|
||||
|
||||
SP37 adds a nullable ``batches.transaction_set_control_number`` column
|
||||
populated from the parsed 837's ST02 (transaction set control number)
|
||||
on every write. The 999 ack join (Pass 1) needs to resolve by ST02,
|
||||
not ISA13, so this column is the join key. This migration is purely
|
||||
additive: nullable, no default, backfills from
|
||||
``raw_result_json.envelope.transaction_set_control_number`` where the
|
||||
source JSON already carries it.
|
||||
|
||||
For the backfill-shape tests we point ``db_migrate.MIGRATIONS_DIR``
|
||||
at the real migrations directory, apply all migrations once to bring
|
||||
the fresh DB up to v20, insert representative rows, then replay the
|
||||
exact UPDATE statement the migration uses. Replaying the UPDATE
|
||||
proves the SQL works as intended even though the migration itself
|
||||
already ran over an empty table.
|
||||
|
||||
The backfill UPDATE is loaded directly from the migration file (not
|
||||
a hand-maintained copy) so the test cannot drift from production —
|
||||
see :func:`_load_migration_0020_backfill_sql` and the regression
|
||||
locks in ``test_migration_0020_no_drift.py``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import sqlalchemy as sa
|
||||
|
||||
from cyclone import db_migrate
|
||||
|
||||
|
||||
def _migration_0020_path() -> Path:
|
||||
return (
|
||||
Path(__file__).parent.parent / "src" / "cyclone" / "migrations"
|
||||
/ "0020_add_batch_txn_set_control_number.sql"
|
||||
)
|
||||
|
||||
|
||||
def _extract_update_statements(sql: str) -> list[str]:
|
||||
"""Split a migration into statements; return the UPDATE ones.
|
||||
|
||||
Mirrors db_migrate.run()'s splitter (strip ``--`` comments,
|
||||
split on ``;``) so the test extraction can never disagree with
|
||||
what the runner actually executes.
|
||||
"""
|
||||
lines = [
|
||||
line for line in sql.splitlines()
|
||||
if not line.strip().startswith("--")
|
||||
]
|
||||
cleaned = "\n".join(lines)
|
||||
return [
|
||||
stmt.strip() for stmt in cleaned.split(";")
|
||||
if stmt.strip()
|
||||
]
|
||||
|
||||
|
||||
def _load_migration_0020_backfill_sql() -> str:
|
||||
"""Return migration 0020's UPDATE statement (the backfill).
|
||||
|
||||
Reads the migration file at test time and extracts its UPDATE.
|
||||
Test code that needs to replay the backfill against rows that
|
||||
didn't exist when init_db ran uses this helper — guarantees the
|
||||
replayed SQL is byte-identical to what production will run.
|
||||
"""
|
||||
sql = _migration_0020_path().read_text()
|
||||
updates = [
|
||||
s for s in _extract_update_statements(sql)
|
||||
if s.upper().startswith("UPDATE ")
|
||||
]
|
||||
assert len(updates) == 1, (
|
||||
f"expected exactly 1 UPDATE in migration 0020, found {len(updates)}: {updates}"
|
||||
)
|
||||
return updates[0]
|
||||
|
||||
|
||||
def _fresh_engine(path: Path) -> sa.Engine:
|
||||
return sa.create_engine(f"sqlite:///{path}", future=True)
|
||||
|
||||
|
||||
def _table_info(engine: sa.Engine, table: str) -> list[tuple]:
|
||||
"""Return PRAGMA table_info rows for ``table`` as plain tuples."""
|
||||
with engine.connect() as conn:
|
||||
return list(conn.exec_driver_sql(f"PRAGMA table_info({table});").tuples())
|
||||
|
||||
|
||||
def _real_migrations_dir() -> Path:
|
||||
return Path(__file__).parent.parent / "src" / "cyclone" / "migrations"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def migrated_engine(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
||||
"""Yield an engine at v20 against which every real migration has run.
|
||||
|
||||
Points ``db_migrate.MIGRATIONS_DIR`` at the real migrations
|
||||
directory so the test exercises the actual 0020 file, then runs
|
||||
the migration runner on a per-test fresh DB.
|
||||
"""
|
||||
monkeypatch.setattr(db_migrate, "MIGRATIONS_DIR", _real_migrations_dir())
|
||||
engine = _fresh_engine(tmp_path / "mig0020.db")
|
||||
db_migrate.run(engine)
|
||||
|
||||
# Confirm head is 22 (every migration applied, including SP41's 0022).
|
||||
with engine.connect() as conn:
|
||||
v = conn.exec_driver_sql("PRAGMA user_version").scalar() or 0
|
||||
assert v == 23, f"expected migration head=23, got {v}"
|
||||
|
||||
yield engine
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_migration_0020_creates_column(migrated_engine) -> None:
|
||||
"""Migration 0020 adds ``transaction_set_control_number`` to ``batches``."""
|
||||
cols = _table_info(migrated_engine, "batches")
|
||||
col_names = {row[1] for row in cols}
|
||||
assert "transaction_set_control_number" in col_names, (
|
||||
"batches.transaction_set_control_number missing — migration 0020 did not run. "
|
||||
f"Existing columns: {sorted(col_names)}"
|
||||
)
|
||||
|
||||
|
||||
def test_migration_0020_column_is_nullable(migrated_engine) -> None:
|
||||
"""The new column is nullable (no DEFAULT, no NOT NULL) so existing
|
||||
batches that don't yet carry the ST02 stay valid."""
|
||||
cols = {row[1]: row for row in _table_info(migrated_engine, "batches")}
|
||||
row = cols["transaction_set_control_number"]
|
||||
# PRAGMA table_info tuples: (cid, name, type, notnull, dflt_value, pk)
|
||||
assert row[3] == 0, f"notnull flag must be 0 (nullable), got {row[3]}"
|
||||
assert row[4] is None, f"dflt_value must be NULL, got {row[4]!r}"
|
||||
assert "TEXT" in (row[2] or "").upper(), f"expected TEXT column, got {row[2]!r}"
|
||||
|
||||
|
||||
def test_migration_0020_backfills_when_key_present(
|
||||
migrated_engine: sa.Engine,
|
||||
) -> None:
|
||||
"""Replay the migration's backfill UPDATE against a row whose
|
||||
``raw_result_json`` carries the key — must populate the new column.
|
||||
|
||||
Replay (rather than waiting for ``db_migrate.run()`` to do it) is
|
||||
the only way to test the SQL against a row that didn't exist when
|
||||
init_db() ran; the migration's UPDATE naturally runs only over
|
||||
pre-existing rows.
|
||||
"""
|
||||
st02_value = "ST0001"
|
||||
raw_json = {"envelope": {"transaction_set_control_number": st02_value}}
|
||||
|
||||
with migrated_engine.begin() as conn:
|
||||
conn.exec_driver_sql(
|
||||
"INSERT INTO batches (id, kind, input_filename, parsed_at, raw_result_json) "
|
||||
"VALUES ('B-ST02-1', '837p', 'mig0020-st02.txt', '2026-07-07 00:00:00', ?)",
|
||||
(json.dumps(raw_json),),
|
||||
)
|
||||
conn.exec_driver_sql(_load_migration_0020_backfill_sql())
|
||||
|
||||
with migrated_engine.connect() as conn:
|
||||
row = conn.exec_driver_sql(
|
||||
"SELECT transaction_set_control_number FROM batches WHERE id='B-ST02-1'"
|
||||
).first()
|
||||
assert row is not None
|
||||
assert row[0] == st02_value, (
|
||||
f"backfill failed: expected {st02_value!r}, got {row[0]!r}"
|
||||
)
|
||||
|
||||
|
||||
def test_migration_0020_backfill_conditional_on_key_present(
|
||||
migrated_engine: sa.Engine,
|
||||
) -> None:
|
||||
"""Replay the UPDATE against a row whose ``raw_result_json`` does NOT
|
||||
carry the key — must stay NULL. Proves the UPDATE is conditional
|
||||
(the ``json_extract(...) IS NOT NULL`` guard) rather than
|
||||
unconditionally overwriting with NULL."""
|
||||
raw_json = {"envelope": {"control_number": "ISA0001"}} # no txn-set key
|
||||
with migrated_engine.begin() as conn:
|
||||
conn.exec_driver_sql(
|
||||
"INSERT INTO batches (id, kind, input_filename, parsed_at, raw_result_json) "
|
||||
"VALUES ('B-NOST-1', '837p', 'mig0020-nost.txt', '2026-07-07 00:00:00', ?)",
|
||||
(json.dumps(raw_json),),
|
||||
)
|
||||
conn.exec_driver_sql(_load_migration_0020_backfill_sql())
|
||||
|
||||
with migrated_engine.connect() as conn:
|
||||
row = conn.exec_driver_sql(
|
||||
"SELECT transaction_set_control_number FROM batches WHERE id='B-NOST-1'"
|
||||
).first()
|
||||
assert row is not None
|
||||
assert row[0] is None, (
|
||||
f"backfill must not overwrite when key is absent; got {row[0]!r}"
|
||||
)
|
||||
|
||||
|
||||
def test_migration_0020_backfill_handles_null_raw_result_json(
|
||||
migrated_engine: sa.Engine,
|
||||
) -> None:
|
||||
"""A Batch row with ``raw_result_json IS NULL`` (the prior SP's
|
||||
unparsed state) must not crash the backfill and must stay NULL."""
|
||||
with migrated_engine.begin() as conn:
|
||||
conn.exec_driver_sql(
|
||||
"INSERT INTO batches (id, kind, input_filename, parsed_at) "
|
||||
"VALUES ('B-NULL-1', '837p', 'mig0020-null.txt', '2026-07-07 00:00:00')"
|
||||
)
|
||||
conn.exec_driver_sql(_load_migration_0020_backfill_sql())
|
||||
|
||||
with migrated_engine.connect() as conn:
|
||||
row = conn.exec_driver_sql(
|
||||
"SELECT transaction_set_control_number FROM batches WHERE id='B-NULL-1'"
|
||||
).first()
|
||||
assert row is not None
|
||||
assert row[0] is None
|
||||
@@ -0,0 +1,66 @@
|
||||
"""SP37 follow-up #4: detect drift between test BACKFILL_SQL and migration SQL.
|
||||
|
||||
Followup #1 of the SP37 final-state tracker. The previous test file
|
||||
maintained a hand-copied ``BACKFILL_SQL`` constant alongside the
|
||||
migration file. If a future contributor edited one but not the other,
|
||||
the test would silently replay a different SQL than production —
|
||||
defeating the regression test.
|
||||
|
||||
The fix: ``test_migration_0020.py`` now reads the migration file at
|
||||
test time and extracts its UPDATE. This file imports the helper and
|
||||
pins the invariant — so a future contributor who edits the migration
|
||||
automatically gets the new SQL replayed in tests, and a contributor
|
||||
who removes the backfill UPDATE gets a loud extraction failure.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from test_migration_0020 import (
|
||||
_extract_update_statements,
|
||||
_load_migration_0020_backfill_sql,
|
||||
_migration_0020_path,
|
||||
)
|
||||
|
||||
|
||||
def test_migration_0020_backfill_sql_uses_migration_file():
|
||||
"""The backfill SQL used in tests must come from the migration file.
|
||||
|
||||
Guards against the previous pattern of a hand-maintained
|
||||
BACKFILL_SQL constant that could drift from the actual migration.
|
||||
"""
|
||||
sql = _load_migration_0020_backfill_sql()
|
||||
# Sanity check: the SQL targets batches.transaction_set_control_number
|
||||
# via json_extract on raw_result_json. If a contributor changes
|
||||
# the column name or the JSON path, this assertion catches it.
|
||||
assert "UPDATE batches" in sql
|
||||
assert "SET transaction_set_control_number" in sql
|
||||
assert "json_extract(raw_result_json" in sql
|
||||
assert "$.envelope.transaction_set_control_number" in sql
|
||||
|
||||
|
||||
def test_migration_0020_backfill_sql_is_non_empty_single_statement():
|
||||
"""The helper returns a non-empty SQL string suitable for direct
|
||||
execution via exec_driver_sql. The existing
|
||||
``test_migration_0020.py`` tests use this helper to replay the
|
||||
SQL against representative rows — so any successful replay
|
||||
exercises the migration's actual UPDATE.
|
||||
"""
|
||||
sql = _load_migration_0020_backfill_sql()
|
||||
assert sql # non-empty
|
||||
assert ";" not in sql # already stripped by the splitter
|
||||
|
||||
|
||||
def test_migration_0020_has_exactly_one_update():
|
||||
"""Guardrail: if a future migration adds a second UPDATE, the
|
||||
extraction fails loudly so the test author can decide which one
|
||||
is the backfill.
|
||||
"""
|
||||
sql = _migration_0020_path().read_text()
|
||||
updates = [
|
||||
s for s in _extract_update_statements(sql)
|
||||
if s.upper().startswith("UPDATE ")
|
||||
]
|
||||
assert len(updates) == 1, (
|
||||
f"migration 0020 should have exactly 1 UPDATE (the backfill); "
|
||||
f"found {len(updates)}. If you added a second UPDATE, update "
|
||||
f"_load_migration_0020_backfill_sql() to pick the right one."
|
||||
)
|
||||
@@ -0,0 +1,56 @@
|
||||
"""Regression test: RateLimitMiddleware must share its bucket across instances.
|
||||
|
||||
When ``importlib.reload(cyclone.api)`` runs (e.g. from
|
||||
``test_cors_extra_origins_via_env``), Starlette rebuilds the middleware
|
||||
stack with a NEW ``RateLimitMiddleware`` instance whose ``_buckets``
|
||||
dict is fresh. Tests that imported ``app`` at module load time still
|
||||
reference the OLD app — and the OLD RateLimitMiddleware — so its
|
||||
private ``_buckets`` dict keeps accumulating requests across the rest
|
||||
of the suite. After ~300 requests it hits the rate limit and the
|
||||
remaining tests get spurious 429s.
|
||||
|
||||
Fix: hoist ``_buckets`` to a class-level dict so every
|
||||
``RateLimitMiddleware`` instance (current + stale) shares the same
|
||||
sliding-window state. The per-instance ``_lock`` stays per-instance
|
||||
since it guards mutation of the shared dict.
|
||||
|
||||
This test pins that invariant: two ``RateLimitMiddleware`` instances
|
||||
share the same underlying bucket dict.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from cyclone.security import RateLimitMiddleware
|
||||
|
||||
|
||||
class _DummyApp:
|
||||
"""Minimal ASGI app stand-in for the middleware's inner app."""
|
||||
|
||||
async def __call__(self, scope, receive, send):
|
||||
pass
|
||||
|
||||
|
||||
def test_rate_limit_buckets_are_shared_across_instances():
|
||||
"""Two RateLimitMiddleware instances must share _buckets."""
|
||||
m1 = RateLimitMiddleware(_DummyApp())
|
||||
m2 = RateLimitMiddleware(_DummyApp())
|
||||
# After the fix: m1._buckets IS m2._buckets (same class-level dict).
|
||||
# Before the fix: each instance had its own {} dict.
|
||||
assert m1._buckets is m2._buckets, (
|
||||
"RateLimitMiddleware instances do NOT share their bucket dict. "
|
||||
"After importlib.reload(cyclone.api), the new instance's "
|
||||
"_buckets is independent of the old one — orphaned buckets "
|
||||
"accumulate across tests that hold stale `app` references and "
|
||||
"trip the 300 req/60s limit. Hoist _buckets to a class-level "
|
||||
"dict so every instance shares state."
|
||||
)
|
||||
|
||||
|
||||
def test_class_level_buckets_attribute_exists():
|
||||
"""The class must declare a class-level _buckets dict."""
|
||||
# This is the structural pre-condition for the sharing invariant.
|
||||
assert hasattr(RateLimitMiddleware, "_buckets"), (
|
||||
"RateLimitMiddleware must declare a class-level _buckets "
|
||||
"attribute so every instance shares it. Without this, "
|
||||
"importlib.reload(cyclone.api) creates an orphaned bucket."
|
||||
)
|
||||
assert isinstance(RateLimitMiddleware._buckets, dict)
|
||||
@@ -0,0 +1,49 @@
|
||||
"""CARC-aware filter for Pipeline A.
|
||||
|
||||
Contractual / non-recoverable denials (CO-45, CO-26, CO-129) must be
|
||||
excluded from rebill. CARCs that need operator review (PI-16, PI-96,
|
||||
PI-15, PI-4, PI-110, OA-18, OA-23) must be surfaced as 'REVIEW' so the
|
||||
operator can decide.
|
||||
"""
|
||||
from cyclone.rebill.carc_filter import (
|
||||
CarcDecision,
|
||||
decide_carc,
|
||||
EXCLUDED_CARCS,
|
||||
REVIEW_CARCS,
|
||||
)
|
||||
|
||||
|
||||
def test_co45_is_excluded():
|
||||
assert decide_carc(("CO-45",)) == CarcDecision.EXCLUDED
|
||||
|
||||
|
||||
def test_co26_is_excluded():
|
||||
assert decide_carc(("CO-26",)) == CarcDecision.EXCLUDED
|
||||
|
||||
|
||||
def test_co129_is_excluded():
|
||||
assert decide_carc(("CO-129",)) == CarcDecision.EXCLUDED
|
||||
|
||||
|
||||
def test_pi16_is_review():
|
||||
assert decide_carc(("PI-16",)) == CarcDecision.REVIEW
|
||||
|
||||
|
||||
def test_o18_is_review():
|
||||
"""OA-18 is the duplicate noise — surface for review, don't auto-rebill."""
|
||||
assert decide_carc(("OA-18",)) == CarcDecision.REVIEW
|
||||
|
||||
|
||||
def test_no_carc_is_rebill():
|
||||
assert decide_carc(()) == CarcDecision.REBILL
|
||||
|
||||
|
||||
def test_mixed_excluded_wins():
|
||||
"""If any CARC is excluded, the whole service is excluded."""
|
||||
assert decide_carc(("PI-16", "CO-45")) == CarcDecision.EXCLUDED
|
||||
|
||||
|
||||
def test_sets_have_expected_members():
|
||||
assert "CO-45" in EXCLUDED_CARCS
|
||||
assert "PI-16" in REVIEW_CARCS
|
||||
assert "OA-18" in REVIEW_CARCS
|
||||
@@ -0,0 +1,172 @@
|
||||
"""SP41 Task 17 — end-to-end smoke test for ``run_rebill``.
|
||||
|
||||
Wires the full SP41 pipeline (835 SVC reparse → reconcile → CARC
|
||||
filter → timely-filing gate → pipeline A → pipeline B → summary CSV)
|
||||
end-to-end on synthetic inputs and pins the summary.csv shape +
|
||||
counts.
|
||||
|
||||
No mocks for ``parse_835_svc`` or ``validate_837`` — the goal is to
|
||||
exercise the real pipeline. The autouse conftest handles Edifabric
|
||||
fail-open (no API key in CI), and since this run produces only
|
||||
quarantined dispositions (no pipeline A/B emissions), ``validate_edi``
|
||||
is never called anyway.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
from datetime import date
|
||||
from pathlib import Path
|
||||
|
||||
from cyclone.rebill.run import run_rebill
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Fixture builders
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def _write_visits_csv(path: Path) -> Path:
|
||||
"""Two visits: one in-window (will match a denied SVC), one past the
|
||||
120-day timely-filing window (no matching SVC).
|
||||
|
||||
DOS column is MM/DD/YYYY per AxisCare's actual export format.
|
||||
"""
|
||||
with path.open("w", newline="") as f:
|
||||
w = csv.writer(f)
|
||||
w.writerow([
|
||||
"Visit Date", "Member ID", "Procedure Code",
|
||||
"Billable Amount", "Authorized",
|
||||
])
|
||||
w.writerow(["06/27/2026", "J813715", "T1019", "2.32", "Y"])
|
||||
w.writerow(["01/01/2026", "OLD001", "T1019", "5.00", "Y"])
|
||||
return path
|
||||
|
||||
|
||||
def _write_835_with_denied_svc(ingest_dir: Path) -> Path:
|
||||
"""Write a real 835 that ``parse_835_svc`` walks end-to-end.
|
||||
|
||||
Contains exactly one CLP block for member J813715 with:
|
||||
- CLP02 = 4 (Denied)
|
||||
- CLP03 = 2.32 (charge)
|
||||
- SVC*HC:T1019*2.32*0** (paid = $0)
|
||||
- DTM*472*20260627 (service date)
|
||||
- CAS*CO*45*2.32 (triggers ``EXCLUDED_CARC``)
|
||||
|
||||
Returns the **directory** path so the caller can pass it straight to
|
||||
``run_rebill(ingest_dir=...)`` — run_rebill does its own
|
||||
``ingest_dir.glob("*.835")`` walk. (Earlier draft returned the
|
||||
inner file path, which made glob come up empty.)
|
||||
|
||||
Shape mirrors the existing ``tests/fixtures/835_sample_svc_with_member.txt``
|
||||
(segments concatenated without ``\\n`` separators — ``parse_835_svc``
|
||||
splits on ``~`` only, and a leading newline makes ``elems[0]`` an empty
|
||||
string that the segment-name match drops silently).
|
||||
"""
|
||||
ingest_dir.mkdir(exist_ok=True)
|
||||
segs = [
|
||||
"ISA*00* *00* *ZZ*CYCLONE *ZZ*GAINWELL *260627*1200*^*00501*000000001*0*P*:~",
|
||||
"GS*HC*CYCLONE*GAINWELL*20260627*1200*1*X*005010X221A1~",
|
||||
"ST*835*0001~",
|
||||
"BPR*I*0*C*ACH*CCP*01*021000021*DA*123456789*1512345678**01*021000021*DA*123456789*20260101~",
|
||||
"TRN*1*TRACE01*1512345678~",
|
||||
"DTM*405*20260118~",
|
||||
"N1*PR*COLORADO MEDICAL ASSISTANCE PROGRAM*XV*COMEDASSISTPROG~",
|
||||
"N3*PO BOX 1100~",
|
||||
"N4*DENVER*CO*80202~",
|
||||
"LX*1~",
|
||||
# CLP02=4 (Denied); CLP03=2.32 (charge); CLP04=0 (paid).
|
||||
"CLP*DENIED-CLM*4*2.32*0**MC*111*11*1~",
|
||||
# NM1*QC.NM109 carries the member_id forward to SVC rows.
|
||||
"NM1*QC*1*DOE*JANE****MR*J813715~",
|
||||
# SVC composite qualifier:procedure (HC:T1019); 4-arg form is fine.
|
||||
"SVC*HC:T1019*2.32*0**~",
|
||||
# DTM*472 carries the service date (matches parse_835_svc's lookup).
|
||||
"DTM*472*20260627~",
|
||||
# CO-45 is in EXCLUDED_CARCS → CarcDecision.EXCLUDED → EXCLUDED_CARC.
|
||||
"CAS*CO*45*2.32~",
|
||||
"SE*14*0001~",
|
||||
"GE*1*1~",
|
||||
"IEA*1*000000001~",
|
||||
]
|
||||
p = ingest_dir / "x.835"
|
||||
p.write_text("".join(segs))
|
||||
return ingest_dir
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Smoke test
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def test_run_rebill_end_to_end_excluded_dispositions(tmp_path):
|
||||
"""End-to-end: 2 visits → 2 quarantined dispositions, no pipeline files.
|
||||
|
||||
Pins the contract that ``run_rebill`` returns a ``RunResult`` whose
|
||||
``summary.csv`` row-shapes match the visit-side input (one row per
|
||||
non-PAID visit, with the right disposition per row).
|
||||
"""
|
||||
visits_csv = _write_visits_csv(tmp_path / "visits.csv")
|
||||
ingest = _write_835_with_denied_svc(tmp_path / "ingest")
|
||||
out_dir = tmp_path / "rebills"
|
||||
|
||||
# ``as_of=2026-07-07`` pins the timely-filing gate so the test is
|
||||
# deterministic — OLD001 (DOS 2026-01-01) is 187 days old, well past
|
||||
# the 120-day HCPF window, so it's EXCLUDED_TIMELY_FILING.
|
||||
result = run_rebill(
|
||||
window_start=date(2026, 1, 1),
|
||||
window_end=date(2026, 6, 27),
|
||||
override_filing=False,
|
||||
visits_csv_path=str(visits_csv),
|
||||
ingest_dir=str(ingest),
|
||||
out_dir=str(out_dir),
|
||||
as_of=date(2026, 7, 7),
|
||||
)
|
||||
|
||||
# --- summary.csv exists and the right text is in it. --- #
|
||||
assert result.summary_path.exists(), result.summary_path
|
||||
text = result.summary_path.read_text()
|
||||
assert "J813715" in text
|
||||
assert "OLD001" in text
|
||||
assert "EXCLUDED_CARC" in text
|
||||
assert "EXCLUDED_TIMELY_FILING" in text
|
||||
|
||||
# --- counts match expectations (one of each excluded disposition). --- #
|
||||
assert result.counts["EXCLUDED_CARC"] == 1, result.counts
|
||||
assert result.counts["EXCLUDED_TIMELY_FILING"] == 1, result.counts
|
||||
# Both visits are excluded — no pipeline emissions.
|
||||
assert result.counts["REBILLED_A"] == 0, result.counts
|
||||
assert result.counts["REBILLED_B"] == 0, result.counts
|
||||
|
||||
# --- pin the per-row shape (stronger than substring checks). --- #
|
||||
with result.summary_path.open(newline="") as f:
|
||||
rows = list(csv.DictReader(f))
|
||||
assert len(rows) == 2, rows
|
||||
|
||||
j_row = next(r for r in rows if r["member_id"] == "J813715")
|
||||
old_row = next(r for r in rows if r["member_id"] == "OLD001")
|
||||
|
||||
# J813715: matched the DENIED SVC, CARC CO-45 is in EXCLUDED_CARCS.
|
||||
assert j_row["disposition"] == "EXCLUDED_CARC", j_row
|
||||
assert j_row["procedure"] == "T1019", j_row
|
||||
assert j_row["dos"] == "2026-06-27", j_row
|
||||
assert j_row["billed"] == "2.32", j_row
|
||||
assert "CO-45" in j_row["cas_reasons"], j_row
|
||||
|
||||
# OLD001: no matching SVC, DOS 187 days old → timely-filing exclusion.
|
||||
assert old_row["disposition"] == "EXCLUDED_TIMELY_FILING", old_row
|
||||
assert old_row["procedure"] == "T1019", old_row
|
||||
assert old_row["dos"] == "2026-01-01", old_row
|
||||
assert old_row["billed"] == "5.00", old_row
|
||||
# No SVC match → empty cas_reasons column.
|
||||
assert old_row["cas_reasons"] == "", old_row
|
||||
|
||||
# --- no pipeline files were emitted (both rows are excluded). --- #
|
||||
assert result.pipeline_a_files == [], result.pipeline_a_files
|
||||
assert result.pipeline_b_files == [], result.pipeline_b_files
|
||||
# The pipeline dirs exist but are empty.
|
||||
assert (out_dir / "pipeline-a").is_dir()
|
||||
assert (out_dir / "pipeline-b").is_dir()
|
||||
assert list((out_dir / "pipeline-a").iterdir()) == []
|
||||
assert list((out_dir / "pipeline-b").iterdir()) == []
|
||||
# No quarantined files either — these dispositions don't emit at all.
|
||||
assert list((out_dir / "quarantine").iterdir()) == []
|
||||
@@ -0,0 +1,38 @@
|
||||
"""835 SVC-level parser with member_id at SVC scope."""
|
||||
from pathlib import Path
|
||||
from collections import defaultdict
|
||||
from decimal import Decimal
|
||||
from cyclone.rebill.parse_835_svc import parse_835_svc
|
||||
|
||||
FIX = Path(__file__).parent / "fixtures" / "835_sample_svc_with_member.txt"
|
||||
|
||||
def test_parse_835_svc_extracts_member_id():
|
||||
"""NM1*QC NM109 at the CLP scope must propagate to that CLP's SVC rows;
|
||||
the multi-claim fixture confirms each CLP's NM1*QC lands on its own SVCs."""
|
||||
rows = list(parse_835_svc(FIX))
|
||||
assert len(rows) >= 1
|
||||
by_claim: dict[str, list[str]] = defaultdict(list)
|
||||
for r in rows:
|
||||
by_claim[r.claim_id].append(r.member_id)
|
||||
assert r.procedure # non-empty
|
||||
assert r.svc_date # non-empty
|
||||
assert r.charge > 0
|
||||
# First CLP (T1001) -> original fixture member
|
||||
assert all(mid == "J813715" for mid in by_claim["T1001"]), by_claim
|
||||
# Second CLP (T1002, status 4) -> its own NM1*QC
|
||||
assert all(mid == "OTHER-MEMBER-A" for mid in by_claim["T1002"]), by_claim
|
||||
# Third CLP (T1003, status 22) -> its own NM1*QC
|
||||
assert all(mid == "OTHER-MEMBER-B" for mid in by_claim["T1003"]), by_claim
|
||||
|
||||
def test_parse_835_svc_extracts_cas_reasons():
|
||||
"""CAS segments after DTM*472 must be captured (post-DTM*472 ordering)."""
|
||||
rows = list(parse_835_svc(FIX))
|
||||
# at least one row should have an OA-18 reason
|
||||
assert any("OA-18" in r.cas_reasons for r in rows)
|
||||
|
||||
def test_parse_835_svc_picks_up_status_22_reversals():
|
||||
"""Status 22 (reversal of previous payment) must be preserved, along with
|
||||
status 1 (primary) and status 4 (denied)."""
|
||||
rows = list(parse_835_svc(FIX))
|
||||
statuses = {r.status for r in rows}
|
||||
assert statuses == {"1", "4", "22"}
|
||||
@@ -0,0 +1,55 @@
|
||||
"""Pipeline A: denied/partial visits become frequency-7 replacement claims."""
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
from cyclone.rebill.pipeline_a import build_pipeline_a_claims
|
||||
from cyclone.rebill.reconcile import VisitRow, ReconcileOutcome, OutcomeCategory
|
||||
from cyclone.rebill.carc_filter import CarcDecision
|
||||
|
||||
|
||||
def _outcome(date_, member, proc, billed, cat, unpaid=Decimal("0")):
|
||||
visit = VisitRow(date=date_, member_id=member, procedure=proc, billed=Decimal(billed))
|
||||
return ReconcileOutcome(visit, cat, unpaid, 0)
|
||||
|
||||
|
||||
def test_pipeline_a_emits_frequency_7():
|
||||
"""CLM05-3 must be 7 (replacement), preserving the original claim_submit_id."""
|
||||
out = build_pipeline_a_claims(
|
||||
original_claim_id="ORIG-001",
|
||||
visit_outcomes=[
|
||||
_outcome(date(2026, 6, 27), "J813715", "T1019", "2.32",
|
||||
OutcomeCategory.DENIED, unpaid=Decimal("2.32")),
|
||||
],
|
||||
carc_decisions=[CarcDecision.REBILL],
|
||||
cas_reasons_per_visit=[()],
|
||||
)
|
||||
assert len(out) == 1
|
||||
assert out[0].claim_id == "ORIG-001"
|
||||
assert out[0].frequency_code == "7"
|
||||
assert out[0].svc_date == date(2026, 6, 27)
|
||||
|
||||
|
||||
def test_pipeline_a_excludes_carc_excluded_visits():
|
||||
out = build_pipeline_a_claims(
|
||||
original_claim_id="ORIG-002",
|
||||
visit_outcomes=[
|
||||
_outcome(date(2026, 6, 27), "J813715", "T1019", "2.32",
|
||||
OutcomeCategory.DENIED),
|
||||
],
|
||||
carc_decisions=[CarcDecision.EXCLUDED],
|
||||
cas_reasons_per_visit=[("CO-45",)],
|
||||
)
|
||||
assert out == [] # not emitted
|
||||
|
||||
|
||||
def test_pipeline_a_surfaces_review_visits_with_flag():
|
||||
out = build_pipeline_a_claims(
|
||||
original_claim_id="ORIG-003",
|
||||
visit_outcomes=[
|
||||
_outcome(date(2026, 6, 27), "J813715", "T1019", "2.32",
|
||||
OutcomeCategory.DENIED),
|
||||
],
|
||||
carc_decisions=[CarcDecision.REVIEW],
|
||||
cas_reasons_per_visit=[("PI-16",)],
|
||||
)
|
||||
assert len(out) == 1
|
||||
assert out[0].needs_review is True
|
||||
@@ -0,0 +1,120 @@
|
||||
"""Pipeline B: NOT_IN_835 visits → fresh 837Ps, batched by (member, ISO-week)."""
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
from cyclone.rebill.pipeline_b import build_pipeline_b_batches
|
||||
from cyclone.rebill.reconcile import VisitRow
|
||||
|
||||
|
||||
def _v(date_, member, proc, amt):
|
||||
return VisitRow(date=date_, member_id=member, procedure=proc, billed=Decimal(amt))
|
||||
|
||||
|
||||
def test_batches_split_by_member_and_iso_week():
|
||||
visits = [
|
||||
_v(date(2026, 6, 23), "J813715", "T1019", "2.32"),
|
||||
_v(date(2026, 6, 25), "J813715", "T1019", "2.32"), # same week
|
||||
_v(date(2026, 6, 23), "OTHER", "T1019", "2.32"), # different member
|
||||
_v(date(2026, 6, 30), "J813715", "T1019", "2.32"), # different week
|
||||
]
|
||||
out = build_pipeline_b_batches(visits, as_of=date(2026, 7, 7), override=False)
|
||||
assert len(out) == 3
|
||||
sizes = sorted(len(b.visits) for b in out)
|
||||
assert sizes == [1, 1, 2]
|
||||
|
||||
|
||||
def test_timely_filing_excludes_old_visits():
|
||||
visits = [
|
||||
_v(date(2026, 1, 1), "OLD", "T1019", "2.32"), # 187 days old
|
||||
_v(date(2026, 6, 27), "NEW", "T1019", "2.32"), # 10 days old
|
||||
]
|
||||
out = build_pipeline_b_batches(visits, as_of=date(2026, 7, 7), override=False)
|
||||
members = {b.member_id for b in out}
|
||||
assert "OLD" not in members
|
||||
assert "NEW" in members
|
||||
|
||||
|
||||
def test_override_relaxes_timely_filing():
|
||||
visits = [_v(date(2026, 1, 1), "OLD", "T1019", "2.32")]
|
||||
out_default = build_pipeline_b_batches(visits, as_of=date(2026, 7, 7), override=False)
|
||||
out_overridden = build_pipeline_b_batches(visits, as_of=date(2026, 7, 7), override=True)
|
||||
assert out_default == []
|
||||
assert len(out_overridden) == 1
|
||||
|
||||
|
||||
def test_override_flag_set_on_past_window_visit_batch():
|
||||
# Same member "OLD" with one past-window visit (187 days old, ISO
|
||||
# week 1) and one within-window visit (10 days old, ISO week 26).
|
||||
# With override=True the past-window visit survives and the batch
|
||||
# for week 1 must be flagged has_overridden_visits=True because the
|
||||
# override saved an otherwise-excluded visit.
|
||||
visits = [
|
||||
_v(date(2026, 1, 1), "OLD", "T1019", "2.32"), # 187 days old (past window, W01)
|
||||
_v(date(2026, 6, 27), "OLD", "T1019", "2.32"), # 10 days old (within window, W26)
|
||||
]
|
||||
out_overridden = build_pipeline_b_batches(
|
||||
visits, as_of=date(2026, 7, 7), override=True,
|
||||
)
|
||||
# Two batches: one per (member, ISO-week) — the past-window visit and
|
||||
# the within-window visit land in different weeks.
|
||||
assert len(out_overridden) == 2
|
||||
by_week = {b.iso_week: b for b in out_overridden}
|
||||
assert by_week[1].member_id == "OLD"
|
||||
assert by_week[1].has_overridden_visits is True # the override saved this batch
|
||||
assert by_week[26].member_id == "OLD"
|
||||
assert by_week[26].has_overridden_visits is False # no override needed here
|
||||
|
||||
# With override=False the past-window visit is dropped. The surviving
|
||||
# within-window batch (W26) must NOT carry the override flag, and the
|
||||
# past-window batch (W01) is absent.
|
||||
out_default = build_pipeline_b_batches(
|
||||
visits, as_of=date(2026, 7, 7), override=False,
|
||||
)
|
||||
assert len(out_default) == 1
|
||||
assert out_default[0].member_id == "OLD"
|
||||
assert out_default[0].iso_week == 26
|
||||
assert out_default[0].has_overridden_visits is False
|
||||
|
||||
|
||||
def test_serialize_member_week_batch_emits_one_envelope():
|
||||
"""One 837P envelope per MemberWeekBatch — one CLM + one SV1 + one
|
||||
DTP*472 service date per visit.
|
||||
|
||||
The SP41 plan spec wrote ``DTM*472*`` but the canonical 837P service
|
||||
date segment is ``DTP*472*`` (per :func:`cyclone.parsers.serialize_837.
|
||||
_build_dtp_472` and X12 005010X222A1). This test asserts against the
|
||||
canonical segment name so the batch overload stays consistent with
|
||||
the existing ``serialize_837`` building blocks.
|
||||
"""
|
||||
from cyclone.parsers.serialize_837 import serialize_member_week_batch
|
||||
visits = [
|
||||
_v(date(2026, 6, 23), "J813715", "T1019", "2.32"),
|
||||
_v(date(2026, 6, 25), "J813715", "T1019", "2.32"),
|
||||
]
|
||||
batches = build_pipeline_b_batches(visits, as_of=date(2026, 7, 7), override=False)
|
||||
assert len(batches) == 1
|
||||
body = serialize_member_week_batch(batches[0])
|
||||
text = body.decode("utf-8", errors="ignore") if isinstance(body, bytes) else body
|
||||
# CLM* segment appears twice (once per visit)
|
||||
assert text.count("CLM*") == 2
|
||||
# SV1* appears once per visit (one service line per claim)
|
||||
assert text.count("SV1*") == 2
|
||||
# DTP*472* service-date segment appears twice (canonical 837P segment name)
|
||||
assert text.count("DTP*472*") == 2
|
||||
# Single envelope (single ISA / single IEA), not per-visit envelopes
|
||||
assert text.count("ISA*") == 1
|
||||
assert text.count("IEA*") == 1
|
||||
# Deterministic per-visit claim_id pattern (member_id + date + 1-based idx)
|
||||
assert "MW-J813715-2026-06-23-01" in text
|
||||
assert "MW-J813715-2026-06-25-02" in text
|
||||
|
||||
|
||||
def test_serialize_member_week_batch_return_type_is_bytes():
|
||||
"""Task 14 spec: ``serialize_member_week_batch`` returns ``bytes``
|
||||
(the existing ``serialize_837`` returns ``str``; this overload
|
||||
diverges intentionally so callers can write the file directly)."""
|
||||
from cyclone.parsers.serialize_837 import serialize_member_week_batch
|
||||
visits = [_v(date(2026, 6, 23), "J813715", "T1019", "2.32")]
|
||||
batches = build_pipeline_b_batches(visits, as_of=date(2026, 7, 7), override=False)
|
||||
assert len(batches) == 1
|
||||
body = serialize_member_week_batch(batches[0])
|
||||
assert isinstance(body, bytes)
|
||||
@@ -0,0 +1,167 @@
|
||||
"""999-ack dump from Gainwell for Mar–Jun 2026.
|
||||
|
||||
Reconciles pulled 999 acks against the in-window 4,509 NOT_IN_835 visits.
|
||||
Visits that are 999-rejected go in one bucket; visits that simply
|
||||
never made it to submission go in another.
|
||||
|
||||
These tests exercise the pure-function surface of
|
||||
``cyclone.rebill.pull_999_acks`` (no SFTP, no DB). The
|
||||
``pull_and_classify`` orchestrator wraps the existing
|
||||
``Scheduler.process_inbound_files`` machinery and is integration-
|
||||
covered by the existing ``test_api_pull_inbound.py`` / CLI smoke
|
||||
tests — adding a new test here would just duplicate that coverage
|
||||
and require a live SFTP server.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
from cyclone.rebill.pull_999_acks import (
|
||||
Bucket,
|
||||
PullResult,
|
||||
classify_not_in_835_visits,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Core: split by 999-rejection presence
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_classify_splits_by_999_rejection_presence() -> None:
|
||||
visits = [
|
||||
("J813715", date(2026, 6, 27), "T1019"),
|
||||
("OTHER", date(2026, 6, 27), "T1019"),
|
||||
]
|
||||
nine99_rejected = {("J813715", date(2026, 6, 27), "T1019")}
|
||||
out = classify_not_in_835_visits(visits, nine99_rejected)
|
||||
assert out["J813715"].value == Bucket.REJECTED_AT_999.value
|
||||
assert out["OTHER"].value == Bucket.NEVER_SUBMITTED.value
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Edge cases
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_classify_empty_input_returns_empty_dict() -> None:
|
||||
"""No visits → empty bucket map (no-op)."""
|
||||
out = classify_not_in_835_visits([], set())
|
||||
assert out == {}
|
||||
# Also: empty visits + non-empty rejection set stays empty.
|
||||
out2 = classify_not_in_835_visits(
|
||||
[], {("J813715", date(2026, 6, 27), "T1019")},
|
||||
)
|
||||
assert out2 == {}
|
||||
|
||||
|
||||
def test_classify_all_rejected() -> None:
|
||||
"""Every visit is in the 999-rejection set → every bucket is REJECTED_AT_999."""
|
||||
v1 = ("MEM001", date(2026, 3, 15), "T1019")
|
||||
v2 = ("MEM002", date(2026, 4, 1), "T1019")
|
||||
out = classify_not_in_835_visits([v1, v2], {v1, v2})
|
||||
assert out == {
|
||||
"MEM001": Bucket.REJECTED_AT_999,
|
||||
"MEM002": Bucket.REJECTED_AT_999,
|
||||
}
|
||||
|
||||
|
||||
def test_classify_all_never_submitted() -> None:
|
||||
"""Visits present, rejection set empty → every bucket is NEVER_SUBMITTED."""
|
||||
visits = [
|
||||
("G1", date(2026, 3, 1), "T1019"),
|
||||
("G2", date(2026, 3, 2), "T1019"),
|
||||
("G3", date(2026, 3, 3), "T1019"),
|
||||
]
|
||||
out = classify_not_in_835_visits(visits, set())
|
||||
assert out == {m: Bucket.NEVER_SUBMITTED for m in ("G1", "G2", "G3")}
|
||||
|
||||
|
||||
def test_classify_keyed_by_member_id() -> None:
|
||||
"""Output is a dict keyed by member_id, not by the visit tuple.
|
||||
|
||||
The spec's contract is "keyed on member_id" — Pipeline B groups
|
||||
by member for the ISO-week rebill, so the classification map
|
||||
collapses to one entry per member. The visit tuple's procedure
|
||||
and dos parts are the *match key* against the 999 rejection set,
|
||||
not the *output key*.
|
||||
|
||||
When two visits for the same member resolve to different
|
||||
buckets (one rejected, one not), the dict-construction order
|
||||
means the *last* visit wins. This test pins that semantic —
|
||||
Pipeline B re-resolves per-visit at the next layer so the
|
||||
member-level bucket is just a coarse pre-filter.
|
||||
"""
|
||||
visits = [
|
||||
("SHARED", date(2026, 5, 1), "T1019"),
|
||||
("SHARED", date(2026, 5, 8), "T1019"),
|
||||
]
|
||||
# First visit IS in the rejected set, second is not.
|
||||
nine99_rejected = {("SHARED", date(2026, 5, 1), "T1019")}
|
||||
out = classify_not_in_835_visits(visits, nine99_rejected)
|
||||
# Last-write-wins: the second visit is NEVER_SUBMITTED, so the
|
||||
# member-level bucket ends up as NEVER_SUBMITTED. This is the
|
||||
# documented coarse-filter semantic — Pipeline B does per-visit
|
||||
# re-resolution downstream.
|
||||
assert out == {"SHARED": Bucket.NEVER_SUBMITTED}
|
||||
|
||||
# Reverse the rejection set: only the second visit is rejected.
|
||||
# Last visit wins → REJECTED_AT_999.
|
||||
nine99_rejected = {("SHARED", date(2026, 5, 8), "T1019")}
|
||||
out = classify_not_in_835_visits(visits, nine99_rejected)
|
||||
assert out == {"SHARED": Bucket.REJECTED_AT_999}
|
||||
|
||||
|
||||
def test_classify_distinct_members_dont_collide() -> None:
|
||||
"""Two distinct members, only one rejected → independent bucket entries."""
|
||||
visits = [
|
||||
("ALICE", date(2026, 6, 1), "T1019"),
|
||||
("BOB", date(2026, 6, 1), "T1019"),
|
||||
]
|
||||
nine99_rejected = {("ALICE", date(2026, 6, 1), "T1019")}
|
||||
out = classify_not_in_835_visits(visits, nine99_rejected)
|
||||
assert out == {
|
||||
"ALICE": Bucket.REJECTED_AT_999,
|
||||
"BOB": Bucket.NEVER_SUBMITTED,
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# PullResult shape — guards against accidental field drift
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_pull_result_is_frozen_dataclass() -> None:
|
||||
"""``PullResult`` must be frozen so callers can't mutate the summary."""
|
||||
pr = PullResult(
|
||||
total_pulled=10,
|
||||
rejected_at_999=3,
|
||||
not_in_835=4,
|
||||
rejected_breakdown={"R": 2, "E": 1},
|
||||
)
|
||||
assert pr.total_pulled == 10
|
||||
assert pr.rejected_at_999 == 3
|
||||
assert pr.not_in_835 == 4
|
||||
assert pr.rejected_breakdown == {"R": 2, "E": 1}
|
||||
# Frozen: assignment must raise.
|
||||
import dataclasses
|
||||
try:
|
||||
pr.total_pulled = 99 # type: ignore[misc]
|
||||
except dataclasses.FrozenInstanceError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("PullResult must be frozen")
|
||||
|
||||
|
||||
def test_bucket_values_are_json_friendly_strings() -> None:
|
||||
"""Bucket values serialize cleanly to JSON (string-enum contract)."""
|
||||
import json
|
||||
payload = {
|
||||
"j1": Bucket.REJECTED_AT_999.value,
|
||||
"j2": Bucket.NEVER_SUBMITTED.value,
|
||||
}
|
||||
# Round-trip — no enum leakage into the JSON output.
|
||||
assert json.loads(json.dumps(payload)) == {
|
||||
"j1": "REJECTED_AT_999",
|
||||
"j2": "NEVER_SUBMITTED",
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user