defmodule DaProductApp.Transactions.TransactionEvent do @moduledoc """ Event record for transaction aggregate with hash chaining. """ use Ecto.Schema import Ecto.Changeset schema "transaction_events" do field :seq, :integer field :event_type, :string field :payload, :map field :prev_hash, :binary field :hash, :binary belongs_to :transaction, DaProductApp.Transactions.Transaction field :inserted_at, :utc_datetime end @required ~w(seq event_type hash inserted_at transaction_id)a @optional ~w(payload prev_hash)a def changeset(struct, attrs) do struct |> cast(attrs, @required ++ @optional) |> validate_required(@required) |> unique_constraint([:transaction_id, :seq]) end end