2023-09-08 13:35:51 +12:00
|
|
|
defmodule AshStateMachine.BuiltinChanges.NextState do
|
|
|
|
@moduledoc """
|
|
|
|
Given the action and the current state, attempt to find the next state to
|
|
|
|
transition into.
|
|
|
|
"""
|
|
|
|
use Ash.Resource.Change
|
|
|
|
|
|
|
|
def change(changeset, _opts, _) do
|
2023-09-15 15:15:29 +12:00
|
|
|
changeset.data
|
|
|
|
|> AshStateMachine.possible_next_states(changeset.action.name)
|
2023-09-08 13:35:51 +12:00
|
|
|
|> case do
|
|
|
|
[to] ->
|
|
|
|
AshStateMachine.transition_state(changeset, to)
|
|
|
|
|
|
|
|
[] ->
|
|
|
|
Ash.Changeset.add_error(changeset, "Cannot determine next state: no next state available")
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Ash.Changeset.add_error(
|
|
|
|
changeset,
|
|
|
|
"Cannot determine next state: multiple next states available"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|