Current section
Files
Jump to
Current section
Files
lib/invoice/invoice_log.ex
defmodule StarkBank.Invoice.Log do
alias __MODULE__, as: Log
alias StarkBank.Utils.Rest
alias StarkBank.Utils.Check
alias StarkBank.Utils.API
alias StarkBank.Invoice
alias StarkBank.User.Project
alias StarkBank.User.Organization
alias StarkBank.Error
@moduledoc """
Groups Invoice.Log related functions
"""
@doc """
Every time a Invoice entity is updated, a corresponding Invoice.Log
is generated for the entity. This log is never generated by the
user, but it can be retrieved to check additional information
on the Invoice.
## Attributes:
- `:id` [string]: unique id returned when the log is created. ex: "5656565656565656"
- `:invoice` [Invoice]: Invoice entity to which the log refers to.
- `:errors` [list of strings]: list of errors linked to this Invoice event
- `:type` [string]: type of the Invoice event which triggered the log creation. ex: "created", "paid", "canceled" or "overdue"
- `:created` [DateTime]: creation datetime for the log. ex: ~U[2020-03-26 19:32:35.418698Z]
"""
@enforce_keys [:id, :invoice, :errors, :type, :created]
defstruct [:id, :invoice, :errors, :type, :created]
@type t() :: %__MODULE__{}
@doc """
Receive a single Log struct previously created by the Stark Bank API by passing its id
## Parameters (required):
- `id` [string]: struct unique id. ex: "5656565656565656"
## Options:
- `:user` [Organization/Project, default nil]: Organization or Project struct returned from StarkBank.project(). Only necessary if default project or organization has not been set in configs.
## Return:
- Log struct with updated attributes
"""
@spec get(binary, user: Project.t() | Organization.t() | nil) :: {:ok, Log.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() | Organization.t() | nil) :: Log.t()
def get!(id, options \\ []) do
Rest.get_id!(resource(), id, options)
end
@doc """
Receive a stream of Log structs previously created in the Stark Bank API
## Options:
- `:limit` [integer, default nil]: maximum number of structs to be retrieved. Unlimited if nil. ex: 35
- `: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]
- `:types` [list of strings, default nil]: filter for log event types. ex: "created", "paid", "canceled" or "overdue"
- `:invoice_ids` [list of strings, default nil]: list of Invoice ids to filter logs. ex: ["5656565656565656", "4545454545454545"]
- `:user` [Organization/Project, default nil]: Organization or Project struct returned from StarkBank.project(). Only necessary if default project or organization has not been set in configs.
## Return:
- stream of Log structs with updated attributes
"""
@spec query(
limit: integer,
after: Date.t() | binary,
before: Date.t() | binary,
types: [binary],
invoice_ids: [binary],
user: Project.t() | Organization.t()
) ::
({:cont, {:ok, [Log.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,
after: Date.t() | binary,
before: Date.t() | binary,
types: [binary],
invoice_ids: [binary],
user: Project.t() | Organization.t()
) ::
({:cont, [Log.t()]} | {:halt, any} | {:suspend, any}, any -> any)
def query!(options \\ []) do
Rest.get_list!(resource(), options)
end
@doc """
Receive a list of up to 100 Invoice.Log objects previously created in the Stark Bank API and the cursor to the next page.
Use this function instead of query if you want to manually page your requests.
## Options:
- `:cursor` [string, default nil]: cursor returned on the previous page function call
- `:limit` [integer, default nil]: maximum number of structs to be retrieved. Unlimited if nil. ex: 35
- `: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]
- `:types` [list of strings, default nil]: filter for log event types. ex: "created", "paid", "canceled" or "overdue"
- `:invoice_ids` [list of strings, default nil]: list of Invoice ids to filter logs. ex: ["5656565656565656", "4545454545454545"]
- `:user` [Organization/Project, default nil]: Organization or Project struct returned from StarkBank.project(). Only necessary if default project or organization has not been set in configs.
## Return:
- list of Invoice.Log structs with updated attributes and cursor to retrieve the next page of Invoice.Log objects
"""
@spec page(
cursor: binary,
limit: integer,
after: Date.t() | binary,
before: Date.t() | binary,
types: [binary],
invoice_ids: [binary],
user: Project.t() | Organization.t()
) ::
{:ok, {binary, [Log.t()]}} | {:error, [%Error{}]}
def page(options \\ []) do
Rest.get_page(resource(), options)
end
@doc """
Same as page(), but it will unwrap the error tuple and raise in case of errors.
"""
@spec page!(
cursor: binary,
limit: integer,
after: Date.t() | binary,
before: Date.t() | binary,
types: [binary],
invoice_ids: [binary],
user: Project.t() | Organization.t()
) ::
[Log.t()]
def page!(options \\ []) do
Rest.get_page!(resource(), options)
end
@doc """
Receive a single Invoice.Log pdf file generated in the Stark Bank API by passing its id.
## Parameters (required):
- `:id` [string]: struct unique id. ex: "5656565656565656"
## Options:
- `:user` [Organization/Project, default nil]: Organization or Project struct returned from StarkBank.project(). Only necessary if default project or organization has not been set in configs.
## Return:
- Invoice.Log pdf file content
"""
@spec pdf(binary, user: Project.t() | Organization.t() | nil) :: {:ok, binary} | {:error, [%Error{}]}
def pdf(id, options \\ []) do
Rest.get_content(resource(), id, "pdf", options |> Keyword.delete(:user), options[:user])
end
@doc """
Same as pdf(), but it will unwrap the error tuple and raise in case of errors.
"""
@spec pdf!(binary, user: Project.t() | Organization.t() | nil) :: binary
def pdf!(id, options \\ []) do
Rest.get_content!(resource(), id, "pdf", options |> Keyword.delete(:user), options[:user])
end
@doc false
def resource() do
{
"InvoiceLog",
&resource_maker/1
}
end
@doc false
def resource_maker(json) do
%Log{
id: json[:id],
invoice: json[:invoice] |> API.from_api_json(&Invoice.resource_maker/1),
created: json[:created] |> Check.datetime(),
type: json[:type],
errors: json[:errors]
}
end
end