How medic/cht-core works
Purpose and runtime topology
CHT Core is an offline-capable community-health application framework: health workers capture contacts, reports, tasks, and messages; supervisors/admins manage, search, export, and monitor them. It is a multi-service system, not just a web app: Angular browser clients, Node.js API and background workers, CouchDB, and reverse proxies are deployed as Docker/Kubernetes services.
Core process entry points are api/server.js and sentinel/server.js; the browser client starts at webapp/src/ts/main.ts; the legacy administration UI starts at admin/src/js/main.js.
Client applications and offline boundary
The primary user client is an Angular/TypeScript SPA organized around routed operational domains—contacts, reports, tasks, messages, analytics, and training—under webapp/src/ts/modules/ and routed by webapp/src/ts/app-routing.module.ts. NgRx-style actions, reducers, selectors, and effects separate UI state from database/network work.
Offline-first behavior is a defining boundary: bootstrapping establishes local storage, initial replication, local design documents/search indexes, purging, translations, and service-worker registration in webapp/src/js/bootstrapper/index.js and webapp/src/js/bootstrapper/initial-replication.js. Database synchronization is managed by webapp/src/ts/services/db-sync.service.ts.
A separate AngularJS admin client provides user/role, settings, upgrade, translation, branding, SMS, and export administration via admin/src/js/controllers/ and admin/src/js/services/db.js.
API gateway and domain API
The Node.js API owns HTTP routing, authentication, authorization, request shaping, rate limiting, and server-side domain operations. Routing and server composition are centered on api/src/routing.js, api/src/auth.js, and api/src/server-utils.js. Controllers expose contacts, people/places, reports, forms, records, users, settings, exports, monitoring, and replication endpoints in api/src/controllers/.
The API is the boundary between clients and CouchDB: its database adapters (api/src/db.js, api/src/db-batch.js) and middleware (api/src/middleware/authorization.js, api/src/middleware/rate-limiter.js) enforce access and operational policy. Login, public privacy pages, and static web assets are also served from api/src/public/.
Document data platform and replication
CouchDB is the source of truth for application documents, users, metadata, logs, SMS, and Sentinel state. Database setup, design-document deployment, index readiness, default-document upload, and upgrade sequencing are orchestrated by api/src/services/setup/, api/src/migrations.js, and api/src/upload-default-docs.js.
Design documents encode data access, validation, and search schemas. Key examples are ddocs/medic-db/medic/validate_doc_update.js, ddocs/medic-db/medic-client/views/, and ddocs/medic-db/medic/nouveau/. Controlled replication APIs protect per-user/offline datasets via api/src/services/replication/ and api/src/controllers/replication.js.
Asynchronous clinical workflow engine
Sentinel is the Node.js background worker that consumes database changes and scheduled jobs. Its core loop and processing hooks are in sentinel/src/transitions.js, sentinel/src/lib/feed.js, and sentinel/src/schedule/index.js.
It applies reusable transition rules from shared-libs/transitions/src/transitions/ to incoming reports/documents: registration, alerts, notifications, scheduled reports/tasks, muting, outbound marking, and data cleanup. It also runs reminder, replication, outbound, purging, and background-cleanup schedules under sentinel/src/schedule/. This separates eventual clinical workflow processing from synchronous user/API requests.
Forms, messaging, and integrations
Forms are XForms/XML artifacts, transformed server-side and rendered in the client through Enketo integration: api/src/services/forms.js, api/src/enketo-transformer/, and webapp/src/ts/services/enketo.service.ts. Submitted forms become report/contact documents, replicate through CouchDB, and then trigger Sentinel transitions.
Messaging supports SMS gateway workflows and external providers including Africa’s Talking and RapidPro through api/src/services/messaging.js, api/src/services/africas-talking.js, and api/src/services/rapidpro.js. Outbound work is persisted and scheduled rather than coupled directly to form submission.
Deployment and shared contracts
Deployments package API, Sentinel, CouchDB, HAProxy, Nginx, health checking, and CouchDB Nouveau search. Container/service definitions are rooted in api/Dockerfile, sentinel/Dockerfile, couchdb/Dockerfile, and Helm templates in scripts/build/helm/templates/. HAProxy fronts CouchDB/service traffic (haproxy/backend.cfg.template); Nginx serves TLS/static edge concerns (nginx/nginx.conf).
shared-libs/ contains cross-runtime business contracts, notably rules evaluation (shared-libs/rules-engine/src/), document access (shared-libs/cht-datasource/src/), lineage (shared-libs/lineage/src/), and transition logic. Application-specific behavior and default health-program content are supplied as deployable configuration bundles in config/default/, including settings, forms, tasks, targets, resources, and translations.
Main entry points: Dockerfile CouchDB · routing.js Domain controllers · server.js HTTP API · main.ts Angular health-worker SPA
Components 22
Client applications
- Angular health-worker SPA · Operational UI for contacts, reports, tasks, messages, analytics, and training.
webapp/src/ts/main.ts - Operational routes
webapp/src/ts/app-routing.module.ts - Offline bootstrap · Initializes local storage, replication, indexes, purge, translations, and service worker.
webapp/src/js/bootstrapper/index.js - Client DB sync
webapp/src/ts/services/db-sync.service.ts - Legacy admin UI · Administration for users, settings, upgrades, exports, and integrations.
admin/src/js/main.js
API service
- HTTP API · Authenticates, authorizes, shapes, and routes client requests.
api/server.js - Domain controllers · Contacts, reports, forms, records, users, settings, exports, monitoring, and replication.
api/src/routing.js - Access and rate policy
api/src/middleware/authorization.js - CouchDB adapters
api/src/db.js - Forms and Enketo transform · Transforms XForms for client rendering and submission.
api/src/services/forms.js - Controlled replication · Protects per-user offline datasets.
- External messaging · Schedules SMS work for Africa's Talking and RapidPro.
api/src/services/messaging.js
Async workflow
- Sentinel worker · Processes changes and scheduled clinical work outside request paths.
sentinel/server.js - Change feed and transitions
sentinel/src/transitions.js - Background schedules · Runs reminder, replication, outbound, purge, and cleanup jobs.
sentinel/src/schedule/index.js - Clinical transition rules · Registration, alerts, notifications, tasks, muting, and cleanup rules.
Document platform
- CouchDB · Source of truth for application, user, messaging, log, and Sentinel documents.
couchdb/Dockerfile - Design docs and search · Validation, views, and Nouveau search schemas.
ddocs/medic-db/medic/validate_doc_update.js - Database setup and migration · Deploys design docs, defaults, indexes, and upgrades.
Deployment
- Health-program bundles · Default settings, forms, tasks, targets, resources, and translations.
- Proxy and TLS edge · Nginx serves edge/static concerns; HAProxy fronts CouchDB and services.
nginx/nginx.conf - Kubernetes packaging · Packages API, Sentinel, CouchDB, proxies, and search services.
Connections 27
- Angular health-worker SPA → Operational routes: navigates
- Angular health-worker SPA → Offline bootstrap: initializes
- Offline bootstrap → Client DB sync: starts
- Client DB sync → Controlled replication: syncs via
- Angular health-worker SPA → HTTP API: uses HTTP API
- Legacy admin UI → HTTP API: admin requests
- Proxy and TLS edge → HTTP API: routes traffic
- HTTP API → Domain controllers: composes
- Domain controllers → Access and rate policy: enforces
- Domain controllers → CouchDB adapters: serves domains through
- Domain controllers → Forms and Enketo transform: serves forms
- Domain controllers → Controlled replication: exposes
- CouchDB adapters → CouchDB: reads and writes
- Controlled replication → CouchDB: filters datasets
- Forms and Enketo transform → CouchDB: persists submissions
- CouchDB → Change feed and transitions: document changes
- Sentinel worker → Change feed and transitions: runs
- Sentinel worker → Background schedules: runs
- Change feed and transitions → Clinical transition rules: applies
- Clinical transition rules → CouchDB: updates workflow documents
- Background schedules → External messaging: dispatches outbound work
- Database setup and migration → CouchDB: provisions
- Database setup and migration → Design docs and search: deploys
- Health-program bundles → Database setup and migration: supplies defaults
- Kubernetes packaging → HTTP API: deploys
- Kubernetes packaging → Sentinel worker: deploys
- Kubernetes packaging → CouchDB: deploys