# tutortoise/backend architecture

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

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

## Overview

##### Main Components
- API Server – Represent the Express.js server as the central “API Gateway”. This is where all incoming HTTP requests are handled.
- Middleware & Common Utilities – Include components for application-level middlewares (e.g., logging, validation, security, file handling with Multer) and shared utilities (e.g., helpers: JWT, image processing, OAuth service, etc.).
- Firebase Realtime Database (for real-time interactions and user messaging).
- Google Cloud Storage (for profile pictures and chat image storage).
- External APIs integrated for specialized functionality, such as:
- FACE_VALIDATION API,
- ABUSIVE_DETECTION API,
- RECOMMENDATION API.
- Testing & Documentation – Although not runtime components, note the use of Vitest and SuperTest (integration tests) and Swagger documentation, indicating a practice of comprehensive testing and documentation.

##### Representing the Relationships & Data Flow
- That each module communicates with the PostgreSQL database (via Drizzle ORM) for CRUD operations.
- Inter-module communication (if any exists) – for instance, if notifications are triggered after order completion or chat actions.

##### Architectural Patterns & Design Principles
- Modularity: The project’s layout, with a separate directory per domain (abusive-detection, auth, category, etc.), is indicative of a modular design – each module is isolated, keeps its own controllers, services, routes, and repositories.
- Separation of Concerns: Use labels to clearly distinguish between components such as middleware, controllers, services, and repositories.

