fix(api): regenerate raw_999_text in /api/acks/{id} detail response (SP3 P3 follow-up)

The P3 subagent shipped a Download button on /acks that fell back to
'no raw text' because the detail endpoint returned raw_json (the parsed
model dump) but never the regenerated X12.

Fix: detail endpoint now re-serializes raw_json via serialize_999 and
returns it as raw_999_text, so the operator can actually download the
999 file. List endpoint unchanged (keeps payload small).
This commit is contained in:
Tyler
2026-06-20 08:06:21 -06:00
parent fb2a98fc7a
commit f3002e4cbe
2 changed files with 46 additions and 0 deletions
+14
View File
@@ -971,6 +971,20 @@ def get_ack_endpoint(ack_id: int) -> dict:
)
body = _ack_to_ui(row)
body["raw_json"] = row.raw_json
# Regenerate the X12 text from raw_json so the operator can download
# the actual 999 file. (SP3 P3 follow-up: list endpoint doesn't carry
# the regenerated text to keep payloads small; detail does.)
if row.raw_json:
try:
from cyclone.parsers.models_999 import ParseResult999
regenerated = ParseResult999.model_validate(row.raw_json)
icn = regenerated.envelope.control_number or "000000001"
body["raw_999_text"] = serialize_999(regenerated, interchange_control_number=icn)
except Exception as exc: # noqa: BLE001 — never 500 on a regen failure
log.warning("Could not regenerate 999 for ack %s: %s", ack_id, exc)
body["raw_999_text"] = None
else:
body["raw_999_text"] = None
return body