Karyo
Documentation/ Decision records

ADR 0044: Orders and counts work in a Facility

Karyo 2.1.1rendered at build time
On this page

Status: Accepted. Converts the order and count consumers that ADR 0040 left keyed on the owning Business Unit, after ADR 0042 converted the product, tasks and layout consumers and ADR 0043 the stock consumers. The fulfilment, wave, streaming, cross-dock and work consumers stay as those records describe them until each one converts.

Context

The order ports (DeliveryOrderLookup, OrderStrategyLookup, OrderProgressionPort, CrossDockOrdersPort, OrderReleasePort, StreamingReleasePort, GoodsReceiptLookup) and the count service read the request scope, or took a clientId, to find their rows. Several came in pairs: an ambient method beside an explicit-clientId method for the schedulers. Orders and counts called the stock and layout ports through OrganizationLookup.requireDefaultFacilityId, the bridge that ADR 0043 left for this conversion to remove. The database already stamped facility_id on the order and count rows, but no order or count read or write named its Facility.

The end state is a Karyo that works with Company, Business Unit and Facility and not with the legacy stock owner. Karyo has no users, so a change here may be disruptive, and compatibility code has no reason to exist.

Decision

  • An order port names its Facility. The ports above take a typed FacilityId. Each ambient and explicit-clientId method pair is one method. OrderStrategyContext carries the Facility, and CountScopeStrategy.resolveLocations takes it. An order, receipt, ASN line or count outside the named Facility is unknown.
  • ASN, goods receipt, delivery order and the count rows are Facility-owned. Asn, GoodsReceipt, DeliveryOrder, CountSession, CountOrder, CountLine and CountCampaign extend FacilityEntity. The services, repositories and resources filter on facility_id. The line rows (AsnLine, DeliveryOrderLine, GoodsReceiptLine, OrderLineReservation) are reached through their document and so belong to its Facility.
  • The owner is the Business Unit of the Facility. A row written in a Facility names that Facility's Business Unit as client_id, as ADR 0043 decided for stock. client_id stays on the row as data and as the owner that products, number sequences, documents and outbox rows name. OrganizationLookup.requireBusinessUnitOf answers it.
  • The state events carry the Facility. AsnStateChangedEvent, DeliveryOrderStateChangedEvent, GoodsReceiptStateChangedEvent, GoodsReceiptLineReceivedEvent and GoodsReceiptLineReversedEvent carry facilityId. The putaway task, the cross-dock interceptor and the wave progress observer act in the event's Facility and no longer look up a default one.
  • Document numbers are unique within a Facility. Delivery order, ASN, goods receipt, count session, count order and count campaign numbers are unique per Facility. Generated numbers still draw on the Business Unit's sequence, so they stay distinct across its Facilities.
  • Order strategies stay system-level. They have no owner or Facility. A strategy's default destination location is checked against the Facility of the request that writes it.
  • Callers that hold only a Business Unit still use its default Facility. Fulfilment, waves, streaming and cross-dock pass OrganizationLookup.requireDefaultFacilityId where they have no Facility yet. Each such call is a bridge that the conversion of its own module removes. The orders and counts modules have none left.
  • Three owner-keyed reads stay. ReservationRefMover and PurgeBlockerLookup (inventory ports implemented in the orders and count modules) and StockSummaryLookup still take the owning Business Unit, as ADR 0043 recorded. Their order-side and count-side implementations keep the client_id predicate until the callers that hold only an owner convert.

Consequences

  • Two Facilities of one Business Unit can hold an order, receipt or count of the same number, and a pick, release, receipt or count in one Facility never reads the other's rows.
  • A scheduler that enumerates owners (OrderReleasePort.clientIdsWithWaveEligibleOrders, StreamingReleasePort.clientIdsWithStreamingWork) still returns Business Unit ids. Its consumer resolves each unit's default Facility until the schedulers fan out per Facility.
  • config/scope-consumers.json shrinks: the order and count ports leave the list of ports without a scope, and the raw-scope and client_id predicate entries of the converted files are removed or lowered. The registry only shrinks.
  • An unknown destination location id on an order is a known gap that this record does not close.

Alternatives considered

  • Keep the explicit-clientId ports for schedulers. Rejected for the reason in ADR 0043: they are the second half of every pair this decision removes.
  • Resolve the Facility inside orders from the Business Unit. Rejected for the reason in ADR 0042: orders would need the organization lookup for every call and would hide the choice.
  • Leave the (client_id, number) unique constraints. Rejected: a second Facility of a Business Unit could not reuse a number that the first holds, though the code checks per Facility.

Evidence

  • services/order-service/karyo-orders-api/src/main/kotlin/com/karyo/orders/spi/ and services/stocktaking-service/karyo-stocktaking-api/src/main/kotlin/com/karyo/stocktaking/spi/ - the ports above
  • services/order-service/karyo-orders-core/src/main/kotlin/com/karyo/orders/service/ and services/stocktaking-service/karyo-stocktaking-core/src/main/kotlin/com/karyo/stocktaking/service/ - the implementations
  • services/karyo-app/src/main/resources/db/migration/orders/V434__numbers_unique_per_facility.sql and .../stocktaking/V707__numbers_unique_per_facility.sql
  • config/scope-consumers.json and services/karyo-app/src/test/kotlin/com/karyo/security/guards/ - the registry and the guards that hold it