# fercracix33/back-master architecture

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

- Interactive diagram: https://gitdiagram.com/fercracix33/back-master
- Repository: https://github.com/fercracix33/back-master

## Overview

##### Project Type and Purpose
- Type: A standalone backend service providing RESTful HTTP APIs plus real-time features via WebSockets (Socket.IO).
- Purpose: Manage users, communities, boards, chat, files, events, notifications, etc., with data stored in a relational database via Prisma ORM.

##### File Structure
- Top-level:
– prisma/schema.prisma → DB schema definitions
– api-documentation.yaml → OpenAPI spec
– src/index.ts → Express + Socket.IO server bootstrap
– tsconfig.json, package.json
- src/middleware/auth.ts → authentication/authorization layer for both HTTP and WS
- src/prisma/client.ts → Prisma client factory
- src/routes/*.ts → REST controllers by domain (auth, users, communities, chat, files, etc.)
- src/socket/ → real-time event system
– eventBus.ts → in-process pub/sub for cross-module events
– index.ts → Socket.IO handlers (chat, notifications, etc.)
– scheduledNotifier.ts → cron-style tasks generating notifications
- src/types/AuthRequest.ts → extended Request type with user context

##### Extract Key Architectural Insights from the README
- Express.js for HTTP pipeline
- Prisma as ORM to a relational database
- Socket.IO for WebSocket connections
- A middleware layer for authentication
- A small in-process event bus to decouple real-time notifications and scheduled tasks

##### Main Components
a. Clients
– Web/Mobile Apps (HTTP + WS consumers)
b. API Gateway / Server
– Express.js HTTP server
– Socket.IO server
c. Middleware
– Auth middleware (JWT/session validation)
d. Route Handlers (grouped by domain)
– Auth, Users, Communities, Boards, Chat, Files, Events, Notifications, etc.
e. Business-Logic / Service Layer (implicit in routes/eventBus)
f. Prisma ORM
g. Database
h. Real-Time Infrastructure
– eventBus (pub/sub)
– scheduledNotifier (cron-triggered events)
i. External Integrations (if any, e.g., file storage, email service)

##### Relationships / Data Flows
- HTTP flow:
Client → Express (index.ts) → Auth Middleware → Route Handler → Prisma Client → Database → Response
- WebSocket flow:
Client ↔ Socket.IO (index.ts) → Auth Middleware → eventBus → Route logic → eventBus → broadcast to Clients
- Scheduled notifications:
scheduledNotifier → eventBus → Socket.IO → Client notifications
- All DB access is funneled through prisma/client.ts

##### Architectural Patterns & Principles
- Layered architecture: Transport (HTTP/WS), Middleware, Controller (Routes), Data Access (Prisma), DB
- Separation of concerns: Each route file covers one domain, eventBus decouples producers/consumers
- Pub/Sub: In-process event bus for real-time updates and scheduling
- Single responsibility: Middleware only handles auth, routes only handle request/response

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    subgraph "Clients"
        WebMobile["Web/Mobile Apps"]:::external
    end

    %% Server
    Server["Express HTTP & Socket.IO Server"]:::server
    click Server "https://github.com/fercracix33/back-master/blob/master/src/index.ts"

    %% Middleware
    AuthMiddleware["Auth Middleware"]:::server
    click AuthMiddleware "https://github.com/fercracix33/back-master/blob/master/src/middleware/auth.ts"

    %% Controllers
    subgraph "Controllers"
        AuthRoutes["Auth Routes"]:::server
        click AuthRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/auth.ts"
        UserRoutes["User Routes"]:::server
        click UserRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/users.ts"
        CommunitiesRoutes["Community Routes"]:::server
        click CommunitiesRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/communities.ts"
        CommResRoutes["Community Resources Routes"]:::server
        click CommResRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/communityResources.ts"
        CommThreadsRoutes["Community Threads Routes"]:::server
        click CommThreadsRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/communityThreads.ts"
        BoardsRoutes["Boards Routes"]:::server
        click BoardsRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/boards.ts"
        ChatRoutes["Chat Routes"]:::server
        click ChatRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/chat.ts"
        FilesRoutes["Files Routes"]:::server
        click FilesRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/files.ts"
        FoldersRoutes["Folders Routes"]:::server
        click FoldersRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/folders.ts"
        FriendsRoutes["Friends Routes"]:::server
        click FriendsRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/friends.ts"
        NotesRoutes["Notes Routes"]:::server
        click NotesRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/notes.ts"
        NotificationsRoutes["Notifications Routes"]:::server
        click NotificationsRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/notifications.ts"
        TagsRoutes["Tags Routes"]:::server
        click TagsRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/tags.ts"
        EventsRoutes["Events Routes"]:::server
        click EventsRoutes "https://github.com/fercracix33/back-master/blob/master/src/routes/events.ts"
    end

    %% Data Access
    Prisma["Prisma ORM Client"]:::server
    click Prisma "https://github.com/fercracix33/back-master/blob/master/src/prisma/client.ts"
    Schema["DB Schema Definitions"]:::db
    click Schema "https://github.com/fercracix33/back-master/blob/master/prisma/schema.prisma"
    DB[(PostgreSQL DB)]:::db

    %% Real-Time Infrastructure
    subgraph "Real-Time"
        SocketIO["Socket.IO Handlers"]:::realtime
        click SocketIO "https://github.com/fercracix33/back-master/blob/master/src/socket/index.ts"
        EventBus["In-Process Event Bus"]:::realtime
        click EventBus "https://github.com/fercracix33/back-master/blob/master/src/socket/eventBus.ts"
        ScheduledNotifier["Scheduled Notifications"]:::realtime
        click ScheduledNotifier "https://github.com/fercracix33/back-master/blob/master/src/socket/scheduledNotifier.ts"
    end

    %% Types & Docs
    AuthRequest["Extended Request Type"]:::server
    click AuthRequest "https://github.com/fercracix33/back-master/blob/master/src/types/AuthRequest.ts"
    OpenAPI["OpenAPI Specification"]:::server
    click OpenAPI "https://github.com/fercracix33/back-master/blob/master/api-documentation.yaml"
    TSConfig["TypeScript Configuration"]:::server
    click TSConfig "https://github.com/fercracix33/back-master/blob/master/tsconfig.json"
    PackageJSON["Package Manifest"]:::server
    click PackageJSON "https://github.com/fercracix33/back-master/blob/master/package.json"

    %% HTTP Flow
    WebMobile -->|HTTP Request| Server
    Server -->|apply| AuthMiddleware
    AuthMiddleware -->|authorize| AuthRoutes
    AuthRoutes -->|use| Prisma
    Prisma -->|query| DB
    DB -->|result| Prisma
    Prisma -->|response| AuthRoutes
    AuthRoutes -->|HTTP Response| Server
    Server -->|send| WebMobile

    %% WS Flow
    WebMobile -->|WS Handshake| SocketIO
    SocketIO -->|auth| AuthMiddleware
    AuthMiddleware -->|validate| EventBus
    EventBus -->|emit| ChatRoutes
    ChatRoutes -->|publish| EventBus
    EventBus -->|broadcast| SocketIO
    SocketIO -->|message| WebMobile

    %% Scheduled Notifications Flow
    ScheduledNotifier -- triggers --> EventBus

    %% Connections to Controllers for other HTTP routes
    AuthMiddleware -->|"route"| UserRoutes
    AuthMiddleware -->|"route"| CommunitiesRoutes
    AuthMiddleware -->|"route"| CommResRoutes
    AuthMiddleware -->|"route"| CommThreadsRoutes
    AuthMiddleware -->|"route"| BoardsRoutes
    AuthMiddleware -->|"route"| ChatRoutes
    AuthMiddleware -->|"route"| FilesRoutes
    AuthMiddleware -->|"route"| FoldersRoutes
    AuthMiddleware -->|"route"| FriendsRoutes
    AuthMiddleware -->|"route"| NotesRoutes
    AuthMiddleware -->|"route"| NotificationsRoutes
    AuthMiddleware -->|"route"| TagsRoutes
    AuthMiddleware -->|"route"| EventsRoutes

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:1px
    classDef server fill:#bbf,stroke:#333,stroke-width:1px
    classDef db fill:#bfb,stroke:#333,stroke-width:1px
    classDef realtime fill:#fb7,stroke:#333,stroke-width:1px
```
