# smoluca123/smospace_nestjs architecture

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

- Interactive diagram: https://gitdiagram.com/smoluca123/smospace_nestjs
- Repository: https://github.com/smoluca123/smospace_nestjs

## Overview

This project is a backend application developed using the NestJS framework. It follows a modular design pattern, where different logical areas of the backend are separated into their own modules. The primary purpose is to provide a scalable and maintainable server-side API that includes features like user authentication, real-time communication via WebSocket gateways, database operations using Prisma, caching with Redis, integration with external services (e.g., AWS S3, Supabase, OpenAI), email notifications, and more.

##### Main Components
- NestJS Application Core: This is the entry point, with the initial configuration in main.ts and the Nest module (app.module.ts) orchestrating imports.
- REST API Controllers: Represent the various resources like authentication, user management, posts, comments, notifications, and emails. Each resource (located under src/resources) illustrates clear separation and dedicated controllers, services, and DTOs.
- WebSocket Gateways: Show components for real-time data handling. The gateways for app, user, post, and comment are organized under src/resources/gateways.
- Database Layer: The Prisma integration (src/prisma folder and the accompanying schema in prisma/schema.prisma) and SQL dump files (db_exports). Represent the database as a separate component where persistence is managed.
- Caching Layer: Include the caching modules (src/cache) and particularly the Redis cache service. This illustrates how caching is managed separately to improve performance.
- JWT and Authentication: Identify the authentication modules including the JWT module (src/jwt), strategies (src/strategy), and various guards and middlewares (src/guards, src/middlewares) that ensure secure access.
- External Integrations: Display integration points with external infrastructure/services:
- AWS S3 (src/services/aws/s3) for file storage.
- Supabase (src/services/supabase) for additional services.
- OpenAI configuration (src/configs/openai.config.ts) as well as email service modules for notifications.
- Utility & Global Modules: Show components for configuration, custom pipes, and decorators that influence global behavior and data validation (src/configs, src/decorators, src/pipes, src/global).

##### Relationships and Interactions
- How controllers interact with authentication guards and middleware to secure endpoints.
- Interactions between WebSocket gateways and both controllers and services to show real-time event handling.
- External integrations (with AWS/Supabase/OpenAI) as separate nodes that the relevant services reach out to.

##### Architectural Patterns and Key Design Principles
- Modular Architecture: Illustrate how each resource (auth, post, user, etc.) is encapsulated and can operate independently.
- MVC Principles within Modules: Emphasize the separation among controllers, services, and data access layers.
- Dependency Injection: As per NestJS standards, indicate how services and modules are injected into controllers and other parts.
- Middleware and Guards: Show security flows (JWT verification, role check, IP validation, etc.) at key entry points in the request lifecycle.
- Real-Time Communication: Represent how WebSocket gateways interface with the rest of the application.

## Diagram (Mermaid)

```mermaid
flowchart TD
    client["Client Request"]:::external

    subgraph "NestJS Application Core"
        CoreA["Main"]:::core
        CoreB["App Module"]:::core
    end

    subgraph "REST API Modules"
        Auth["Auth Module"]:::module
        Email["Email Module"]:::module
        Notif["Notification Module"]:::module
        Post["Post Module"]:::module
        PostComment["Post-Comment Module"]:::module
        User["User Module"]:::module
    end

    subgraph "WebSocket Gateways"
        AppGW["App Gateway"]:::gateway
        CommentGW["Comment Gateway"]:::gateway
        PostGW["Post Gateway"]:::gateway
        UserGW["User Gateway"]:::gateway
    end

    subgraph "Database Layer"
        Prisma["Prisma Integration"]:::database
        SQLDumps["SQL Dumps"]:::database
    end

    subgraph "Caching Layer"
        RedisCache["Redis Cache"]:::cache
    end

    subgraph "JWT & Authentication"
        JWTMod["JWT Module"]:::auth
        JWTGuards["JWT Strategies & Guards"]:::auth
    end

    subgraph "External Integrations"
        AWS["AWS S3 Service"]:::external
        Supa["Supabase Service"]:::external
        OpenAI["OpenAI Config"]:::external
    end

    subgraph "Utility & Global Modules"
        Configs["Configurations"]:::utility
        Globals["Global Utils & Decorators & Pipes"]:::utility
    end

    %% Relationships
    client --> CoreB

    CoreB --> Auth
    CoreB --> Email
    CoreB --> Notif
    CoreB --> Post
    CoreB --> PostComment
    CoreB --> User
    CoreB --> AppGW
    CoreB --> RedisCache
    CoreB --> JWTMod
    CoreB --> Configs

    %% REST API Modules interactions
    Auth -->|"uses"| JWTMod
    Post -->|"persists"| Prisma
    PostComment -->|"persists"| Prisma
    User -->|"integrates"| Supa
    Email -->|"stores attachments"| AWS
    Email -->|"uses"| Supa
    Email -->|"configures"| OpenAI
    Notif -->|"triggers"| AppGW

    %% WebSocket Gateways interactions with REST API Modules
    AppGW -->|"notifies"| Notif
    CommentGW -->|"notifies"| Notif
    PostGW -->|"updates"| Post
    UserGW -->|"updates"| User

    %% JWT & Authentication additional interaction
    JWTMod -->|"defines"| JWTGuards

    %% Database Layer connection
    Prisma -->|"backup"| SQLDumps

    %% Utility connections
    Configs --> Globals

    %% Click Events for NestJS Core
    click CoreA "https://github.com/smoluca123/smospace_nestjs/blob/main/src/main.ts"
    click CoreB "https://github.com/smoluca123/smospace_nestjs/blob/main/src/app.module.ts"

    %% Click Events for REST API Modules
    click Auth "https://github.com/smoluca123/smospace_nestjs/blob/main/src/resources/auth/auth.module.ts"
    click Email "https://github.com/smoluca123/smospace_nestjs/blob/main/src/resources/email/email.module.ts"
    click Notif "https://github.com/smoluca123/smospace_nestjs/blob/main/src/resources/notification/notification.module.ts"
    click Post "https://github.com/smoluca123/smospace_nestjs/blob/main/src/resources/post/post.module.ts"
    click PostComment "https://github.com/smoluca123/smospace_nestjs/blob/main/src/resources/post-comment/post-comment.module.ts"
    click User "https://github.com/smoluca123/smospace_nestjs/blob/main/src/resources/user/user.module.ts"

    %% Click Events for WebSocket Gateways
    click AppGW "https://github.com/smoluca123/smospace_nestjs/blob/main/src/resources/gateways/app.gateway.ts"
    click CommentGW "https://github.com/smoluca123/smospace_nestjs/blob/main/src/resources/gateways/comment/comment.gateway.ts"
    click PostGW "https://github.com/smoluca123/smospace_nestjs/blob/main/src/resources/gateways/post/post.gateway.ts"
    click UserGW "https://github.com/smoluca123/smospace_nestjs/blob/main/src/resources/gateways/user/user.gateway.ts"

    %% Click Events for Database Layer
    click Prisma "https://github.com/smoluca123/smospace_nestjs/blob/main/prisma/schema.prisma"
    click SQLDumps "https://github.com/smoluca123/smospace_nestjs/blob/main/db_exports/db.sql"

    %% Click Event for Caching Layer
    click RedisCache "https://github.com/smoluca123/smospace_nestjs/blob/main/src/cache/cache.module.ts"

    %% Click Events for JWT & Authentication
    click JWTMod "https://github.com/smoluca123/smospace_nestjs/blob/main/src/jwt/jwt.module.ts"
    click JWTGuards "https://github.com/smoluca123/smospace_nestjs/blob/main/src/strategy/jwt.strategy.ts"

    %% Click Events for External Integrations
    click AWS "https://github.com/smoluca123/smospace_nestjs/blob/main/src/services/aws/s3/s3.service.ts"
    click Supa "https://github.com/smoluca123/smospace_nestjs/blob/main/src/services/supabase/supabase.service.ts"
    click OpenAI "https://github.com/smoluca123/smospace_nestjs/blob/main/src/configs/openai.config.ts"

    %% Click Events for Utility & Global Modules
    click Configs "https://github.com/smoluca123/smospace_nestjs/blob/main/src/configs/configuration.ts"
    click Globals "https://github.com/smoluca123/smospace_nestjs/blob/main/src/global/constant.global.ts"

    %% Styles
    classDef core fill:#FFCC00,stroke:#333,stroke-width:2px;
    classDef module fill:#B3E1FF,stroke:#333,stroke-width:2px;
    classDef gateway fill:#C9F9BF,stroke:#333,stroke-width:2px;
    classDef database fill:#FFE6CC,stroke:#333,stroke-width:2px;
    classDef cache fill:#FFD9B3,stroke:#333,stroke-width:2px;
    classDef auth fill:#FFADAD,stroke:#333,stroke-width:2px;
    classDef external fill:#D0F0C0,stroke:#333,stroke-width:2px;
    classDef utility fill:#E6CCFF,stroke:#333,stroke-width:2px;
```
