# userrounakk/kisanpedia-backend architecture

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

- Interactive diagram: https://gitdiagram.com/userrounakk/kisanpedia-backend
- Repository: https://github.com/userrounakk/kisanpedia-backend

## Overview

##### Project Type and Purpose
- This is a RESTful backend service (“Kisanpedia Backend”) built with Node.js, Express, and MongoDB.
- Purpose: expose CRUD APIs for five core domain entities (Location, Plant, Seller, Store, User), enforce authentication/authorization, and serve data to a frontend or other clients.

##### File Structure
- app.js: entry point—initializes Express, middleware, connects to MongoDB, mounts routers.
- app/controller: business-logic layer; one controller per entity.
- app/middleware: request-processing filters (auth checks, validation, file uploads).
- routes: Express routers mapping HTTP endpoints to controllers.
- models: Mongoose schemas representing domain entities.
- helpers: utility functions (e.g., JWT token generation).
- package.json/.env: configuration, dependencies, environment variables.

##### README for Insights
- Tech stack: Node.js, Express, MongoDB, Mongoose.
- Entities and relationships:
- Location: unique name
- Plant: description, image URL, availability (locations), price
- Seller: contact details, plants they sell, location
- Store: address, map location
- User: roles (Admin, Super Admin)
- Features: authentication, role-based access, entity management.

##### Main Components & Their Relationships
a. API Layer (Express Server)
- app.js loads global middleware (JSON parser, CORS, logging).
- Routes: /auth, /location, /plant, /seller, /store; each routed to its controller.
b. Middleware Layer
- auth.middleware: verifies JWT and user role.
- entity-specific middleware: e.g., plant.middleware validates incoming plant data.
- uploader.middleware: handles multipart file uploads for plant images.
c. Controller Layer
- auth.controller: login, signup, token refresh.
- X.controller: CRUD handlers for each domain model.
d. Data Layer
- Mongoose models (Location, Plant, Seller, Store, User) interact with MongoDB.
e. External Services
- MongoDB Atlas (or local instance).
- (Optionally) Object storage if images are stored externally.
f. Helpers
- token.helper: JWT creation and verification.

##### Architectural Patterns & Design Principles
- Layered Architecture: clear separation of concerns (Routing → Middleware → Controller → Data).
- Single Responsibility: each middleware and controller has a distinct purpose.
- Role-Based Access Control: enforce via auth.middleware.
- Configuration via environment variables (.env).

##### Final Recommendations
- Every component is labeled, and data flow is unambiguous.
- Related files under their layer names for clarity.
- Call out any environment-driven configurations (e.g., DB URI, JWT_SECRET) near the configuration file.
- Role checks (Admin vs Super Admin) in the auth middleware block.
- Review with a team member to confirm no major component is missing.

## Diagram (Mermaid)

