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 ( Ian Luites - From Legacy To Event Sourcing | CBL AMSTERDAM 19 - YouTube). 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
fmn

fmn

Hi Hubert, apologies, not very helpful but:

  • here’s related discussion.
  • I feel OTP/Elixir would be very good base for BPM software (like Camunda), but I am not aware of anything like it.
hubertlepicki

hubertlepicki

Where Next?

Popular in Questions Top

Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement