fix: always return all notifications if return_notifications?: true

This commit is contained in:
Zach Daniel 2022-06-09 13:52:32 -04:00
parent 084b998cab
commit e19ca1ec66
6 changed files with 19 additions and 7 deletions

View file

@ -51,6 +51,7 @@ defmodule Ash.Actions.Create do
authorize?: authorize?,
notification_metadata: opts[:notification_metadata],
timeout: opts[:timeout] || changeset.timeout || Ash.Api.timeout(api),
return_notifications?: opts[:return_notifications?],
transaction?: Keyword.get(opts, :transaction?, true)
)
|> case do

View file

@ -58,6 +58,7 @@ defmodule Ash.Actions.Destroy do
resource: resource,
verbose?: verbose?,
actor: actor,
return_notifications?: opts[:return_notifications?],
notification_metadata: opts[:notification_metadata],
authorize?: authorize?,
timeout: opts[:timeout] || changeset.timeout || Ash.Api.timeout(api),

View file

@ -42,6 +42,7 @@ defmodule Ash.Actions.Update do
verbose?: verbose?,
actor: actor,
notification_metadata: opts[:notification_metadata],
return_notifications?: opts[:return_notifications?],
authorize?: authorize?,
timeout: opts[:timeout] || changeset.timeout || Ash.Api.timeout(api),
default_timeout: Ash.Api.timeout(api),

View file

@ -57,6 +57,7 @@ defmodule Ash.Engine do
# There are no other failure modes, but this is there
# to express the intent for there to eventually be.
failure_mode: :complete,
return_notifications?: false,
opts: [],
requests: [],
data: %{},
@ -99,8 +100,14 @@ defmodule Ash.Engine do
end
|> case do
{:ok, %{resource_notifications: resource_notifications} = result} ->
unsent = Ash.Notifier.notify(resource_notifications)
{:ok, %{result | resource_notifications: unsent}}
notifications =
if opts[:return_notifications?] do
resource_notifications
else
Ash.Notifier.notify(resource_notifications)
end
{:ok, %{result | resource_notifications: notifications}}
other ->
other
@ -114,6 +121,7 @@ defmodule Ash.Engine do
id: System.unique_integer([:positive, :monotonic]),
resolved_fields: %{},
actor: opts[:actor],
return_notifications?: opts[:return_notifications?],
concurrency_limit: System.schedulers_online() * 2,
authorize?: opts[:authorize?] || false,
verbose?: opts[:verbose?] || false,
@ -711,12 +719,13 @@ defmodule Ash.Engine do
end
defp add_resource_notification(state, resource_notification) do
if Ash.DataLayer.in_transaction?(resource_notification.resource) do
if Ash.DataLayer.in_transaction?(resource_notification.resource) ||
state.return_notifications? do
%{state | resource_notifications: [resource_notification | state.resource_notifications]}
else
Ash.Notifier.notify(resource_notification)
unsent = Ash.Notifier.notify(resource_notification)
state
%{state | resource_notifications: unsent ++ state.resource_notifications}
end
end

View file

@ -9,7 +9,7 @@ defmodule Ash.Resource.Dsl do
examples: [
"""
attribute :first_name, :string do
primary_key? true
allow_nil? false
end
"""
],

View file

@ -155,7 +155,7 @@ defmodule Ash.Test.Actions.DestroyTest do
post_id = post.id
assert {:ok, %{id: ^post_id}, []} =
assert {:ok, %{id: ^post_id}, [_]} =
Api.destroy(post, return_destroyed?: true, return_notifications?: true)
refute Api.read_one!(Ash.Query.filter(Post, id == ^post.id))