# theunstopabble/swadkart architecture

> Architecture diagram of the GitHub repository theunstopabble/swadkart, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/theunstopabble/swadkart
- Repository: https://github.com/theunstopabble/swadkart

## Overview

1. **Overall shape**
   SwadKart is a two-tier MERN platform: a React/Vite frontend and an Express/Node backend. The backend exposes a large REST API plus Socket.io real-time channels; the frontend consumes both and is organized around role-based pages for customers, admins, restaurant owners, and delivery partners.

2. **Frontend boundary**
   The frontend is a single-page app built with React 19, Redux Toolkit, React Router, Tailwind, and Socket.io-client. It owns UI state, cart/user state, i18n, PWA behavior, voice search, biometric login UX, and live views such as order tracking and chat streaming. The app shell in `App.jsx` is the main composition point for routing, guards, and lazy-loaded pages.

3. **Backend entry and request pipeline**
   `backend/server.js` is the backend composition root. Requests flow through middleware for auth, roles, validation, cache, fraud detection, and error handling before reaching controllers. This boundary is important: controllers orchestrate business actions, while middleware enforces cross-cutting policy such as JWT auth, rate limits, and sanitization.

4. **Domain/API layer**
   The backend is split into many route/controller pairs, roughly by business domain: auth/users, products/restaurants, orders/payments, reservations/group orders, delivery, coupons, subscriptions, loyalty/gamification, analytics, GDPR, notifications, and image upload. This suggests a modular monolith rather than microservices; each route module maps to a feature area and delegates to a matching controller.

5. **Persistence layer**
   MongoDB is the primary system of record through Mongoose models. The models cover core commerce entities like users, restaurants, products, orders, reservations, coupons, subscriptions, referrals, group orders, notifications, payouts, and conversations. The architecture likely centers on MongoDB documents as the source of truth for both transactional flows and conversational history.

6. **Caching and fast-path reads**
   Redis is used for response caching and also backs jobs/queues where available, with an in-memory fallback mentioned in config. This cache layer sits beside MongoDB to reduce repeated reads for GET-heavy endpoints, chat rate limiting, and async work coordination.

7. **Payments, fulfillment, and commerce flow**
   The core transaction path is: browse menu → build cart → apply coupon/subscription/surge logic → pay via Razorpay or COD → create order → dispatch to delivery tracking/updates. Order-related controllers and calculators indicate pricing is computed server-side, not trusted from the client, which is important for diagramming the trust boundary.

8. **Real-time and delivery tracking**
   Socket.io supports live order status and GPS-style delivery tracking. Frontend components such as live maps and order progress consume these updates, while backend delivery/order controllers publish state changes. This is a separate real-time channel from the REST API and should be shown as a parallel path.

9. **AI chatbot subsystem**
   The `backend/services/chat` subtree is a distinct subsystem: intent classification, language detection, sentiment, retrieval, token budgeting, fallback responses, and Groq integration. Tooling modules allow the chatbot to invoke business actions like order status, cancel, reorder, coupon lookup, delivery ETA, and FAQ. On the frontend, the chatbot UI streams responses via SSE-style event parsing, so the chatbot path is a mix of REST, streaming, and action execution.

10. **Media, identity, and external integrations**
   Cloudinary handles uploaded images, Firebase is used for Google auth and push-related client setup, WebAuthn supports biometric login, and email delivery is offloaded through a worker queue. These integrations are not incidental: they define external boundaries for authentication, storage, notifications, and asynchronous processing.

11. **Async processing**
   `backend/workers/emailWorker.js` and the chat cleanup/queue services show that the backend includes background jobs, not just synchronous API handlers. Email delivery and chat maintenance are decoupled from request-response latency, which matters for showing non-blocking workflows in the architecture diagram.

12. **Security and policy layer**
   The repo emphasizes multiple enforcement layers: JWT cookies, role-based authorization, CSRF/origin checks, rate limiting, fraud detection heuristics, and input sanitization. There is also GDPR-specific functionality for export and deletion. These are horizontal controls across the API rather than separate features.

13. **Frontend product areas**
   The UI is segmented by user role and workflow: public marketing/home, restaurant browsing, cart/checkout/payment, customer order history, admin analytics, restaurant owner dashboards, delivery partner dashboard, subscription/gamification, reservations, and support/chat. Reusable components under `src/components` are organized by these domains, not by generic design-system layers.

