25. Declarative bin registration over REST, and the pick location on every reservation allocation
Status
Accepted. Implemented in the change that introduces this record.
Context
The fleet is getting a warehouse-day simulator that drives every service only through its public REST API, the way real associates do with RF guns. Two gaps in this service blocked that.
- Bins could not be created over HTTP. A
Bin(a coded slot with a capacity) had to exist beforeStowStockwould accept stock, but no endpoint created one. The README said to seed bins straight into Postgres, ande2e-testsinserted rows intobinsdirectly. The service's own BDD suite did the same through theLocationRepo. Bin provisioning was described as "an infrastructure/seed-data concern", but registering and re-sizing slots is ordinary inventory-control work, and doing it by writing to another service's database breaks the hexagonal boundary (ADR 0001). It also skips the aggregate's invariants and the optimistic-concurrency version (ADR 0019). - Reservations did not say where to pick.
POST /reservationsandGET /reservations?demandRef=returned allocations as{stockUnitId, quantity}. TheStockUnitknows its bin (StockUnit.BinID()), but the caller would need one extra lookup per stock unit to find it, and no endpoint offers that lookup. A picker's RF gun therefore had no pick location to display.
Decision
PUT /bins/{binId}: declarative, idempotent registration
A new RegisterBin use case backs PUT /bins/{binId} with body
{"capacity": <int > 0>}. The caller states the capacity it wants, and the
bin converges to that state:
| Current state | Result | Status |
|---|---|---|
| No bin with this id | location.NewBin(id, capacity), saved | 201 Created + Location: /bins/{binId} |
| Bin exists, same capacity | no-op, nothing written (no version bump) | 200 OK |
| Bin exists, different capacity ≥ occupied | Bin.Resize(capacity), saved | 200 OK |
| Bin exists, capacity < occupied | rejected, location.ErrCapacityBelowOccupancy | 409 capacity-below-occupancy |
Every response body is {binId, capacity, occupied, available}. The same
shape comes back from the new read endpoint GET /bins/{binId}, which
returns 404 bin-not-found for an unknown bin.
- The invariant lives in the aggregate.
Bin.Resizerejects a capacity of zero or less (ErrInvalidCapacity) and any capacity below current occupancy (ErrCapacityBelowOccupancy). Shrinking a bin below what it holds would breaksum(stock in bin) <= capacityfor stock that is physically in the slot. Resizing to exactly the occupancy is allowed; the bin simply becomes full.Resizenever changes occupancy, which moves only through stow and pick. - Why PUT and not POST. The caller supplies the identity: a bin id is a
physical label on the rack. Repeating the request must be safe, because
the simulator and real RF guns retry after a dropped response. So
PUTis the natural verb, and it needs noIdempotency-Keymiddleware (ADR 0018 limits that to the two POSTs that create server-identified resources). The 201-vs-200 split matchesPUT /products/{sku}/classification. - Persistence. Writes go through the existing version-guarded
LocationRepo.Save(ADR 0019), inside the optionalUnitOfWork(ADR 0017). If a resize races a concurrent stow on the same bin, it gets409 concurrent-modificationand does not overwrite the stow's occupancy. - Validation mapping follows the existing 400/422 split (ADR 0005):
- malformed JSON is
400 malformed-request-body; - an omitted
capacityis400 capacity-required; 0is422 invalid-bin-capacity(the existingErrInvalidCapacitymapping);- a negative value is
422 negative-quantity; - a value above int32 (the wire format and the
INTEGERcolumn) is422 capacity-out-of-range.
- malformed JSON is
- No domain or integration event. Bin registration is local topology
master data. No consumer in the fleet needs it today, and adding an event
would widen the Kafka contract (ADR 0004) for no reader.
apis/asyncapi.yamlis deliberately unchanged.
allocations[].binId: the pick location
reservation.AllocationgainsBinID.ReserveStock.allocatecaptures it fromunit.BinID()when it draws from a unit. AStockUnitnever changes bin: it is created by a stow into one bin, and nothing moves it. So the bin recorded at reserve time is still the right pick location when the pick happens.- Migration
0008_reservation_allocation_bin_idadds a nullablereservation_allocations.bin_id TEXTand backfills it fromstock_units.bin_id. Because units never move, the backfill is exact. The column stays nullable so that a row the backfill cannot resolve still reads (as an emptyBinID) instead of failing. On upsert the Postgres repo writesbin_idwithCOALESCE, so a later status change can never erase it. - The HTTP DTO exposes it as
allocations[].binId(omitempty) on bothPOST /reservationsandGET /reservations?demandRef=. For any allocation made by this version it is always present.
Hardening found along the way
When apis/openapi.yaml changed, the Schemathesis contract job
(scripts/contract-test.sh) found that POST /reservations accepted an
empty demandRef, even though the spec says minLength: 1. An empty
demandRef is useless: it is the key that ReserveStock's retry guard
and GET /reservations?demandRef= both look up by, and the GET handler
already rejects it as 400 missing-demand-ref. The POST handler now
rejects it the same way.
Consequences
Easier
- Callers can register, inspect and resize bins entirely over REST,
including the simulator,
e2e-tests, and this repo's own BDD suite (features/bin_registration.feature). Nobody needs to write tobinsdirectly any more. - A reservation response now answers "where do I pick?" without a second
lookup.
GET /reservations?demandRef=already backs the Order Lifecycle console, so that screen gets the pick location as well. - The resize rule is unit-tested once, in the aggregate, and every caller shares it.
Harder / accepted trade-offs
PUT /bins/{binId}cannot delete or decommission a bin. A bin that is no longer used just stays registered. Decommissioning would need its own decision about what happens to any stock still in the bin.- There is no authorization on who may register or resize bins: the whole API is unauthenticated (ADR 0015). Inventory control and every other caller have the same reach.
binIdon an allocation is a snapshot taken when the reservation was made. If stock could ever move between bins (re-slotting, consolidation), the snapshot could go stale. That would need a new decision, either re-resolving the bin at read time or re-allocating.- Registering bins here does not tell facility-layout about them. The two bin registries stay separate: facility-layout owns the physical layout and zone attributes (ADR 0009/0013), and this service owns capacity and occupancy. A bin unknown to facility-layout still fails open at stow, exactly as before.