Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Orders & Inventory DDD - Where should allocation/reservation be handled?

I am trying to refactor a legacy order handling and stock system with into a cleaner service oriented event-driven architecture. However, I am having some difficulty deciding what service should be responsible for the reservation/allocation of stock.

A brief overview of the current system

  1. Sales orders are placed with us via third party system but we do not necessarily have all order lines in stock.

    • If an order item is in stock then we allocate/reserve the stock for that order straight away.
    • However, if we do not have enough stock then we procure the stock from our suppliers via a purchasing system.
  2. When the item arrives from the supplier, the system will search through all open sales orders for the item and reserve/allocate the available stock to them, prioritising by sales order date. ***

I have already identified two services that I think need to be developed

  • Sales - Responsible for receiving the sales order and inserting into the database. Has domain entities such as Order, OrderLine etc.

  • Inventory - Responsible for keeping track of how much stock is available in our warehouse. Has domain entities such as StockItem.

However, as the allocation/reservation of stock concerns both inventory and sales I am not sure where the behaviour in point 2 above should be put.

I welcome any help or thoughts on this.

like image 951
J. Lewis Avatar asked Mar 05 '23 03:03

J. Lewis


1 Answers

I think you have 2 BCs (bounded contexts): Inventory and Sales. For the integration between them I would probably go for domain events approach.

When a new item arrives at the warehouse, the Inventory BC increments the stock for the item, and publish an event.

Sales BC subscribes to the event, and it updates the opened sales that are waiting for the stock item.

So, behaviour of "point 2" are shared by both BC:

  • Sales BC search for opened orders waiting for that item. And then it asks Inventory BC to get the number of items it needs (this request is synchronous) and close the order.

  • Inventory BC receives the request and decrements the stock for the item.

like image 161
choquero70 Avatar answered Mar 07 '23 17:03

choquero70