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

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement