# sameer19-03/order-routing-engine architecture

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

- Interactive diagram: https://gitdiagram.com/sameer19-03/order-routing-engine
- Repository: https://github.com/sameer19-03/order-routing-engine

## Overview

#### Purpose and entry points
Order Routing Engine is a full-stack logistics decision platform that assigns customer orders to warehouses using configurable weighted scoring, reserves stock, and records an AI-readable rationale. Backend startup is `server/server.js`; optional data bootstrap is `server/seed.js`. The React/Vite SPA starts at `client/src/main.jsx` and defines application navigation in `client/src/App.jsx`.

#### Client application and access boundary
The frontend is a protected operations console for dashboards, orders, inventory, warehouses, routing, settings, history, and maps. Authentication state is centralized in `client/src/context/AuthContext.jsx`; route gating is enforced by `client/src/components/ProtectedRoute.jsx`; the shared authenticated shell is `client/src/components/Layout.jsx`. API communication is centralized through `client/src/api/axios.js`.

#### API and authorization layer
Express exposes resource-oriented API modules, with routes delegating to controllers. JWT-based authentication and role enforcement form the backend boundary before protected operational actions. Key paths are `server/server.js`, `server/middleware/authMiddleware.js`, `server/routes/routingRoutes.js`, and `server/controllers/routingController.js`. Auth registration/login are handled through `server/routes/authRoutes.js` and `server/controllers/authController.js`.

#### Routing decision pipeline
Routing is the core transaction: receive an order, filter active warehouses and adequate inventory, calculate geographic distance, score candidates using configured distance/inventory/delivery/cost weights, select a warehouse, reserve inventory, generate an explanation, then persist assignment and decision history. The orchestration and scoring live in `server/services/routingEngine.js`, invoked via `server/controllers/routingController.js`; configurable scoring is managed through `server/services/orderService.js` and `server/controllers/configController.js`.

#### Persistent domain model
MongoDB via Mongoose stores operational state. Orders connect customer demand and fulfillment assignment in `server/models/Order.js`; per-warehouse product availability and reservation state are in `server/models/Inventory.js`; warehouse location/status are in `server/models/Warehouse.js`. Supporting persisted entities include catalog products, routing weights, user identities/roles, and explainable routing audit records in `server/models/Product.js`, `server/models/RoutingConfig.js`, `server/models/User.js`, and `server/models/RoutingHistory.js`.

#### Operations and visualization
Management pages manipulate the resource APIs for inventory, warehouses, products, and order lifecycle: `server/controllers/inventoryController.js`, `server/controllers/warehouseController.js`, and `server/controllers/orderController.js`. The SPA presents routing execution/history and Leaflet-based geographic visualization through `client/src/pages/RoutingDecision.jsx`, `client/src/pages/RoutingHistory.jsx`, and `client/src/pages/WarehouseMap.jsx`. Settings change the persisted scoring configuration via `client/src/pages/RoutingSettings.jsx`.

#### External services and background work
Required infrastructure is a MongoDB cluster, configured through `MONGODB_URI`. Groq’s Llama 3.1 is called after deterministic routing to turn decision facts into business explanations; integration is isolated in `server/services/aiExplanation.js` and requires `GROQ_API_KEY`. JWT signing requires `JWT_SECRET`. When enabled by environment configuration, a node-cron-driven order simulator generates workload through `server/services/orderGenerator.js`, rather than requiring external order ingress.

