improvement: don't use raising variations of resource calls

This commit is contained in:
Zach Daniel 2024-06-18 18:20:49 -04:00
parent c91b456d03
commit 1d732a0c6c

View file

@ -87,61 +87,80 @@ defmodule AshDoubleEntry.Transfer.Changes.VerifyTransfer do
balance_resource = balance_resource =
AshDoubleEntry.Transfer.Info.transfer_balance_resource!(changeset.resource) AshDoubleEntry.Transfer.Info.transfer_balance_resource!(changeset.resource)
unless changeset.action.type == :destroy do bulk_destroy_errors =
Ash.bulk_create!( unless changeset.action.type == :destroy do
[ Ash.bulk_create(
%{ [
account_id: from_account.id, %{
transfer_id: result.id, account_id: from_account.id,
balance: new_from_account_balance transfer_id: result.id,
}, balance: new_from_account_balance
%{ },
account_id: to_account.id, %{
transfer_id: result.id, account_id: to_account.id,
balance: new_to_account_balance transfer_id: result.id,
} balance: new_to_account_balance
], }
balance_resource, ],
:upsert_balance, balance_resource,
Ash.Context.to_opts(context, :upsert_balance,
domain: changeset.domain, Ash.Context.to_opts(context,
upsert_fields: [:balance], domain: changeset.domain,
return_errors?: true, upsert_fields: [:balance],
stop_on_error?: true return_errors?: true,
stop_on_error?: true
)
) )
) end
end |> case do
nil ->
[]
amount_delta = %Ash.BulkResult{status: :success} ->
if changeset.action.type == :destroy do []
Money.mult!(amount_delta, -1)
else %Ash.BulkResult{errors: errors} ->
amount_delta errors
end end
Ash.bulk_update!( case bulk_destroy_errors do
balance_resource, [] ->
:adjust_balance, amount_delta =
%{ if changeset.action.type == :destroy do
from_account_id: from_account.id, Money.mult!(amount_delta, -1)
to_account_id: to_account.id, else
transfer_id: result.id, amount_delta
delta: amount_delta end
},
Ash.Context.to_opts(context,
domain: changeset.domain,
strategy: [:atomic, :stream, :atomic_batches],
return_errors?: true,
stop_on_error?: true
)
|> Keyword.update(
:context,
%{ash_double_entry?: true},
&Map.put(&1, :ash_double_entry?, true)
)
)
{:ok, result} Ash.bulk_update(
balance_resource,
:adjust_balance,
%{
from_account_id: from_account.id,
to_account_id: to_account.id,
transfer_id: result.id,
delta: amount_delta
},
Ash.Context.to_opts(context,
domain: changeset.domain,
strategy: [:atomic, :stream, :atomic_batches],
return_errors?: true,
stop_on_error?: true
)
|> Keyword.update(
:context,
%{ash_double_entry?: true},
&Map.put(&1, :ash_double_entry?, true)
)
)
|> case do
%Ash.BulkResult{status: :success} -> {:ok, result}
%Ash.BulkResult{errors: errors} -> {:error, errors}
end
errors ->
{:error, errors}
end
end) end)
end end
end end
@ -162,7 +181,7 @@ defmodule AshDoubleEntry.Transfer.Changes.VerifyTransfer do
balance_resource balance_resource
|> Ash.Query.filter(transfer_id == ^changeset.data.id) |> Ash.Query.filter(transfer_id == ^changeset.data.id)
|> Ash.Query.set_context(%{ash_double_entry?: true}) |> Ash.Query.set_context(%{ash_double_entry?: true})
|> Ash.bulk_destroy!( |> Ash.bulk_destroy(
destroy_action, destroy_action,
%{}, %{},
Ash.Context.to_opts(context, Ash.Context.to_opts(context,
@ -171,8 +190,10 @@ defmodule AshDoubleEntry.Transfer.Changes.VerifyTransfer do
strategy: [:stream, :atomic, :atomic_batches] strategy: [:stream, :atomic, :atomic_batches]
) )
) )
|> case do
changeset %Ash.BulkResult{status: :success} -> changeset
%Ash.BulkResult{errors: errors} -> {:error, Ash.Changeset.add_error(changeset, errors)}
end
end) end)
else else
changeset changeset