andreh

andreh OP

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!

First Post! Switch mode

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement