Skip to content

Commit

Permalink
fix: handle nil notification results better
Browse files Browse the repository at this point in the history
fix: don't emit after batch notifications if `notify?: false`
  • Loading branch information
zachdaniel committed May 16, 2024
1 parent 51512f7 commit 23d7479
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
15 changes: 8 additions & 7 deletions lib/ash/actions/destroy/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,10 @@ defmodule Ash.Actions.Destroy.Bulk do

notifications =
if notify? do
List.wrap(bulk_result.notifications) ++ Process.delete(:ash_notifications)
List.wrap(bulk_result.notifications) ++
List.wrap(Process.delete(:ash_notifications))
else
List.wrap(bulk_result.notifications)
[]
end

if opts[:return_notifications?] do
Expand Down Expand Up @@ -401,8 +402,8 @@ defmodule Ash.Actions.Destroy.Bulk do
%{
bulk_result
| notifications:
(bulk_result.notifications || []) ++ Process.delete(:ash_notifications) ||
[]
(bulk_result.notifications || []) ++
List.wrap(Process.delete(:ash_notifications))
}
else
bulk_result
Expand Down Expand Up @@ -1426,7 +1427,7 @@ defmodule Ash.Actions.Destroy.Bulk do

defp errors(result, {:error, error}, opts) do
if opts[:return_errors?] do
{result.error_count + 1, [error | result.errors]}
{result.error_count + 1, [error | List.wrap(result.errors)]}
else
{result.error_count + 1, []}
end
Expand Down Expand Up @@ -1546,9 +1547,9 @@ defmodule Ash.Actions.Destroy.Bulk do

new_notifications =
if is_list(notification) do
notification ++ notifications
notification ++ List.wrap(notifications)
else
[notification | notifications]
[notification | List.wrap(notifications)]
end

Process.put({:bulk_destroy_notifications, ref}, new_notifications)
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/actions/update/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ defmodule Ash.Actions.Update.Bulk do
List.wrap(bulk_result.notifications) ++
List.wrap(Process.delete(:ash_notifications))
else
List.wrap(bulk_result.notifications)
[]
end

if opts[:return_notifications?] do
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/changeset/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3397,7 +3397,7 @@ defmodule Ash.Changeset do
case result do
{:ok, new_result, new_notifications} ->
all_notifications =
Enum.map(notifications ++ new_notifications, fn notification ->
Enum.map(notifications ++ List.wrap(new_notifications), fn notification ->
%{
notification
| resource: notification.resource || changeset.resource,
Expand Down
2 changes: 1 addition & 1 deletion lib/ash/resource/change/cascade_destroy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ defmodule Ash.Resource.Change.CascadeDestroy do

case {destroy_related(records, opts, context), opts[:return_notifications?]} do
{_, false} -> result
{%{notifications: []}, true} -> result
{%{notifications: empty}, true} when empty in [nil, []] -> result
{%{notifications: notifications}, true} -> Enum.concat(result, notifications)
end
else
Expand Down
2 changes: 0 additions & 2 deletions test/resource/changes/cascade_destroy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ defmodule Ash.Test.Resource.Change.CascadeDestroy do
1..Enum.random(3..5)
|> Enum.map(fn _ -> Post.create!(%{author_id: author.id}) end)

# This doesn't work either
# Author.no_notification_destroy!([author])
Ash.bulk_destroy!([author], :no_notification_destroy, %{})

assert [] = Post.read!()
Expand Down

0 comments on commit 23d7479

Please sign in to comment.