mirror of
https://github.com/ash-project/ash_double_entry.git
synced 2024-09-21 14:03:18 +12:00
35 lines
1 KiB
Elixir
35 lines
1 KiB
Elixir
|
defmodule AshDoubleEntry.Transfer.Changes.SetAmounts do
|
||
|
use Ash.Resource.Change
|
||
|
|
||
|
def change(changeset, _, _) do
|
||
|
case Ash.Changeset.fetch_argument(changeset, :amount) do
|
||
|
{:ok, amount} when not is_nil(amount) ->
|
||
|
changeset
|
||
|
|> Ash.Changeset.force_change_new_attribute(:to_amount, amount)
|
||
|
|> Ash.Changeset.force_change_new_attribute(:from_amount, amount)
|
||
|
|
||
|
_ ->
|
||
|
cond do
|
||
|
is_nil(Ash.Changeset.get_attribute(changeset, :to_amount)) ->
|
||
|
{:error,
|
||
|
Ash.Error.Changes.Required.exception(
|
||
|
field: :to_amount,
|
||
|
type: :attribute,
|
||
|
resource: changeset.resource
|
||
|
)}
|
||
|
|
||
|
is_nil(Ash.Changeset.get_attribute(changeset, :from_amount)) ->
|
||
|
{:error,
|
||
|
Ash.Error.Changes.Required.exception(
|
||
|
field: :from_amount,
|
||
|
type: :attribute,
|
||
|
resource: changeset.resource
|
||
|
)}
|
||
|
|
||
|
true ->
|
||
|
changeset
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|