Karyo
Documentation/ Decision records

ADR 0040: The Facility is the third tier, and the Business Unit stays a key until its consumers convert

Karyo 2.1.1rendered at build time
On this page

Status: Accepted. Supersedes the staged access cutover of ADR 0038 (there is no activation marker) and the shared-owner target of ADR 0036 (there is no Shared target). ADR 0042 converts the product, tasks and layout consumers, ADR 0043 the stock consumers, and ADR 0044 the order and count consumers, that this record leaves keyed on the Business Unit. ADR 0050 supersedes the temporary Business Unit key it keeps on operational rows. ADR 0051 renames the remaining client and tenant vocabulary.

Context

Karyo scoped operational rows by a client_id, the stock owner. The organization it is moving to has three tiers: Company, Business Unit and Facility, with one Facility the place a warehouse operates. ADR 0039 put the typed ids, the declared scope kinds and the guards in place without changing any behaviour. This record makes the model real.

Karyo has no users and no deployment to carry forward. The demo deployment is wiped and seeded again. That removes the reasons for a marker that switches grant scope on, for backfilling a Facility into existing rows, and for keeping a shared layout that belonged to no owner.

Decision

  • A Facility is a row. karyo.facilities belongs to one Business Unit (a positive clients row) for life and has a code that is unique within the unit. Ids start at 1,000,000, so an id from the wrong tier fails at once instead of matching by coincidence. Every Business Unit has exactly one default Facility, MAIN: ClientService.create makes it in the same transaction as the unit, and the migration made one for each seeded unit. SYS (client_id 0) is a platform row, owns no Facility, and so can own no operational row.
  • A grant can name a Facility. The node type FACILITY is valid for the operational actions (operations.read, operations.write, inventory.adjust, orders.release, counts.approve, reports.read, reports.export) and for no administrative one. A Company grant reaches every Facility, a Business Unit grant the Facilities of its unit, and a Facility grant that Facility, with its unit as context. scripts/identity_migration.py validates a Facility grant against the same rule.
  • A grant is always in force. There is no activation marker. Every principal that arrives with a provider issuer is resolved from its grants. The issuer-less identity that TenantFilter accepts only for resource tests keeps the affiliation-derived scope it had, and its Facilities follow its kind and affiliation.
  • A request works in one Facility. X-Karyo-Facility-Id selects an authorized Facility, and selecting a Business Unit leaves only that unit's Facilities in play. The Facility an operation acts in is the selected one, else the only authorized one. An absent or ambiguous choice is refused with facility-context-required (403), never guessed. TenantContext.operationalFacilityId gives the typed id; operationalFacilityIdOrNull is for a read that has an answer without one.
  • Operational tables carry facility_id NOT NULL. Every writer sets its Facility. The trigger karyo.validate_facility_write() refuses a null Facility and one that belongs to another unit than the row's client_id; it never fills the column in. A change of owner moves the row to a Facility of the new owner, named by the writer.
  • The layout belongs to a Facility. Areas, zones, location types, clusters, storage areas, working areas, the floor arrangement and the setup runs have a facility_id and no client_id, and their names are unique within the Facility. Locations, strategies, fixed assignments and item-area rows carry both keys. Names, scan codes, the clearing location and a setup idempotency key are unique per Facility, so two Facilities can use the same label. Every layout request needs a Facility; there is no shared layout, and the setup page has no Shared target.
  • The catalogue is the Business Unit's and the policy is the Facility's. A product stays one row for its unit. What a Facility does with it, its preferred zone and default putaway strategy, is a product_facility_policies row keyed by product and Facility, with the same stamping trigger. ProductLookup.facilityPolicy is how other modules read it; the REST product shows the settings of the caller's Facility.
  • Until consumers convert, a Business Unit has one Facility. Stock, orders, fulfilment and the other consumers are still keyed on the owning Business Unit and are listed in config/scope-consumers.json; they use the unit's default Facility where they need one. Nothing creates a second Facility through the API in this step. (The finder, the layout ports and the transport orders have since converted: ADR 0042.)

Consequences

  • A fresh database can be built and seeded with no backfill. A layout row with no Facility cannot exist, and neither can one for SYS 0.
  • Every writer sets its own Facility and the trigger only validates it. The client_id column on operational rows is the next thing to go.
  • StorageStrategy.onlyClientLocation no longer changes a result, because no shared location exists for it to include or exclude. The column and the field stay until the strategy form is reworked.
  • A test names its Business Unit by a number it invented. The test classpath carries V90000001__test_business_unit_provisioning.sql, which TestOrganizations.facilityOf and a request filter call to give such a number a Business Unit and a fixture Facility. No row insert provisions one.
  • Multi-Facility operation, enforcement of Facility grants in every consumer and the Facility administration screen are not built. The database and the resolver allow a second Facility, and tests create one, but no screen or endpoint does.

Alternatives considered

  • Add facility_id to every writer at once. Rejected for this step: it touches about a hundred construction sites in code that the next steps convert on purpose. The trigger gives the same stored shape with no change to those sites.
  • Keep an activation marker for grant scope. Rejected: with no deployment there is nothing to protect during a staged switch, and two scope behaviours doubled every test.
  • Fill facility_id in application code only. Rejected: a writer that forgot would store a row with no Facility, and the trigger makes that a database error instead of a later surprise.
  • Keep layout rows with no owner as shared reference data. Rejected: a Facility's names and arrangement are its own, and two Facilities of one unit could not hold the same label.
  • Put the zone and strategy on the catalogue product. Rejected: they name rows of one Facility's layout, so a second Facility could never set them apart.

No record states why the first Facility is MAIN or why ids start at 1,000,000 beyond the reasons above; both are choices of this change.

Evidence

  • services/karyo-app/src/main/resources/db/migration/auth/V1211__create_facilities.sql, V1212__facility_grant_node.sql and common/V3__facility_stamping.sql - the table, the grant node and the trigger
  • services/karyo-app/src/main/resources/db/migration/layout/V321__facility_scope.sql and the per-module *__facility_key.sql files - the Facility columns and per-Facility uniqueness
  • services/karyo-app/src/main/resources/db/migration/product/V208__product_facility_policy.sql
  • services/auth-service/karyo-auth-core/src/main/kotlin/com/karyo/auth/service/ FacilityService.kt, DefaultOrganizationAccessLookup.kt, CurrentAccessService.kt
  • libs/karyo-security/src/main/kotlin/com/karyo/security/TenantContext.kt - the Facility fields
  • services/karyo-app/src/main/kotlin/com/karyo/app/auth/TenantFilter.kt - populateOrganizationScope
  • libs/karyo-common/src/main/kotlin/com/karyo/common/domain/BaseEntity.kt - FacilityEntity and FacilityConfigEntity
  • services/karyo-app/src/test/kotlin/com/karyo/layout/FacilityStampingTest.kt, FacilityLayoutScopeTest.kt, auth/FacilityModelTest.kt and product/service/ProductFacilityPolicyTest.kt - the behaviour above