Current section

Files

Jump to
starkbank lib dict_key dict_key.ex
Raw

lib/dict_key/dict_key.ex

defmodule StarkBank.DictKey do
alias __MODULE__, as: DictKey
alias StarkBank.Utils.Rest
alias StarkBank.Utils.Check
alias StarkBank.User.Project
alias StarkBank.Error
@moduledoc """
Groups DictKey related functions
"""
@doc """
DictKey represents a PIX key registered in Bacen's DICT system.
## Parameters (optional):
- `:id` [string]: DictKey object unique id and PIX key itself. ex: "tony@starkbank.com", "722.461.430-04", "20.018.183/0001-80", "+5511988887777", "b6295ee1-f054-47d1-9e90-ee57b74f60d9"
## Attributes (return-only):
- `:type` [string, default nil]: DICT key type. ex: "email", "cpf", "cnpj", "phone" or "evp"
- `:name` [string, default nil]: key owner full name. ex: "Tony Stark"
- `:tax_id` [string, default nil]: key owner tax ID (CNPJ or masked CPF). ex: "***.345.678-**" or "20.018.183/0001-80"
- `:owner_type` [string, default nil]: DICT key owner type. ex "naturalPerson" or "legalPerson"
- `:ispb` [string, default nil]: bank ISPB associated with the DICT key. ex: "20018183"
- `:branch_code` [string, default nil]: bank account branch code associated with the DICT key. ex: "9585"
- `:account_number` [string, default nil]: bank account number associated with the DICT key. ex: "9828282578010513"
- `:account_type` [string, default nil]: bank account type associated with the DICT key. ex: "checking", "saving" e "salary"
- `:status` [string, default nil]: current DICT key status. ex: "created", "registered", "canceled" or "failed"
- `:account_created` [datetime.datetime, default nil]: creation datetime of the bank account associated with the DICT key. ex: datetime.date(2020, 1, 12, 11, 14, 8)
- `:owned` [DateTime, default null]: datetime since when the current owner hold this DICT key. ex: ~U[2020-11-26 17:31:45.482618Z]
- `:created` [DateTime, default null]: creation datetime for the DICT key. ex: ~U[2020-11-26 17:31:45.482618Z]
"""
defstruct [
:id,
:type,
:name,
:tax_id,
:owner_type,
:ispb,
:branch_code,
:account_number,
:account_type,
:status,
:account_created,
:owned,
:created
]
@type t() :: %__MODULE__{}
@doc """
Receive a single DictKey struct by passing its id
## Parameters (required):
- `:id` [string]: DictKey object unique id and PIX key itself. ex: "tony@starkbank.com", "722.461.430-04", "20.018.183/0001-80", "+5511988887777", "b6295ee1-f054-47d1-9e90-ee57b74f60d9"
## Options:
- `:user` [Project]: Project struct returned from StarkBank.project(). Only necessary if default project has not been set in configs.
## Return:
- DictKey struct with updated attributes
"""
@spec get(binary, user: Project.t() | nil) :: {:ok, DictKey.t()} | {:error, [%Error{}]}
def get(id, options \\ []) do
Rest.get_id(resource(), id, options)
end
@doc """
Same as get(), but it will unwrap the error tuple and raise in case of errors.
"""
@spec get!(binary, user: Project.t() | nil) :: DictKey.t()
def get!(id, options \\ []) do
Rest.get_id!(resource(), id, options)
end
@doc """
Receive a stream of DictKey structs associated with your Stark Bank Workspace
## Options:
- `:limit` [integer, default nil]: maximum number of structs to be retrieved. Unlimited if nil. ex: 35
- `:type` [string, default nil]: DictKey type. ex: "cpf", "cnpj", "phone", "email" or "evp"
- `:after` [Date or string, default nil]: date filter for structs created only after specified date. ex: ~D[2020-03-25]
- `:before` [Date or string, default nil]: date filter for structs created only before specified date. ex: ~D[2020-03-25]
- `:ids` [list of strings, default null]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"]
- `:status` [string, default nil]: filter for status of retrieved structs. ex: "registered"
- `:user` [Project]: Project struct returned from StarkBank.project(). Only necessary if default project has not been set in configs.
## Return:
- stream of DictKey structs with updated attributes
"""
@spec query(
limit: integer,
type: binary,
after: Date.t() | binary,
before: Date.t() | binary,
ids: [binary],
status: binary,
user: Project.t()
) ::
({:cont, {:ok, [DictKey.t()]}}
| {:error, [Error.t()]}
| {:halt, any}
| {:suspend, any},
any ->
any)
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!(
limit: integer,
type: binary,
after: Date.t() | binary,
before: Date.t() | binary,
ids: [binary],
status: binary,
user: Project.t()
) ::
({:cont, [DictKey.t()]} | {:halt, any} | {:suspend, any}, any -> any)
def query!(options \\ []) do
Rest.get_list!(resource(), options)
end
@doc false
def resource() do
{
"DictKey",
&resource_maker/1
}
end
@doc false
def resource_maker(json) do
%DictKey{
id: json[:id],
type: json[:type],
name: json[:name],
tax_id: json[:tax_id],
owner_type: json[:owner_type],
ispb: json[:ispb],
branch_code: json[:branch_code],
account_number: json[:account_number],
account_type: json[:account_type],
status: json[:status],
account_created: json[:account_created] |> Check.datetime(),
owned: json[:owned] |> Check.datetime(),
created: json[:created] |> Check.datetime()
}
end
end