# tuandangvan/my-pets-api architecture

> Architecture diagram of the GitHub repository tuandangvan/my-pets-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tuandangvan/my-pets-api
- Repository: https://github.com/tuandangvan/my-pets-api

## Overview

##### Main Components
- API Server: The central Express.js server (see src/server.js) which is responsible for routing incoming HTTP requests.
- Routes: Located in src/routes/v1, these are grouped by functionality (e.g., auth, admin, pet, center, chat, order, user, and post). Each route file maps specific endpoints to controller methods.
- Controllers: Found in src/controllers, controllers handle the incoming requests and delegate to the business logic in services. There are separate controllers for various domains such as authentication, pets, centers, chats, posts, etc.
- Services: Contained in src/services, these modules encapsulate business logic (e.g., accountService, adoptService, petService) and interact with the data layer.
- Database / Models: The models in src/models represent MongoDB collections. The database configuration is handled in src/config/mongodb.js.
- Middlewares: These include authentication and error handling middleware located in src/middlewares, ensuring that requests are properly authorized and handled for errors.
- Authentication & Token Utilities: Components like src/auth/authenticationToken.js and utility files (e.g., jwtUtils.js, token.js) work together for token generation and validation using JWT.
- Third-Party Integrations:
- Email Service: (src/sendEmail/emailService.js) which handles mailing functionalities (e.g., OTP codes).
- Upload Service: (src/upload/uploadCloudinary.js) for handling file uploads to Cloudinary.
- Socket Server: (src/socketServer.js) likely for real-time messaging or notifications.

##### Relationships and Interactions
- Incoming HTTP requests reach the Express server, which then routes calls to the appropriate route modules.
- Each route is connected to a controller method that processes the request.
- Controllers delegate operations to corresponding service functions (e.g., petService for pet-related logic).
- Services interact with the database via Mongoose models.
- Middlewares (for authentication, authorization, and error handling) are applied at various stages in the request lifecycle.
- Utility modules are used throughout to support functionalities like token generation and error formatting.
- Integration points for external services (email, file upload, real-time notifications) are clearly defined.

##### Architectural Patterns and Design Principles
- Layered Architecture: The clear separation between routes, controllers, services, and models enables better maintainability and testability.
- Modularity: Each feature (e.g., authentication, pet management, messaging) has its own folder grouping related controllers, routes, and services.
- Dependency Injection and Middleware Pattern: Middlewares ensure pre-processing (e.g., authentication) before reaching the core business logic.
- Reusability: Utility functions and constants (in src/utils and src/enums) reduce code duplication.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "API Layer"
        API_Server["API Server"]:::server
        Middlewares["Middlewares"]:::middleware
    end

    subgraph "Route Layer"
        Routes["Routes"]:::routes
    end

    subgraph "Controller Layer"
        Controllers["Controllers"]:::controller
    end

    subgraph "Service Layer"
        Services["Services"]:::service
    end

    subgraph "Data Layer"
        Models["Models"]:::model
        DB_Config["DB Config"]:::model
    end

    subgraph "Utilities"
        Auth_Token["Auth & Token Utilities"]:::utility
        JWT_Utils["JWT Utils"]:::utility
        Token_Utils["Token Utils"]:::utility
    end

    subgraph "External Integrations"
        Email_Service["Email Service"]:::integration
        Upload_Service["Upload Service"]:::integration
        Socket_Server["Socket Server"]:::integration
    end

    API_Server -->|"routes"| Routes
    API_Server -->|"applies"| Middlewares
    Routes -->|"maps_to"| Controllers
    Controllers -->|"delegates_to"| Services
    Services -->|"queries"| Models
    Services -->|"queries"| DB_Config

    Controllers -->|"uses"| Auth_Token
    Auth_Token -->|"utilizes"| JWT_Utils
    Auth_Token -->|"utilizes"| Token_Utils

    Services -->|"integrates"| Email_Service
    Services -->|"integrates"| Upload_Service
    Services -->|"integrates"| Socket_Server

    click API_Server "https://github.com/tuandangvan/my-pets-api/blob/main/src/server.js"
    click Routes "https://github.com/tuandangvan/my-pets-api/tree/main/src/routes/v1"
    click Controllers "https://github.com/tuandangvan/my-pets-api/tree/main/src/controllers"
    click Services "https://github.com/tuandangvan/my-pets-api/tree/main/src/services"
    click Models "https://github.com/tuandangvan/my-pets-api/tree/main/src/models"
    click DB_Config "https://github.com/tuandangvan/my-pets-api/blob/main/src/config/mongodb.js"
    click Middlewares "https://github.com/tuandangvan/my-pets-api/tree/main/src/middlewares"
    click Auth_Token "https://github.com/tuandangvan/my-pets-api/blob/main/src/auth/authenticationToken.js"
    click JWT_Utils "https://github.com/tuandangvan/my-pets-api/blob/main/src/utils/jwtUtils.js"
    click Token_Utils "https://github.com/tuandangvan/my-pets-api/blob/main/src/utils/token.js"
    click Email_Service "https://github.com/tuandangvan/my-pets-api/blob/main/src/sendEmail/emailService.js"
    click Upload_Service "https://github.com/tuandangvan/my-pets-api/blob/main/src/upload/uploadCloudinary.js"
    click Socket_Server "https://github.com/tuandangvan/my-pets-api/blob/main/src/socketServer.js"

    classDef server fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef routes fill:#A3E4D7,stroke:#138D75,stroke-width:2px;
    classDef controller fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef service fill:#A9DFBF,stroke:#27AE60,stroke-width:2px;
    classDef model fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef middleware fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;
    classDef utility fill:#FCF3CF,stroke:#B7950B,stroke-width:2px;
    classDef integration fill:#F1948A,stroke:#CB4335,stroke-width:2px;
```