##### Including Technologies and Frameworks
- Clearly label "Express.js" in the API Server component.
- "Drizzle ORM" next to the PostgreSQL database component.
- External services: “Firebase Realtime Database”, “Google Cloud Storage”, along with the external URLs like FACE_VALIDATION_URL, ABUSIVE_DETECTION_URL, and RECOMMENDATION_URL.
- Optionally denote the testing framework (Vitest & SuperTest) and mention Swagger for API documentation.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% API Layer
    subgraph "API Layer"
        API("API Server (Express.js)"):::api
    end

    %% Middleware & Utilities
    subgraph "Middleware & Utilities"
        MW("Middleware (Logging, Multer, Security, Validation)"):::middleware
        HELP("Helpers (JWT, Image Processing)"):::utility
        COMMON("Common Services (OAuth, FCM)"):::utility
    end

    %% Modules
    subgraph "Modules"
        AUTH("Auth Module"):::module
        CATEGORY("Category Module"):::module
        CHAT("Chat Module"):::module
        LEARNER("Learner Module"):::module
        NOTIFICATION("Notification Module"):::module
        ORDER("Order Module"):::module
        RECOMMENDATION("Recommendation Module"):::module
        REVIEW("Review Module"):::module
        TUTOR("Tutor Module"):::module
        TUTORIES("Tutories Module"):::module
        ABUSIVE("Abusive Detection Module"):::module
        FACEVAL("Face Validation Module"):::module
    end

    %% Data Layer
    subgraph "Data Layer"
        DB("PostgreSQL (Drizzle ORM)"):::database
        MIGR("Migrations (Drizzle)"):::database
    end

    %% External Integrations
    subgraph "External Integrations"
        FB("Firebase Realtime Database & Storage"):::external
        GCS("Google Cloud Storage"):::external
    end

    %% Testing & Documentation
    subgraph "Testing & Documentation"
        TESTS("Tests (Vitest & SuperTest)"):::testing
        SWAG("Swagger Documentation"):::testing
    end

    %% Relationships between API and Modules via Middleware
    API -->|"routes"| MW
    MW -->|"passesRequest"| AUTH
    MW -->|"passesRequest"| CATEGORY
    MW -->|"passesRequest"| CHAT
    MW -->|"passesRequest"| LEARNER
    MW -->|"passesRequest"| NOTIFICATION
    MW -->|"passesRequest"| ORDER
    MW -->|"passesRequest"| RECOMMENDATION
    MW -->|"passesRequest"| REVIEW
    MW -->|"passesRequest"| TUTOR
    MW -->|"passesRequest"| TUTORIES
    MW -->|"passesRequest"| ABUSIVE
    MW -->|"passesRequest"| FACEVAL

    %% Direct API routing as well
    API -->|"HTTP_Routes"| AUTH
    API -->|"HTTP_Routes"| CATEGORY
    API -->|"HTTP_Routes"| CHAT
    API -->|"HTTP_Routes"| LEARNER
    API -->|"HTTP_Routes"| NOTIFICATION
    API -->|"HTTP_Routes"| ORDER
    API -->|"HTTP_Routes"| RECOMMENDATION
    API -->|"HTTP_Routes"| REVIEW
    API -->|"HTTP_Routes"| TUTOR
    API -->|"HTTP_Routes"| TUTORIES
    API -->|"HTTP_Routes"| ABUSIVE
    API -->|"HTTP_Routes"| FACEVAL

    %% Modules interacting with the Database
    AUTH -->|"CRUD"| DB
    CATEGORY -->|"CRUD"| DB
    CHAT -->|"CRUD"| DB
    LEARNER -->|"CRUD"| DB
    NOTIFICATION -->|"CRUD"| DB
    ORDER -->|"CRUD"| DB
    RECOMMENDATION -->|"CRUD"| DB
    REVIEW -->|"CRUD"| DB
    TUTOR -->|"CRUD"| DB
    TUTORIES -->|"CRUD"| DB
    ABUSIVE -->|"CRUD"| DB
    FACEVAL -->|"CRUD"| DB

    %% Migrations updating the Database
    MIGR -->|"updates"| DB

    %% External Integrations with Modules
    CHAT -->|"storesFiles"| FB
    CHAT -->|"storesFiles"| GCS

    %% Inter-module communication example
    ORDER -->|"triggers"| NOTIFICATION

    %% Testing & Documentation associations
    API --- TESTS
    API --- SWAG

    %% Utilities support
    AUTH --- HELP
    AUTH --- COMMON

    %% Classes for styling
    classDef api fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef module fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef middleware fill:#ffcdd2,stroke:#c62828,stroke-width:2px;
    classDef utility fill:#ffecb3,stroke:#ff6f00,stroke-width:2px;
    classDef database fill:#ffe0b2,stroke:#ef6c00,stroke-width:2px;
    classDef external fill:#d1c4e9,stroke:#5e35b1,stroke-width:2px;
    classDef testing fill:#e0e0e0,stroke:#424242,stroke-width:2px;

    %% Click Events
    click API "https://github.com/tutortoise/backend/blob/master/src/main.ts"
    click AUTH "https://github.com/tutortoise/backend/tree/master/src/module/auth"
    click CATEGORY "https://github.com/tutortoise/backend/tree/master/src/module/category"
    click CHAT "https://github.com/tutortoise/backend/tree/master/src/module/chat"
    click LEARNER "https://github.com/tutortoise/backend/tree/master/src/module/learner"
    click NOTIFICATION "https://github.com/tutortoise/backend/tree/master/src/module/notification"
    click ORDER "https://github.com/tutortoise/backend/tree/master/src/module/order"
    click RECOMMENDATION "https://github.com/tutortoise/backend/tree/master/src/module/recommendation"
    click REVIEW "https://github.com/tutortoise/backend/tree/master/src/module/review"
    click TUTOR "https://github.com/tutortoise/backend/tree/master/src/module/tutor"
    click TUTORIES "https://github.com/tutortoise/backend/tree/master/src/module/tutories"
    click ABUSIVE "https://github.com/tutortoise/backend/tree/master/src/module/abusive-detection"
    click FACEVAL "https://github.com/tutortoise/backend/tree/master/src/module/face-validation"
    click DB "https://github.com/tutortoise/backend/tree/master/src/db"
    click MIGR "https://github.com/tutortoise/backend/tree/master/drizzle"
    click MW "https://github.com/tutortoise/backend/tree/master/src/middleware"
    click HELP "https://github.com/tutortoise/backend/tree/master/src/helpers"
    click COMMON "https://github.com/tutortoise/backend/tree/master/src/common"
    click FB "https://github.com/tutortoise/backend/blob/master/firebase.json"
    click TESTS "https://github.com/tutortoise/backend/tree/master/tests"
    click SWAG "https://github.com/tutortoise/backend/tree/master/docs"
```
