andreh

andreh

Using Ash.Reactor with AshPhoenix.Form

Hi! First, I’d like to say that AshPhoenix.Form is really great, it makes dealing with forms so much easier! To implement an e-invoicing pipeline, I’m playing with Reactor and would like to know if it is possible to use it together with AshPhoenix.Form.

The main issue I’m facing is that my Invoice resource handles many items through manage_relationship and I don’t know how to pass that to the Reactor.

Here’s my Invoice resource:

defmodule Sales.Invoice do
  actions do
    defaults [:read]

    create :issue do
      primary? true

      accept [
        :discount_tax_rate,
        :discount_type,
        :discount_value,
        :issue_date,
        :payment_mean,
        :payment_terms_days,
        :payment_terms_days_end_of_month,
        :status,
        :contact_id
      ]

      argument :items, {:array, :map}, allow_nil?: false
      change manage_relationship(:items, type: :direct_control, order_is_key: :number)

      # many more validations and change
    end

    action :complete_issue, :struct do
     # Do I have to repeat alls arguments here?
     # And what about change manage_relationship(:items, type: :direct_control, order_is_key: :number)?
 
      run Reactors.IssueInvoice
    end
  end
end

And here’s the Reactor:

defmodule Reactors.IssueInvoice do
  use Ash.Reactor

  # Do I have to repeat all the inputs here?
  input :discount_tax_rate
  input :discount_type
  input :discount_value
  input :issue_date
  input :payment_mean
  input :payment_terms_days
  input :payment_terms_days_end_of_month
  input :status
  input :contact_id
  input :items

  step :check_access_point, Sales.Invoice.Steps.CheckAccessPoint do
    max_retries 3
  end

  transaction :create_invoice_transaction, [Sales.Invoice, Audit.AuditLog] do
    wait_for :check_access_point

    # Do I have to repeat all the inputs here?
    create :create_invoice, Sales.Invoice, :issue do
      inputs %{
        discount_tax_rate: input(:discount_tax_rate),
        discount_type: input(:discount_type),
        discount_value: input(:discount_value),
        issue_date: input(:issue_date),
        payment_mean: input(:payment_mean),
        payment_terms_days: input(:payment_terms_days),
        payment_terms_days_end_of_month: input(:payment_terms_days_end_of_month),
        status: input(:status),
        contact_id: input(:contact_id),
        items: input(:items)
      }
    end
    
     # Other steps...
    return :create_invoice
  end

  return :create_invoice_transaction
end

And of course, this is the error I get:

[error] ** (AshPhoenix.Form.NoFormConfigured) items at path [] must be configured in the form to be used with `inputs_for`. For example:

There is an argument called `items` on the action `Sales.Invoice.complete_issue`.

Perhaps you are missing a `change manage_relationship` for that argument, or it is not a type that can have forms generated for it?

So I guess the question is: it is possible to benefit from all the goodness of Ash.Changeset and Ash.Phoenix.Form while using Ash.Reactor?

I’ve managed to get a working solution using hooks, but it would be nice to benefit from the compensation and retry/backoff features of Reactor.

Hope I’m clear enough, let me know!

Most Liked

ken-kost

ken-kost

Assuming you’re auto deriving the form; it doesn’t work in this case because it’s going through a generic action which has no manage_relationship that could be picked up. Hence the error, and the solution is to manually configure the nested forms, i.e. manually initialize the form and be explicit in what you want:

AshPhoenix.Form.for_action(Sales.Invoice, :complete_issue,  
  forms: [  
    items: [  
      type: :list,  
      resource: Sales.InvoiceItem,  
      create_action: :create 
    ]  
  ]  
)

More on this here.

When AshPhoenix.Form.params/1 is called on submit, it collects all nested item params and assembles them under the "items" key, which gets passed as the items argument to complete_issue — and from there into your Reactor.

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement