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?

4 Likes

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.

4 Likes

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.
1 Like

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

How is it made?

2 Likes

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
2 Likes

It’s made in this tool: https://plantuml.com/activity-diagram-beta

1 Like

I am very late to the discussion and assume that you are past this problem now in one way or another, but, as others have said, Business Process Management (BPM) is definitely applicable. Someday, hopefully, my open source BPM project, Mozart (shameless plug), will see the light of day.

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?