Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

🌠Stock

Overview

Stock is the supply-chain ledger for a health campaign. Every time a commodity (vaccine, bed net, deworming tablet) moves — received into a warehouse, sent to a facility, handed out to a household, lost, or adjusted — Stock records one row for that movement. It also stores physical-count reconciliations so a facility's book stock can be checked against what's actually on the shelf.

In short: "what did we have, where did it go, and does the count still add up?"

Business Flow

  • During campaign setup, opening stock is received into central/warehouse facilities.

  • During the campaign (runtime), field workers and storekeepers record transfers (warehouse → facility → sub-facility) and distributions (facility → beneficiary). Every hand-out of a commodity is a stock OUT transaction.

  • Periodically, a storekeeper does a physical count and submits a reconciliation so discrepancies (theft, breakage, miscount) surface early.

  • The numbers feed the dashboards (via the transformer → Elasticsearch) so programme managers can see stock-on-hand and consumption per facility.

Key APIs / Entry Points

Base path /stock/v1 (transactions) and /stock/reconciliation/v1 (counts). Each entity has single + bulk create/update/delete, and a search.

Transaction endpoints record commodity movements. Reconciliation endpoints record physical stock counts.

Endpoint
Purpose

POST /stock/v1/_create, /bulk/_create

Record a stock movement (single or bulk).

POST /stock/v1/_update, /bulk/_update

Correct a movement.

POST /stock/v1/_delete, /bulk/_delete

Soft-delete a movement.

POST /stock/v1/_search

Find movements (by facility, sender/receiver, product variant, campaignNumber, since-time …).

POST /stock/reconciliation/v1/_create … _search

Same shape for physical-count reconciliations.

Kafka entry points (async). Bulk requests land on create-stock-bulk-topic / update-… / delete-… (and the reconciliation equivalents) and are processed by the service's own consumer. Persisted results go out on save-stock-topic / update-stock-topic / delete-stock-topic (+ reconciliation topics) for the persister and transformer.

Swagger contract:

Kafka topics

Topic
Dir
Purpose

create-stock-bulk-topic

in

Bulk stock-movement create requests

update-stock-bulk-topic

in

Bulk stock-movement update requests

delete-stock-bulk-topic

in

Bulk stock-movement (soft) delete requests

create-stock-reconciliation-bulk-topic

in

Bulk reconciliation create requests

update-stock-reconciliation-bulk-topic

in

Bulk reconciliation update requests

delete-stock-reconciliation-bulk-topic

in

Bulk reconciliation delete requests

save-stock-topic

out

Persist new stock rows (persister + transformer)

update-stock-topic

out

Persist stock updates

delete-stock-topic

out

Persist stock soft-deletes

save-stock-reconciliation-topic

out

Persist new reconciliations

update-stock-reconciliation-topic

out

Persist reconciliation updates

delete-stock-reconciliation-topic

out

Persist reconciliation deletes

Dependencies

  • idgen — generates stock record IDs.

  • facility — validates the facility a movement is for.

  • product — validates the product variant being moved.

  • project / project-facility — optional link of a movement to a project's facility.

  • health-services-common / -models — shared clients, validators, POJOs.

  • Kafka — async create/update/delete pipeline.

  • egov-persister (deployed via the configs/ repo) — actually writes the rows to Postgres off the save-* topics.

  • transformer → Elasticsearch — builds the dashboard read-model from the same topics.

  • Redis — caching used by the shared repository layer.

Process Flow

Writes are asynchronous: the API validates, enriches and acknowledges, then a Kafka consumer persists. The service does not write Postgres directly — it emits a save-* event that egov-persister turns into a row, while the transformer indexes the same event into Elasticsearch for dashboards.

Sequence Diagrams

Stock - Create
Stock - Update
Stock - Search
Reconciliation - Create
Reconciliation - Update
Reconciliation - Search

Data Model

Failure / Retry Handling

  • Async, no batch rollback. A bulk request returns 202 before persistence. If one record in the batch fails validation in the consumer, it does not roll back the others — check consumer logs and the record's status.

  • Idempotency is via clientReferenceId — re-submitting the same one should not create a duplicate row.

  • Optimistic locking via rowVersion protects concurrent transfer updates. Reconciliation count submissions allow concurrent writes and use last-write-wins.

  • Soft delete (isDeleted) everywhere — nothing is hard-deleted; unique constraints include the delete flag.

  • If the persister config for the stock topics is missing/stale in an environment, the API will accept writes but rows will silently not appear in Postgres — a classic "it worked in QA" trap.

Known Risks / Limitations

  • quantity is signed — negative values are valid (loss/consumption) and there is no DB constraint; bad data is an app/validation concern, not a DB guard.

  • transactingPartyId + transactingPartyType are polymorphic with no foreign key — validation is app-level only.

  • transactionType / transactionReason are free strings. Convention is IN/OUT/LOSS/ADJUSTMENT/TRANSFER, but the DB won't stop other values.

  • Reconciliation is per facility, not per product variant — variant-level discrepancies live in a comments field, not structured columns.

  • dateOfEntry (field date) ≠ createdTime (system date) — both matter for audit; don't conflate them.

  • Relaxed concurrency on the count path (v2.1) means simultaneous count submissions are accepted with last-write-wins — acceptable for counts, but a behavioural change QA must be aware of.

Last updated

Was this helpful?