lib/responses/instruments/create_card.ex

defmodule Responses.Instruments.CreateCard do
  alias Customers.Customer

  @derive Jason.Encoder

  @type t :: %__MODULE__{
          type: String.t(),
          id: String.t(),
          fingerprint: String.t(),
          expiry_month: integer(),
          expiry_year: integer(),
          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: String.t(),
          product_id: String.t(),
          product_type: String.t(),
          customer: %{
            id: String.t(),
            email: String.t(),
            name: String.t(),
            phone: %{
              country_code: String.t(),
              number: String.t()
            }
          }
        }

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

  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"],
      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"])
    }
  end

  def build(_), do: nil
end