# cpavnn/rideshare architecture

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

- Interactive diagram: https://gitdiagram.com/cpavnn/rideshare
- Repository: https://github.com/cpavnn/rideshare

## Overview

##### Project Type & Purpose
- This is a full-stack, Firebase-driven, serverless ride-sharing app that connects colleagues for carpooling.
- Its primary purpose is to help employees find rides along similar routes while ensuring privacy and reducing overall costs.
- Key features include user authentication via Google, real-time matching of ride requests, email notifications (using nodemailer), and administrative functions like a cron-triggered cleanup of ride requests.

##### Main Components
a. Frontend (Client-Side):
- Located in the “public” directory.
- Contains HTML pages (index.html, mydetails.html, requestride.html, etc.), CSS (app.css), JavaScript (app.js, mydetails.js, requestride.js, register-sw.js, and service worker scripts).
- Integrates the Firebase client libraries (in the “firebasejs” directory) and the firebaseConfig.js that initializes Firebase.
- Acts as the primary interface through which employees interact with the app (request rides, view details, etc.).

b. Backend / Serverless Functions:
- Resides inside the “functions” directory.
- Contains Cloud Functions code (functions/index.js) that handles important backend tasks including:
- Resetting active ride requests (removeActiveRequests endpoint for cron job triggers)
- Processing emails via nodemailer (after configuring credentials in Firebase Functions)
- Acts as the middleware between the frontend actions and the database operations.

c. Firebase Infrastructure:
- Firebase Hosting: Serves the static frontend from the “public” folder.
- Firebase Cloud Functions: Runs the backend logic as explained above.
- Firebase Database: Managed by the “database.rules.json” which defines security rules, supporting real-time data operations (likely using the Realtime Database given the structure).
- Firebase Authentication: Enabled using Google sign-in, facilitating secure user access.

d. External Services & Integrations:
- Cron Job Service (e.g., Cron-job.org): Used to schedule nightly triggering of the “removeActiveRequests” endpoint.
- Google Maps API: Integrated via API keys for location-based services (configured by editing mydetails.js and related files).
- Email Services: Using nodemailer integrated in Cloud Functions once Gmail credentials are set and proper security configuration is done.

##### Relationships and Data Flow
- The Frontend communicates directly with Firebase services:
- Interacts with Firebase Authentication for user sign-in.
- Reads and writes data to the Firebase Database.
- Calls Firebase Cloud Functions endpoints for backend operations.
- Cloud Functions act as a secure backend layer to process operations that require server-side logic (ride capacity resets, sending emails, etc.).
- External cron job triggers hit a defined Cloud Function endpoint (secured by a key) to clean up or reset active ride requests on a scheduled basis.
- Google Maps API and nodemailer are used by the Cloud Functions and client code to enrich user experience (maps interactions) and ensure notifications (email).

##### Architectural Patterns & Design Principles
- Serverless architecture: Emphasize the use of Firebase Cloud Functions to handle business logic without managing your own server.
- Separation of concerns:
- The frontend delivers a rich user interface.
- The backend functions focus on business logic, maintenance tasks, and integration with external services.
- Security and Private Access: Specific replacements in the code (like using a company domain) ensure that the app remains private to employees.
- Extensibility: The modular structure (separate directories for functions, public assets, and Firebase configs) allows each component to be extended or modified independently.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        FE["Frontend (Public Assets)"]:::frontend
        FL["Firebase Client Libraries"]:::frontendDetail
        FC["Firebase Configuration"]:::frontendDetail
    end

    %% Firebase Services Layer
    subgraph "Firebase Services"
        H["Firebase Hosting"]:::firebase
        A["Firebase Authentication"]:::firebase
        D["Firebase Realtime Database"]:::database
        CF["Cloud Functions"]:::backend
        PC["Firebase Project Config"]:::firebase
    end

    %% External Services Layer
    subgraph "External Services"
        CR["Cron Job Service"]:::external
        GM["Google Maps API"]:::external
        EM["Email/nodemailer"]:::external
    end

    %% Data flows and relationships
    FE -->|"authRequest"| A
    FE -->|"dataRW"| D
    FE -->|"backendCall"| CF
    H -->|"serves"| FE
    CF -->|"dbQuery"| D
    CF -->|"sendEmail"| EM
    CF -->|"mapCall"| GM
    CR -->|"triggerCleanup"| CF
    FE ---|"initializes"| FL
    FE ---|"initializes"| FC

    %% Click Events
    click FE "https://github.com/cpavnn/rideshare/tree/master/public/"
    click FL "https://github.com/cpavnn/rideshare/tree/master/public/firebasejs/"
    click FC "https://github.com/cpavnn/rideshare/blob/master/public/firebaseConfig.js"
    click CF "https://github.com/cpavnn/rideshare/tree/master/functions/"
    click H "https://github.com/cpavnn/rideshare/blob/master/firebase.json"
    click D "https://github.com/cpavnn/rideshare/blob/master/database.rules.json"
    click PC "https://github.com/cpavnn/rideshare/blob/master/.firebaserc"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef frontendDetail fill:#BFEFFF,stroke:#000,stroke-width:1px;
    classDef firebase fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef database fill:#98FB98,stroke:#000,stroke-width:2px;
    classDef backend fill:#FFD700,stroke:#000,stroke-width:2px;
    classDef external fill:#FFB6C1,stroke:#000,stroke-width:2px;
```
