lib/pix_domain/pix_domain.ex

defmodule StarkInfra.PixDomain do
  alias __MODULE__, as: PixDomain
  alias StarkInfra.Utils.API
  alias StarkInfra.Utils.Rest
  alias StarkInfra.User.Project
  alias StarkInfra.User.Organization
  alias StarkInfra.Error
  alias StarkInfra.PixDomain.Certificate

  @moduledoc """
  Groups PixDomain related functions
  """

  @doc """
  The PixDomain struct displays the QR Code domain certificate information of Pix participants.
  All certificates must be registered with the Central Bank.

  ## Attributes (return-only):
    - certificates [list of PixDomain.Certificate]: certificate information of the Pix participant.
    - name [string]: current active domain (URL) of the Pix participant.
  """
  @enforce_keys [
    :certificates,
    :name
  ]
  defstruct [
    :certificates,
    :name
  ]

  @type t() :: %__MODULE__{}

  @doc """
  Receive a stream of PixDomain objects.

  ## Options:
    - `:user` [Organization/Project, default nil]: Organization or Project struct returned from StarkInfra.project(). Only necessary if default project or organization has not been set in configs.

  ## Return:
    - stream of PixDomain structs with updated attributes
  """
  @spec query(
    user: Organization.t() | Project.t() | nil
  ) ::
    {:ok, [PixDomain.t()]} |
    {:error, Error.t()}
  def query(options \\ []) do
    Rest.get_list(
      resource(),
      options
    )
  end

  @doc """
  Same as query(), but it will unwrap the error tuple and raise in case of errors.
  """
  @spec query!(
    user: Organization.t() | Project.t() | nil
  ) :: any
  def query!(options \\ []) do
    Rest.get_list!(
      resource(),
      options
    )
  end

  @doc false
  def resource() do
    {
      "PixDomain",
      &resource_maker/1
    }
  end

  @doc false
  def resource_maker(json) do
    %PixDomain{
      certificates: json[:certificates] |> Enum.map(fn invoice -> API.from_api_json(invoice, &Certificate.resource_maker/1) end),
      name: json[:name]
    }
  end
end