# mauriceos/ubora-health architecture

> Architecture diagram of the GitHub repository mauriceos/ubora-health, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mauriceos/ubora-health
- Repository: https://github.com/mauriceos/ubora-health

## Overview

#### Purpose and entry points
Ubora is a human-governed primary-healthcare stock reconciliation and redistribution demo for Kenyan facilities. It converts bilingual nurse shift reports into structured inventory observations, deterministically audits stock movement, proposes surplus-to-stockout transfers, and produces an approval-gated MoH Form S11.

The Next.js App Router UI starts at `src/app/page.tsx`, with voucher detail/print workflow at `src/app/voucher/[id]/page.tsx`. Server operations are exposed through route handlers under `src/app/api/`.

#### Client application and role workflows
`src/components/UboraApp.tsx` is the principal client shell, coordinating nurse, DHO, and pharmacist experiences. Authentication is demo-local through `src/components/LoginScreen.tsx`; onboarding and language presentation are client concerns in `src/components/Onboarding.tsx` and `src/lib/i18n.ts`.

Nurses submit/review reports and send verified findings onward. DHO users assess ranked redistribution candidates and initiate voucher drafts. Pharmacists complete authorization and print approved paperwork.

#### Extraction boundary: model produces fields only
`src/app/api/extract/route.ts` accepts report text and delegates language extraction to the Gemma integration in `src/lib/gemma/index.ts`. Provider selection, cloud/Ollama connectivity, normalization, and rule fallback policy are isolated in:
- `src/lib/gemma/providers.ts`
- `src/lib/gemma/config.ts`
- `src/lib/gemma/normalize.ts`

The extraction contract is defined by `src/lib/domain/extractionSchema.ts`. This is the critical trust boundary: model output is structured input for deterministic logic, never authorization or transfer arithmetic.

#### Deterministic stock audit
`src/app/api/audit/route.ts` invokes `src/lib/auditor.ts` to reconcile reported shelf counts, bin-card expectations, case mix, and drawdown reasons. Domain catalogs and dispensing baselines live in:
- `src/lib/domain/commodities.ts`
- `src/lib/domain/caseMix.ts`
- `src/lib/domain/extractionSchema.ts`

Audit results distinguish reconciling stock, plausible wastage, missing mandatory lot details, register gaps, and unexplained drawdown. Only verified surplus can cross into redistribution; contradictions are held for recount.

#### Redistribution matching
`src/app/api/match/route.ts` fronts `src/lib/matcher.ts`, which matches eligible surplus lots to facility deficits and ranks candidates using expiry loss avoided minus transport cost. Facility/corridor reference data is in `src/lib/data/facilities.ts`.

This stage consumes audited, eligible lots rather than raw model output, preserving the audit gate before any logistics proposal.

#### Voucher lifecycle and governance
Voucher creation, retrieval, approval, and printing are served by:
- `src/app/api/vouchers/route.ts`
- `src/app/api/vouchers/[id]/approve/route.ts`
- `src/app/api/vouchers/[id]/print/route.ts`

State and authorization sequencing are maintained in `src/lib/store.ts`; S11 fields and rendering templates are separated into `src/lib/s11-fields.ts` and `src/lib/s11-template.ts`. The enforced lifecycle is draft, issuer confirmation, receiver confirmation, then pharmacist authorization; no model or single actor can finalize a transfer.

#### Runtime, persistence, and external services
The runtime is Next.js 16/React 19/TypeScript, with API routes running server-side. Google AI Studio/Gemini is the primary Gemma 4 provider; local Ollama is the offline HTTP alternative. `GOOGLE_API_KEY` remains server-only. Persistence appears application-local via `src/lib/store.ts`, appropriate for the demo rather than durable multi-user production storage.

Audit reporting is available through `src/app/api/audit/route.ts`; demo/corridor setup endpoints are `src/app/api/demo/route.ts` and `src/app/api/bootstrap/route.ts`.

