defmodule DaProductApp.YspNotificationLog do use Ecto.Schema import Ecto.Changeset alias DaProductApp.Transactions.Transaction @primary_key {:id, :id, autogenerate: true} schema "ysp_notification_logs" do field :payment_id, :string field :notification_url, :string field :payload, :map field :attempt_number, :integer, default: 1 field :max_attempts, :integer, default: 3 field :status, Ecto.Enum, values: [:pending, :success, :failed, :max_attempts_reached], default: :pending field :http_status_code, :integer field :response_body, :string field :error_message, :string field :next_retry_at, :utc_datetime field :completed_at, :utc_datetime belongs_to :transaction, Transaction timestamps() end @doc false def changeset(notification_log, attrs) do notification_log |> cast(attrs, [ :transaction_id, :payment_id, :notification_url, :payload, :attempt_number, :max_attempts, :status, :http_status_code, :response_body, :error_message, :next_retry_at, :completed_at ]) |> validate_required([:transaction_id, :notification_url, :payload]) |> validate_inclusion(:status, [:pending, :success, :failed, :max_attempts_reached]) |> validate_number(:attempt_number, greater_than: 0) |> validate_number(:max_attempts, greater_than: 0) end end