Karyo
Documentation/ Decision records

ADR 0042: The layout and transport orders work in a Facility, products in a Business Unit, and unconverted callers use the default Facility

Karyo 2.1.1rendered at build time
On this page

Status: Accepted. Converts the product, tasks and layout consumers that ADR 0040 left keyed on the owning Business Unit. The stock, orders, fulfilment and other consumers ADR 0040 lists stay as that record describes them until each one converts.

Context

ADR 0040 made the Facility a row and put it on the layout, but the consumers still asked for "the owner's" layout: the location finder, the layout ports and the transport-order port took a clientId or read an ambient TenantContext, and the database filled the Facility in behind them. A few of those reads were wrong once a Business Unit could own a second Facility. The staging lookups listed the areas of every Facility, the clearing lookup assumed one clearing location for the whole system, and the putaway candidate query had no Facility predicate.

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

  • A layout port names its Facility. StorageLocationLookup, StorageStrategyLookup, WorkingAreaLookup, StagingLocationLookup, ClearingLocationLookup, LocationAreaUsageLookup, FixAssignmentLookup, ItemDataAreaLookup, TransportDemandLookup, LocationFinder, LocationLockPort, LocationFilter and PutawayLocationStrategy take a typed FacilityId. The layout services, repositories and resources take the Facility as a plain id and filter on facility_id. No ambient overload remains. A location, strategy, fixed assignment, item-area row, area or reservation outside the named Facility is treated as unknown.
  • A transport order belongs to a Facility. TransportOrder is a FacilityEntity. TransportOrderPort takes a FacilityId, and the four commands that mint an order carry one instead of a clientId. The queue, the claim and the open-demand reads filter on the Facility. The Business Unit of the order is the Facility's own, and it is still the owner the inventory ports take.
  • A product read names its Business Unit. ProductLookup.findById, findNamesByIds and findMeasuresByIds, and SubstitutionLookup.findSubstitutes, take the owning Business Unit and return only that unit's products. The ambient overloads and findReadableOwnedBy are deleted, and no read grant widens or narrows the answer. findMeasuresOfHeldProducts stays owner-independent because its caller already holds the product ids. facilityPolicy takes the unit and the Facility.
  • A caller that holds only a Business Unit uses its default Facility. Stock, orders, fulfilment, waves, cross-dock, the replenishment scan, work dispatch, counts and the purge-blocker lookup of the tasks module call OrganizationLookup.requireDefaultFacilityId (or findDefaultFacilityId where "no Facility" is an answer) and pass the result. Each of those call sites is a bridge that the conversion of its own module deletes. The bridge is correct while a unit has one Facility.
  • Warehouse setup reads no ambient context. WarehouseSetupService takes the Facilities the caller may write in from the resource and refuses a target outside them.
  • A location's client_id stays data. It is always the Business Unit of the location's Facility. The finder compares no owner: a Facility has one owner.

Consequences

  • Two Facilities of one Business Unit can each have a clearing location, a pack and a ship staging area, a picking area and a transport queue, and the finder never offers one Facility's location for another's unit load.
  • A product reference no longer succeeds because the caller may read a sibling unit. The write-only and read-only differences the earlier inventory table described for product references are gone.
  • config/scope-consumers.json shrinks: the product, finder and setup files no longer opt in to raw scope, the layout repositories and most of TransportOrderRepository no longer name client_id, and the layout, product and filter ports leave the list of ports without a scope. The four TransportOrderPort create methods stay on it, because the Facility travels inside the command and the guard reads only the parameters.
  • The default-Facility bridge is a known wart: it sits in the callers of every converted port until their modules convert, and a Business Unit with no default Facility fails with an IllegalStateException there. The database already guarantees one for every positive unit.
  • StorageStrategy.onlyClientLocation and the shared client_id = 0 location are not read by the finder. The strategy field stays until the form is reworked, as ADR 0040 says.

Alternatives considered

  • Pass the Business Unit and resolve the Facility inside layout. Rejected: the layout would need the organization lookup and would hide the Facility that a second Facility makes a real choice.
  • Keep the ambient overloads for the callers that have no Facility. Rejected: they are the reads that failed on a scheduler thread, and each one is a place the final model has to find and delete again.
  • Make the callers carry a Facility now. Rejected for this step: the stock, orders, fulfilment and wave rows still belong to a Business Unit, so a Facility taken from the row would be the same default one the bridge returns, with more code.
  • Teach the port guard to read a Facility inside a command. Not recorded; the four exemptions are the smaller change.

Evidence

  • services/warehouse-layout-service/karyo-layout-api/src/main/kotlin/com/karyo/layout/spi/ - the ports above
  • services/warehouse-layout-service/karyo-layout-core/src/main/kotlin/com/karyo/layout/service/LocationFinderService.kt and repository/StorageLocationRepository.kt - findPutawayCandidates
  • services/task-service/karyo-tasks-api/src/main/kotlin/com/karyo/tasks/spi/TransportOrderPort.kt and karyo-tasks-core/.../domain/model/TransportOrder.kt
  • services/product-service/karyo-product-api/src/main/kotlin/com/karyo/product/spi/ProductLookup.kt
  • services/auth-service/karyo-auth-api/src/main/kotlin/com/karyo/auth/spi/OrganizationLookupExt.kt - the bridge helpers
  • config/scope-consumers.json and services/karyo-app/src/test/kotlin/com/karyo/security/guards/ - the registry and the guards that hold it
  • services/karyo-app/src/test/kotlin/com/karyo/layout/FacilityLayoutScopeTest.kt, layout/service/LocationFinderTest.kt, tasks/service/TransportDemandLookupTest.kt and product/service/DefaultProductLookupNamesTest.kt - the behaviour above