Current section

Files

Jump to
starkbank lib ledger balance.ex
Raw

lib/ledger/balance.ex

defmodule StarkBank.Balance do
alias __MODULE__, as: Balance
alias StarkBank.Utils.Rest, as: Rest
alias StarkBank.Utils.Checks, as: Checks
alias StarkBank.User.Project, as: Project
alias StarkBank.Error, as: Error
@moduledoc """
Groups Balance related functions
# Balance struct:
The Balance struct displays the current balance of the workspace,
which is the result of the sum of all transactions within this
workspace. The balance is never generated by the user, but it
can be retrieved to see the information available.
## Attributes (return-only):
- id [string, default nil]: unique id returned when Boleto is created. ex: "5656565656565656"
- amount [integer, default nil]: current balance amount of the workspace in cents. ex: 200 (= R$ 2.00)
- currency [string, default nil]: currency of the current workspace. Expect others to be added eventually. ex: "BRL"
- updated [DateTime, default nil]: update datetime for the balance. ex: ~U[2020-03-26 19:32:35.418698Z]
"""
defstruct [:id, :amount, :currency, :updated]
@type t() :: %__MODULE__{}
@doc """
# Retrieve the Balance entity
Receive the Balance entity linked to your workspace in the Stark Bank API
## Parameters (required):
- user [Project]: Project struct returned from StarkBank.project().
## Return:
- Balance struct with updated attributes
"""
@spec get(Project.t()) :: {:ok, Balance.t()} | {:error, [Error]}
def get(%Project{} = user) do
case Rest.get_list(user, resource()) |> Enum.take(1) do
[{:ok, balance}] -> {:ok, balance}
[{:error, error}] -> {:error, error}
end
end
@doc """
Same as get(), but it will unwrap the error tuple and raise in case of errors.
"""
@spec get!(Project.t()) :: Balance.t()
def get!(%Project{} = user) do
{:ok, balance} = get(user)
balance
end
@doc false
def resource() do
{
"Balance",
&resource_maker/1
}
end
@doc false
def resource_maker(json) do
%Balance{
id: json[:id],
amount: json[:amount],
currency: json[:currency],
updated: json[:updated] |> Checks.check_datetime
}
end
end