Main entry points: [`server/services/routingEngine.js`](https://github.com/sameer19-03/order-routing-engine/blob/main/server/services/routingEngine.js) (Routing engine), [`server/server.js`](https://github.com/sameer19-03/order-routing-engine/blob/main/server/server.js) (Express server), [`client/src/App.jsx`](https://github.com/sameer19-03/order-routing-engine/blob/main/client/src/App.jsx) (App navigation), [`server/controllers/orderController.js`](https://github.com/sameer19-03/order-routing-engine/blob/main/server/controllers/orderController.js) (Operations controllers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Operations Console"]
  node_spa["React/Vite SPA<br/>frontend<br/>[main.jsx]"]
  node_app["App navigation<br/>frontend routes<br/>[App.jsx]"]
  node_auth_context["Auth state<br/>client auth<br/>[AuthContext.jsx]"]
  node_protected_route["Route gate<br/>access control<br/>[ProtectedRoute.jsx]"]
  node_api_client["API client<br/>Axios<br/>[axios.js]"]
  node_routing_views["Routing views &amp; map<br/>React pages"]
end

subgraph group_api["Express API"]
  node_server["Express server<br/>backend entry<br/>[server.js]"]
  node_auth_api["Auth API<br/>routes/controllers<br/>[authRoutes.js]"]
  node_auth_middleware["JWT &amp; role guard<br/>middleware<br/>[authMiddleware.js]"]
  node_routing_api["Routing controller<br/>routes/controllers"]
  node_operations_api["Operations controllers<br/>resource APIs<br/>[orderController.js]"]
end

subgraph group_routing["Routing Pipeline"]
  node_routing_engine{{"Routing engine<br/>decision orchestration<br/>[routingEngine.js]"}}
  node_scoring_config["Scoring configuration<br/>config service<br/>[orderService.js]"]
  node_ai_explanation["AI explanation<br/>Groq integration<br/>[aiExplanation.js]"]
  node_order_generator["Order simulator<br/>cron workload<br/>[orderGenerator.js]"]
end

subgraph group_data["Persistent Domain"]
  node_orders["Orders<br/>Mongoose model<br/>[Order.js]"]
  node_inventory["Inventory<br/>Mongoose model<br/>[Inventory.js]"]
  node_warehouses["Warehouses<br/>Mongoose model<br/>[Warehouse.js]"]
  node_routing_config["Routing weights<br/>Mongoose model<br/>[RoutingConfig.js]"]
  node_routing_history["Routing history<br/>Mongoose model<br/>[RoutingHistory.js]"]
end

subgraph group_external["Infrastructure &amp; AI"]
  node_mongodb[("MongoDB cluster<br/>database")]
  node_groq["Groq Llama 3.1<br/>external AI"]
end

node_spa -->|"boots"| node_app
node_app -->|"uses"| node_auth_context
node_app -->|"gates routes"| node_protected_route
node_app -->|"renders"| node_routing_views
node_routing_views -->|"calls"| node_api_client
node_api_client -->|"HTTP API"| node_server
node_server -->|"mounts"| node_auth_api
node_server -->|"protects APIs"| node_auth_middleware
node_server -->|"mounts"| node_routing_api
node_server -->|"mounts"| node_operations_api
node_routing_api -->|"invokes"| node_routing_engine
node_routing_engine -->|"reads weights"| node_scoring_config
node_routing_engine -->|"assigns order"| node_orders
node_routing_engine -->|"checks and reserves"| node_inventory
node_routing_engine -->|"filters and scores"| node_warehouses
node_routing_engine -->|"requests rationale"| node_ai_explanation
node_ai_explanation -->|"Llama request"| node_groq
node_routing_engine -->|"records decision"| node_routing_history
node_scoring_config -->|"persists"| node_routing_config
node_operations_api -->|"manages"| node_orders
node_operations_api -->|"manages"| node_inventory
node_operations_api -->|"manages"| node_warehouses
node_order_generator -.->|"generates workload"| node_routing_engine
node_orders -->|"stored in"| node_mongodb
node_inventory -->|"stored in"| node_mongodb
node_warehouses -->|"stored in"| node_mongodb
node_routing_config -->|"stored in"| node_mongodb
node_routing_history -->|"stored in"| node_mongodb

click node_spa "https://github.com/sameer19-03/order-routing-engine/blob/main/client/src/main.jsx"
click node_app "https://github.com/sameer19-03/order-routing-engine/blob/main/client/src/App.jsx"
click node_auth_context "https://github.com/sameer19-03/order-routing-engine/blob/main/client/src/context/AuthContext.jsx"
click node_protected_route "https://github.com/sameer19-03/order-routing-engine/blob/main/client/src/components/ProtectedRoute.jsx"
click node_api_client "https://github.com/sameer19-03/order-routing-engine/blob/main/client/src/api/axios.js"
click node_routing_views "https://github.com/sameer19-03/order-routing-engine/blob/main/client/src/pages/RoutingDecision.jsx"
click node_server "https://github.com/sameer19-03/order-routing-engine/blob/main/server/server.js"
click node_auth_api "https://github.com/sameer19-03/order-routing-engine/blob/main/server/routes/authRoutes.js"
click node_auth_middleware "https://github.com/sameer19-03/order-routing-engine/blob/main/server/middleware/authMiddleware.js"
click node_routing_api "https://github.com/sameer19-03/order-routing-engine/blob/main/server/controllers/routingController.js"
click node_operations_api "https://github.com/sameer19-03/order-routing-engine/blob/main/server/controllers/orderController.js"
click node_routing_engine "https://github.com/sameer19-03/order-routing-engine/blob/main/server/services/routingEngine.js"
click node_scoring_config "https://github.com/sameer19-03/order-routing-engine/blob/main/server/services/orderService.js"
click node_ai_explanation "https://github.com/sameer19-03/order-routing-engine/blob/main/server/services/aiExplanation.js"
click node_order_generator "https://github.com/sameer19-03/order-routing-engine/blob/main/server/services/orderGenerator.js"
click node_orders "https://github.com/sameer19-03/order-routing-engine/blob/main/server/models/Order.js"
click node_inventory "https://github.com/sameer19-03/order-routing-engine/blob/main/server/models/Inventory.js"
click node_warehouses "https://github.com/sameer19-03/order-routing-engine/blob/main/server/models/Warehouse.js"
click node_routing_config "https://github.com/sameer19-03/order-routing-engine/blob/main/server/models/RoutingConfig.js"
click node_routing_history "https://github.com/sameer19-03/order-routing-engine/blob/main/server/models/RoutingHistory.js"

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_spa,node_app,node_auth_context,node_protected_route,node_api_client,node_routing_views toneBlue
class node_server,node_auth_api,node_auth_middleware,node_routing_api,node_operations_api toneAmber
class node_routing_engine,node_scoring_config,node_ai_explanation,node_order_generator toneMint
class node_orders,node_inventory,node_warehouses,node_routing_config,node_routing_history toneRose
class node_mongodb,node_groq toneIndigo
```

## Components

### Operations Console

- [React/Vite SPA](https://github.com/sameer19-03/order-routing-engine/blob/main/client/src/main.jsx): frontend · Operations dashboards and management views. · `client/src/main.jsx`
- [App navigation](https://github.com/sameer19-03/order-routing-engine/blob/main/client/src/App.jsx): frontend routes · `client/src/App.jsx`
- [Auth state](https://github.com/sameer19-03/order-routing-engine/blob/main/client/src/context/AuthContext.jsx): client auth · `client/src/context/AuthContext.jsx`
- [Route gate](https://github.com/sameer19-03/order-routing-engine/blob/main/client/src/components/ProtectedRoute.jsx): access control · `client/src/components/ProtectedRoute.jsx`
- [API client](https://github.com/sameer19-03/order-routing-engine/blob/main/client/src/api/axios.js): Axios · `client/src/api/axios.js`
- [Routing views & map](https://github.com/sameer19-03/order-routing-engine/blob/main/client/src/pages/RoutingDecision.jsx): React pages · Decision, history, settings, and warehouse map views. · `client/src/pages/RoutingDecision.jsx`

### Express API

- [Express server](https://github.com/sameer19-03/order-routing-engine/blob/main/server/server.js): backend entry · `server/server.js`
- [Auth API](https://github.com/sameer19-03/order-routing-engine/blob/main/server/routes/authRoutes.js): routes/controllers · Registration and login endpoints. · `server/routes/authRoutes.js`
- [JWT & role guard](https://github.com/sameer19-03/order-routing-engine/blob/main/server/middleware/authMiddleware.js): middleware · `server/middleware/authMiddleware.js`
- [Routing controller](https://github.com/sameer19-03/order-routing-engine/blob/main/server/controllers/routingController.js): routes/controllers · `server/controllers/routingController.js`
- [Operations controllers](https://github.com/sameer19-03/order-routing-engine/blob/main/server/controllers/orderController.js): resource APIs · Order, inventory, and warehouse lifecycle management. · `server/controllers/orderController.js`

### Routing Pipeline

- [Routing engine](https://github.com/sameer19-03/order-routing-engine/blob/main/server/services/routingEngine.js): decision orchestration · Filters candidates, scores warehouses, selects and reserves stock. · `server/services/routingEngine.js`
- [Scoring configuration](https://github.com/sameer19-03/order-routing-engine/blob/main/server/services/orderService.js): config service · Manages weighted distance, inventory, delivery, and cost scoring. · `server/services/orderService.js`
- [AI explanation](https://github.com/sameer19-03/order-routing-engine/blob/main/server/services/aiExplanation.js): Groq integration · Converts deterministic decision facts into a business rationale. · `server/services/aiExplanation.js`
- [Order simulator](https://github.com/sameer19-03/order-routing-engine/blob/main/server/services/orderGenerator.js): cron workload · Optional node-cron order generation. · `server/services/orderGenerator.js`

### Persistent Domain

- [Orders](https://github.com/sameer19-03/order-routing-engine/blob/main/server/models/Order.js): Mongoose model · `server/models/Order.js`
- [Inventory](https://github.com/sameer19-03/order-routing-engine/blob/main/server/models/Inventory.js): Mongoose model · Per-warehouse availability and reservations. · `server/models/Inventory.js`
- [Warehouses](https://github.com/sameer19-03/order-routing-engine/blob/main/server/models/Warehouse.js): Mongoose model · Warehouse location and active status. · `server/models/Warehouse.js`
- [Routing weights](https://github.com/sameer19-03/order-routing-engine/blob/main/server/models/RoutingConfig.js): Mongoose model · `server/models/RoutingConfig.js`
- [Routing history](https://github.com/sameer19-03/order-routing-engine/blob/main/server/models/RoutingHistory.js): Mongoose model · Explainable assignment audit records. · `server/models/RoutingHistory.js`

### Infrastructure & AI

- **MongoDB cluster**: database · Operational state via Mongoose.
- **Groq Llama 3.1**: external AI

## Connections

- React/Vite SPA → App navigation: boots
- App navigation → Auth state: uses
- App navigation → Route gate: gates routes
- App navigation → Routing views & map: renders
- Routing views & map → API client: calls
- API client → Express server: HTTP API
- Express server → Auth API: mounts
- Express server → JWT & role guard: protects APIs
- Express server → Routing controller: mounts
- Express server → Operations controllers: mounts
- Routing controller → Routing engine: invokes
- Routing engine → Scoring configuration: reads weights
- Routing engine → Orders: assigns order
- Routing engine → Inventory: checks and reserves
- Routing engine → Warehouses: filters and scores
- Routing engine → AI explanation: requests rationale
- AI explanation → Groq Llama 3.1: Llama request
- Routing engine → Routing history: records decision
- Scoring configuration → Routing weights: persists
- Operations controllers → Orders: manages
- Operations controllers → Inventory: manages
- Operations controllers → Warehouses: manages
- Order simulator → Routing engine: generates workload
- Orders → MongoDB cluster: stored in
- Inventory → MongoDB cluster: stored in
- Warehouses → MongoDB cluster: stored in
- Routing weights → MongoDB cluster: stored in
- Routing history → MongoDB cluster: stored in
