Ash FrameworkAF
Ash Framework3y ago
23 replies
Blibs

Pass create changeset or inputs from it to Flow?

In my resource I have these two create actions:

    create :create do
      primary? true

      accept [:price, :payment_type]

      argument :property_id, :uuid, allow_nil?: false

      change fn changeset, %{actor: actor} ->
        Ash.Changeset.force_change_attribute(changeset, :offeror_id, actor.id)
      end

      change set_attribute(:property_id, arg(:property_id))
    end

    create :place_offer do
      accept [:price, :payment_type]

      argument :property_id, :uuid, allow_nil?: false

      manual fn changeset, %{actor: actor} ->
        %{
          attributes: %{price: price, payment_type: payment_type},
          arguments: %{property_id: property_id}
        } = changeset

        with %Ash.Flow.Result{valid?: true, result: result} <-
               Flows.MakeOffer.run!(price, payment_type, property_id, actor: actor) do
          {:ok, result}
        else
          {:error, error} ->
            {:error, error}
        end
      end
    end


As you can see, the place_offer action will call my flow Flows.MakeOffer. Inside that flow, I have this step:

        create :make_offer, Offer, :create do
          input %{
            price: arg(:price),
            payment_type: arg(:payment_type),
            property_id: arg(:property_id)
          }
        end


This step calls the
create
action and creates the offer.

Now, is this the best/correct way to do it?

What I see as probably wrong is that I'm already receiving a changeset in the place_offer create action, but I'm just getting the inputs from it and ignoring the rest because the
create
action will generate a new changeset later.

I was thinking that maybe I should pass the changeset to the Flow, but then I'm not sure how I would pass it to the
create
action.

What is your suggestion?
Was this page helpful?