# tcc-fafram/backend architecture

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

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

## Overview

##### Project Type and Purpose
- This is a backend‐only service (a RESTful API) written in Node.js/TypeScript.
- It exposes endpoints for managing courses, modules, lessons (aulas), users, roles (funções), exams (provas), completions, certificates, etc.
- It uses Express for routing, Prisma as the ORM for a relational database, and a layered architecture (Controller → Service → Repository).

##### File Structure
- Top‐level directory “backend” contains:
- package.json, tsconfig.json, Vitest config → TypeScript/Node setup
- prisma/schema.prisma + SQL triggers → database schema & migrations
- src/
– routers/…Router.ts → maps URLs to controllers
– controllers/…Controller.ts → HTTP request handling
– services/…Service.ts → business logic
– models/…Repository.ts → Prisma queries
– bases/BaseController, BaseService, BaseRepository → shared abstractions
– middleware/auth, exception, sessionUser → cross‐cutting concerns
– utils/app_error.ts → error modeling
– server.ts → app bootstrap
- src/test/… → unit/integration tests

##### README
- Minimal: refers to good‐practices doc.

a. Main Components
- API Gateway (Express Server)
- Routing Layer (Router modules)
- Controller Layer (Controllers orchestrating requests/responses)
- Service Layer (Business rules & transactions)
- Data Access Layer (Prisma Repositories)
- Relational Database (PostgreSQL/MySQL via Prisma)
- Cross‐Cutting Middleware (Auth, Session, Error Handling)
- Optional: External services (e.g., email/S3 if used later)

b. Relationships & Data Flow
1. Client → HTTP Request → Express Server
2. Express applies Middleware (auth check, sessionUser, exception catcher)
3. Router matching → invokes appropriate Controller
4. Controller extracts payloads, calls Service
5. Service invokes Repositories for CRUD via Prisma
6. Repository executes SQL on the Database
7. Prisma returns data → Service processes → Controller formats → Response to Client

c. Architectural Patterns & Principles
- Layered (Clean) Architecture: separation of concerns (routing, presentation, use‐cases, data)
- Dependency Injection (repositories/services wired in server.ts or via import)
- DRY / base classes for controllers, services, repositories
- Error‐First Middleware pattern

d. Key Technologies
- Node.js + TypeScript
- Express.js
- Prisma ORM
- Vitest (testing)
- SQL triggers (backend/prisma/sql)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Client
    Client["Client"]:::external

    %% API Gateway
    APIGateway["Express Server"]:::infra
    click APIGateway "https://github.com/tcc-fafram/backend/blob/main/backend/src/server.ts"

    %% Middleware Layer
    subgraph "Middleware Layer"
        direction TB
        Auth["Auth Middleware"]:::infra
        Session["SessionUser Middleware"]:::infra
        Exception["Exception Middleware"]:::infra
    end
    click Auth "https://github.com/tcc-fafram/backend/blob/main/backend/src/middleware/auth.ts"
    click Session "https://github.com/tcc-fafram/backend/blob/main/backend/src/middleware/sessionUser.ts"
    click Exception "https://github.com/tcc-fafram/backend/blob/main/backend/src/middleware/exception.ts"

    %% Routing Layer
    Routers["Routing Layer (Routers)"]:::routing
    click Routers "https://github.com/tcc-fafram/backend/blob/main/backend/src/routers/index.ts"

    %% Controller Layer
    Controllers["Controller Layer"]:::routing
    click Controllers "https://github.com/tcc-fafram/backend/tree/main/backend/src/controllers/"

    %% Base Abstractions
    BaseController["BaseController"]:::base
    BaseService["BaseService"]:::base
    BaseRepository["BaseRepository"]:::base
    click BaseController "https://github.com/tcc-fafram/backend/blob/main/backend/src/bases/BaseController.ts"
    click BaseService "https://github.com/tcc-fafram/backend/blob/main/backend/src/bases/BaseService.ts"
    click BaseRepository "https://github.com/tcc-fafram/backend/blob/main/backend/src/bases/BaseRepository.ts"

    %% Service Layer
    Services["Service Layer"]:::business
    click Services "https://github.com/tcc-fafram/backend/tree/main/backend/src/services/"

    %% Data Access Layer
    Repositories["Repository Layer"]:::data
    click Repositories "https://github.com/tcc-fafram/backend/tree/main/backend/src/models/"

    %% Utilities & Types
    AppError["app_error.ts"]:::infra
    TypesExp["express.d.ts"]:::infra
    click AppError "https://github.com/tcc-fafram/backend/blob/main/backend/src/utils/app_error.ts"
    click TypesExp "https://github.com/tcc-fafram/backend/blob/main/backend/src/types/express.d.ts"

    %% Database & ORM
    subgraph "Prisma Schema & Migrations"
        direction TB
        Schema["schema.prisma"]:::infra
        Trigger["trigger_log.sql"]:::infra
    end
    click Schema "https://github.com/tcc-fafram/backend/blob/main/backend/prisma/schema.prisma"
    click Trigger "https://github.com/tcc-fafram/backend/blob/main/backend/prisma/sql/trigger_log.sql"

    DB[(Database)]:::db

    %% Connections
    Client -->|"HTTP Request"| APIGateway
    APIGateway --> Auth
    Auth --> Session
    Session --> Exception
    Exception --> Routers
    Routers --> Controllers
    Controllers --> Services
    Services --> Repositories
    Repositories --> DB
    Repositories --> Schema
    Repositories --> Trigger

    %% Base classes usage
    Controllers --> BaseController
    Services --> BaseService
    Repositories --> BaseRepository

    %% Utilities usage
    APIGateway --> AppError
    APIGateway --> TypesExp

    %% Legend
    subgraph Legend
        direction TB
        ext["Client"]:::external
        rt["Routing"]:::routing
        biz["Business"]:::business
        dat["Data"]:::data
        infraL["Infrastructure"]:::infra
        baseL["Base Classes"]:::base
        dbL["Database"]:::db
    end

    classDef external fill:#EFEFEF,stroke:#999
    classDef routing fill:#ADD8E6,stroke:#0077B6
    classDef business fill:#90EE90,stroke:#2E8B57
    classDef data fill:#FFA500,stroke:#CC8400
    classDef infra fill:#D3D3D3,stroke:#666
    classDef base fill:#FFFACD,stroke:#DAA520
    classDef db fill:#CCCCCC,stroke:#333,shape:cylinder
```
