defmodule Mix.Tasks.SeedTestData do @moduledoc "Seeds the database with test data for YSP transaction processing" use Mix.Task alias DaProductApp.Repo alias DaProductApp.Acquirer.Schemas.{AcquirerTerminal, AcquirerTerminalStan} @shortdoc "Seeds database with YSP test data" def run(_) do Mix.Task.run("app.start") IO.puts("🌱 Starting database seeding for YSP testing...") seed_terminals() seed_stan_management() IO.puts("āœ… Database seeding completed!") verify_data() end defp seed_terminals do IO.puts("šŸ“Ÿ Creating acquirer terminal configurations...") now = DateTime.utc_now() |> DateTime.truncate(:second) terminals = [ # YSP Test Terminal (matches the one from your log: 12345673) %{ tid: "12345673", mid: "123456789012345", acquirer_id: 1, terminal_name: "YSP Test Terminal 1", terminal_type: "POS", terminal_location: "Test Location 1", currency_code: "840", # USD country_code: "USA", mcc_code: "5411", # Grocery stores nii: "782", # YSP Network Identification Number fiid: "12345678901", application_type: "01", current_batch_no: "000001", batch_date: Date.to_string(Date.utc_today()), batch_time: Time.to_string(Time.utc_now()), max_batch_size: 1000, status: "ACTIVE", last_settlement_date: Date.to_string(Date.utc_today()) }, # Additional test terminals # Additional test terminals %{ tid: "11111111", mid: "123456789012345", acquirer_id: 1, terminal_name: "YSP Test Terminal 2", terminal_type: "POS", terminal_location: "Test Location 2", currency_code: "840", country_code: "USA", mcc_code: "5411", nii: "782", # YSP Network Identification Number fiid: "12345678901", application_type: "01", current_batch_no: "000001", batch_date: Date.to_string(Date.utc_today()), batch_time: Time.to_string(Time.utc_now()), max_batch_size: 1000, status: "ACTIVE", last_settlement_date: Date.to_string(Date.utc_today()) }, %{ tid: "22222222", mid: "123456789012345", acquirer_id: 1, terminal_name: "YSP Test Terminal 3", terminal_type: "POS", terminal_location: "Test Location 3", currency_code: "840", country_code: "USA", mcc_code: "5411", nii: "782", # YSP Network Identification Number fiid: "12345678901", application_type: "01", current_batch_no: "000001", batch_date: Date.to_string(Date.utc_today()), batch_time: Time.to_string(Time.utc_now()), max_batch_size: 1000, status: "ACTIVE", last_settlement_date: Date.to_string(Date.utc_today()) } ] Enum.each(terminals, fn terminal_attrs -> case Repo.get_by(AcquirerTerminal, tid: terminal_attrs.tid) do nil -> case %AcquirerTerminal{} |> AcquirerTerminal.changeset(terminal_attrs) |> Repo.insert() do {:ok, terminal} -> IO.puts(" āœ“ Created terminal: #{terminal.tid} (#{terminal.terminal_name})") {:error, changeset} -> IO.puts(" āœ— Failed to create terminal #{terminal_attrs.tid}: #{inspect(changeset.errors)}") end existing -> IO.puts(" ⚠ Terminal #{existing.tid} already exists, skipping...") end end) end defp seed_stan_management do IO.puts("šŸ”¢ Creating STAN management records...") terminals = ["12345673", "11111111", "22222222"] now = DateTime.utc_now() |> DateTime.truncate(:second) today = Date.to_string(Date.utc_today()) Enum.each(terminals, fn tid -> stan_attrs = %{ tid: tid, acquirer_id: 1, current_stan: "000001", stan_date: today, last_used_stan: "000000", last_reset_date: today, last_reset_time: "000000", max_stan_value: 999999, auto_reset_daily: true, reset_time: "0000", daily_transaction_count: 0, total_transaction_count: 0, last_transaction_date: today, last_transaction_time: "000000", rollover_count: 0, recovery_mode: false, duplicate_stan_count: 0, status: "ACTIVE" } case Repo.get_by(AcquirerTerminalStan, tid: tid) do nil -> case %AcquirerTerminalStan{} |> AcquirerTerminalStan.changeset(stan_attrs) |> Repo.insert() do {:ok, stan} -> IO.puts(" āœ“ Created STAN management for terminal: #{stan.tid}") {:error, changeset} -> IO.puts(" āœ— Failed to create STAN for terminal #{tid}: #{inspect(changeset.errors)}") end existing -> IO.puts(" ⚠ STAN management for terminal #{existing.tid} already exists, skipping...") end end) end defp verify_data do IO.puts("\nšŸ” Verifying seeded data...") terminal_count = Repo.aggregate(AcquirerTerminal, :count, :tid) stan_count = Repo.aggregate(AcquirerTerminalStan, :count, :tid) IO.puts(" šŸ“Ÿ Acquirer Terminals: #{terminal_count}") IO.puts(" šŸ”¢ STAN Management Records: #{stan_count}") # Check the specific terminal from your log case Repo.get_by(AcquirerTerminal, tid: "12345673") do %AcquirerTerminal{} = terminal -> IO.puts(" āœ“ Test terminal 12345673 is configured and ready") IO.puts(" - MID: #{terminal.mid}") IO.puts(" - NII: #{terminal.nii}") IO.puts(" - Status: #{terminal.status}") nil -> IO.puts(" āœ— Test terminal 12345673 not found!") end IO.puts("\nšŸŽ‰ Your database is now ready for transaction testing!") IO.puts("You can test with terminal ID: 12345673") end end