# ananthu-kp/prodtrack architecture

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

- Interactive diagram: https://gitdiagram.com/ananthu-kp/prodtrack
- Repository: https://github.com/ananthu-kp/prodtrack

## Overview

**ProdTrack** is a **single-node, monolithic web application**:
- **Backend**: Node.js + Express REST API serving JSON and handling file uploads
- **Frontend**: Static HTML/CSS + vanilla JS (Fetch API) served from `public/`
- **Database**: MySQL (users + products tables)
- **Storage**: Local filesystem for uploaded product images (`public/uploads/`)
- **Primary use case**: Admin logs in → manages products (CRUD) → views dashboard analytics/report.

##### Runtime entry + composition
- `app.js`
The **main process**: Express app creation, middleware registration, static file hosting, route mounting, server listen.

##### Configuration / infrastructure
- `config/db.js`
**MySQL connection pool** (mysql2) + env wiring (dotenv).

##### HTTP routing layer
- `routes/authRoutes.js`
- `routes/productRoutes.js`
Express routers that define endpoints (`/api/auth/login`, `/api/products/...`).

##### Controller layer (HTTP → use-case)
- `controllers/authController.js`
- `controllers/productController.js`
Handles request parsing, response formatting, status codes, error mapping.

##### Service layer (business logic)
- `services/authService.js`
- `services/productService.js`
Encapsulates domain logic: credential verification, product CRUD, report aggregates, and likely image handling semantics (e.g., delete old image on update).

##### Middleware (cross-cutting)
- `middleware/upload.js`
Multer configuration: file size/type validation, storage destination, filename strategy.

##### Frontend (static)
- `public/login.html`, `public/dashboard.html`
- `public/js/dashboard.js` (Fetch calls to API)
- `public/css/*.css`

##### Database + ops
- `database.sql` (schema for `users`, `products`)
- `scripts/create-admin.js` (one-off admin bootstrap)
- `package.json` (dependencies: express, mysql2, bcrypt, multer, dotenv)

##### Public pages
- `GET /` (implicitly serves `public/login.html` if configured that way in `app.js`)
- `public/dashboard.html` is the authenticated UI.

- `POST /api/auth/login`
- `GET /api/products`
- `GET /api/products/:id`
- `POST /api/products` (multipart/form-data + image)
- `PUT /api/products/:id` (multipart/form-data)
- `DELETE /api/products/:id`
- `GET /api/products/report`

##### Data stores
- MySQL tables: `users`, `products`
- Upload dir: `public/uploads/`
- “Session management using localStorage” → represent as **Browser LocalStorage** storing auth/session token/flag (even if simplistic).

##### Login
- `dashboard/login.html` JS → `POST /api/auth/login`
- `authRoutes` → `authController` → `authService`
- `authService` → MySQL (`users`) + bcrypt verify
- Response → Browser stores “session” in **LocalStorage** → navigate to dashboard

##### Create/Update product with image
- Browser → `POST/PUT /api/products...` (multipart/form-data)
- `productRoutes` → `upload middleware (multer)` writes file to `public/uploads`
- `productController` → `productService`
- `productService` → MySQL (`products` insert/update)
- On update: `productService` deletes old image from filesystem (as README states)

##### Dashboard report
- Browser → `GET /api/products/report`
- `productController` → `productService`
- `productService` → MySQL aggregate queries
- Response renders totals/category breakdown in dashboard UI

##### HTTP paths (show direction + responsibility)
- `public/js/dashboard.js` **calls** `/api/*` endpoints (Fetch API)
- Express **routes** forward to controllers
- Controllers **depend on** services
- Services **depend on**:
- `config/db.js` pool for MySQL
- filesystem (`public/uploads`) for image lifecycle
- bcrypt (auth)

##### Data ownership
- MySQL is system of record for:
- users (credentials)
- product metadata (incl. image filename)
- filesystem stores:
- actual image bytes (named file)
- browser localStorage stores:
- auth/session indicator (as implemented)

##### Architectural patterns/principles to call out (and encode
- **Layered architecture**: Routes → Controllers → Services → DB
- **Separation of concerns**:
- Controllers: HTTP boundary
- Services: business rules + orchestration (DB + file cleanup)
- Middleware: cross-cutting request concerns (multer validation)
- **Stateless backend (mostly)**:
- “Session in localStorage” implies backend isn’t maintaining server-side sessions; represent backend as stateless per-request (unless you see session middleware in `app.js`)
- **Local disk storage for assets**:
- Images are not in S3; they are in `public/uploads`

**Client side**
- Admin User
- Browser
- `login.html`
- `dashboard.html`
- `dashboard.js`
- LocalStorage (auth/session flag)

**Server side (Node/Express)**
- `app.js` (Express server)
- Static file server (`express.static(public)`)
- `authRoutes.js`
- `productRoutes.js`
- `authController.js`
- `productController.js`
- `authService.js` (bcrypt)
- `productService.js` (CRUD + report + image cleanup)
- `upload.js` (multer: validation/storage)
- `config/db.js` (mysql2 pool)

