-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(balance): make balance error an embedded schema (#352)
- Loading branch information
Showing
4 changed files
with
97 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
defmodule App.Balance.BalanceError do | ||
@moduledoc """ | ||
Balance errors are instantiated when the balance of a book cannot be computed, | ||
and are used to store the kind of error along with some extra information. | ||
""" | ||
use Ecto.Schema | ||
|
||
@type t :: %__MODULE__{ | ||
kind: atom(), | ||
extra: map(), | ||
uniq_hash: String.t(), | ||
private: map() | ||
} | ||
|
||
@primary_key false | ||
embedded_schema do | ||
field :kind, Ecto.Enum, values: [:missing_revenues] | ||
field :extra, :map | ||
|
||
field :uniq_hash, :string, virtual: true | ||
field :private, :map, virtual: true, default: %{} | ||
end | ||
|
||
@spec new(kind :: atom(), extra :: map()) :: t() | ||
def new(kind, extra) when is_atom(kind) and is_map(extra) do | ||
%__MODULE__{ | ||
kind: kind, | ||
extra: extra, | ||
uniq_hash: uniq_hash(kind, extra) | ||
} | ||
end | ||
|
||
defp uniq_hash(:revenues_missing, extra) do | ||
"revenues_missing_#{extra.member_id}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters