# painnha/tuyensinhthongminh-monorepo architecture

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

- Interactive diagram: https://gitdiagram.com/painnha/tuyensinhthongminh-monorepo
- Repository: https://github.com/painnha/tuyensinhthongminh-monorepo

## Overview

##### Main Components
- Backend API Server: Built with Node.js and Express, using MongoDB (Mongoose) for data storage. This includes routes, controllers (for authentication, universities, and subject combinations), models, middleware (for authentication), utility functions (such as OTP utilities), and script tools (to import CSV data and create admin users).
- Frontend Application: A React (with TypeScript) application using Material-UI, React Query, and structured into components such as authentication screens, dashboards, consultation, news, transcript calculators, and university details.
- Shared Package: Contains shared utilities and type definitions, ensuring consistency between the frontend and backend.
- External Services: As indicated by the usage of Infobip for OTP management and JWT for authentication. Also consider MongoDB as an external database.

##### Relationships and Interactions
- The frontend communicates with the backend via RESTful API calls. It consumes APIs for authentication (sending and verifying OTPs, login, register, password reset), fetching university data (/api/universities endpoints), and subject combinations.
- The backend interacts with MongoDB for data persistence. It employs a layered pattern where the routes pass requests to controllers, which then manage business logic and data via models.
- Shared utilities are used by both the frontend and backend to maintain consistency (e.g., type definitions, common logic).

##### Architectural Patterns and Design Principles
- Separation of Concerns: Clearly divided modules for controllers, models, middleware, and routes in the backend, as well as a component-based architecture in the frontend.
- API Driven: A RESTful API design for backend communication.
- Containerization: Docker is used to containerize both the frontend and backend, ensuring consistent and scalable deployments.
- Monorepo Structure: The three packages (backend, frontend, shared) are integrated, so drawing module interactions is important.

##### Relevant Technologies
- Backend: Node.js, Express, MongoDB, Mongoose, JWT, RESTful API.
- Frontend: React, TypeScript, Material-UI, React Query.
- Shared: TypeScript, utilities, type definitions.
- Additional: Docker for containerization, CI/CD pipelines as evident from the GitHub workflows.

##### Specific Elements
- Clear Labels: Use clear labels for each component (e.g., “React Frontend,” “Express API Server,” “MongoDB Database,” “Shared Utilities,” “External Infobip Service”).

