Skip to content

Commit

Permalink
fix: ensure that all notifications are sent for bulk destroy/update
Browse files Browse the repository at this point in the history
closes #1186
  • Loading branch information
zachdaniel committed May 22, 2024
1 parent e01e952 commit 67c6e5e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/ash/actions/destroy/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ defmodule Ash.Actions.Destroy.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 All @@ -304,9 +304,11 @@ defmodule Ash.Actions.Destroy.Bulk do

%{bulk_result | notifications: notifications}
else
Ash.Actions.Helpers.warn_missed!(atomic_changeset.resource, action, %{
resource_notifications: notifications
})
if opts[:notify?] do
Ash.Actions.Helpers.warn_missed!(atomic_changeset.resource, action, %{
resource_notifications: notifications
})
end

%{bulk_result | notifications: []}
end
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 @@ -237,7 +237,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
19 changes: 19 additions & 0 deletions test/actions/bulk/bulk_destroy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,25 @@ defmodule Ash.Test.Actions.BulkDestroyTest do
assert_received {:notification, %{data: %{title: "title2"}}}
end

test "doesn't send notifications if not asked to" do
assert %Ash.BulkResult{records: [%{}, %{}]} =
Ash.bulk_create!([%{title: "title1"}, %{title: "title2"}], Post, :create,
return_stream?: true,
return_records?: true
)
|> Stream.map(fn {:ok, result} ->
result
end)
|> Ash.bulk_destroy!(:destroy, %{},
resource: Post,
strategy: :stream,
return_records?: true,
return_errors?: true
)

refute_received {:notification, _}
end

test "notifications can be returned" do
assert %Ash.BulkResult{records: [%{}, %{}], notifications: [%{}, %{}]} =
Ash.bulk_create!([%{title: "title1"}, %{title: "title2"}], Post, :create,
Expand Down

0 comments on commit 67c6e5e

Please sign in to comment.