# Test creating a new risk hit to verify automatic action history initialization # Run with: mix run test_new_risk_hit.exs alias PlatformCore.Repo alias RiskCore.Context alias RiskCore.RiskRule import Ecto.Query IO.puts("\n๐Ÿงช Testing automatic action history initialization for new risk hits...\n") # Find an active risk rule to use rule = Repo.one( from r in RiskRule, where: r.enabled == true, limit: 1 ) if rule do IO.puts("Step 1: Creating a new risk rule hit...") IO.puts(" Using Rule: #{rule.name}") # Create a new risk hit with test data attrs = %{ transaction_id: 99999, transaction_type: "QR", # Must be QR or POS merchant_id: "TEST_MID_001", category: "Test Category", status: "Hold", triggered_at: DateTime.utc_now(), rule_id: rule.id, metadata: %{ test_data: true, amount: "1000.00" } } case Context.create_risk_rule_hit(attrs) do {:ok, new_hit} -> IO.puts("โœ… Risk hit created successfully!") IO.puts(" ID: #{new_hit.id}") IO.puts(" Status: #{new_hit.status}") # Check the action history IO.puts("\nStep 2: Checking action history...") action_history = Context.get_hit_action_history(new_hit.id) if length(action_history) > 0 do IO.puts("โœ… Action history initialized with #{length(action_history)} action(s):\n") Enum.each(action_history, fn action -> timestamp_str = if action.timestamp do Calendar.strftime(action.timestamp, "%Y-%m-%d %H:%M:%S") else "N/A" end IO.puts(" ๐Ÿ“ #{timestamp_str}") IO.puts(" Action: #{action.action}") IO.puts(" Status: #{action.from_status || "N/A"} โ†’ #{action.to_status}") IO.puts(" By: #{action.supervisor_name}") if action.notes, do: IO.puts(" Notes: #{action.notes}") IO.puts("") end) IO.puts("โœ… SUCCESS: New risk hits automatically get 'Created' system action!") else IO.puts("โŒ FAILED: No action history found in newly created hit") end # Clean up - delete the test hit IO.puts("\nStep 3: Cleaning up test data...") Repo.delete(new_hit) IO.puts("โœ… Test data cleaned up\n") {:error, changeset} -> IO.puts("โŒ Failed to create risk hit:") IO.inspect(changeset.errors) end else IO.puts("โŒ No active risk rules found. Please create a rule first.\n") end