# ajithesh9/one-store architecture

> Architecture diagram of the GitHub repository ajithesh9/one-store, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ajithesh9/one-store
- Repository: https://github.com/ajithesh9/one-store

## Overview

##### Project Type & Purpose
- This is a full-stack MERN (MongoDB, Express, React, Node.js) web application called “Watcher.”
- Purpose: Provide parents with device monitoring & control, subscription management (Stripe), user/admin dashboards, and marketing pages.

##### Main Architectural Layers
a. Client Layer (Frontend)
- Vite-powered React app with React Router DOM for page navigation.
- React Context API for global state (AuthContext, CartContext).
- UI components and pages (Login, Dashboard, AdminDashboard, Cart, Payment, marketing pages).
- Stripe Elements integration for payments.
- Dev proxy (`/api` → localhost:5000) configured in vite.config.js.
b. Application Layer (Backend API)
- Node.js + Express.js REST API.
- MVC pattern:
– Routes (userRoutes, orderRoutes, paymentRoutes)
– Middleware (authMiddleware: protect, admin)
– Controllers (userController, orderController, paymentController)
– Models (User, Order via Mongoose)
- Security: JWT auth, Bcrypt password hashing, CORS, dotenv.
c. Data Layer
- MongoDB (hosted locally or Atlas) accessed via Mongoose ODM.
- Schemas: User (with isAdmin flag), Order (line items, payment status).
d. External Services
- Stripe API (payment intent creation & confirmation).
- Optional: CDN or static asset hosting for front-end build, email or analytics (not in code).

##### Component Interactions & Data Flow
- User Browser → HTTP(S) → Frontend React App
- React App → `/api/...` fetch calls → Express Server
- Auth flows: POST `/api/users` & `/api/users/login` → userController → User model → DB
- Order flows: POST `/api/orders`, GET `/api/orders/myorders`, GET `/api/orders` (admin) → orderController → Order model → DB
- Payment flow: POST `/api/payment/create-payment-intent` → paymentController → Stripe SDK → returns clientSecret
- Express Server ↔ MongoDB (read/write)
- Stripe SDK ↔ Stripe Cloud
- React Stripe Elements uses `clientSecret` to complete payment, then calls `/api/orders` to persist the order

##### Key Architectural Patterns & Principles
- Separation of Concerns:
- Frontend handles presentation, routing, state management, and Stripe Elements.
- Backend handles authentication, business logic, data validation, and database access.
- Layered Architecture: clear client → API → database layers.
- MVC on the server: keeps controllers lean, models reusable, routes declarative.
- Context-based state management on the client: isolates auth and cart logic.
- Protected routes (frontend) and protected endpoints (backend) enforce security via JWT.

