Advice on "chaining" actions/state transitions

Hey all!

I have a resource called Assignment that’s backed by AshStateMachine and can go through several states: draftassignedclosedarchived.

When an assignment is closed we need to perform a few side effects like triggering Notifiers or kicking off background jobs.

We’d like to allow users to archive an assignment that’s in the assigned state but we want to trigger all of the same side effects that happen when it gets closed (you can think of it like we want the assignment to go through the closed state before we archive it).

My initial thought was to add a before_action change on the archive action that checks the current state and conditionally calls the close action on the resource.

Is that the best way to “chain” actions? Is there a better way to conditionally force an action/state transition to happen before another one?

The before_action hook would likely be the first thing that I reach for, yes. You can do something like this:

Ash.Changeset.before_action(changeset, fn changeset, result ->
  case do_first_change(changeset) do
    {:ok, result} -> %{changeset | data: result}
    {:error, error} -> Ash.Changeset.add_error(changeset, error)
  end  
end)