Skip to content

Commit

Permalink
chore: can't update balance atomically, because we don't know the delta
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jun 23, 2024
1 parent 16721bc commit d95a887
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
17 changes: 14 additions & 3 deletions lib/transfer/changes/verify_transfer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ defmodule AshDoubleEntry.Transfer.Changes.VerifyTransfer do
require Ash.Query

def atomic(changeset, opts, context) do
if AshDoubleEntry.Transfer.Info.transfer_destroy_balances?(changeset.resource) do
{:error, "Cannot destroy a transfer atomically if balances must be destroyed manually"}
if AshDoubleEntry.Transfer.Info.transfer_destroy_balances?(changeset.resource) ||
Ash.Changeset.changing_attribute?(changeset, :amount) do
{:error,
"Cannot destroy a transfer atomically if balances must be destroyed manually, or if balance is being updated"}
else
{:ok, change(changeset, opts, context)}
end
Expand Down Expand Up @@ -52,7 +54,16 @@ defmodule AshDoubleEntry.Transfer.Changes.VerifyTransfer do
if changeset.action.type == :destroy do
Money.new!(0, new_amount.currency)
else
changeset.data.amount || Money.new!(0, new_amount.currency)
case changeset.data do
%{amount: nil} ->
Money.new!(0, new_amount.currency)

%Ash.Changeset.OriginalDataNotAvailable{} ->
Money.new!(0, new_amount.currency)

%{amount: amount} ->
amount
end
end

amount_delta =
Expand Down
3 changes: 2 additions & 1 deletion lib/transfer/transfer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ defmodule AshDoubleEntry.Transfer do
],
destroy_balances?: [
type: :boolean,
doc: "Whether or not balances must be manually destroyed. See the getting started guide for more.",
doc:
"Whether or not balances must be manually destroyed. See the getting started guide for more.",
default: false
]
]
Expand Down

0 comments on commit d95a887

Please sign in to comment.