Closed-Loop Pharmaceutical Reverse Supply Chain & Anti-Counterfeit Platform
Tracks medicine batches from manufacturing to pharmacy dispensing, controls reverse returns, verifies SHA-256 destruction, and blocks dead-batch re-entry.
PharmaSafe Intelligence is a full-stack pharmaceutical traceability and anti-counterfeit platform engineered to prevent expired, recalled, and destroyed medicines from re-entering the market. It connects manufacturers, distributors, pharmacies, authorized disposal facilities, and regulators through a single closed-loop custody ledger across a FastAPI backend, React + TypeScript command center, and Flutter field-scanner app.
- Closed-Loop Custody Tracking — End-to-end batch lifecycle (
MANUFACTURED→IN_DISTRIBUTION→AT_PHARMACY→ Return Logistics →DESTROYED/DEAD_BATCH) with full audit trails. - Deterministic POS Sale Blocking — Unconditionally returns
BLOCK_SALEat point of sale for expired, recalled, destroyed, or dead-batch stock. - SHA-256 Destruction Certificates — Generates tamper-evident disposal certificates and permanently registers destroyed batches in the Dead Batch Registry.
- Anti-Re-Entry & Online Surveillance — Flags post-destruction scans (
DEAD_BATCH_REENTRY_DETECTED) and suspicious online marketplace listings referencing dead or recalled batches. - AI & Vision Decision Support — Ensemble risk scoring (
ai_engine/) for expiry degradation, custody anomalies, and re-entry threat, plus OpenCV packaging image-quality screening (vision_service.py) as advisory support that never overrides deterministic safety rules. - 6-Role RBAC & Tenant Isolation — JWT authentication across
MANUFACTURER,DISTRIBUTOR,PHARMACY,DISPOSAL_FACILITY,REGULATOR_AUDITOR, andADMIN.
MANUFACTURER ──► DISTRIBUTOR ──► PHARMACY ──► POS VERIFICATION (ALLOW / BLOCK SALE)
│
(Expiry / Recall / Suspicion)
▼
MANUFACTURER ◄── DISTRIBUTOR ◄── RETURN REQUEST (Reverse Logistics)
│
▼
DISPOSAL FACILITY ──► DESTRUCTION ──► SHA-256 CERTIFICATE ──► DEAD BATCH REGISTRY
│
▼
RE-ENTRY & ONLINE LISTING MONITORING
| Layer | Technologies |
|---|---|
| Backend API | Python 3.10+, FastAPI, SQLAlchemy, Pydantic v2, Alembic, JWT (python-jose, passlib) |
| Database | PostgreSQL (production / Docker) & SQLite (local development & test suite) |
| Web Frontend | React 18, TypeScript, Vite, Tailwind CSS, React Router, Recharts, jsqr / qrcode |
| Mobile App | Flutter (Dart 3.3+), Riverpod, GoRouter, Dio, mobile_scanner |
| AI & Vision | scikit-learn, pandas, numpy, OpenCV (cv2) preliminary packaging screening |
| Deployment | Docker Compose, Render (render.yaml — FastAPI serving the React SPA) |
PHARMASAFE/
├── ai_engine/ # Expiry, anomaly & re-entry risk scoring interfaces and ensemble engine
├── backend/
│ ├── app/
│ │ ├── api/v1/ # 20 versioned REST routers (auth, batches, verify, returns, disposal, etc.)
│ │ ├── core/ # Settings, JWT security, RBAC guards
│ │ ├── db/ # SQLAlchemy engine, session & base models
│ │ ├── models/ # Relational & ledger ORM models
│ │ ├── schemas/ # Pydantic request/response DTOs
│ │ ├── seeds/ # Database seeder with realistic supply-chain fixtures
│ │ ├── services/ # Business logic, SHA-256 certificates, vision screening
│ │ └── main.py # FastAPI app & SPA static serving
│ ├── tests/ # Automated pytest suite (168 tests)
│ ├── Dockerfile
│ └── requirements.txt
├── contracts/ # OpenAPI v3 schema export (openapi.json)
├── docs/ # Architecture, API reference, schema & security documentation
├── frontend/ # React + TypeScript Command Center web application
│ ├── src/
│ │ ├── components/ # Layout & shared UI components
│ │ ├── context/ # AuthContext & role state
│ │ ├── pages/ # Dashboards, Batch Passport, POS Verify, Returns, Disposal, AI Risk
│ │ └── services/ # Typed API clients
│ ├── Dockerfile
│ └── package.json
├── mobile/ # Flutter mobile client (QR/barcode scanner, custody, POS & disposal)
├── docker-compose.yml # Local PostgreSQL + FastAPI + Vite orchestration
├── render.yaml # Unified Render deployment configuration
├── run_backend.py # Convenience local API launcher
├── .env.example # Environment variable template
└── LICENSE # MIT License
cp .env.example .env
docker-compose up --build- Web App:
http://localhost:5173 - API & Swagger Docs:
http://localhost:8000/docs
1. Backend API & Seed Data
python -m venv .venv
# Windows: .venv\Scripts\activate
# macOS/Linux: source .venv/bin/activate
pip install -r backend/requirements.txt
python -m backend.app.seeds.seed_data
python run_backend.pySwagger UI is available at http://localhost:8000/docs and health check at http://localhost:8000/api/health.
2. Web Frontend
cd frontend
npm install
npm run devOpen http://localhost:5173.
3. Mobile App (Flutter)
cd mobile
flutter pub get
flutter runSee mobile/README.md for configuring AppConstants.defaultHost for local USB/emulator testing vs. cloud deployment.
After running the seeder (python -m backend.app.seeds.seed_data), you can sign in with any role using password password123:
| Role | |
|---|---|
| Manufacturer | manufacturer@pharmasafe.demo |
| Distributor | distributor@pharmasafe.demo |
| Pharmacy | pharmacy@pharmasafe.demo |
| Disposal Facility | disposal@pharmasafe.demo |
| Regulator Auditor | regulator@pharmasafe.demo |
| Admin | admin@pharmasafe.demo |
# Backend automated test suite (from repository root)
python -m pytest backend/tests
# Frontend type-check & production build
cd frontend && npm run build- Architecture Overview
- API Reference
- Database Schema
- RBAC Matrix
- Batch State Machine
- Security Review
- Deployment Guide
This project is licensed under the MIT License.