# winzepz/backend architecture

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

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

## Overview

##### Project type and purpose
- This is a monolithic backend API built with Node.js and Express.
- Purpose: Provide user authentication (multi-step registration + login), author role management, CRUD operations for “news” items, and admin moderation endpoints.
- Key use cases:
- Multi-step session-backed user sign-up
- Secure JWT-based login
- File uploads (images, content) for news articles
- Author and admin workflows (approve, reject, delete)

##### File structure
- Top‐level files
- server.js: entry point, configures Express app and middleware
- .env, config/: environment and connection setup (MongoDB, Cloudinary)
- package.json: lists dependencies (Express, Mongoose, JWT, Joi, Bcrypt, etc.)
- Directories
- routes/: defines URL endpoints grouped by feature (authRoutes.js, newsRoutes.js, adminRoutes.js, devRoutes.js)
- controllers/: business logic for each route group (authController.js, newsController.js, adminController.js)
- models/: Mongoose schema definitions (User, News, DeletedNews, DeletedUser)
- middlewares/: request-level processing (auth checks, validation errors, file uploads via Multer, Cloudinary)
- utils/: helper functions (ID generators, validators)
- Patterns and architecture
- Layered MVC-style structure: Routes → Controllers → Models
- Middleware chain for authentication, authorization, error handling
- Session fallback for multi-step registration and JWT for persistent auth
- External integration: Cloudinary for media storage

##### README for additional insights
- Describes multi-step registration endpoints with session storage
- Lists all route groups and their access rules (public, author, admin)
- Mentions core technologies: Express, MongoDB/Mongoose, JWT, Joi, Bcrypt, dotenv, session storage
- Error handling via centralized errorHandler middleware