**Data stores**
- MySQL `prodtrack` database
- `users` table
- `products` table
- Filesystem `public/uploads/`

**Ops**
- `scripts/create-admin.js` (one-off CLI → MySQL insert with bcrypt)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% =========================
    %% Level 1 — System Context
    %% =========================
    subgraph "Level 1 — System Context"
        Admin["Admin User"]:::external
        Browser["Browser Client"]:::client
        LocalStorage["Browser LocalStorage\n(auth/session flag)"]:::store

        Server["ProdTrack Server\n(Node.js + Express)"]:::server
        MySQL[("MySQL Database\n(prodtrack)")]:::datastore
        Uploads[/"Local Filesystem\npublic/uploads/"/]:::datastore
    end

    Admin -->|"uses"| Browser
    Browser -->|"stores session"| LocalStorage
    Browser -->|"HTTP(S)\nstatic+REST"| Server
    Server -->|"SQL over TCP"| MySQL
    Server -->|"Filesystem I/O\nread/write/delete"| Uploads

    %% =========================
    %% Level 2 — Container / Runtime View
    %% =========================
    subgraph "Level 2 — Container / Runtime View"
        direction TB

        subgraph "Client (Static Frontend)"
            direction TB
            LoginHTML["public/login.html"]:::client
            DashHTML["public/dashboard.html"]:::client
            DashJS["public/js/dashboard.js\n(Fetch API)"]:::client
            CSS1["public/css/style.css"]:::client
            CSS2["public/css/dashboard.css"]:::client
        end

        subgraph "Node.js / Express Process (single instance)"
            direction TB
            AppJS["app.js (Express App)"]:::server
            StaticMW["Static file server\n(express.static(public))"]:::infra

            subgraph "HTTP Routing Layer"
                direction TB
                AuthRoutes["routes/authRoutes.js"]:::api
                ProductRoutes["routes/productRoutes.js"]:::api
            end

            subgraph "Middleware"
                direction TB
                UploadMW["middleware/upload.js (multer)"]:::infra
            end

            subgraph "Controller Layer"
                direction TB
                AuthController["controllers/authController.js"]:::api
                ProductController["controllers/productController.js"]:::api
            end

            subgraph "Service Layer"
                direction TB
                AuthService["services/authService.js\n(bcrypt verify)"]:::service
                ProductService["services/productService.js\n(CRUD+report+image cleanup)"]:::service
            end

            DBPool["config/db.js (mysql2 pool)"]:::infra
        end

        subgraph "Data Stores"
            direction TB
            Schema["database.sql\n(users, products schema)"]:::note
            Users[("users table")]:::datastore
            Products[("products table")]:::datastore
        end

        subgraph "Ops / Admin Bootstrap"
            direction TB
            CreateAdmin["scripts/create-admin.js\n(one-off CLI bootstrap)"]:::ops
            Pkg["package.json\n(express, mysql2, bcrypt, multer, dotenv)"]:::ops
        end
    end

    %% Static delivery
    Browser -->|"GET / (login)\nGET /dashboard.html\nGET /css/*\nGET /js/*"| StaticMW
    StaticMW -->|"serves"| LoginHTML
    StaticMW -->|"serves"| DashHTML
    StaticMW -->|"serves"| DashJS
    StaticMW -->|"serves"| CSS1
    StaticMW -->|"serves"| CSS2

    %% Frontend API calls
    DashJS -->|"POST /api/auth/login (JSON)"| AuthRoutes
    DashJS -->|"GET /api/products"| ProductRoutes
    DashJS -->|"GET /api/products/:id"| ProductRoutes
    DashJS -->|"POST /api/products (multipart/form-data)"| ProductRoutes
    DashJS -->|"PUT /api/products/:id (multipart/form-data)"| ProductRoutes
    DashJS -->|"DELETE /api/products/:id"| ProductRoutes
    DashJS -->|"GET /api/products/report"| ProductRoutes
    DashJS -->|"updates auth flag"| LocalStorage

    %% Express composition
    AppJS -->|"mounts"| StaticMW
    AppJS -->|"mounts /api/auth"| AuthRoutes
    AppJS -->|"mounts /api/products"| ProductRoutes
    AppJS -->|"uses"| DBPool
    AppJS -->|"uses"| UploadMW

    %% Routing -> Controllers
    AuthRoutes -->|"calls"| AuthController
    ProductRoutes -->|"calls"| ProductController

    %% Upload middleware in product routes
    ProductRoutes -->|"uses multer for\nPOST/PUT"| UploadMW
    UploadMW -->|"write image"| Uploads

    %% Controllers -> Services
    AuthController -->|"delegates"| AuthService
    ProductController -->|"delegates"| ProductService

    %% Services -> DB + FS
    AuthService -->|"SELECT users"| DBPool
    ProductService -->|"CRUD products\nSELECT aggregates"| DBPool
    DBPool -->|"queries"| MySQL
    MySQL -->|"contains"| Users
    MySQL -->|"contains"| Products
    Schema -.->|"defines"| MySQL

    ProductService -->|"delete old image\n(on update/delete)"| Uploads
    StaticMW -->|"serve images\nGET /uploads/*"| Uploads

    %% Ops script
    CreateAdmin -->|"bcrypt hash\n+ INSERT admin"| DBPool
    Pkg -.->|"declares deps for"| AppJS

    %% =========================
    %% Level 3 — Key Flows (mini sequences)
    %% =========================
    subgraph "Level 3 — Key Request Flows (Sequences)"
        direction TB

        subgraph "1) Login"
            direction TB
            L1["Browser UI (login.html)"]:::client
            L2["POST /api/auth/login"]:::api
            L3["authRoutes -> authController -> authService"]:::service
            L4["MySQL users + bcrypt verify"]:::datastore
            L5["Store session flag in LocalStorage\nthen open dashboard"]:::store
        end

        subgraph "2) Create/Update Product with Image"
            direction TB
            P1["Dashboard (dashboard.js)"]:::client
            P2["POST/PUT /api/products (multipart/form-data)"]:::api
            P3["multer upload middleware writes file"]:::infra
            P4["productController -> productService"]:::service
            P5["MySQL products insert/update\n(store image filename)"]:::datastore
            P6["On update: delete old image from uploads"]:::datastore
        end

        subgraph "3) Dashboard Report"
            direction TB
            R1["Dashboard (dashboard.js)"]:::client
            R2["GET /api/products/report"]:::api
            R3["productController -> productService"]:::service
            R4["MySQL aggregate queries"]:::datastore
            R5["Render totals/breakdown in dashboard UI"]:::client
        end
    end

    L1 --> L2 --> L3 --> L4 --> L5
    P1 --> P2 --> P3 --> P4 --> P5 --> P6
    R1 --> R2 --> R3 --> R4 --> R5

    %% =========================
    %% Click Events (component_mapping)
    %% =========================
    click AppJS "https://github.com/ananthu-kp/prodtrack/blob/main/app.js"
    click DBPool "https://github.com/ananthu-kp/prodtrack/blob/main/config/db.js"
    click AuthRoutes "https://github.com/ananthu-kp/prodtrack/blob/main/routes/authRoutes.js"
    click ProductRoutes "https://github.com/ananthu-kp/prodtrack/blob/main/routes/productRoutes.js"
    click AuthController "https://github.com/ananthu-kp/prodtrack/blob/main/controllers/authController.js"
    click ProductController "https://github.com/ananthu-kp/prodtrack/blob/main/controllers/productController.js"
    click AuthService "https://github.com/ananthu-kp/prodtrack/blob/main/services/authService.js"
    click ProductService "https://github.com/ananthu-kp/prodtrack/blob/main/services/productService.js"
    click UploadMW "https://github.com/ananthu-kp/prodtrack/blob/main/middleware/upload.js"
    click LoginHTML "https://github.com/ananthu-kp/prodtrack/blob/main/public/login.html"
    click DashHTML "https://github.com/ananthu-kp/prodtrack/blob/main/public/dashboard.html"
    click DashJS "https://github.com/ananthu-kp/prodtrack/blob/main/public/js/dashboard.js"
    click CSS2 "https://github.com/ananthu-kp/prodtrack/blob/main/public/css/dashboard.css"
    click CSS1 "https://github.com/ananthu-kp/prodtrack/blob/main/public/css/style.css"
    click Uploads "https://github.com/ananthu-kp/prodtrack/tree/main/public/uploads/"
    click Schema "https://github.com/ananthu-kp/prodtrack/blob/main/database.sql"
    click CreateAdmin "https://github.com/ananthu-kp/prodtrack/blob/main/scripts/create-admin.js"
    click Pkg "https://github.com/ananthu-kp/prodtrack/blob/main/package.json"

    %% =========================
    %% Styles
    %% =========================
    classDef external fill:#ffffff,stroke:#111827,stroke-width:2px,color:#111827
    classDef client fill:#dbeafe,stroke:#1d4ed8,stroke-width:1.5px,color:#0b2559
    classDef server fill:#bfdbfe,stroke:#1e40af,stroke-width:2px,color:#0b2559
    classDef api fill:#bbf7d0,stroke:#15803d,stroke-width:1.5px,color:#052e12
    classDef service fill:#fed7aa,stroke:#c2410c,stroke-width:1.5px,color:#431407
    classDef infra fill:#e9d5ff,stroke:#6d28d9,stroke-width:1.5px,color:#2e1065
    classDef datastore fill:#e5e7eb,stroke:#374151,stroke-width:1.5px,color:#111827
    classDef store fill:#e5e7eb,stroke:#4b5563,stroke-width:1.5px,color:#111827
    classDef ops fill:#ffe4e6,stroke:#be123c,stroke-width:1.5px,color:#4c0519
    classDef note fill:#fef9c3,stroke:#a16207,stroke-width:1.5px,color:#3f2d06
```