```mermaid
graph TB
    Client["Client (Web/App/Postman)"]:::external

    subgraph "Kisanpedia Backend"
        App["Express Server Entry Point (app.js)"]:::config

        subgraph "Routes"
            AuthRoutes["Authentication Routes"]:::routes
            LocRoutes["Location Routes"]:::routes
            PlantRoutes["Plant Routes"]:::routes
            SellerRoutes["Seller Routes"]:::routes
            StoreRoutes["Store Routes"]:::routes
            CentralRoutes["Central Route Loader"]:::routes
        end

        subgraph "Middleware"
            AuthMiddleware["Authentication Middleware"]:::middleware
            LocMiddleware["Location Validation Middleware"]:::middleware
            PlantMiddleware["Plant Validation Middleware"]:::middleware
            SellerMiddleware["Seller Validation Middleware"]:::middleware
            StoreMiddleware["Store Validation Middleware"]:::middleware
            UploadMiddleware["File Upload Middleware"]:::middleware
        end

        subgraph "Controllers"
            AuthController["Authentication Controller"]:::controller
            DashController["Dashboard Controller"]:::controller
            LocController["Location Controller"]:::controller
            PlantController["Plant Controller"]:::controller
            SellerController["Seller Controller"]:::controller
            StoreController["Store Controller"]:::controller
        end

        subgraph "Models"
            LocModel["Location Model"]:::model
            PlantModel["Plant Model"]:::model
            SellerModel["Seller Model"]:::model
            StoreModel["Store Model"]:::model
            UserModel["User Model"]:::model
        end

        subgraph "Helpers"
            TokenHelper["JWT Token Helper"]:::helper
        end

        subgraph "Config"
            Env[".env Example"]:::config
            Package["package.json"]:::config
        end
    end

    MongoDB["MongoDB"]:::database
    S3["S3 Storage"]:::external

    Client --> App
    App --> CentralRoutes
    CentralRoutes --> AuthRoutes
    CentralRoutes --> LocRoutes
    CentralRoutes --> PlantRoutes
    CentralRoutes --> SellerRoutes
    CentralRoutes --> StoreRoutes

    AuthRoutes --> AuthMiddleware
    LocRoutes --> AuthMiddleware
    PlantRoutes --> AuthMiddleware
    SellerRoutes --> AuthMiddleware
    StoreRoutes --> AuthMiddleware

    LocRoutes --> LocMiddleware
    PlantRoutes --> PlantMiddleware
    SellerRoutes --> SellerMiddleware
    StoreRoutes --> StoreMiddleware

    PlantRoutes --> UploadMiddleware
    UploadMiddleware --> S3

    AuthMiddleware --> AuthController
    LocMiddleware --> LocController
    PlantMiddleware --> PlantController
    SellerMiddleware --> SellerController
    StoreMiddleware --> StoreController

    AuthController --> UserModel
    LocController --> LocModel
    PlantController --> PlantModel
    SellerController --> SellerModel
    StoreController --> StoreModel

    UserModel --> MongoDB
    LocModel --> MongoDB
    PlantModel --> MongoDB
    SellerModel --> MongoDB
    StoreModel --> MongoDB

    App --> TokenHelper
    App --> Env
    App --> Package

    classDef routes fill:#cce5ff,stroke:#666;
    classDef middleware fill:#ccefff,stroke:#666;
    classDef controller fill:#b3d9ff,stroke:#666;
    classDef model fill:#d9f2d9,stroke:#666;
    classDef helper fill:#ffffcc,stroke:#666;
    classDef config fill:#f2f2f2,stroke:#666;
    classDef external fill:#ffe6cc,stroke:#666;
    classDef database fill:#ffddb3,stroke:#666;

    click App "https://github.com/userrounakk/kisanpedia-backend/blob/main/app.js"
    click AuthRoutes "https://github.com/userrounakk/kisanpedia-backend/blob/main/routes/auth.routes.js"
    click LocRoutes "https://github.com/userrounakk/kisanpedia-backend/blob/main/routes/location.routes.js"
    click PlantRoutes "https://github.com/userrounakk/kisanpedia-backend/blob/main/routes/plant.routes.js"
    click SellerRoutes "https://github.com/userrounakk/kisanpedia-backend/blob/main/routes/seller.routes.js"
    click StoreRoutes "https://github.com/userrounakk/kisanpedia-backend/blob/main/routes/store.routes.js"
    click CentralRoutes "https://github.com/userrounakk/kisanpedia-backend/blob/main/routes/routes.js"
    click AuthMiddleware "https://github.com/userrounakk/kisanpedia-backend/blob/main/app/middleware/auth.middleware.js"
    click LocMiddleware "https://github.com/userrounakk/kisanpedia-backend/blob/main/app/middleware/location.middleware.js"
    click PlantMiddleware "https://github.com/userrounakk/kisanpedia-backend/blob/main/app/middleware/plant.middleware.js"
    click SellerMiddleware "https://github.com/userrounakk/kisanpedia-backend/blob/main/app/middleware/seller.middleware.js"
    click StoreMiddleware "https://github.com/userrounakk/kisanpedia-backend/blob/main/app/middleware/store.middleware.js"
    click UploadMiddleware "https://github.com/userrounakk/kisanpedia-backend/blob/main/app/middleware/uploader.middleware.js"
    click AuthController "https://github.com/userrounakk/kisanpedia-backend/blob/main/app/controller/auth.controller.js"
    click DashController "https://github.com/userrounakk/kisanpedia-backend/blob/main/app/controller/dashboard.controller.js"
    click LocController "https://github.com/userrounakk/kisanpedia-backend/blob/main/app/controller/location.controller.js"
    click PlantController "https://github.com/userrounakk/kisanpedia-backend/blob/main/app/controller/plant.controller.js"
    click SellerController "https://github.com/userrounakk/kisanpedia-backend/blob/main/app/controller/seller.controller.js"
    click StoreController "https://github.com/userrounakk/kisanpedia-backend/blob/main/app/controller/store.controller.js"
    click LocModel "https://github.com/userrounakk/kisanpedia-backend/blob/main/models/Location.js"
    click PlantModel "https://github.com/userrounakk/kisanpedia-backend/blob/main/models/Plant.js"
    click SellerModel "https://github.com/userrounakk/kisanpedia-backend/blob/main/models/Seller.js"
    click StoreModel "https://github.com/userrounakk/kisanpedia-backend/blob/main/models/Store.js"
    click UserModel "https://github.com/userrounakk/kisanpedia-backend/blob/main/models/User.js"
    click TokenHelper "https://github.com/userrounakk/kisanpedia-backend/blob/main/helpers/token.helper.js"
    click Env "https://github.com/userrounakk/kisanpedia-backend/blob/main/.env.example"
    click Package "https://github.com/userrounakk/kisanpedia-backend/blob/main/package.json"
```
