63 lines
2.8 KiB
Python
63 lines
2.8 KiB
Python
"""Translate a frozen-delivery acknowledgement into a Web task outcome.
|
|
|
|
Call only with a receipt returned by processing_handoff.deliver and the expected
|
|
identity of an accepted source package. This does not validate API/source mapping,
|
|
read a cached receipt as authority, compose an executor, or enable downloading.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from datetime import date
|
|
from typing import Mapping
|
|
|
|
from arr_web.arr_downloads import DownloadOutcome
|
|
from arr_web.contracts import validate_job_id
|
|
|
|
|
|
def outcome_from_handoff(
|
|
receipt: Mapping[str, object], *, job_id: str, report_date: date,
|
|
manifest_sha256: str, expected_version: str = "arr-frozen-xml-handoff/v1",
|
|
) -> DownloadOutcome:
|
|
"""Reject mismatched/ambiguous acknowledgements; never default to success."""
|
|
validate_job_id(job_id)
|
|
if type(report_date) is not date or not isinstance(manifest_sha256, str) or not re.fullmatch(
|
|
r"[0-9a-f]{64}", manifest_sha256
|
|
):
|
|
raise ValueError("invalid expected handoff identity")
|
|
if not isinstance(receipt, Mapping) or any((
|
|
receipt.get("version") != expected_version,
|
|
receipt.get("job_id") != job_id,
|
|
receipt.get("manifest_sha256") != manifest_sha256,
|
|
receipt.get("delivery_id") != f"local-{job_id}-a1",
|
|
)):
|
|
raise ValueError("handoff acknowledgement identity mismatch")
|
|
|
|
status = receipt.get("ingestion_status")
|
|
if not isinstance(status, str) or status not in {"committed", "recorded_review", "recorded_failure"}:
|
|
raise ValueError("unknown handoff acknowledgement status")
|
|
business_date = receipt.get("business_date")
|
|
if business_date != report_date.isoformat() and not (
|
|
status == "recorded_failure" and business_date is None and "business_date" in receipt
|
|
):
|
|
raise ValueError("handoff acknowledgement date mismatch")
|
|
for key in ("daily_version_id", "version_no"):
|
|
if key not in receipt:
|
|
raise ValueError("incomplete handoff acknowledgement")
|
|
value = receipt[key]
|
|
if status == "committed":
|
|
if type(value) is not int or value <= 0:
|
|
raise ValueError("handoff has no acknowledged Finance version")
|
|
elif status == "recorded_failure" and key == "daily_version_id":
|
|
# PostgreSQL retains a rejected audit row, without activating a
|
|
# daily version. In-memory/legacy receipts may have no audit ID.
|
|
if value is not None and (type(value) is not int or value <= 0):
|
|
raise ValueError("invalid rejected-version identity")
|
|
elif value is not None:
|
|
raise ValueError("noncommitted handoff contains a Finance version")
|
|
|
|
return DownloadOutcome(
|
|
{"committed": "succeeded", "recorded_review": "needs_review", "recorded_failure": "failed"}[status],
|
|
job_id=job_id,
|
|
)
|