14. **Testing, documentation, and delivery**
   The repo includes substantial automated testing, especially property-based tests for the chat pipeline, tools, and streaming behavior, plus integration tests. GitHub Actions runs backend syntax checks, frontend lint/build, and deployment-related validation. The `docs/` folder contains explicit architecture, API, DB schema, deployment, and workflow docs, indicating the repo is designed to be diagrammed and operated as a production system.

15. **Deployment topology**
   The intended deployment splits frontend and backend: frontend on Vercel, backend on Render, MongoDB Atlas for persistence, Redis as optional cache/job backend, and Cloudinary/Razorpay/Firebase/Groq as external SaaS dependencies. For an architecture diagram, it’s best to show the frontend as an edge-hosted SPA that talks to the backend API and Socket.io endpoint, which in turn connects to MongoDB and external service providers.

Main entry points: [`backend/controllers/orderController.js`](https://github.com/theunstopabble/swadkart/blob/main/backend/controllers/orderController.js) (Domain Controllers), [`frontend/src/App.jsx`](https://github.com/theunstopabble/swadkart/blob/main/frontend/src/App.jsx) (App Shell), [`backend/server.js`](https://github.com/theunstopabble/swadkart/blob/main/backend/server.js) (Server), [`backend/services/chat/chatPipeline.js`](https://github.com/theunstopabble/swadkart/blob/main/backend/services/chat/chatPipeline.js) (Chat Pipeline)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_fe_app["App Shell<br/>React router shell<br/>[App.jsx]"]
  node_fe_store["Redux Store<br/>client state<br/>[store.js]"]
  node_fe_chat["Chatbot UI<br/>streaming UI<br/>[ChatBot.jsx]"]
  node_fe_order["Order Flow<br/>commerce UI<br/>[Cart.jsx]"]
  node_fe_role["Role Dashboards<br/>role pages<br/>[AdminDashboard.jsx]"]
  node_fe_i18n["PWA/i18n<br/>client platform<br/>[i18n.js]"]
end

subgraph group_backend["Backend"]
  node_be_server["Server<br/>express entry<br/>[server.js]"]
  node_be_mw["Middleware Gate<br/>policy pipeline<br/>[authMiddleware.js]"]
  node_be_routes["REST Routes<br/>api routing<br/>[orderRoutes.js]"]
  node_be_domain["Domain Controllers<br/>business orchestration<br/>[orderController.js]"]
  node_be_chat["Chat Pipeline<br/>ai subsystem<br/>[chatPipeline.js]"]
  node_be_models[("Mongo Models<br/>mongoose models<br/>[orderModel.js]")]
  node_be_workers["Email Worker<br/>background jobs<br/>[emailWorker.js]"]
end

subgraph group_infra["Data & Infra"]
  node_be_config["Runtime Config<br/>connectors<br/>[db.js]"]
  node_be_cache[("Redis Cache<br/>cache/job backend<br/>[redis.js]")]
  node_db[("MongoDB<br/>system of record")]
  node_redis[("Redis<br/>cache/queue")]
  node_cloudinary["Cloudinary<br/>media storage"]
  node_firebase["Firebase<br/>auth/push<br/>[firebaseConfig.js]"]
  node_razorpay["Payments<br/>payment provider"]
  node_groq["Groq AI<br/>llm provider"]
end

node_fe_app -->|"reads/writes"| node_fe_store
node_fe_app -->|"mounts"| node_fe_chat
node_fe_app -->|"routes to"| node_fe_order
node_fe_app -->|"routes to"| node_fe_role
node_fe_app -->|"enables"| node_fe_i18n
node_fe_order -->|"API calls"| node_be_server
node_fe_chat -->|"stream/api"| node_be_server
node_fe_role -->|"API calls"| node_be_server
node_be_server -->|"passes through"| node_be_mw
node_be_mw -->|"allows"| node_be_routes
node_be_routes -->|"delegates"| node_be_domain
node_be_domain -->|"reads/writes"| node_be_models
node_be_domain -->|"fast path"| node_be_cache
node_be_cache -->|"backed by"| node_redis
node_be_models -->|"stored in"| node_db
node_be_server -->|"hosts"| node_be_chat
node_be_chat -->|"generates"| node_groq
node_be_chat -->|"reads/writes"| node_be_models
node_be_chat -->|"queues/cache"| node_redis
node_be_domain -->|"charges"| node_razorpay
node_be_domain -->|"uploads"| node_cloudinary
node_be_domain -->|"auth/push"| node_firebase
node_be_workers -->|"consumes jobs from"| node_be_domain
node_be_workers -->|"pulls from"| node_redis
node_be_config -->|"connects"| node_db
node_be_config -->|"connects"| node_redis

click node_fe_app "https://github.com/theunstopabble/swadkart/blob/main/frontend/src/App.jsx"
click node_fe_store "https://github.com/theunstopabble/swadkart/blob/main/frontend/src/redux/store.js"
click node_fe_chat "https://github.com/theunstopabble/swadkart/blob/main/frontend/src/components/ChatBot.jsx"
click node_fe_order "https://github.com/theunstopabble/swadkart/blob/main/frontend/src/pages/Cart.jsx"
click node_fe_role "https://github.com/theunstopabble/swadkart/blob/main/frontend/src/pages/AdminDashboard.jsx"
click node_fe_i18n "https://github.com/theunstopabble/swadkart/blob/main/frontend/src/i18n.js"
click node_be_server "https://github.com/theunstopabble/swadkart/blob/main/backend/server.js"
click node_be_mw "https://github.com/theunstopabble/swadkart/blob/main/backend/middleware/authMiddleware.js"
click node_be_routes "https://github.com/theunstopabble/swadkart/blob/main/backend/routes/orderRoutes.js"
click node_be_domain "https://github.com/theunstopabble/swadkart/blob/main/backend/controllers/orderController.js"
click node_be_chat "https://github.com/theunstopabble/swadkart/blob/main/backend/services/chat/chatPipeline.js"
click node_be_models "https://github.com/theunstopabble/swadkart/blob/main/backend/models/orderModel.js"
click node_be_workers "https://github.com/theunstopabble/swadkart/blob/main/backend/workers/emailWorker.js"
click node_be_config "https://github.com/theunstopabble/swadkart/blob/main/backend/config/db.js"
click node_be_cache "https://github.com/theunstopabble/swadkart/blob/main/backend/config/redis.js"
click node_firebase "https://github.com/theunstopabble/swadkart/blob/main/frontend/src/utils/firebaseConfig.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_fe_app,node_fe_store,node_fe_chat,node_fe_order,node_fe_role,node_fe_i18n toneBlue
class node_be_server,node_be_mw,node_be_routes,node_be_domain,node_be_chat,node_be_models,node_be_workers toneAmber
class node_be_config,node_be_cache,node_db,node_redis,node_cloudinary,node_firebase,node_razorpay,node_groq toneMint
```

## Components

### Frontend

React/Vite SPA with role-based UX, cart/order flows, and live chatbot/tracking views

- [App Shell](https://github.com/theunstopabble/swadkart/blob/main/frontend/src/App.jsx): React router shell · Composes routes, guards, lazy pages, and global app state · `frontend/src/App.jsx`
- [Redux Store](https://github.com/theunstopabble/swadkart/blob/main/frontend/src/redux/store.js): client state · Cart and user state shared across the SPA · `frontend/src/redux/store.js`
- [Chatbot UI](https://github.com/theunstopabble/swadkart/blob/main/frontend/src/components/ChatBot.jsx): streaming UI · Chat widget, streaming parser, voice controls, and past conversations · `frontend/src/components/ChatBot.jsx`
- [Order Flow](https://github.com/theunstopabble/swadkart/blob/main/frontend/src/pages/Cart.jsx): commerce UI · Cart, checkout, payment, and live tracking screens · `frontend/src/pages/Cart.jsx`
- [Role Dashboards](https://github.com/theunstopabble/swadkart/blob/main/frontend/src/pages/AdminDashboard.jsx): role pages · Admin, restaurant owner, delivery partner, and customer pages · `frontend/src/pages/AdminDashboard.jsx`
- [PWA/i18n](https://github.com/theunstopabble/swadkart/blob/main/frontend/src/i18n.js): client platform · Internationalization, install prompt, voice, and biometric login UX · `frontend/src/i18n.js`

### Backend

Express/Node modular monolith with REST, realtime, and background jobs

- [Server](https://github.com/theunstopabble/swadkart/blob/main/backend/server.js): express entry · Backend composition root and HTTP/Socket.io bootstrap · `backend/server.js`
- [Middleware Gate](https://github.com/theunstopabble/swadkart/blob/main/backend/middleware/authMiddleware.js): policy pipeline · Auth, roles, cache, validation, fraud detection, and error handling · `backend/middleware/authMiddleware.js`
- [REST Routes](https://github.com/theunstopabble/swadkart/blob/main/backend/routes/orderRoutes.js): api routing · Feature routes for auth, commerce, delivery, analytics, and utilities · `backend/routes/orderRoutes.js`
- [Domain Controllers](https://github.com/theunstopabble/swadkart/blob/main/backend/controllers/orderController.js): business orchestration · Controllers coordinating orders, products, payments, delivery, and admin features · `backend/controllers/orderController.js`
- [Chat Pipeline](https://github.com/theunstopabble/swadkart/blob/main/backend/services/chat/chatPipeline.js): ai subsystem · Intent, retrieval, tools, streaming, fallback, and Groq integration · `backend/services/chat/chatPipeline.js`
- [Mongo Models](https://github.com/theunstopabble/swadkart/blob/main/backend/models/orderModel.js): mongoose models · Source-of-truth documents for users, orders, restaurants, conversations, and commerce entities · `backend/models/orderModel.js`
- [Email Worker](https://github.com/theunstopabble/swadkart/blob/main/backend/workers/emailWorker.js): background jobs · Decoupled async delivery for email and maintenance tasks · `backend/workers/emailWorker.js`

### Data & Infra

Persistence, cache, and external service boundaries

- [Runtime Config](https://github.com/theunstopabble/swadkart/blob/main/backend/config/db.js): connectors · Database, Redis, Cloudinary, and WebAuthn setup · `backend/config/db.js`
- [Redis Cache](https://github.com/theunstopabble/swadkart/blob/main/backend/config/redis.js): cache/job backend · Fast-path reads, rate limiting support, and queue/fallback coordination · `backend/config/redis.js`
- **MongoDB**: system of record · Primary persistence for transactional and conversational data
- **Redis**: cache/queue · Shared cache and job backend for low-latency and async workflows
- **Cloudinary**: media storage · External image upload and asset hosting boundary
- [Firebase](https://github.com/theunstopabble/swadkart/blob/main/frontend/src/utils/firebaseConfig.js): auth/push · External Google auth and push-related client integration · `frontend/src/utils/firebaseConfig.js`
- **Payments**: payment provider · External checkout payment boundary used by the backend payment flow
- **Groq AI**: llm provider · External model provider used by chatbot generation and fallback flows

## Connections

- App Shell → Redux Store: reads/writes · App shell uses shared client state
- App Shell → Chatbot UI: mounts · Chatbot is composed into the SPA shell
- App Shell → Order Flow: routes to · App shell exposes commerce and tracking pages
- App Shell → Role Dashboards: routes to · Role-specific dashboards are loaded from the main shell
- App Shell → PWA/i18n: enables · Global UX features are initialized at app level
- Order Flow → Server: API calls · Checkout and tracking screens consume backend endpoints
- Chatbot UI → Server: stream/api · Chat widget consumes REST and streaming responses
- Role Dashboards → Server: API calls · Dashboards fetch and mutate server state
- Server → Middleware Gate: passes through · Requests enter the middleware policy layer first
- Middleware Gate → REST Routes: allows · Validated requests reach feature routes
- REST Routes → Domain Controllers: delegates · Routes hand off to business controllers
- Domain Controllers → Mongo Models: reads/writes · Controllers persist and load Mongo documents
- Domain Controllers → Redis Cache: fast path · Controllers use cache for repeated reads and coordination
- Redis Cache → Redis: backed by · Redis provides shared cache and queue storage
- Mongo Models → MongoDB: stored in · MongoDB is the primary system of record
- Server → Chat Pipeline: hosts · Chat subsystem is exposed alongside REST
- Chat Pipeline → Groq AI: generates · LLM provider powers assistant responses
- Chat Pipeline → Mongo Models: reads/writes · Conversation history and chat state persist in Mongo
- Chat Pipeline → Redis: queues/cache · Chat rate limiting and async handling can use Redis
- Domain Controllers → Payments: charges · Payment controllers integrate with external checkout provider
- Domain Controllers → Cloudinary: uploads · Image and media flows reach Cloudinary
- Domain Controllers → Firebase: auth/push · Client and auth flows rely on Firebase integration
- Email Worker → Domain Controllers: consumes jobs from · Background work is decoupled from request latency
- Email Worker → Redis: pulls from · Worker queue can be backed by Redis
- Runtime Config → MongoDB: connects · Database configuration initializes Mongo connectivity
- Runtime Config → Redis: connects · Runtime config wires Redis when enabled
