# hieuchichau/danagreen architecture

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

- Interactive diagram: https://gitdiagram.com/hieuchichau/danagreen
- Repository: https://github.com/hieuchichau/danagreen

## Overview

##### Project type and purpose
- This is a full-stack web application comprised of:
- A user‐facing frontend (“frontend” folder, React + Vite)
- An admin console frontend (“admin” folder, React + Vite)
- A Node.js/Express backend REST API (“backend” folder)
- Purpose: Manage events, vouchers, users, histories, support requests.

##### File structure
- Top-level:
- admin/ → React admin dashboard
- frontend/ → React public site and user dashboard
- backend/ → Express server, controllers, models, routes, config
- Backend uses MVC: models/, controllers/, routes/
- Config: database connection (db.js), Cloudinary integration, .env files, SSL certs
- Storage: local “uploads/” folder + Cloudinary for images

##### README
- Rely on folder naming, config files, and code layout to infer components

##### Main components and interactions
1. Admin Frontend (React/Vite)
2. User Frontend (React/Vite)
3. Backend REST API (Node.js + Express)
4. Database (MongoDB or other)
5. Local File Storage (“uploads/” directory)
6. External Image Storage (Cloudinary)
7. Authentication (JWT middleware)

- Frontends → Backend API: HTTP/HTTPS requests for auth, CRUD operations on users, events, vouchers, histories, support, images
- Backend → Database: read/write via Mongoose models (userModel, eventModel, etc.)
- Backend → Local Storage: write uploaded files into /uploads (via ImageUploadController)
- Backend → Cloudinary: upload and fetch image URLs (via cloudinary.js config)
- Backend → Frontends: JSON responses (data, status codes)
- Frontends → 3rd‐party APIs (if any – e.g. QR code scanning library)

c. Architectural patterns & design principles:
- Three-tier architecture (UI, API, Persistence)
- RESTful controllers & routes (separation of concerns)
- JWT‐based stateless auth (auth middleware protects routes)
- Component-based UI (React) with centralized API modules under src/api
- Environment configuration via .env in each app

d. Key technologies/frameworks (annotate each component):
- React + Vite (admin & user UIs)
- Axios or fetch (src/api/*.js → HTTP client)
- Node.js + Express (backend/server.js)
- Mongoose or native MongoDB (models/*.js)
- Cloudinary SDK (config/cloudinary.js)
- JSON sample files (backend/config/*.json) for initial seed data
- SSL certificates (key.pem, cert.pem) for HTTPS

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients Layer
    subgraph "Clients"
        Admin["Admin UI\n(React/Vite)"]:::frontend
        User["User UI\n(React/Vite)"]:::frontend
    end

    %% Backend API
    subgraph "Backend API" 
        direction TB
        API["Backend API\n(Node.js/Express)"]:::server

        subgraph "HTTP Layer"
            direction TB
            Routes["Routes\n(auth, event, user, voucher, support, history, image)"]:::server
            Controllers["Controllers\nAuth, Event, User, Voucher, Support, History, ImageUpload"]:::server
            ImageCtrl["ImageUploadController"]:::server
        end

        subgraph "Business Logic & Middleware"
            direction TB
            AuthMid["JWT Auth Middleware"]:::server
            Services["Business Services"]:::server
        end

        subgraph "Data Access Layer"
            direction TB
            Models["Models\n(Mongoose)"]:::server
            DBConn["Database Connector\ndb.js"]:::server
            Seed["Seed Data JSONs"]:::server
        end
    end

    %% Persistence & External
    DB[(Database\nMongoDB)]:::database
    FS["📁 Local Storage\n/uploads"]:::storage
    CI["☁️ Cloudinary"]:::external

    %% Connections
    Admin -->|HTTPS requests| API
    User -->|HTTPS requests| API
    API --> Routes --> Controllers
    Controllers --> Services --> Models --> DBConn --> DB
    Controllers --> AuthMid
    Controllers --> ImageCtrl --> FS
    Controllers --> ImageCtrl --> CI

    %% Click Events
    click Admin "https://github.com/hieuchichau/danagreen/tree/main/admin/"
    click User "https://github.com/hieuchichau/danagreen/tree/main/frontend/"
    click API "https://github.com/hieuchichau/danagreen/blob/main/backend/server.js"
    click Routes "https://github.com/hieuchichau/danagreen/tree/main/backend/routes/"
    click Controllers "https://github.com/hieuchichau/danagreen/tree/main/backend/controllers/"
    click ImageCtrl "https://github.com/hieuchichau/danagreen/blob/main/backend/controllers/ImageUploadController.js"
    click AuthMid "https://github.com/hieuchichau/danagreen/blob/main/backend/middleware/auth.js"
    click Services "https://github.com/hieuchichau/danagreen/tree/main/backend/controllers/"
    click Models "https://github.com/hieuchichau/danagreen/tree/main/backend/models/"
    click DBConn "https://github.com/hieuchichau/danagreen/blob/main/backend/config/db.js"
    click Seed "https://github.com/hieuchichau/danagreen/tree/main/backend/config/"
    click FS "https://github.com/hieuchichau/danagreen/tree/main/backend/uploads/"
    click CI "https://github.com/hieuchichau/danagreen/blob/main/backend/config/cloundinary.js"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366d6,color:#0366d6;
    classDef server fill:#C8FACC,stroke:#2c662d,color:#2c662d;
    classDef database fill:#FFE5B4,stroke:#d2691e,color:#d2691e;
    classDef storage fill:#FFDAB9,stroke:#cd853f,color:#cd853f;
    classDef external fill:#E0E0E0,stroke:#888888,color:#555555
```
