# anirban4ru/meditrace architecture

> Architecture diagram of the GitHub repository anirban4ru/meditrace, 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/anirban4ru/meditrace
- Repository: https://github.com/anirban4ru/meditrace

## Overview

#### Purpose and Entry Points
MediTrace is a Next.js 14 pharmaceutical supply-chain ledger that combines on-chain batch provenance, cold-chain telemetry, inspection, and compliance reporting. The primary UI entry is `app/page.tsx`; batch-specific tracking is served by `app/track/[batchId]/page.tsx`. Global rendering, styles, and error boundaries originate in `app/layout.tsx`, `app/globals.css`, and `app/error.tsx`.

#### Application Shell and Role Experiences
The App Router frontend composes authentication, theme, and ledger state around specialized operational views. Manufacturer, pharmacy/inspection, role-administration, and telemetry workflows are represented by `components/views/manufacturer-dashboard.tsx`, `components/views/pharmacy-terminal.tsx`, `components/views/role-allocation-dashboard.tsx`, and `components/views/telemetry-console.tsx`. Shared state boundaries include `components/auth-context.tsx` and `components/ledger-context.tsx`.

#### Blockchain Ledger Boundary
Immutable supply-chain operations and access roles are defined in the Solidity contract at `contracts/MedicineTracker.sol`, configured for Hardhat in `hardhat.config.ts`. Frontend integration depends on the contract ABI and ledger-facing client logic in `lib/abi.ts` and `lib/engine.ts`. The intended network is Arbitrum Sepolia; wallet signatures authorize role changes, batch creation/handovers, and irreversible spoilage state.

#### Batch Lifecycle and Tracking Flow
A manufacturer creates or advances a batch through the ledger; subsequent carrier/pharmacy custody and status changes become contract-backed provenance. The application’s batch detail route, `app/track/[batchId]/page.tsx`, reads and presents this lifecycle, with route/chain visualization in `components/route-map.tsx` and explorer-oriented access in `lib/explorer.ts`. Simulation/reference data is centralized rather than embedded solely in views via `lib/engine.ts` and `lib/reference-library.ts`.

#### Telemetry, Webhook, and Alert Ingestion
Cold-chain conditions feed the telemetry console and may result in an on-chain spoiled batch. Alchemy sends blockchain event notifications to `app/api/webhooks/alchemy/route.ts`; this handler is the trust boundary for HMAC-verified external payloads before persistence/alert propagation. The webhook simulator at `scripts/alchemy-webhook-simulator.js` supports local event-flow validation. User-facing alert aggregation is implemented in `components/alerts-inbox.tsx`.

#### Supabase Data and Authorization
Supabase provides PostgreSQL-backed application state and RBAC-adjacent data outside the immutable ledger. Client connection code is in `lib/supabase-client.ts`; database shape and enforced row-access policy are defined in `supabase/schema.sql` and `supabase/rls-policies.sql`. Authentication context (`components/auth-context.tsx`) controls frontend identity/role state, while the smart contract remains the authority for on-chain permissions.

#### Inspection and Compliance Outputs
Pharmacy verification includes client-side computer-vision processing through `lib/cv-pipeline.ts` and the browser worker `public/cv.worker.js`, surfaced through `components/views/pharmacy-terminal.tsx`. Audit evidence combines ledger history and telemetry into downloadable compliance material through `components/audit-log.tsx` and `lib/pdf-report.ts`. Batch/operational exports are supported by `lib/csv-export.ts`.

#### Supporting Server Interfaces
The server exposes a news endpoint at `app/api/news/route.ts` and webhook ingestion at `app/api/webhooks/alchemy/route.ts`; these are the repository’s explicit backend HTTP boundaries. Deployment is a Next.js serverless/Edge-oriented runtime, backed by Supabase, Arbitrum Sepolia, and Alchemy, with client-side OpenCV for physical-package checks.