Main entry points: [`src/components/UboraApp.tsx`](https://github.com/mauriceos/ubora-health/blob/main/src/components/UboraApp.tsx) (Ubora client shell), [`src/app/api/vouchers/route.ts`](https://github.com/mauriceos/ubora-health/blob/main/src/app/api/vouchers/route.ts) (Voucher lifecycle API), [`src/app/api/audit/route.ts`](https://github.com/mauriceos/ubora-health/blob/main/src/app/api/audit/route.ts) (Audit endpoint), [`src/lib/gemma/index.ts`](https://github.com/mauriceos/ubora-health/blob/main/src/lib/gemma/index.ts) (Gemma extraction gateway)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client workflows"]
  node_ui["Ubora client shell<br/>React client<br/>[UboraApp.tsx]"]
  node_login["Demo login<br/>local auth<br/>[LoginScreen.tsx]"]
  node_home["Application entry<br/>Next.js page<br/>[page.tsx]"]
  node_voucher_page["Voucher detail &amp; print<br/>Next.js page<br/>[page.tsx]"]
end

subgraph group_api["Server API"]
  node_extract_api["Extraction endpoint<br/>route handler<br/>[route.ts]"]
  node_audit_api["Audit endpoint<br/>route handler<br/>[route.ts]"]
  node_match_api["Matching endpoint<br/>route handler<br/>[route.ts]"]
  node_voucher_api["Voucher lifecycle API<br/>route handlers<br/>[route.ts]"]
  node_setup_api["Demo setup<br/>route handlers<br/>[route.ts]"]
end

subgraph group_domain["Deterministic domain"]
  node_extraction_contract["Extraction contract<br/>domain schema"]
  node_auditor{{"Stock auditor<br/>deterministic reconciliation<br/>[auditor.ts]"}}
  node_clinical_baselines["Clinical baselines<br/>domain catalogs<br/>[commodities.ts]"]
  node_matcher{{"Redistribution matcher<br/>deterministic ranking<br/>[matcher.ts]"}}
  node_facility_data["Facility corridors<br/>reference data<br/>[facilities.ts]"]
  node_store[("Application state<br/>local persistence<br/>[store.ts]")]
  node_s11["MoH Form S11<br/>fields and template<br/>[s11-template.ts]"]
end

subgraph group_external["Model providers"]
  node_gemma{{"Gemma extraction gateway<br/>model integration<br/>[index.ts]"}}
  node_providers["Gemini / Ollama<br/>AI providers<br/>[providers.ts]"]
end

node_home -->|"renders"| node_ui
node_login -->|"starts demo session"| node_ui
node_ui -->|"submits shift report"| node_extract_api
node_extract_api -->|"delegates extraction"| node_gemma
node_gemma -->|"selects provider"| node_providers
node_gemma -->|"returns validated fields"| node_extraction_contract
node_ui -->|"sends reported observations"| node_audit_api
node_audit_api -->|"reconciles stock"| node_auditor
node_extraction_contract -->|"structured observations"| node_auditor
node_clinical_baselines -->|"expected dispensing inputs"| node_auditor
node_ui -->|"requests redistribution candidates"| node_match_api
node_audit_api -->|"verified eligible surplus only"| node_match_api
node_match_api -->|"ranks transfers"| node_matcher
node_facility_data -->|"deficits and transport corridors"| node_matcher
node_ui -->|"drafts and confirms transfer"| node_voucher_api
node_matcher -->|"proposed transfer"| node_voucher_api
node_voucher_api -->|"enforces staged approvals"| node_store
node_voucher_api -->|"generates approved form"| node_s11
node_voucher_page -->|"retrieves, authorizes, prints"| node_voucher_api
node_setup_api -->|"seeds demo state"| node_store

click node_ui "https://github.com/mauriceos/ubora-health/blob/main/src/components/UboraApp.tsx"
click node_login "https://github.com/mauriceos/ubora-health/blob/main/src/components/LoginScreen.tsx"
click node_home "https://github.com/mauriceos/ubora-health/blob/main/src/app/page.tsx"
click node_voucher_page "https://github.com/mauriceos/ubora-health/blob/main/src/app/voucher/%5Bid%5D/page.tsx"
click node_extract_api "https://github.com/mauriceos/ubora-health/blob/main/src/app/api/extract/route.ts"
click node_audit_api "https://github.com/mauriceos/ubora-health/blob/main/src/app/api/audit/route.ts"
click node_match_api "https://github.com/mauriceos/ubora-health/blob/main/src/app/api/match/route.ts"
click node_voucher_api "https://github.com/mauriceos/ubora-health/blob/main/src/app/api/vouchers/route.ts"
click node_gemma "https://github.com/mauriceos/ubora-health/blob/main/src/lib/gemma/index.ts"
click node_providers "https://github.com/mauriceos/ubora-health/blob/main/src/lib/gemma/providers.ts"
click node_extraction_contract "https://github.com/mauriceos/ubora-health/blob/main/src/lib/domain/extractionSchema.ts"
click node_auditor "https://github.com/mauriceos/ubora-health/blob/main/src/lib/auditor.ts"
click node_clinical_baselines "https://github.com/mauriceos/ubora-health/blob/main/src/lib/domain/commodities.ts"
click node_matcher "https://github.com/mauriceos/ubora-health/blob/main/src/lib/matcher.ts"
click node_facility_data "https://github.com/mauriceos/ubora-health/blob/main/src/lib/data/facilities.ts"
click node_store "https://github.com/mauriceos/ubora-health/blob/main/src/lib/store.ts"
click node_s11 "https://github.com/mauriceos/ubora-health/blob/main/src/lib/s11-template.ts"
click node_setup_api "https://github.com/mauriceos/ubora-health/blob/main/src/app/api/bootstrap/route.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_ui,node_login,node_home,node_voucher_page toneBlue
class node_extract_api,node_audit_api,node_match_api,node_voucher_api,node_setup_api toneAmber
class node_extraction_contract,node_auditor,node_clinical_baselines,node_matcher,node_facility_data,node_store,node_s11 toneMint
class node_gemma,node_providers toneRose
```

## Components

### Client workflows

- [Ubora client shell](https://github.com/mauriceos/ubora-health/blob/main/src/components/UboraApp.tsx): React client · Coordinates nurse, DHO, and pharmacist workflows. · `src/components/UboraApp.tsx`
- [Demo login](https://github.com/mauriceos/ubora-health/blob/main/src/components/LoginScreen.tsx): local auth · `src/components/LoginScreen.tsx`
- [Application entry](https://github.com/mauriceos/ubora-health/blob/main/src/app/page.tsx): Next.js page · `src/app/page.tsx`
- [Voucher detail & print](https://github.com/mauriceos/ubora-health/blob/main/src/app/voucher/%5Bid%5D/page.tsx): Next.js page · `src/app/voucher/[id]/page.tsx`

### Server API

- [Extraction endpoint](https://github.com/mauriceos/ubora-health/blob/main/src/app/api/extract/route.ts): route handler · `src/app/api/extract/route.ts`
- [Audit endpoint](https://github.com/mauriceos/ubora-health/blob/main/src/app/api/audit/route.ts): route handler · `src/app/api/audit/route.ts`
- [Matching endpoint](https://github.com/mauriceos/ubora-health/blob/main/src/app/api/match/route.ts): route handler · `src/app/api/match/route.ts`
- [Voucher lifecycle API](https://github.com/mauriceos/ubora-health/blob/main/src/app/api/vouchers/route.ts): route handlers · Creates, approves, and prints vouchers. · `src/app/api/vouchers/route.ts`
- [Demo setup](https://github.com/mauriceos/ubora-health/blob/main/src/app/api/bootstrap/route.ts): route handlers · Bootstraps demo and corridor data. · `src/app/api/bootstrap/route.ts`

### Deterministic domain

- [Extraction contract](https://github.com/mauriceos/ubora-health/blob/main/src/lib/domain/extractionSchema.ts): domain schema · Model output is structured input, never transfer authority. · `src/lib/domain/extractionSchema.ts`
- [Stock auditor](https://github.com/mauriceos/ubora-health/blob/main/src/lib/auditor.ts): deterministic reconciliation · Verifies surplus and holds contradictions for recount. · `src/lib/auditor.ts`
- [Clinical baselines](https://github.com/mauriceos/ubora-health/blob/main/src/lib/domain/commodities.ts): domain catalogs · Commodity and case-mix dispensing references. · `src/lib/domain/commodities.ts`
- [Redistribution matcher](https://github.com/mauriceos/ubora-health/blob/main/src/lib/matcher.ts): deterministic ranking · Ranks eligible-lot transfers by expiry loss avoided less transport cost. · `src/lib/matcher.ts`
- [Facility corridors](https://github.com/mauriceos/ubora-health/blob/main/src/lib/data/facilities.ts): reference data · `src/lib/data/facilities.ts`
- [Application state](https://github.com/mauriceos/ubora-health/blob/main/src/lib/store.ts): local persistence · Maintains voucher state and authorization sequence. · `src/lib/store.ts`
- [MoH Form S11](https://github.com/mauriceos/ubora-health/blob/main/src/lib/s11-template.ts): fields and template · Renders approval-gated transfer paperwork. · `src/lib/s11-template.ts`

### Model providers

- [Gemma extraction gateway](https://github.com/mauriceos/ubora-health/blob/main/src/lib/gemma/index.ts): model integration · Selects provider and normalizes extracted fields. · `src/lib/gemma/index.ts`
- [Gemini / Ollama](https://github.com/mauriceos/ubora-health/blob/main/src/lib/gemma/providers.ts): AI providers · Cloud primary with offline local alternative. · `src/lib/gemma/providers.ts`

## Connections

- Application entry → Ubora client shell: renders
- Demo login → Ubora client shell: starts demo session
- Ubora client shell → Extraction endpoint: submits shift report
- Extraction endpoint → Gemma extraction gateway: delegates extraction
- Gemma extraction gateway → Gemini / Ollama: selects provider
- Gemma extraction gateway → Extraction contract: returns validated fields
- Ubora client shell → Audit endpoint: sends reported observations
- Audit endpoint → Stock auditor: reconciles stock
- Extraction contract → Stock auditor: structured observations
- Clinical baselines → Stock auditor: expected dispensing inputs
- Ubora client shell → Matching endpoint: requests redistribution candidates
- Audit endpoint → Matching endpoint: verified eligible surplus only
- Matching endpoint → Redistribution matcher: ranks transfers
- Facility corridors → Redistribution matcher: deficits and transport corridors
- Ubora client shell → Voucher lifecycle API: drafts and confirms transfer
- Redistribution matcher → Voucher lifecycle API: proposed transfer
- Voucher lifecycle API → Application state: enforces staged approvals
- Voucher lifecycle API → MoH Form S11: generates approved form
- Voucher detail & print → Voucher lifecycle API: retrieves, authorizes, prints
- Demo setup → Application state: seeds demo state
