# nguyenminhduc163/fire-guard-be architecture

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

- Interactive diagram: https://gitdiagram.com/nguyenminhduc163/fire-guard-be
- Repository: https://github.com/nguyenminhduc163/fire-guard-be

## Overview

##### Project Type Analysis
This is a backend service for a Smart Fire Alarm System with IoT integration, following a clear MVC-like architecture pattern with additional service layers.

##### Main Components

A. External Interfaces:
- IoT Devices (Fire Sensors)
- Mobile/Web Clients
- Firebase (for notifications)
- Email Service

B. Core Application Layers:
- API Gateway Layer (Express.js Routes)
- Authentication Layer
- Controller Layer
- Service Layer
- Data Access Layer
- Database (PostgreSQL)

**[External Clients/Devices] (Top)**
- ↓
**[API Gateway/Routes Layer]**
- ↓
**[Authentication Middleware]**
- ↓
**[Controllers]**
- ↓
**[Services]**
- ↓
**[Data Models]**
- ↓
**[Database] (Bottom)**

##### Key Relationships to Highlight

a) Data Flow Paths:
- IoT Sensor Data Path:
Sensors → iotStatusSave.router → iotStatusSave.controller → iotStatusSave.service → Database

- Emergency Alerts Path:
emergency.router → emergency.controller → emergency.service → notifications.service → Firebase/Email

b) Authentication Flow:
- auth.router → auth.controller → token.service
- authMiddleware interaction with protected routes

##### Specific Components to Detail

Core Features:
- Sensor Data Management
- Emergency Detection & Alerts
- User Management
- Family Notifications
- Guides & News System
- Historical Data Tracking

Support Systems:
- Logging System
- Response Handling
- Input Validation
- Error Management

- Client Layer       |

- Security Layer     |

- Business Layer     |

- Data Layer        |

- External Services  |

Remember to:
- Keep components properly grouped
- Clear data flow directions
- Async operations
- Critical security checkpoints
- Document all external dependencies
- Monitoring and logging points

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Layer
    subgraph External
        IoT[IoT Devices/Sensors]:::external
        Clients[Mobile/Web Clients]:::external
        Firebase[Firebase Service]:::external
        Email[Email Service]:::external
    end

    %% API Gateway Layer
    subgraph Gateway
        APIGateway[API Gateway]:::gateway
        click APIGateway "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/app.js"
        Routes[Routes Handler]:::gateway
        click Routes "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/tree/main/src/routes/"
    end

    %% Auth Layer
    subgraph Auth
        AuthMiddleware[Auth Middleware]:::security
        click AuthMiddleware "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/middleware/authMiddleware.js"
        AuthRouter[Auth Router]:::security
        click AuthRouter "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/routes/auth.router.js"
        AuthController[Auth Controller]:::security
        click AuthController "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/controllers/auth.controller.js"
        TokenService[Token Service]:::security
        click TokenService "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/configs/token.service.js"
    end

    %% Core Features Layer
    subgraph CoreFeatures
        direction TB
        %% Sensor Management
        SensorRouter[Sensor Router]:::core
        click SensorRouter "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/routes/sensorData.router.js"
        SensorController[Sensor Controller]:::core
        click SensorController "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/controllers/sensorData.controller.js"
        SensorService[Sensor Service]:::core
        click SensorService "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/services/sensorData.service.js"

        %% Emergency System
        EmergencyRouter[Emergency Router]:::core
        click EmergencyRouter "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/routes/emergency.router.js"
        EmergencyController[Emergency Controller]:::core
        click EmergencyController "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/controllers/emergency.controller.js"
        EmergencyService[Emergency Service]:::core
        click EmergencyService "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/services/emergency.service.js"

        %% Family Notifications
        FamilyRouter[Family Router]:::core
        click FamilyRouter "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/routes/familyNotification.router.js"
        FamilyController[Family Controller]:::core
        click FamilyController "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/controllers/familyNotification.controller.js"
        FamilyService[Family Service]:::core
        click FamilyService "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/services/familyNotification.service.js"

        %% IoT Status
        IoTRouter[IoT Router]:::core
        click IoTRouter "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/routes/iotStatus.router.js"
        IoTController[IoT Controller]:::core
        click IoTController "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/controllers/iotStatus.controller.js"
        IoTService[IoT Service]:::core
        click IoTService "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/services/iotStatus.service.js"
    end

    %% Support Systems
    subgraph Support
        Logger[Logger]:::support
        click Logger "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/utils/logger.js"
        ResponseHandler[Response Handler]:::support
        click ResponseHandler "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/utils/responseHelper.js"
        Validation[Input Validation]:::support
        click Validation "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/utils/validation.js"
        LoggerMiddleware[Logger Middleware]:::support
        click LoggerMiddleware "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/middleware/loggerMiddleware.js"
    end

    %% Data Layer
    subgraph Data
        UserModel[User Model]:::data
        click UserModel "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/models/user.model.js"
        NotificationModel[Notification Model]:::data
        click NotificationModel "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/models/notification.model.js"
        SensorModel[Sensor Model]:::data
        click SensorModel "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/models/sensorData.model.js"
        EmergencyModel[Emergency Model]:::data
        click EmergencyModel "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/models/emergency.model.js"
        FamilyModel[Family Model]:::data
        click FamilyModel "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/models/familyNotification.model.js"
        Database[(PostgreSQL)]:::data
        click Database "https://github.com/NguyenMinhDuc163/Fire-Guard-BE/blob/main/src/configs/db.config.js"
    end

    %% Connections
    IoT --> APIGateway
    Clients --> APIGateway
    APIGateway --> Routes
    Routes --> AuthMiddleware
    AuthMiddleware --> AuthRouter & SensorRouter & EmergencyRouter & FamilyRouter & IoTRouter
    AuthRouter --> AuthController
    AuthController --> TokenService
    SensorRouter --> SensorController
    SensorController --> SensorService
    EmergencyRouter --> EmergencyController
    EmergencyController --> EmergencyService
    FamilyRouter --> FamilyController
    FamilyController --> FamilyService
    IoTRouter --> IoTController
    IoTController --> IoTService
    SensorService & EmergencyService & FamilyService & IoTService --> UserModel & NotificationModel & SensorModel & EmergencyModel & FamilyModel
    UserModel & NotificationModel & SensorModel & EmergencyModel & FamilyModel --> Database
    EmergencyService --> Firebase
    EmergencyService --> Email

    %% Styles
    classDef external fill:#68B7FF,stroke:#333,stroke-width:2px
    classDef gateway fill:#90EE90,stroke:#333,stroke-width:2px
    classDef security fill:#FFB6C1,stroke:#333,stroke-width:2px
    classDef core fill:#FFE4B5,stroke:#333,stroke-width:2px
    classDef support fill:#DDA0DD,stroke:#333,stroke-width:2px
    classDef data fill:#B8860B,stroke:#333,stroke-width:2px

    %% Legend
    subgraph Legend
        L1[External Systems]:::external
        L2[Gateway Layer]:::gateway
        L3[Security Layer]:::security
        L4[Core Features]:::core
        L5[Support Systems]:::support
        L6[Data Layer]:::data
    end
```
