Powered by AppSignal & Oban Pro

field_log testing

_practice/field_log_testing.livemd

field_log testing

Section

changes = %{}

record =
  {:ok,
   %{
     id: 1,
     name: "Zsolt Kreisz",
     username: "ZSK",
     password: "éfnawédklwa",
     email: "zskreisz@hotmail.hu",
     phone: "231231231",
     deleted_at: nil,
     inserted_at: ~U[2024-12-28 22:30:58Z],
     updated_at: ~U[2024-12-29 11:52:03Z]
   }}

action = :update
# with one actual change
changes = %{"name" => {"old value: Zsolt Kreisz5", "new_value: Zsolt Kreisz"}}
defmodule Practice1 do
  def create_change_log(changes, record, action) do
    IO.puts("Changes: #{inspect(changes)}")
    IO.puts("Record: #{inspect(record)}")
    IO.puts("Action: #{inspect(action)}")
    # case changes do
    # %{} -> %{}
    # _ ->
    attrs =
      Enum.map(changes, fn {key, value} ->
        {:ok, new_record} = record
        IO.puts("key in map: #{inspect(key)}")
        IO.puts("within enum map #{inspect(value)}")
        {old_val, new_val} = value

        %{
          table_name: "users",
          row_id: Map.get(new_record, :id),
          action: action,
          # field_name: Map.keys(changes),
          old_value: old_val,
          new_value: new_val,
          time_of_change: DateTime.utc_now(),
          changed_by: "system"
        }
      end)

    # end
    attrs
    |> IO.inspect()

    # %UserFieldLog{}
    # |> UserFieldLog.changeset(attrs)
    # |> IO.inspect()
    # |> Repo.insert()
  end
end
changes
|> Practice1.create_change_log(record, action)