Skip to content

About

Closed-loop pharmaceutical reverse supply chain & anti-counterfeit platform with batch traceability, POS sale blocking, SHA-256 destruction certificates, and AI re-entry detection — FastAPI + React + TypeScript + Flutter.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

PharmaSafe Intelligence

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.

Python FastAPI React TypeScript Flutter Docker License: MIT


Overview

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.


Key Capabilities

  • 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_SALE at 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, and ADMIN.

Supply-Chain Lifecycle

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

Tech Stack

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)

Repository Structure

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

Quick Start

Option 1: Docker Compose (PostgreSQL + Backend + Frontend)

cp .env.example .env
docker-compose up --build
  • Web App: http://localhost:5173
  • API & Swagger Docs: http://localhost:8000/docs

Option 2: Local Development

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.py

Swagger 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 dev

Open http://localhost:5173.

3. Mobile App (Flutter)

cd mobile
flutter pub get
flutter run

See mobile/README.md for configuring AppConstants.defaultHost for local USB/emulator testing vs. cloud deployment.


Demo Accounts

After running the seeder (python -m backend.app.seeds.seed_data), you can sign in with any role using password password123:

Role Email
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

Testing & Verification

# Backend automated test suite (from repository root)
python -m pytest backend/tests

# Frontend type-check & production build
cd frontend && npm run build

Documentation


License

This project is licensed under the MIT License.

About

Closed-loop pharmaceutical reverse supply chain & anti-counterfeit platform with batch traceability, POS sale blocking, SHA-256 destruction certificates, and AI re-entry detection — FastAPI + React + TypeScript + Flutter.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages