fix: various ash flow return value fixes

This commit is contained in:
Zach Daniel 2023-04-04 15:36:08 -04:00
parent ad0a0ee8f7
commit a6e3ff233e
5 changed files with 52 additions and 14 deletions

View file

@ -37,6 +37,17 @@ defmodule Ash.Flow.Executor.AshEngine do
step = step =
case step do case step do
%{steps: steps} = step -> %{steps: steps} = step ->
step =
if Map.has_key?(step, :output) && is_nil(step.output) do
if last_step = List.last(steps) do
%{step | output: last_step.name}
else
step
end
else
step
end
%{step | steps: to_steps(steps, input)} %{step | steps: to_steps(steps, input)}
step -> step ->
@ -279,6 +290,9 @@ defmodule Ash.Flow.Executor.AshEngine do
nil -> nil ->
nil nil
%Ash.Flow.Step.Transaction{name: ^name, output: output} ->
get_return_value(steps, data, output)
%Ash.Flow.Step.Transaction{name: transaction_name} -> %Ash.Flow.Step.Transaction{name: transaction_name} ->
Ash.Flow.do_get_in(data, [transaction_name, name]) Ash.Flow.do_get_in(data, [transaction_name, name])
@ -1784,6 +1798,13 @@ defmodule Ash.Flow.Executor.AshEngine do
no_depend? \\ false no_depend? \\ false
) do ) do
Enum.find_value(steps, fn Enum.find_value(steps, fn
%Step{step: %Ash.Flow.Step.Transaction{name: ^dep} = step} ->
if transaction_name && no_depend? do
{step, :no_depend}
else
step
end
%Step{step: %Ash.Flow.Step.Transaction{name: ^transaction_name}} %Step{step: %Ash.Flow.Step.Transaction{name: ^transaction_name}}
when stop_at_transaction_name? -> when stop_at_transaction_name? ->
nil nil

View file

@ -1,7 +1,7 @@
defmodule Ash.FlowTest.BranchingTransactionMappingTest do defmodule Ash.FlowTest.BranchingTransactionMappingTest do
@moduledoc false @moduledoc false
use ExUnit.Case, async: false use ExUnit.Case, async: false
alias Ash.Test.Flow.{Api, Org, User} alias Ash.Test.Flow.{Api, Org}
alias Ash.Test.Flow.Flows.SignUpUser alias Ash.Test.Flow.Flows.SignUpUser
setup do setup do
@ -26,18 +26,26 @@ defmodule Ash.FlowTest.BranchingTransactionMappingTest do
end end
test "transaction map with internal branching and custom create step returns properly" do test "transaction map with internal branching and custom create step returns properly" do
%{result: created_users} = Ash.Test.Flow.Flows.BranchingTransactionMapping.run("Org 1", [%{firstname: "Clark", lastname: "Kent"}]) %{result: created_users} =
Ash.Test.Flow.Flows.BranchingTransactionMapping.run("Org 1", [
%{first_name: "Clark", last_name: "Kent"}
])
assert length(created_users) == 1 assert length(created_users) == 1
assert hd(created_users).lastname == "Kent" assert hd(created_users).last_name == "Kent"
assert hd(created_users).approved == true assert hd(created_users).approved == true
end end
test "transaction map with internal branching and custom update step returns properly" do test "transaction map with internal branching and custom update step returns properly" do
%{result: updated_users} = Ash.Test.Flow.Flows.BranchingTransactionMapping.run("Org 1", [%{firstname: "Bruce", lastname: "Willis"}], %{update_users: true}) %{result: updated_users} =
Ash.Test.Flow.Flows.BranchingTransactionMapping.run(
"Org 1",
[%{first_name: "Bruce", last_name: "Willis"}],
%{update_users: true}
)
assert length(updated_users) == 1 assert length(updated_users) == 1
assert hd(updated_users).lastname == "Willis" assert hd(updated_users).last_name == "Willis"
assert hd(updated_users).approved == true assert hd(updated_users).approved == true
end end
end end

View file

@ -29,6 +29,7 @@ defmodule Ash.FlowTest.BranchingTransactionNoRecordTest do
test "if user exists does not create user" do test "if user exists does not create user" do
assert %Ash.Flow.Result{result: %{user_not_found: nil, get_user: user}} = assert %Ash.Flow.Result{result: %{user_not_found: nil, get_user: user}} =
Ash.Test.Flow.Flows.BranchingTransactionNoRecord.run!("Bruce") Ash.Test.Flow.Flows.BranchingTransactionNoRecord.run!("Bruce")
assert user.first_name == "Bruce" assert user.first_name == "Bruce"
end end
@ -40,14 +41,14 @@ defmodule Ash.FlowTest.BranchingTransactionNoRecordTest do
assert user == [] assert user == []
%Ash.Flow.Result{result: %{user_not_found: user, get_user: nil}} = Ash.Test.Flow.Flows.BranchingTransactionNoRecord.run!("Bat") %Ash.Flow.Result{result: %{user_not_found: user, get_user: nil}} =
IO.inspect(user) Ash.Test.Flow.Flows.BranchingTransactionNoRecord.run!("Bat")
assert user.create_user.first_name == "Bat" assert user.create_user.first_name == "Bat"
users = users =
User User
|> Ash.Query.for_read(:index) |> Ash.Query.for_read(:read)
|> Api.read!() |> Api.read!()
assert length(users) == 2 assert length(users) == 2

View file

@ -19,7 +19,7 @@ defmodule Ash.Test.Flow.Flows.BranchingTransactionMapping do
default nil default nil
end end
returns :create_or_update_users returns :cycle_users
end end
steps do steps do
@ -47,12 +47,19 @@ defmodule Ash.Test.Flow.Flows.BranchingTransactionMapping do
} }
end end
end end
custom :return_value,
fn value, _ ->
{:ok, value}
end,
input: expr(^result(:creation_bulk) || ^result(:update_bulk))
end end
end end
end end
end end
defmodule MmsBiztalk.Flows.Steps.CreateUser do defmodule Ash.Test.Flow.Steps.CreateUser do
@moduledoc false
use Ash.Flow.Step use Ash.Flow.Step
alias Ash.Changeset alias Ash.Changeset
@ -72,7 +79,8 @@ defmodule MmsBiztalk.Flows.Steps.CreateUser do
end end
end end
defmodule MmsBiztalk.Flows.Steps.UpdateUser do defmodule Ash.Test.Flow.Steps.UpdateUser do
@moduledoc false
use Ash.Flow.Step use Ash.Flow.Step
alias Ash.Changeset alias Ash.Changeset
@ -81,9 +89,9 @@ defmodule MmsBiztalk.Flows.Steps.UpdateUser do
Ash.Test.Flow.User Ash.Test.Flow.User
|> Ash.Query.for_read( |> Ash.Query.for_read(
:by_name, :by_name,
%{name: input.attributes.firstname} %{name: input.attributes.first_name}
) )
|> Ash.Test.Flow.Api.read!() |> Ash.Test.Flow.Api.read_one!()
user_updated = user_updated =
user_to_update user_to_update

View file

@ -42,7 +42,7 @@ defmodule Ash.Test.Flow.User do
end end
code_interface do code_interface do
define_for Ash.Test.Flow define_for Ash.Test.Flow.Api
define :to_approved, action: :approve define :to_approved, action: :approve
end end