lib/responses/instruments/card_details.ex

defmodule Responses.Instruments.CardDetails do
  @moduledoc false
  alias Customers.AccountHolder
  alias Customers.Customer

  @derive Jason.Encoder

  @type t() :: %__MODULE__{
          type: String.t(),
          id: String.t(),
          fingerprint: String.t(),
          expiry_month: integer(),
          expiry_year: integer(),
          name: String.t(),
          scheme: String.t(),
          scheme_local: String.t(),
          last4: String.t(),
          bin: String.t(),
          card_type: String.t(),
          card_category: String.t(),
          issuer: String.t(),
          issuer_country: <<_::2>>,
          product_id: String.t(),
          product_type: String.t(),
          customer: %{
            default: true | false,
            email: String.t(),
            id: String.t(),
            name: String.t()
          },
          account_holder: AccountHolder.t()
        }

  defstruct [
    :type,
    :id,
    :fingerprint,
    :expiry_month,
    :expiry_year,
    :name,
    :scheme,
    :scheme_local,
    :last4,
    :bin,
    :card_type,
    :card_category,
    :issuer,
    :issuer_country,
    :product_id,
    :product_type,
    :customer,
    :account_holder
  ]

  def build(params) when is_map(params) do
    %__MODULE__{
      type: params["type"],
      id: params["id"],
      fingerprint: params["fingerprint"],
      expiry_month: params["expiry_month"],
      expiry_year: params["expiry_year"],
      name: params["name"],
      scheme: params["scheme"],
      scheme_local: params["scheme_local"],
      last4: params["last4"],
      bin: params["bin"],
      card_type: params["card_type"],
      card_category: params["card_category"],
      issuer: params["issuer"],
      issuer_country: params["issuer_country"],
      product_id: params["product_id"],
      product_type: params["product_type"],
      customer: Customer.build(params["customer"]),
      account_holder: AccountHolder.build(params["account_holder"])
    }
  end

  def build(_), do: nil
end