##### Elements
- Clear labels for each module/component (e.g., “authRoutes”, “authMiddleware”)
- Presentation (Routes)
- Middleware
- Business Logic (Controllers)
- Data Access (Models + DB)
- External Services (Cloudinary, Session Store)
- Annotations for technologies used in each component

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client
    Client["API Consumer / Frontend"]:::external

    %% Entry Point
    subgraph "Entry Point"
        Server["Express API Server<br/>(server.js)"]:::server
    end

    %% Configuration
    subgraph "Configuration"
        Env[".env"]:::config
        DBConfig["db.js"]:::config
        CloudConfig["cloudinary.js"]:::config
    end

    %% Presentation Layer
    subgraph "Routes (Presentation)" 
        AuthRoutes["authRoutes.js"]:::routes
        NewsRoutes["newsRoutes.js"]:::routes
        AdminRoutes["adminRoutes.js"]:::routes
        DevRoutes["devRoutes.js"]:::routes
    end

    %% Middleware Layer
    subgraph "Middleware"
        AuthMW["authMiddleware.js"]:::middleware
        Verified["isVerified.js"]:::middleware
        MulterMW["multer.js"]:::middleware
        CloudUploader["cloudinaryUploader.js"]:::middleware
        ErrorHandler["errorHandler.js"]:::middleware
    end

    %% Controllers Layer
    subgraph "Controllers"
        AuthCtrl["authController.js"]:::controller
        NewsCtrl["newsController.js"]:::controller
        AdminCtrl["adminController.js"]:::controller
    end

    %% Data Layer
    subgraph "Models & Database"
        UserModel["User.js"]:::model
        NewsModel["News.js"]:::model
        DeletedNews["DeletedNews.js"]:::model
        DeletedUser["DeletedUser.js"]:::model
        MongoDB[(MongoDB)]:::external
    end

    %% Utilities
    subgraph "Utilities"
        GenNewsId["generateUniqueNewsId.js"]:::util
        GenUserId["generateUserId.js"]:::util
        Validators["validators.js"]:::util
    end

    %% External Services
    subgraph "External Services"
        Sessions[(Session Store)]:::external
        Cloudinary[(Cloudinary Service)]:::external
    end

    %% Data Flows
    Client -->|"HTTP Request"| Server
    Server -->|uses| Env
    Server -->|connects| DBConfig
    Server -->|configures| CloudConfig
    Server -->|routes to| AuthRoutes
    Server -->|routes to| NewsRoutes
    Server -->|routes to| AdminRoutes
    Server -->|routes to| DevRoutes

    AuthRoutes -->|passes through| AuthMW
    AuthRoutes -->|passes through| Verified
    NewsRoutes -->|passes through| AuthMW
    NewsRoutes -->|passes through| Verified
    NewsRoutes -->|file upload| MulterMW
    NewsRoutes -->|upload to| CloudUploader
    AdminRoutes -->|passes through| AuthMW
    AdminRoutes -->|passes through| Verified
    DevRoutes -->|passes through| ErrorHandler

    AuthMW --> AuthCtrl
    Verified --> AuthCtrl
    MulterMW --> CloudUploader
    CloudUploader --> NewsCtrl
    ErrorHandler --> AuthCtrl
    ErrorHandler --> NewsCtrl
    ErrorHandler --> AdminCtrl

    AuthCtrl -->|read/write| UserModel
    NewsCtrl -->|read/write| NewsModel
    AdminCtrl -->|read/write| DeletedNews
    AdminCtrl -->|read/write| DeletedUser

    UserModel --> MongoDB
    NewsModel --> MongoDB
    DeletedNews --> MongoDB
    DeletedUser --> MongoDB

    AuthCtrl -->|issue JWT| Client
    AuthCtrl -->|use Sessions| Sessions
    NewsCtrl -->|generate ID| GenNewsId
    AuthCtrl -->|generate ID| GenUserId
    AuthCtrl -->|validate| Validators
    NewsCtrl -->|validate| Validators

    NewsCtrl -->|store media| Cloudinary

    %% Click Events
    click Server "https://github.com/winzepz/backend/blob/master/server.js"
    click DBConfig "https://github.com/winzepz/backend/blob/master/config/db.js"
    click CloudConfig "https://github.com/winzepz/backend/blob/master/config/cloudinary.js"
    click AuthRoutes "https://github.com/winzepz/backend/blob/master/routes/authRoutes.js"
    click NewsRoutes "https://github.com/winzepz/backend/blob/master/routes/newsRoutes.js"
    click AdminRoutes "https://github.com/winzepz/backend/blob/master/routes/adminRoutes.js"
    click DevRoutes "https://github.com/winzepz/backend/blob/master/routes/devRoutes.js"
    click AuthCtrl "https://github.com/winzepz/backend/blob/master/controllers/authController.js"
    click NewsCtrl "https://github.com/winzepz/backend/blob/master/controllers/newsController.js"
    click AdminCtrl "https://github.com/winzepz/backend/blob/master/controllers/adminController.js"
    click AuthMW "https://github.com/winzepz/backend/blob/master/middlewares/authMiddleware.js"
    click Verified "https://github.com/winzepz/backend/blob/master/middlewares/isVerified.js"
    click MulterMW "https://github.com/winzepz/backend/blob/master/middlewares/multer.js"
    click CloudUploader "https://github.com/winzepz/backend/blob/master/middlewares/cloudinaryUploader.js"
    click ErrorHandler "https://github.com/winzepz/backend/blob/master/middlewares/errorHandler.js"
    click UserModel "https://github.com/winzepz/backend/blob/master/models/User.js"
    click NewsModel "https://github.com/winzepz/backend/blob/master/models/News.js"
    click DeletedNews "https://github.com/winzepz/backend/blob/master/models/DeletedNews.js"
    click DeletedUser "https://github.com/winzepz/backend/blob/master/models/DeletedUser.js"
    click GenNewsId "https://github.com/winzepz/backend/blob/master/utils/generateUniqueNewsId.js"
    click GenUserId "https://github.com/winzepz/backend/blob/master/utils/generateUserId.js"
    click Validators "https://github.com/winzepz/backend/blob/master/utils/validators.js"
    click Env "https://github.com/winzepz/backend/blob/master/.env"

    %% Styles
    classDef server fill:#f9f,stroke:#333,stroke-width:1px
    classDef config fill:#ccc,stroke:#333,stroke-width:1px
    classDef routes fill:#bbf,stroke:#333,stroke-width:1px
    classDef middleware fill:#f96,stroke:#333,stroke-width:1px
    classDef controller fill:#9f9,stroke:#333,stroke-width:1px
    classDef model fill:#ff9,stroke:#333,stroke-width:1px
    classDef util fill:#dcb,stroke:#333,stroke-width:1px
    classDef external fill:#fde,stroke:#333,stroke-width:1px
```
