# Offline synthetic-response reading exercise; accepts an already decoded map. # Not a JSON decoder, schema validator or SDK. No dependency installation needed. defmodule EntityResponseReading do def summarise(%{"data" => data, "receipt" => receipt}) do outcome = Map.fetch!(data, "outcome") units = Map.fetch!(receipt, "charged_units") recovery = Map.fetch!(data, "recovery") unless outcome in ["established", "ambiguous", "no_match", "insufficient_evidence"], do: raise(ArgumentError, "Unknown outcome: stop interpretation and check the contract") unless is_binary(units) and Regex.match?(~r/\A(?:0|[1-9][0-9]*)\z/, units), do: raise(ArgumentError, "Atomic units must remain canonical decimal strings") unless Map.fetch!(recovery, "automatic_retry_allowed") === false, do: raise(ArgumentError, "This exercise cannot authorise an automatic retry") %{ "synthetic" => true, "outcome" => outcome, "entity_id" => if(outcome == "established", do: get_in(data, ["entity", "subject", "entity_id"]), else: nil), "identity_assessment" => Map.fetch!(data, "identity_assessment"), "recovery" => recovery, "receipt" => Map.take(receipt, ["receipt_id", "operation_id", "charged_units", "unit", "unit_scale", "settlement"]) } end end