##### Essential Elements
- Component labels with technology (e.g., “AuthContext (React Context)”).
- Annotations for key flows: Authentication flow, Purchase & Order flow, Admin analytics flow.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client (Browser)"
        direction TB
        Browser["User Browser"]:::external
    end

    %% Frontend App Layer
    subgraph "Frontend App" 
        direction TB
        HTML["HTML Shell & Mount Point"]:::ui
        Main["App Entry (main.jsx)"]:::bizlogic
        ViteConfig["Vite Config & Dev Proxy"]:::security
        subgraph "React Context Providers"
            direction TB
            AuthContext["AuthContext (React Context)"]:::bizlogic
            CartContext["CartContext (React Context)"]:::bizlogic
        end
        Router["React Router"]:::bizlogic
        subgraph "UI Components & Pages"
            direction TB
            CheckoutForm["CheckoutForm (Stripe Elements)"]:::ui
            LoginPage["Login Page"]:::ui
            UserDashboard["User Dashboard"]:::ui
            AdminDashboard["Admin Dashboard"]:::ui
            CartPage["Cart Page"]:::ui
            PaymentPage["Payment Page"]:::ui
            MarketingPages["Marketing Pages"]:::ui
        end
    end

    %% Backend API Layer
    subgraph "Backend API"
        direction TB
        Server["Express Server Bootstrap"]:::bizlogic
        DBConfig["DB Connection (db.js)"]:::bizlogic
        subgraph "Routes"
            direction TB
            UserRoutes["User Routes"]:::bizlogic
            OrderRoutes["Order Routes"]:::bizlogic
            PaymentRoutes["Payment Routes"]:::bizlogic
        end
        AuthMiddleware["Auth Middleware"]:::security
        subgraph "Controllers"
            direction TB
            UserController["User Controller"]:::bizlogic
            OrderController["Order Controller"]:::bizlogic
            PaymentController["Payment Controller"]:::bizlogic
        end
        subgraph "Models"
            direction TB
            UserModel["User Model"]:::datastore
            OrderModel["Order Model"]:::datastore
        end
    end

    %% Data & External Services Layer
    subgraph "Database & External Services"
        direction TB
        MongoDB["MongoDB (Mongoose)"]:::datastore
        StripeAPI["Stripe Payment API"]:::external
    end

    %% Connections: Frontend Flow
    Browser -->|loads| HTML
    HTML --> Main
    Main --> AuthContext
    Main --> CartContext
    Main --> Router
    Router --> LoginPage
    Router --> UserDashboard
    Router --> AdminDashboard
    Router --> CartPage
    Router --> PaymentPage
    Router --> MarketingPages
    Main --> ViteConfig
    ViteConfig -->|proxy /api| Server

    %% Payment Flow
    CheckoutForm -->|"POST /api/payment/create-payment-intent"| PaymentController
    PaymentController -->|"uses Stripe SDK"| StripeAPI
    StripeAPI -->|"clientSecret"| CheckoutForm
    CheckoutForm -->|finalize order| OrderController

    %% Auth Flow
    LoginPage -->|"POST /api/users & /api/users/login"| UserController
    UserController --> UserModel
    UserModel --> MongoDB

    %% Order Flow
    CartPage -->|"POST /api/orders"| OrderController
    UserDashboard -->|"GET /api/orders/myorders"| OrderController
    AdminDashboard -->|"GET /api/orders"| OrderController
    OrderController --> OrderModel
    OrderModel --> MongoDB

    %% Backend Structure
    Server --> DBConfig
    Server --> UserRoutes
    Server --> OrderRoutes
    Server --> PaymentRoutes
    UserRoutes --> AuthMiddleware
    OrderRoutes --> AuthMiddleware
    PaymentRoutes --> AuthMiddleware
    AuthMiddleware --> UserController
    AuthMiddleware --> OrderController
    AuthMiddleware --> PaymentController

    %% Database
    UserController --> MongoDB
    OrderController --> MongoDB

    %% Click Events
    click HTML "https://github.com/ajithesh9/one-store/blob/main/Frontend/index.html"
    click Main "https://github.com/ajithesh9/one-store/blob/main/Frontend/src/main.jsx"
    click ViteConfig "https://github.com/ajithesh9/one-store/blob/main/Frontend/vite.config.js"
    click AuthContext "https://github.com/ajithesh9/one-store/blob/main/Frontend/src/context/AuthContext.jsx"
    click CartContext "https://github.com/ajithesh9/one-store/blob/main/Frontend/src/context/CartContext.jsx"
    click CheckoutForm "https://github.com/ajithesh9/one-store/blob/main/Frontend/src/components/CheckoutForm.jsx"
    click LoginPage "https://github.com/ajithesh9/one-store/blob/main/Frontend/src/pages/Login.jsx"
    click UserDashboard "https://github.com/ajithesh9/one-store/blob/main/Frontend/src/pages/Dashboard.jsx"
    click AdminDashboard "https://github.com/ajithesh9/one-store/blob/main/Frontend/src/pages/AdminDashboard.jsx"
    click CartPage "https://github.com/ajithesh9/one-store/blob/main/Frontend/src/pages/Cart.jsx"
    click PaymentPage "https://github.com/ajithesh9/one-store/blob/main/Frontend/src/pages/Payment.jsx"
    click Server "https://github.com/ajithesh9/one-store/blob/main/Backend/server.js"
    click DBConfig "https://github.com/ajithesh9/one-store/blob/main/Backend/config/db.js"
    click UserRoutes "https://github.com/ajithesh9/one-store/blob/main/Backend/routes/userRoutes.js"
    click OrderRoutes "https://github.com/ajithesh9/one-store/blob/main/Backend/routes/orderRoutes.js"
    click PaymentRoutes "https://github.com/ajithesh9/one-store/blob/main/Backend/routes/paymentRoutes.js"
    click AuthMiddleware "https://github.com/ajithesh9/one-store/blob/main/Backend/middleware/authMiddleware.js"
    click UserController "https://github.com/ajithesh9/one-store/blob/main/Backend/controllers/userController.js"
    click OrderController "https://github.com/ajithesh9/one-store/blob/main/Backend/controllers/orderController.js"
    click PaymentController "https://github.com/ajithesh9/one-store/blob/main/Backend/controllers/paymentController.js"
    click UserModel "https://github.com/ajithesh9/one-store/blob/main/Backend/models/User.js"
    click OrderModel "https://github.com/ajithesh9/one-store/blob/main/Backend/models/Order.js"

    %% Styles
    classDef ui fill:#E8F4FD,stroke:#3498DB,color:#0E47A1;
    classDef bizlogic fill:#FDEDE8,stroke:#E74C3C,color:#922B21;
    classDef datastore fill:#FEF9E7,stroke:#F1C40F,color:#7D6608;
    classDef external fill:#ECEFF1,stroke:#607D8B,color:#263238;
    classDef security fill:#EBF5FB,stroke:#3498DB,color:#1B4F72;
```