Main entry points: [`lib/engine.ts`](https://github.com/anirban4ru/meditrace/blob/main/lib/engine.ts) (Ledger engine), [`app/layout.tsx`](https://github.com/anirban4ru/meditrace/blob/main/app/layout.tsx) (App shell), [`components/audit-log.tsx`](https://github.com/anirban4ru/meditrace/blob/main/components/audit-log.tsx) (Audit evidence), [`app/track/[batchId]/page.tsx`](https://github.com/anirban4ru/meditrace/blob/main/app/track/%5BbatchId%5D/page.tsx) (Batch tracking)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Next.js frontend"]
  node_shell["App shell<br/>Next.js App Router<br/>[layout.tsx]"]
  node_home["Operations home<br/>page<br/>[page.tsx]"]
  node_tracking["Batch tracking<br/>dynamic page<br/>[page.tsx]"]
  node_auth["Identity context<br/>React context<br/>[auth-context.tsx]"]
  node_ledger_context["Ledger state<br/>React context<br/>[ledger-context.tsx]"]
  node_role_views["Role operations<br/>dashboard views"]
  node_route_map["Provenance map<br/>visualization<br/>[route-map.tsx]"]
end

subgraph group_ledger["Ledger integration"]
  node_ledger_engine["Ledger engine<br/>chain client<br/>[engine.ts]"]
  node_contract_abi["Contract ABI<br/>contract interface<br/>[abi.ts]"]
  node_contract{{"MedicineTracker<br/>Solidity contract"}}
  node_explorer["Explorer access<br/>chain utility<br/>[explorer.ts]"]
end

subgraph group_data["Supabase"]
  node_supabase_client["Supabase client<br/>database client<br/>[supabase-client.ts]"]
  node_schema[("Database schema<br/>PostgreSQL schema<br/>[schema.sql]")]
  node_rls["Row access policies<br/>RLS policy<br/>[rls-policies.sql]"]
end

subgraph group_ingestion["Event ingestion"]
  node_alchemy_webhook["Alchemy webhook<br/>HTTP route<br/>[route.ts]"]
  node_webhook_simulator["Webhook simulator<br/>local script"]
  node_alerts["Alerts inbox<br/>alert UI<br/>[alerts-inbox.tsx]"]
end

subgraph group_compliance["Inspection &amp; reporting"]
  node_pharmacy["Pharmacy terminal<br/>inspection view"]
  node_cv_pipeline["Package CV pipeline<br/>browser vision pipeline<br/>[cv-pipeline.ts]"]
  node_cv_worker["OpenCV worker<br/>browser worker<br/>[cv.worker.js]"]
  node_audit_log["Audit evidence<br/>audit UI<br/>[audit-log.tsx]"]
  node_pdf_report["Compliance PDF<br/>report generator<br/>[pdf-report.ts]"]
  node_csv_export["Operational CSV<br/>export utility<br/>[csv-export.ts]"]
end

node_shell -->|"renders"| node_home
node_shell -->|"renders"| node_tracking
node_shell -->|"provides"| node_auth
node_shell -->|"provides"| node_ledger_context
node_home -->|"hosts"| node_role_views
node_auth -->|"identity and role state"| node_role_views
node_ledger_context -->|"ledger state"| node_role_views
node_role_views -->|"creates and advances batches"| node_ledger_engine
node_tracking -->|"reads lifecycle"| node_ledger_engine
node_tracking -->|"visualizes route"| node_route_map
node_tracking -->|"links transactions"| node_explorer
node_ledger_engine -->|"uses"| node_contract_abi
node_ledger_engine -->|"reads and writes"| node_contract
node_auth -->|"uses identity session"| node_supabase_client
node_supabase_client -->|"persists application state"| node_schema
node_rls -->|"enforces row access"| node_schema
node_webhook_simulator -.->|"sends test events"| node_alchemy_webhook
node_alchemy_webhook -->|"persists event state"| node_supabase_client
node_alchemy_webhook -->|"propagates alerts"| node_alerts
node_alerts -.->|"surfaces spoilage status"| node_ledger_engine
node_pharmacy -->|"runs inspection"| node_cv_pipeline
node_cv_pipeline -->|"offloads browser CV"| node_cv_worker
node_pharmacy -->|"verifies batch"| node_ledger_engine
node_audit_log -->|"collects ledger history"| node_ledger_engine
node_audit_log -->|"collects telemetry evidence"| node_supabase_client
node_audit_log -->|"generates compliance report"| node_pdf_report
node_audit_log -->|"exports operations"| node_csv_export

click node_shell "https://github.com/anirban4ru/meditrace/blob/main/app/layout.tsx"
click node_home "https://github.com/anirban4ru/meditrace/blob/main/app/page.tsx"
click node_tracking "https://github.com/anirban4ru/meditrace/blob/main/app/track/%5BbatchId%5D/page.tsx"
click node_auth "https://github.com/anirban4ru/meditrace/blob/main/components/auth-context.tsx"
click node_ledger_context "https://github.com/anirban4ru/meditrace/blob/main/components/ledger-context.tsx"
click node_role_views "https://github.com/anirban4ru/meditrace/blob/main/components/views/manufacturer-dashboard.tsx"
click node_route_map "https://github.com/anirban4ru/meditrace/blob/main/components/route-map.tsx"
click node_ledger_engine "https://github.com/anirban4ru/meditrace/blob/main/lib/engine.ts"
click node_contract_abi "https://github.com/anirban4ru/meditrace/blob/main/lib/abi.ts"
click node_contract "https://github.com/anirban4ru/meditrace/blob/main/contracts/MedicineTracker.sol"
click node_explorer "https://github.com/anirban4ru/meditrace/blob/main/lib/explorer.ts"
click node_supabase_client "https://github.com/anirban4ru/meditrace/blob/main/lib/supabase-client.ts"
click node_schema "https://github.com/anirban4ru/meditrace/blob/main/supabase/schema.sql"
click node_rls "https://github.com/anirban4ru/meditrace/blob/main/supabase/rls-policies.sql"
click node_alchemy_webhook "https://github.com/anirban4ru/meditrace/blob/main/app/api/webhooks/alchemy/route.ts"
click node_webhook_simulator "https://github.com/anirban4ru/meditrace/blob/main/scripts/alchemy-webhook-simulator.js"
click node_alerts "https://github.com/anirban4ru/meditrace/blob/main/components/alerts-inbox.tsx"
click node_pharmacy "https://github.com/anirban4ru/meditrace/blob/main/components/views/pharmacy-terminal.tsx"
click node_cv_pipeline "https://github.com/anirban4ru/meditrace/blob/main/lib/cv-pipeline.ts"
click node_cv_worker "https://github.com/anirban4ru/meditrace/blob/main/public/cv.worker.js"
click node_audit_log "https://github.com/anirban4ru/meditrace/blob/main/components/audit-log.tsx"
click node_pdf_report "https://github.com/anirban4ru/meditrace/blob/main/lib/pdf-report.ts"
click node_csv_export "https://github.com/anirban4ru/meditrace/blob/main/lib/csv-export.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_shell,node_home,node_tracking,node_auth,node_ledger_context,node_role_views,node_route_map toneBlue
class node_ledger_engine,node_contract_abi,node_contract,node_explorer toneAmber
class node_supabase_client,node_schema,node_rls toneMint
class node_alchemy_webhook,node_webhook_simulator,node_alerts toneRose
class node_pharmacy,node_cv_pipeline,node_cv_worker,node_audit_log,node_pdf_report,node_csv_export toneIndigo
```

## Components

### Next.js frontend

- [App shell](https://github.com/anirban4ru/meditrace/blob/main/app/layout.tsx): Next.js App Router · Global rendering, styling, and error boundary. · `app/layout.tsx`
- [Operations home](https://github.com/anirban4ru/meditrace/blob/main/app/page.tsx): page · `app/page.tsx`
- [Batch tracking](https://github.com/anirban4ru/meditrace/blob/main/app/track/%5BbatchId%5D/page.tsx): dynamic page · Presents a batch lifecycle and provenance. · `app/track/[batchId]/page.tsx`
- [Identity context](https://github.com/anirban4ru/meditrace/blob/main/components/auth-context.tsx): React context · Holds frontend identity and role state. · `components/auth-context.tsx`
- [Ledger state](https://github.com/anirban4ru/meditrace/blob/main/components/ledger-context.tsx): React context · `components/ledger-context.tsx`
- [Role operations](https://github.com/anirban4ru/meditrace/blob/main/components/views/manufacturer-dashboard.tsx): dashboard views · Manufacturer, pharmacy, role administration, and telemetry workflows. · `components/views/manufacturer-dashboard.tsx`
- [Provenance map](https://github.com/anirban4ru/meditrace/blob/main/components/route-map.tsx): visualization · `components/route-map.tsx`

### Ledger integration

- [Ledger engine](https://github.com/anirban4ru/meditrace/blob/main/lib/engine.ts): chain client · Centralizes ledger-facing operations and simulation data. · `lib/engine.ts`
- [Contract ABI](https://github.com/anirban4ru/meditrace/blob/main/lib/abi.ts): contract interface · `lib/abi.ts`
- [MedicineTracker](https://github.com/anirban4ru/meditrace/blob/main/contracts/MedicineTracker.sol): Solidity contract · Immutable provenance, custody, roles, and spoilage authority. · `contracts/MedicineTracker.sol`
- [Explorer access](https://github.com/anirban4ru/meditrace/blob/main/lib/explorer.ts): chain utility · `lib/explorer.ts`

### Supabase

- [Supabase client](https://github.com/anirban4ru/meditrace/blob/main/lib/supabase-client.ts): database client · `lib/supabase-client.ts`
- [Database schema](https://github.com/anirban4ru/meditrace/blob/main/supabase/schema.sql): PostgreSQL schema · `supabase/schema.sql`
- [Row access policies](https://github.com/anirban4ru/meditrace/blob/main/supabase/rls-policies.sql): RLS policy · `supabase/rls-policies.sql`

### Event ingestion

- [Alchemy webhook](https://github.com/anirban4ru/meditrace/blob/main/app/api/webhooks/alchemy/route.ts): HTTP route · HMAC-verifies external blockchain event payloads. · `app/api/webhooks/alchemy/route.ts`
- [Webhook simulator](https://github.com/anirban4ru/meditrace/blob/main/scripts/alchemy-webhook-simulator.js): local script · `scripts/alchemy-webhook-simulator.js`
- [Alerts inbox](https://github.com/anirban4ru/meditrace/blob/main/components/alerts-inbox.tsx): alert UI · `components/alerts-inbox.tsx`

### Inspection & reporting

- [Pharmacy terminal](https://github.com/anirban4ru/meditrace/blob/main/components/views/pharmacy-terminal.tsx): inspection view · Surfaces package verification for pharmacy workflows. · `components/views/pharmacy-terminal.tsx`
- [Package CV pipeline](https://github.com/anirban4ru/meditrace/blob/main/lib/cv-pipeline.ts): browser vision pipeline · `lib/cv-pipeline.ts`
- [OpenCV worker](https://github.com/anirban4ru/meditrace/blob/main/public/cv.worker.js): browser worker · `public/cv.worker.js`
- [Audit evidence](https://github.com/anirban4ru/meditrace/blob/main/components/audit-log.tsx): audit UI · Combines ledger history and telemetry evidence. · `components/audit-log.tsx`
- [Compliance PDF](https://github.com/anirban4ru/meditrace/blob/main/lib/pdf-report.ts): report generator · `lib/pdf-report.ts`
- [Operational CSV](https://github.com/anirban4ru/meditrace/blob/main/lib/csv-export.ts): export utility · `lib/csv-export.ts`

## Connections

- App shell → Operations home: renders
- App shell → Batch tracking: renders
- App shell → Identity context: provides
- App shell → Ledger state: provides
- Operations home → Role operations: hosts
- Identity context → Role operations: identity and role state
- Ledger state → Role operations: ledger state
- Role operations → Ledger engine: creates and advances batches
- Batch tracking → Ledger engine: reads lifecycle
- Batch tracking → Provenance map: visualizes route
- Batch tracking → Explorer access: links transactions
- Ledger engine → Contract ABI: uses
- Ledger engine → MedicineTracker: reads and writes · Wallet signatures authorize state transitions.
- Identity context → Supabase client: uses identity session
- Supabase client → Database schema: persists application state
- Row access policies → Database schema: enforces row access
- Webhook simulator → Alchemy webhook: sends test events
- Alchemy webhook → Supabase client: persists event state
- Alchemy webhook → Alerts inbox: propagates alerts
- Alerts inbox → Ledger engine: surfaces spoilage status
- Pharmacy terminal → Package CV pipeline: runs inspection
- Package CV pipeline → OpenCV worker: offloads browser CV
- Pharmacy terminal → Ledger engine: verifies batch
- Audit evidence → Ledger engine: collects ledger history
- Audit evidence → Supabase client: collects telemetry evidence
- Audit evidence → Compliance PDF: generates compliance report
- Audit evidence → Operational CSV: exports operations
