mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 05:23:03 +12:00
fix: various ash flow return value fixes
This commit is contained in:
parent
ad0a0ee8f7
commit
a6e3ff233e
5 changed files with 52 additions and 14 deletions
|
@ -37,6 +37,17 @@ defmodule Ash.Flow.Executor.AshEngine do
|
|||
step =
|
||||
case step do
|
||||
%{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 ->
|
||||
|
@ -279,6 +290,9 @@ defmodule Ash.Flow.Executor.AshEngine do
|
|||
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.do_get_in(data, [transaction_name, name])
|
||||
|
||||
|
@ -1784,6 +1798,13 @@ defmodule Ash.Flow.Executor.AshEngine do
|
|||
no_depend? \\ false
|
||||
) do
|
||||
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}}
|
||||
when stop_at_transaction_name? ->
|
||||
nil
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
defmodule Ash.FlowTest.BranchingTransactionMappingTest do
|
||||
@moduledoc 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
|
||||
|
||||
setup do
|
||||
|
@ -26,18 +26,26 @@ defmodule Ash.FlowTest.BranchingTransactionMappingTest do
|
|||
end
|
||||
|
||||
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 hd(created_users).lastname == "Kent"
|
||||
assert hd(created_users).last_name == "Kent"
|
||||
assert hd(created_users).approved == true
|
||||
end
|
||||
|
||||
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 hd(updated_users).lastname == "Willis"
|
||||
assert hd(updated_users).last_name == "Willis"
|
||||
assert hd(updated_users).approved == true
|
||||
end
|
||||
end
|
||||
|
|
|
@ -29,6 +29,7 @@ defmodule Ash.FlowTest.BranchingTransactionNoRecordTest do
|
|||
test "if user exists does not create user" do
|
||||
assert %Ash.Flow.Result{result: %{user_not_found: nil, get_user: user}} =
|
||||
Ash.Test.Flow.Flows.BranchingTransactionNoRecord.run!("Bruce")
|
||||
|
||||
assert user.first_name == "Bruce"
|
||||
end
|
||||
|
||||
|
@ -40,14 +41,14 @@ defmodule Ash.FlowTest.BranchingTransactionNoRecordTest do
|
|||
|
||||
assert user == []
|
||||
|
||||
%Ash.Flow.Result{result: %{user_not_found: user, get_user: nil}} = Ash.Test.Flow.Flows.BranchingTransactionNoRecord.run!("Bat")
|
||||
IO.inspect(user)
|
||||
%Ash.Flow.Result{result: %{user_not_found: user, get_user: nil}} =
|
||||
Ash.Test.Flow.Flows.BranchingTransactionNoRecord.run!("Bat")
|
||||
|
||||
assert user.create_user.first_name == "Bat"
|
||||
|
||||
users =
|
||||
User
|
||||
|> Ash.Query.for_read(:index)
|
||||
|> Ash.Query.for_read(:read)
|
||||
|> Api.read!()
|
||||
|
||||
assert length(users) == 2
|
||||
|
|
|
@ -19,7 +19,7 @@ defmodule Ash.Test.Flow.Flows.BranchingTransactionMapping do
|
|||
default nil
|
||||
end
|
||||
|
||||
returns :create_or_update_users
|
||||
returns :cycle_users
|
||||
end
|
||||
|
||||
steps do
|
||||
|
@ -47,12 +47,19 @@ defmodule Ash.Test.Flow.Flows.BranchingTransactionMapping do
|
|||
}
|
||||
end
|
||||
end
|
||||
|
||||
custom :return_value,
|
||||
fn value, _ ->
|
||||
{:ok, value}
|
||||
end,
|
||||
input: expr(^result(:creation_bulk) || ^result(:update_bulk))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule MmsBiztalk.Flows.Steps.CreateUser do
|
||||
defmodule Ash.Test.Flow.Steps.CreateUser do
|
||||
@moduledoc false
|
||||
use Ash.Flow.Step
|
||||
alias Ash.Changeset
|
||||
|
||||
|
@ -72,7 +79,8 @@ defmodule MmsBiztalk.Flows.Steps.CreateUser do
|
|||
end
|
||||
end
|
||||
|
||||
defmodule MmsBiztalk.Flows.Steps.UpdateUser do
|
||||
defmodule Ash.Test.Flow.Steps.UpdateUser do
|
||||
@moduledoc false
|
||||
use Ash.Flow.Step
|
||||
alias Ash.Changeset
|
||||
|
||||
|
@ -81,9 +89,9 @@ defmodule MmsBiztalk.Flows.Steps.UpdateUser do
|
|||
Ash.Test.Flow.User
|
||||
|> Ash.Query.for_read(
|
||||
: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_to_update
|
||||
|
|
|
@ -42,7 +42,7 @@ defmodule Ash.Test.Flow.User do
|
|||
end
|
||||
|
||||
code_interface do
|
||||
define_for Ash.Test.Flow
|
||||
define_for Ash.Test.Flow.Api
|
||||
|
||||
define :to_approved, action: :approve
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue