# gustavo210/be-the-hero architecture

> Architecture diagram of the GitHub repository gustavo210/be-the-hero, 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/gustavo210/be-the-hero
- Repository: https://github.com/gustavo210/be-the-hero

## Overview

##### Main Components
- Backend API Server: This is located in the “backend” folder. It handles HTTP requests, contains controllers for ONGs, profiles, sessions, and incidents, and also deals with database interactions (via migrations and connection setup using Knex).
- Database: The backend uses SQLite (configured via Knex in the migrations). This component should be represented as a persistent storage layer.
- Web Frontend: Found in the “frontend” folder, it includes pages such as Logon, New Incident, Profile, Register, and is responsible for interfacing with the backend API via the defined routes.
- Mobile App: Present in the “mobile” folder, which is built with React Native (using Expo) and has similar pages (Incidents, Detail) as the web client. It also interacts with the backend API.
- API Communication Layer: Both web and mobile clients use an “api.js” service to define endpoints and make HTTP calls to the backend. This layer is implicitly a part of the client applications.
- Integration & Testing: There’s a unit and integration test structure in the backend (shown in backend/tests) that verifies the functionality of components like unique ID generation and endpoint integrations.

##### Relationships and Interactions
- The Web and Mobile clients send HTTP requests (e.g., POST /ongs, GET /incidents, etc.) to the Backend API.
- The Backend processes these requests via controllers, interacts with the SQLite database (using Knex for queries and migrations), and then returns responses to the clients.
- Data flows from the clients to the server (and vice versa) through clearly defined REST endpoints.
- Separation of concerns: Controllers handle routing logic, while the database layer manages persistence.

##### Important Architectural Patterns
- Separation of Concerns: Frontend (presentation logic), Backend (API and business logic), and Database (data persistence) are separately managed.
- RESTful Design: Several endpoints are provided for different resources (ONGs, sessions, incidents) that use common HTTP verbs.
- Modular Structure: Within the backend, code is modularly divided into controllers, routes, database, and utility functions.

##### Final Touches
- Every component is clearly labeled.
- To highlight the use of technologies like Node.js, Express, React, React Native, Knex, and SQLite.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients Subgraph
    subgraph "Clients"
        WebApp["React Web App"]:::web
        WebAPI["Web API Service"]:::web
        MobileApp["React Native Mobile App"]:::mobile
        MobileAPI["Mobile API Service"]:::mobile
    end

    %% Backend API Subgraph
    subgraph "Backend API"
        AppInit["App Initialization (app.js)"]:::backend
        Server["Server (server.js)"]:::backend
        Controllers["Controllers"]:::backend
        Routes["Routes (routes.js)"]:::backend
        Utils["Utils"]:::backend
        DBLayer["Database Layer (Knex)"]:::backend
        Tests["Integration Tests"]:::test
    end

    %% Database Subgraph
    subgraph "Database"
        DB["SQLite Database"]:::db
    end

    %% Internal Composition within Clients
    WebApp -->|"uses"| WebAPI
    MobileApp -->|"uses"| MobileAPI

    %% API Calls from Clients to Backend
    WebAPI -->|"HTTPcalls"| Server
    MobileAPI -->|"HTTPcalls"| Server

    %% Backend Internal Flow
    AppInit -->|"initializes"| Server
    Server -->|"delegates"| Controllers
    Server -->|"uses"| Routes
    Controllers -->|"queries"| DBLayer
    Routes -->|"queries"| DBLayer

    %% Backend Test Connection
    Tests ---|"validates"| Server

    %% Backend Database Communication
    DBLayer -->|"KnexQueries"| DB

    %% Click Events for Backend API Server
    click AppInit "https://github.com/gustavo210/be-the-hero/blob/master/backend/src/app.js"
    click Server "https://github.com/gustavo210/be-the-hero/blob/master/backend/src/server.js"
    click Controllers "https://github.com/gustavo210/be-the-hero/tree/master/backend/src/controllers"
    click Routes "https://github.com/gustavo210/be-the-hero/blob/master/backend/src/routes.js"
    click Utils "https://github.com/gustavo210/be-the-hero/tree/master/backend/src/utils"
    click DBLayer "https://github.com/gustavo210/be-the-hero/tree/master/backend/src/database"
    click Tests "https://github.com/gustavo210/be-the-hero/tree/master/backend/tests"

    %% Click Events for Web Frontend and API Communication
    click WebApp "https://github.com/gustavo210/be-the-hero/blob/master/frontend/src/App.js"
    click WebAPI "https://github.com/gustavo210/be-the-hero/blob/master/frontend/src/services/api.js"

    %% Click Events for Mobile App and API Communication
    click MobileApp "https://github.com/gustavo210/be-the-hero/blob/master/mobile/App.js"
    click MobileAPI "https://github.com/gustavo210/be-the-hero/blob/master/mobile/src/Services/api.js"

    %% Styles
    classDef web fill:#FFEBEE,stroke:#C62828,stroke-width:2px;
    classDef mobile fill:#E3F2FD,stroke:#1565C0,stroke-width:2px;
    classDef backend fill:#E8F5E9,stroke:#2E7D32,stroke-width:2px;
    classDef db fill:#FFF3E0,stroke:#EF6C00,stroke-width:2px;
    classDef test fill:#F3E5F5,stroke:#6A1B9A,stroke-width:2px;
```
