# andersonkaiti/ws-rede-ct architecture

> Architecture diagram of the GitHub repository andersonkaiti/ws-rede-ct, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/andersonkaiti/ws-rede-ct
- Repository: https://github.com/andersonkaiti/ws-rede-ct

## Overview

##### Project Type and Purpose
- This is a standalone RESTful API backend (“API da RedeCT”) built with Node.js/Express, TypeScript and Prisma for PostgreSQL.
- Its responsibilities: authentication (JWT + bcrypt), user & team management, news, certifications, pendencies, file uploads to Firebase Storage, and interactive docs via OpenAPI.

##### Architectural Overview
- Layered (MVC-style) architecture with clear separation:
- HTTP Layer (bin/server.ts, src/app.ts, src/routes)
- Controller Layer (src/controllers)
- Service Layer (src/services + src/factories/services)
- Repository/Data Access Layer (src/repositories + Prisma ORM)
- Infrastructure/Config (src/config, .env, Docker, Prisma migrations)
- External dependencies: PostgreSQL database, Firebase Storage, OpenAPI docs endpoint.

##### Main Components
a. Clients & Entry Point
- External clients (mobile/web) or API consumers.
- HTTP server bootstrap (bin/server.ts).
- Express app module (src/app.ts).
b. Routing & Middleware
- Route definitions (src/routes/*.ts).
- Global middleware (CORS, JSON parser), auth middleware (JWT), multer file‐upload middleware.
c. Controllers
- One controller per resource (auth, users, news, teams, certifications, pendencies).
- Handles HTTP request → calls services.
d. Services
- Auth services: JWT issuance/validation, bcrypt password hashing.
- Firebase Storage service for file uploads.
- Any other business logic.
e. Repositories & Database
- Prisma Client (schema.prisma) as ORM.
- Repositories implement interfaces (icertification-repository, iuser-repository, etc).
- PostgreSQL database (hosted via Docker Compose).
f. OpenAPI Documentation
- OpenAPI spec modules (src/openapi) powering `/docs` endpoint.
g. External Systems
- Firebase Storage bucket.
- PostgreSQL instance (docker).

##### Interactions & Data Flow
1. Client → HTTP(S) Request → Express (bin/server.ts → src/app.ts)
2. Request passes through global middleware (CORS, JSON parsing) → auth middleware if protected → multer for file uploads if applicable.
3. Routed to appropriate controller (e.g., Sign-In Controller).
4. Controller delegates to a Service (e.g., JWT service, Firebase service, business service).
5. Service uses one or more Repositories to perform DB reads/writes via Prisma.
6. For file operations, service calls Firebase Storage API.
7. Responses bubble back: Service → Controller → Express → Client.
8. API docs served by OpenAPI module at `/docs`.

##### Architectural Patterns & Principles
- Layered Separation of Concerns
- Factory Pattern for creating controllers, services, repositories, and middleware (src/factories).
- Dependency Injection via factories to decouple implementations from interfaces.
- Interface-based repositories for testability and future swapping.
- Environment-based configuration (src/config/env.ts).
- Declarative DB migrations and seeding via Prisma.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients & Entry Point
    Client["External Client"]:::external
    Server["bin/server.ts\n(HTTP Server Bootstrap)"]:::http
    App["src/app.ts\n(Express App)"]:::http
    Client -->|HTTPS Request| Server
    Server -->|initialize| App

    %% Configuration
    subgraph "Configuration & Env" 
        EnvConfig["src/config/env.ts"]:::config
        DBConfig["config/database.ts"]:::config
        FirebaseConfig["config/firebase/index.ts"]:::config
        StorageConfig["config/firebase/storage.ts"]:::config
        EnvExample[".env.example"]:::config
    end
    App --> EnvConfig
    EnvConfig --> DBConfig
    EnvConfig --> FirebaseConfig

    %% Routing & Middleware
    subgraph "HTTP & Routing Layer" 
        Routes["src/routes"]:::http
        AuthMiddleware["Auth Middleware"]:::http
        MulterMiddleware["Multer Middleware"]:::http
        AuthMwFactory["src/factories/middlewares/auth-middleware.ts"]:::http
    end
    App -->|use routes| Routes
    App -->|use CORS & JSON| AuthMiddleware
    App -->|use file upload| MulterMiddleware
    AuthMiddleware --> AuthMwFactory

    %% Controllers
    subgraph "Controllers" 
        AuthController["src/controllers/auth"]:::http
        UserController["src/controllers/users"]:::http
        NewsController["src/controllers/news"]:::http
        TeamController["src/controllers/team"]:::http
        TeamMemberController["src/controllers/team"]:::http
        CertificationController["src/controllers/certifications"]:::http
        PendencyController["src/controllers/pendencies"]:::http
    end
    Routes --> AuthController
    Routes --> UserController
    Routes --> NewsController
    Routes --> TeamController
    Routes --> TeamMemberController
    Routes --> CertificationController
    Routes --> PendencyController

    %% Services
    subgraph "Services" 
        JWTService["src/services/auth/jwt/jwt.service.ts"]:::service
        BcryptService["src/services/auth/bcrypt/bcrypjs.service.ts"]:::service
        FirebaseStorageService["src/services/firebase-storage/firebase-storage.service.ts"]:::service
        JWTFactory["src/factories/services/auth/jwt.ts"]:::service
        BcryptFactory["src/factories/services/auth/bcryptjs.ts"]:::service
        FirebaseFactory["src/factories/services/firebase-storage.factory.ts"]:::service
    end
    AuthController --> JWTService
    AuthController --> BcryptService
    AnyController["*Controller"] --> FirebaseStorageService
    JWTService --> JWTFactory
    BcryptService --> BcryptFactory
    FirebaseStorageService --> FirebaseFactory

    %% Repositories & Database
    subgraph "Data Access Layer" 
        UserRepo["src/repositories/user/user-repository.ts"]:::data
        NewsRepo["src/repositories/news/news-repository.ts"]:::data
        TeamRepo["src/repositories/team/team-repository.ts"]:::data
        TeamMemberRepo["src/repositories/team-member/team-member-repository.ts"]:::data
        CertificationRepo["src/repositories/certification/certification-repository.ts"]:::data
        PendencyRepo["src/repositories/pendency/pendency-repository.ts"]:::data
        PrismaSchema["prisma/schema.prisma"]:::data
        Migrations["prisma/migrations"]:::data
    end
    ServiceLayer["*Service"] --> UserRepo
    ServiceLayer --> NewsRepo
    ServiceLayer --> TeamRepo
    ServiceLayer --> TeamMemberRepo
    ServiceLayer --> CertificationRepo
    ServiceLayer --> PendencyRepo
    UserRepo --> PrismaSchema
    PrismaSchema --> Migrations

    %% External Systems
    subgraph "External Systems" 
        Postgres[(PostgreSQL DB)]:::external
        Firebase[(Firebase Storage)]:::external
        OpenAPIDocs["/docs\n(OpenAPI)"]:::external
        DockerCompose["docker-compose.yml\n(Postgres Container)"]:::external
    end
    Migrations -->|deploy| DockerCompose
    UserRepo -->|SQL Queries| Postgres
    FirebaseStorageService -->|upload| Firebase
    App -->|serve docs| OpenAPIDocs

    %% Flow connections
    Client --> Server
    Server --> App
    App --> Routes
    Routes --> AuthController
    AuthController --> JWTService
    JWTService --> UserRepo
    UserRepo --> Postgres
    AnyController --> FirebaseStorageService
    FirebaseStorageService --> Firebase

    %% Legend
    subgraph "Legend"
        L1["Rectangle: Modules"]:::legend
        L2["Cylinder: Datastore"]:::legend
        L3["Cloud: External Service"]:::legend
        L4["Document: Docs"]:::legend
    end

    %% Click Events
    click Server "https://github.com/andersonkaiti/ws-rede-ct/blob/main/bin/server.ts"
    click App "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/app.ts"
    click Routes "https://github.com/andersonkaiti/ws-rede-ct/tree/main/src/routes"
    click AuthMiddleware "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/middlewares/auth-middleware.ts"
    click MulterMiddleware "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/middlewares/multer.ts"
    click AuthMwFactory "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/factories/middlewares/auth-middleware.ts"
    click AuthController "https://github.com/andersonkaiti/ws-rede-ct/tree/main/src/controllers/auth"
    click UserController "https://github.com/andersonkaiti/ws-rede-ct/tree/main/src/controllers/users"
    click NewsController "https://github.com/andersonkaiti/ws-rede-ct/tree/main/src/controllers/news"
    click TeamController "https://github.com/andersonkaiti/ws-rede-ct/tree/main/src/controllers/team"
    click TeamMemberController "https://github.com/andersonkaiti/ws-rede-ct/tree/main/src/controllers/team"
    click CertificationController "https://github.com/andersonkaiti/ws-rede-ct/tree/main/src/controllers/certifications"
    click PendencyController "https://github.com/andersonkaiti/ws-rede-ct/tree/main/src/controllers/pendencies"
    click JWTService "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/services/auth/jwt/jwt.service.ts"
    click BcryptService "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/services/auth/bcrypt/bcrypjs.service.ts"
    click FirebaseStorageService "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/services/firebase-storage/firebase-storage.service.ts"
    click JWTFactory "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/factories/services/auth/jwt.ts"
    click BcryptFactory "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/factories/services/auth/bcryptjs.ts"
    click FirebaseFactory "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/factories/services/firebase-storage.factory.ts"
    click UserRepo "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/repositories/user/user-repository.ts"
    click NewsRepo "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/repositories/news/news-repository.ts"
    click TeamRepo "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/repositories/team/team-repository.ts"
    click TeamMemberRepo "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/repositories/team-member/team-member-repository.ts"
    click CertificationRepo "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/repositories/certification/certification-repository.ts"
    click PendencyRepo "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/repositories/pendency/pendency-repository.ts"
    click PrismaSchema "https://github.com/andersonkaiti/ws-rede-ct/blob/main/prisma/schema.prisma"
    click Migrations "https://github.com/andersonkaiti/ws-rede-ct/tree/main/prisma/migrations"
    click EnvConfig "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/config/env.ts"
    click DBConfig "https://github.com/andersonkaiti/ws-rede-ct/blob/main/config/database.ts"
    click FirebaseConfig "https://github.com/andersonkaiti/ws-rede-ct/blob/main/config/firebase/index.ts"
    click StorageConfig "https://github.com/andersonkaiti/ws-rede-ct/blob/main/config/firebase/storage.ts"
    click EnvExample "https://github.com/andersonkaiti/ws-rede-ct/blob/main/.env.example"
    click OpenAPIDocs "https://github.com/andersonkaiti/ws-rede-ct/blob/main/src/openapi/index.ts"
    click DockerCompose "https://github.com/andersonkaiti/ws-rede-ct/blob/main/docker-compose.yml"
    click CtrlFactory "https://github.com/andersonkaiti/ws-rede-ct/tree/main/src/factories/controllers"
    click RepoFactory "https://github.com/andersonkaiti/ws-rede-ct/tree/main/src/factories/repositories"

    %% Classes
    classDef http fill:#D0E8FF,stroke:#0066CC,color:#000;
    classDef service fill:#E0F9D0,stroke:#33A02C,color:#000;
    classDef data fill:#FFE5B4,stroke:#FF8C00,color:#000;
    classDef external fill:#F3E5FF,stroke:#800080,color:#000;
    classDef config fill:#F0F0F0,stroke:#AAAAAA,color:#000;
    classDef legend fill:#FFFFFF,stroke:#000000,color:#000;
```
