hubertlepicki

hubertlepicki

Business logic library / framework / pipeline

I am helping to model an e-commerce store backend, that also handles shipment processes, returns etc.

We have orders that are coming through API, and should be automatically actioned upon, often after some delay. And order can be temporarily “held” for some minutes, to make sure the user didn’t make a mistake ordering their things. It can be released from “holding” and either split to multiple shipments, merged with other shipments or can be moved to “backorder” status. It can also be a mixture of all above, where an order is held, then released after some minutes, then split, some the split shipments can be merged with others and some may be hold in “backorder” status if we have no items available on hand. When these missing items become available, we need to “release” the shipment, and it can again - be split, merged or even moved to backorder status if only some of the ordered items became available and not others.

If you plot the business logic as a activity chart, it’d look like something like this, but even more complicated:

Now, you can see that there are some chunks of logic that repeat itself, and “released” shipments can enter into the algorithm into other places after some time passes, or some event happens.

What I am struggling to do, is to find a nice way to model this code in a high-level terms, where I’d have developers being able to have a look at the code and understand what’s going on, or what the business logic is.

The closest I got was with Opus, Opus.Pipeline – Opus v0.3.1 but it doesn’t do great with multiple levels of branching - you end up with creating very small “linked” Pipelines that conditionally call other pipelines. It also doesn’t have the built-in means to handle “jumping into a step”, when a timeout, event or a condition is met that the shipment was waiting on to happen. We use Oban and Oban scheduler to handle that.

How do you structure code like that? Are there libraries / frameworks / tools in Elixir ecosystem that’d be helpful, or maybe you know of something like that from other ecosystems for me to have a look at?

Most Liked

barttenbrinke

barttenbrinke

It might be smart to look at some CQRS / event sourcing for this ( https://youtu.be/719lAP5fzFk). Just having an order_events table and an order_table together with an OTP handler might be more than enough to deal with this.

Events come in as OTP messages, they load the order_state if needed (or maybe even spawn an dedicated order worker). The worker changes state, depending on the order_state, the event, and maybe even some event history. New state is persisted & subsequent events can be fired.

dimitarvp

dimitarvp

Don’t mind me, I am just admiring the nice diagram. :007:

How is it made?

wpiekutowski

wpiekutowski

Another option would be to build such functional workflows with something similar to a Result/Either/Option, but with more possible values: {:yes | :no | :error | :done, _}. :done to break out from the pipeline. Depending on how foolproof you need it to be, it might need more sophisticated API.

shipment
|> hold_shipment?()
# set_status returns {:done | :error, _}, so further YesNo calls are ignored – flow terminates
|> YesNo.and_then(yes: &set_status(&1, :on_hold), no: &review_rules_match?/1)
|> YesNo.and_then(yes: &set_status(&1.shipment, &1.status), no: &can_be_fully_shipped_now?/1)
|> YesNo.and_then(yes: &fully_ship/1, no: &attempt_partial_shipment/1)

fully_ship = fn shipment ->
  {:ok, shipment}
  # use and_then when function may fail, in other words when it returns {:ok, _}, {:error, _} instead of a plain value
  |> Result.map(&create_shipment_with_available_items_only/1)
  |> Result.map(&attempt_combine_on_the_same_addres/1)
  |> Result.map(&calculate_shipment_fees/1)
  |> Result.map(&add_shipment_to_batch/1)
end

attempt_combine_on_the_same_address = fn shipment ->
  shipment
  |> combine_on_the_same_address?()
  |> YesNo.and_then(yes: &combine_with_other_shipments_matching_address/1, no: &{:ok, &1})
end

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement