# c137519edbb3/backend architecture

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

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

## Overview

##### Main Components

A. API Layer
- Routes into logical sections:
- Authentication Routes (/routes/authRoutes.js)
- Admin Routes (/routes/adminRoutes.js)
- Analytics Routes (/routes/analyticsRoutes.js)
- Organization Routes (/routes/organizationRoutes.js)

B. Middleware Layer
- Authentication Middleware
- Organization Access Check Middleware

C. Controller Layer
- Auth Controller
- Admin Controller
- Analytics Controller
- Anomaly Controller
- Camera Controller
- Organization Controller

D. Model Layer
- User Model
- Organization Model
- Camera Model
- Anomaly Model
- AnomalyCamera Model
- Role Model

E. External Services
- Firebase (indicated by config/firebase.js)
- Database (indicated by config/database.js)

**[Client Applications]**
- ↓ ↑
**[API Gateway Layer (Express.js)]**
- ↓ ↑
**[Middleware Layer]**
- ↓ ↑
**[Controller Layer]**
- ↓ ↑
**[Model Layer]**
- ↓ ↑
**[External Services]**

##### Detailed Component Relationships

A. API Routes → Middleware → Controllers
- How auth middleware protects routes
- Demonstrate organization access checking flow

B. Controllers → Models
- CRUD operations between controllers and their respective models
- Relationships between models (e.g., Organization-User, Camera-Anomaly)

C. Models → External Services
- Database interactions
- Firebase authentication flow

##### Additional Elements

A. Configuration Layer
- How database.js and firebase.js configure external services
- Environment variables influence (referenced from .env.template)

B. Constants and Utils
- How constants (criticality.js, status.js, roles.js) are used across the application
- Demonstrate utility functions' role in the architecture

##### Deployment Context

A separate section showing:
- Docker container boundary
- Database connection
- External service connections
- Port exposures (3000 as mentioned in README)

##### Key Relationships to Emphasize

- Authentication flow through Firebase
- Organization-based access control
- Camera and anomaly detection relationship
- Analytics data flow
- Admin privileges and role-based access

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        CL[Client Applications]
    end

    subgraph API_Gateway
        AR["Authentication Routes"]:::api
        ADR["Admin Routes"]:::api
        ANR["Analytics Routes"]:::api
        OR["Organization Routes"]:::api
    end

    subgraph Middleware
        AM["Authentication Middleware"]:::middleware
        OAM["Organization Access Check"]:::middleware
    end

    subgraph Controllers
        AUC["Auth Controller"]:::controller
        ADC["Admin Controller"]:::controller
        ANC["Analytics Controller"]:::controller
        ANOC["Anomaly Controller"]:::controller
        CC["Camera Controller"]:::controller
        OC["Organization Controller"]:::controller
    end

    subgraph Models
        UM["User Model"]:::model
        OM["Organization Model"]:::model
        CM["Camera Model"]:::model
        ANM["Anomaly Model"]:::model
        ACM["AnomalyCamera Model"]:::model
        RM["Role Model"]:::model
    end

    subgraph External_Services
        FB["Firebase"]:::external
        DB["Database"]:::external
    end

    subgraph Config
        FBC["Firebase Config"]:::config
        DBC["Database Config"]:::config
    end

    subgraph Constants
        CRIT["Criticality Constants"]:::constant
        ROLE["Role Constants"]:::constant
        STAT["Status Constants"]:::constant
        DAYS["Days Constants"]:::constant
    end

    subgraph Utils
        AU["Auth Utils"]:::util
    end

    %% Main Flow
    CL <--> API_Gateway
    API_Gateway --> Middleware
    Middleware --> Controllers
    Controllers --> Models
    Models --> External_Services

    %% Component Relationships
    FBC --> FB
    DBC --> DB
    Constants --> Controllers
    AU --> Controllers

    %% Style Definitions
    classDef api fill:#2196F3,stroke:#1565C0,color:white
    classDef middleware fill:#F44336,stroke:#B71C1C,color:white
    classDef controller fill:#4CAF50,stroke:#2E7D32,color:white
    classDef model fill:#FFC107,stroke:#FFA000,color:black
    classDef external fill:#9C27B0,stroke:#6A1B9A,color:white
    classDef config fill:#795548,stroke:#4E342E,color:white
    classDef constant fill:#607D8B,stroke:#37474F,color:white
    classDef util fill:#FF9800,stroke:#F57C00,color:white

    %% Click Events
    click AR "https://github.com/c137519edbb3/Backend/blob/main/routes/authRoutes.js"
    click ADR "https://github.com/c137519edbb3/Backend/blob/main/routes/adminRoutes.js"
    click ANR "https://github.com/c137519edbb3/Backend/blob/main/routes/analyticsRoutes.js"
    click OR "https://github.com/c137519edbb3/Backend/blob/main/routes/organizationRoutes.js"
    click AM "https://github.com/c137519edbb3/Backend/blob/main/middlewares/authMiddleware.js"
    click OAM "https://github.com/c137519edbb3/Backend/blob/main/middlewares/checkOrgAccess.js"
    click AUC "https://github.com/c137519edbb3/Backend/blob/main/controllers/authController.js"
    click ADC "https://github.com/c137519edbb3/Backend/blob/main/controllers/adminController.js"
    click ANC "https://github.com/c137519edbb3/Backend/blob/main/controllers/analyticsController.js"
    click ANOC "https://github.com/c137519edbb3/Backend/blob/main/controllers/anomalyController.js"
    click CC "https://github.com/c137519edbb3/Backend/blob/main/controllers/cameraController.js"
    click OC "https://github.com/c137519edbb3/Backend/blob/main/controllers/organizationController.js"
    click UM "https://github.com/c137519edbb3/Backend/blob/main/models/User.js"
    click OM "https://github.com/c137519edbb3/Backend/blob/main/models/Organization.js"
    click CM "https://github.com/c137519edbb3/Backend/blob/main/models/Camera.js"
    click ANM "https://github.com/c137519edbb3/Backend/blob/main/models/Anomaly.js"
    click ACM "https://github.com/c137519edbb3/Backend/blob/main/models/AnomalyCamera.js"
    click RM "https://github.com/c137519edbb3/Backend/blob/main/models/Role.js"
    click FBC "https://github.com/c137519edbb3/Backend/blob/main/config/firebase.js"
    click DBC "https://github.com/c137519edbb3/Backend/blob/main/config/database.js"
    click CRIT "https://github.com/c137519edbb3/Backend/blob/main/constants/criticality.js"
    click ROLE "https://github.com/c137519edbb3/Backend/blob/main/constants/roles.js"
    click STAT "https://github.com/c137519edbb3/Backend/blob/main/constants/status.js"
    click DAYS "https://github.com/c137519edbb3/Backend/blob/main/constants/days.js"
    click AU "https://github.com/c137519edbb3/Backend/blob/main/utils/authUtils.js"
```
