Passing other attributes to actions used by AshStateMachine

I have an invitation resource that has a status, user_id among other attributes. I use AshStateMachine to update the status attribute, to accepted when the invited user registers in the app, and to expired, when the invitation expires.
When the invitee registers, I need to save their user ID in the invitation.user_id field, as well as transition the invitation status to accepted. I do it this way, in an after_action hook:

%{status: :success} =
  Invitation.update(invitation, %{user_id: user_id}, authorize?: false)

%{status: :success} = Invitation.accept(invitation, authorize?: false)

This works, but I was wondering if it is possible to pass the user_id to the Invitation.accept/2 action, which is used by AshStateMachine, so that I don’t have to call a separate action (Invitation.update/3) to update the user ID field.

Can you accomplish that with the :actor option?

2 Likes

Agreed that using the actor would be the most natural way. With that said, you can definitely just add accept [:user_id] to the action you’re using to transition the state.

Then Invitation.accept(invitation, %{user_id: user_id}, ...)

Additionally, you can add it to the args of the code interface.

define :accept, args: [:user_id]
Invitation.accept( invitation, user_id, ....)

If you do it with the actor, though:

change relate_actor(:user) # set the user to the current actor

then you can just Invitation.accept(invitation, actor: user)

2 Likes

Thanks @almirsarajcic and @zachdaniel. Both solutions work!

1 Like