ADR 0046: Monitors, reporting, the demand readers and the document archive work in a Facility
Status: Accepted. Converts the last consumers that
ADR 0045 left on the Business Unit key:
the event monitors, reporting, forecasting, simulation, slotting and the document archive. It removes
the default-Facility bridge, OrganizationLookup.requireDefaultFacilityId.
Context
After ADR 0045 the operational flow named
its Facility, but the readers still found their rows by the owning Business Unit. The monitor
evaluator enumerated owners and ran every detector for each. The KPI and occupancy views grouped by
client_id. The forecasting, simulation and slotting readers filtered stock, picks and locations by
client_id. A stored document, a saved report and an alert belonged to the owner only.
BinBelowReorderDetector reached the replenishment scan through the bridge.
A Business Unit with two Facilities would have mixed them: one alert list, one KPI series, one demand history and one archive for two warehouses. 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 reader names its Facility.
DemandReader,OnHandReader,SimDemandReader,VelocityReader,SlotReader,FixAssignmentReader, the detector readers and the KPI, category-volume, occupancy and report-definition repositories take a typedFacilityIdand filter onfacility_id. The services that call them (ForecastService,SimulationService,SlottingService, the reporting services) take the Facility, and the resources passoperationalFacilityId. - Monitors run per Facility.
MonitorEvaluatorenumerates every active Facility in the organization registry (OrganizationLookup.activeFacilityIds), not the Facilities that hold stock, because detectors also read orders, counts and putaway work. Its first sweep of a Facility seeds that Facility's defaultmonitor_configrows. It runs each Facility in its own failure boundary: one Facility's failure is logged and the others still run. Its registry scope isPER_FACILITY.DetectionContextcarries the Facility, soBinBelowReorderDetectorcalls the replenishment scan for it and no longer resolves a default Facility. - Monitor configuration, alerts and deliveries are Facility-owned.
monitor_configis unique on(facility_id, monitor_key), so two Facilities of one Business Unit configure their monitors apart.AlertextendsFacilityEntity. An alert delivery repeats the Facility of its alert, so the delivery log is read per Facility without a join (V1106). Channel settings (recipients, webhook URL) still resolve through the Business Unit of the Facility. - An alert is a Facility event. It is written with
publishInFacility, not as a master-data event, because an alert describes the state of one Facility. - Reporting reads one Facility. The KPI and occupancy views are keyed by
facility_id, and a saved report definition belongs to one Facility (V1008). The views are dropped and created again, because a view that renames its first column cannot be replaced in place. - The document archive belongs to a Facility.
DocumentStore.storetakes the Facility of the document's subject entity, never the acting principal's.StoredDocumentextendsFacilityEntity, the list and read routes filter on the readable Facilities, and a write outside the writable Facilities is refused withForbidden(V1304).DocumentStoredEventandDocumentDeletedEventcarryfacilityIdand are Facility events. Document templates stay owned by the Business Unit. - The default-Facility bridge is deleted.
OrganizationLookup.requireDefaultFacilityIdand the nullablefindDefaultFacilityIdhave no caller.OrganizationLookup.findDefaultFacilitystays, because two callers legitimately start from a Business Unit and need its only Facility: the demo seeder and warehouse setup, whose request names the unit it works for. - The registry shrinks. The converted resources move from
operationalBusinessUnitIdtooperationalFacilityId, the document store fromreadScope/writeScopetoreadFacilityScope/writeFacilityScope, theclient_idpredicate entries of the converted readers and repositories are removed, andPER_OWNERhas no job left.
Consequences
- Two Facilities of one Business Unit have separate alerts, monitor settings, KPIs, forecasts, simulations, slotting facts, saved reports and stored documents.
- A request that reads or writes one of these needs a Facility, from the principal's grant or from the
X-Karyo-Facility-Idheader, as every other converted area does. - A test that seeds a row for one of these must provision the Facility first
(
TestOrganizations.facilityOf); the row triggers stampfacility_idfromclient_idafterwards. - No module reads ambient scope for a Facility now except through the registered accessors, and no caller reaches a port through the unit's default Facility except the two named above.
client_idstays on the rows as data, as ADRs 0043 to 0045 decided.
Alternatives considered
- Keep the monitors per Business Unit and fan out to Facilities inside each detector. Rejected for the reason in ADR 0044: the bridge hides which Facility a job works in and fails for a unit with two.
- Keep the document archive keyed by owner. Rejected: a document records an entity of one Facility, and an owner-keyed archive would reopen the cross-Facility read the other conversions close.
- Delete
findDefaultFacilitywith the rest of the bridge. Not done: the demo seeder and warehouse setup have a Business Unit and no Facility in hand, and a second lookup would say the same thing.
Evidence
services/monitoring-service/karyo-monitors-core/-MonitorEvaluator,AlertService,MonitorConfigServiceand the detector readersservices/reporting-service/karyo-reporting-core/andservices/karyo-app/src/main/resources/db/migration/reporting/V1008__facility_reporting.sqlservices/forecasting-service/,services/simulation-service/andservices/slotting-service/- the readers under-coreservices/document-service/karyo-docstore-core/,libs/karyo-documents/and.../db/migration/docstore/V1304__facility_documents.sql.../db/migration/monitors/V1106__facility_monitoring.sqlconfig/scope-consumers.jsonandservices/karyo-app/src/test/kotlin/com/karyo/security/guards/- the registry and the guards that hold it