##### Detailed Steps for the Principal Software Engineer
- Within the Backend block, further divide into sub-components (Authentication, API Controllers, Data Models, Business Logic, Middleware, Utility Scripts) and add a side note for Docker and GitHub workflow integrations.
- External services like Infobip for OTP and JWT for authentication flows.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Main Components
    subgraph "Frontend Layer"
        FE["React Frontend (Auth, Dashboard, News, Transcript Calculators, University Details)"]:::frontend
    end

    subgraph "Backend Layer"
        BE["Express API Server"]:::backend
        subgraph "API Internals"
            ROUTES["Routes"]:::backendDetail
            CTRL["Controllers (Authentication, Universities, Subjects)"]:::backendDetail
            MODELS["Data Models"]:::backendDetail
            MIDDLEWARE["Authentication Middleware"]:::backendDetail
            UTILS["Utility Functions (e.g., OTP utilities)"]:::backendDetail
            SCRIPTS["Script Tools (CSV Import/Admin creation)"]:::backendDetail
            CONFIG["Configuration"]:::backendDetail
        end
    end

    subgraph "Shared Package"
        SH_SRC["Shared Utilities & Types (Source)"]:::shared
        SH_PKG["Shared Package (package.json)"]:::shared
    end

    subgraph "Deployment & Infrastructure"
        DC["Docker Compose"]:::container
        FE_DOCKER["Frontend Dockerfile"]:::container
        BE_DOCKER["Backend Dockerfile"]:::container
        FE_DC["Frontend Docker Compose"]:::container
        CICD["CI/CD Workflows (.github/workflows)"]:::container
    end

    subgraph "External Services"
        MONGO["MongoDB Database"]:::external
        INFOBIP["Infobip Service"]:::external
        JWT["JWT Auth Service"]:::external
    end

    %% Interactions and Relationships

    %% Frontend to Backend API
    FE -->|"REST_API_calls"| BE

    %% Backend internal flows
    BE -->|"delegates_to"| ROUTES
    ROUTES -->|"calls"| CTRL
    CTRL -->|"queries"| MODELS
    CTRL -->|"invokes"| MIDDLEWARE
    CTRL -->|"utilizes"| UTILS
    BE -->|"config_used_by"| CONFIG
    BE -->|"script_invokes"| SCRIPTS

    %% Shared utilities used by both Frontend and Backend
    BE ---|"uses"| SH_SRC
    FE ---|"uses"| SH_SRC
    SH_SRC ---|"defined_in"| SH_PKG

    %% Backend interactions with external services
    BE -->|"Mongoose_queries"| MONGO
    BE -->|"OTP_Integration"| INFOBIP
    BE -->|"Token_Validation"| JWT

    %% Deployment: Containers orchestrated by Docker Compose
    DC -->|"orchestrates"| BE_DOCKER
    DC -->|"orchestrates"| FE_DOCKER
    DC -->|"orchestrates"| MONGO
    FE_DOCKER ---|"build_from"| FE
    BE_DOCKER ---|"build_from"| BE
    FE_DC ---|"defines_frontend"| FE
    CICD -->|"triggers"| DC

    %% Click Events for Backend Components
    click BE "https://github.com/painnha/tuyensinhthongminh-monorepo/blob/main/backend/server.js"
    click ROUTES "https://github.com/painnha/tuyensinhthongminh-monorepo/tree/main/backend/routes"
    click CTRL "https://github.com/painnha/tuyensinhthongminh-monorepo/tree/main/backend/controllers"
    click MODELS "https://github.com/painnha/tuyensinhthongminh-monorepo/tree/main/backend/models"
    click MIDDLEWARE "https://github.com/painnha/tuyensinhthongminh-monorepo/tree/main/backend/middleware"
    click UTILS "https://github.com/painnha/tuyensinhthongminh-monorepo/tree/main/backend/utils"
    click SCRIPTS "https://github.com/painnha/tuyensinhthongminh-monorepo/tree/main/backend/scripts"
    click CONFIG "https://github.com/painnha/tuyensinhthongminh-monorepo/tree/main/backend/config"

    %% Click Events for Frontend
    click FE "https://github.com/painnha/tuyensinhthongminh-monorepo/tree/main/frontend/"
    
    %% Click Events for Shared Components
    click SH_SRC "https://github.com/painnha/tuyensinhthongminh-monorepo/tree/main/shared/src"
    click SH_PKG "https://github.com/painnha/tuyensinhthongminh-monorepo/blob/main/shared/package.json"

    %% Click Events for Deployment/CI
    click DC "https://github.com/painnha/tuyensinhthongminh-monorepo/blob/main/docker-compose.yml"
    click FE_DOCKER "https://github.com/painnha/tuyensinhthongminh-monorepo/tree/main/frontend/Dockerfile"
    click BE_DOCKER "https://github.com/painnha/tuyensinhthongminh-monorepo/tree/main/backend/Dockerfile"
    click FE_DC "https://github.com/painnha/tuyensinhthongminh-monorepo/blob/main/frontend/docker-compose.yml"
    click CICD "https://github.com/painnha/tuyensinhthongminh-monorepo/tree/main/.github/workflows"

    %% Styles
    classDef frontend fill:#BBDEFB,stroke:#0277BD,stroke-width:2px;
    classDef backend fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px;
    classDef backendDetail fill:#DCEDC8,stroke:#558B2F,stroke-width:1px;
    classDef shared fill:#FFF9C4,stroke:#F9A825,stroke-width:2px;
    classDef container fill:#E1BEE7,stroke:#6A1B9A,stroke-width:2px;
    classDef external fill:#FFCDD2,stroke:#C62828,stroke-width:2px;
```
