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 Switch mode

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

Last Post!

CharlesIrvine

CharlesIrvine

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.

Hello @hubertlepicki
I’m curious about a couple of things. 1) What was the goal of your project, that is, what were developers charged with building? 2) What is the status of your project?

Where Next?

Trending in Questions Top

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
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
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
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
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
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
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement