# imquangthang/pbl4-backend architecture

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

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

## Overview

##### Project type and purpose
- This is a Node.js/Express-based web application with both server-side rendered views and a JSON API.
- It follows an MVC (Model-View-Controller) and layered-service architecture, exposing endpoints for doctors, hospitals, users, roles, messages, etc., and uses JWT for authentication.

##### File-structure analysis
- Top-level
- index.js / src/server.js – application entry points
- .babelrc – ES6+ transpilation config
- package.json – dependencies and scripts (e.g. “npm run dev”)
- src/Config
- viewEngine.js – sets up the template engine for server-side rendering
- src/Middleware
- CORS.js – cross-origin config
- JWTActions.js – token parsing/verification
- src/Routes
- Web.js – routes that render HTML views
- Api.js – JSON API endpoints
- src/Controllers
- One controller per resource (doctorController, userController, etc.) – handle HTTP requests, invoke services
- src/Services
- Business-logic layer (loginRegisterService, userApiService, messageService, etc.)
- JWTService for token issuance and validation
- src/Models
- Database connection (ConnectDB.js), table definitions (CreateTable.js)
- One model per domain entity (Accounts, Appointment, Faculty, Groups, HealthRecord, MedicalRecord, Roles, Message, Group_Role)
- src/Models/index.js – aggregates and exports all models

##### Insights from README
- Minimal instructions: “dùng ‘npm run dev’ để chạy” (use npm run dev to launch in development mode).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Clients
    Client["Client<br/>(Browser, Postman)"]:::client

    %% Entry Point
    subgraph "Entry Point"
        EP1["index.js"]:::server
        EP2["src/server.js"]:::server
    end

    %% Configuration
    subgraph "Configuration Layer"
        Config["viewEngine.js"]:::config
    end

    %% Middleware Layer
    subgraph "Middleware Layer"
        CORS["CORS.js"]:::middleware
        JWTMW["JWTActions.js"]:::middleware
    end

    %% Routing Layer
    subgraph "Routing Layer"
        WebRoutes["Web.js"]:::routing
        ApiRoutes["Api.js"]:::routing
    end

    %% Controller Layer
    subgraph "Controller Layer"
        APIController["apiController.js"]:::controller
        DoctorController["doctorController.js"]:::controller
        HospitalController["hospitalController.js"]:::controller
        MessageController["messageController.js"]:::controller
        RoleController["roleController.js"]:::controller
        UserController["userController.js"]:::controller
    end

    %% Service Layer
    subgraph "Service Layer"
        JWTService["JWTService.js"]:::service
        AuthService["loginRegisterService.js"]:::service
        UserService["userApiService.js"]:::service
        DoctorService["doctorApiService.js"]:::service
        HospitalService["hospitalApiServirce.js"]:::service
        RoleService["roleApiService.js"]:::service
        StaffService["staffApiService.js"]:::service
        MessageService["messageService.js"]:::service
    end

    %% Model Layer
    subgraph "Data-access Layer"
        DBConn["ConnectDB.js"]:::model
        TableInit["CreateTable.js"]:::model
        subgraph "Entity Models"
            Accounts["Accounts.js"]:::model
            Appointment["Appointment.js"]:::model
            Faculty["Faculty.js"]:::model
            Groups["Groups.js"]:::model
            GroupRole["Group_Role.js"]:::model
            HealthRecord["HealthRecord.js"]:::model
            MedicalRecord["MedicalRecord.js"]:::model
            MessageModel["Message.js"]:::model
            Roles["Roles.js"]:::model
        end
        ModelsIndex["index.js"]:::model
    end

    %% Database
    Database["Relational Database"]:::database

    %% Flow
    Client -->|"HTTP Request"| EP1
    Client -->|"HTTP Request"| EP2
    EP1 -->|configures| Config
    EP2 -->|configures| Config
    EP1 -->|registers| CORS
    EP1 -->|registers| JWTMW
    EP2 -->|registers| CORS
    EP2 -->|registers| JWTMW
    CORS -->|passes request| WebRoutes
    JWTMW -->|verifies token| ApiRoutes
    WebRoutes -->|routes to| APIController
    WebRoutes -->|routes to| UserController
    WebRoutes -->|routes to| DoctorController
    WebRoutes -->|routes to| HospitalController
    WebRoutes -->|routes to| MessageController
    WebRoutes -->|routes to| RoleController
    ApiRoutes -->|routes to| APIController
    ApiRoutes -->|routes to| UserController
    ApiRoutes -->|routes to| DoctorController
    ApiRoutes -->|routes to| HospitalController
    ApiRoutes -->|routes to| MessageController
    ApiRoutes -->|routes to| RoleController

    APIController -->|uses| AuthService
    UserController -->|uses| UserService
    DoctorController -->|uses| DoctorService
    HospitalController -->|uses| HospitalService
    RoleController -->|uses| RoleService
    MessageController -->|uses| MessageService

    AuthService -->|accesses| ModelsIndex
    UserService -->|accesses| ModelsIndex
    DoctorService -->|accesses| ModelsIndex
    HospitalService -->|accesses| ModelsIndex
    RoleService -->|accesses| ModelsIndex
    MessageService -->|accesses| ModelsIndex
    StaffService -->|accesses| ModelsIndex
    JWTService -->|accesses| ModelsIndex

    ModelsIndex -->|aggregates| DBConn
    ModelsIndex -->|aggregates| TableInit
    ModelsIndex -->|aggregates| Accounts
    ModelsIndex -->|aggregates| Appointment
    ModelsIndex -->|aggregates| Faculty
    ModelsIndex -->|aggregates| Groups
    ModelsIndex -->|aggregates| GroupRole
    ModelsIndex -->|aggregates| HealthRecord
    ModelsIndex -->|aggregates| MedicalRecord
    ModelsIndex -->|aggregates| MessageModel
    ModelsIndex -->|aggregates| Roles

    DBConn -->|connects to| Database
    TableInit -->|initializes| Database
    Accounts -->|maps to| Database
    Appointment -->|maps to| Database
    Faculty -->|maps to| Database
    Groups -->|maps to| Database
    GroupRole -->|maps to| Database
    HealthRecord -->|maps to| Database
    MedicalRecord -->|maps to| Database
    MessageModel -->|maps to| Database
    Roles -->|maps to| Database

    %% Click events
    click EP1 "https://github.com/imquangthang/pbl4-backend/blob/main/index.js"
    click EP2 "https://github.com/imquangthang/pbl4-backend/blob/main/src/server.js"
    click Config "https://github.com/imquangthang/pbl4-backend/blob/main/src/Config/viewEngine.js"
    click CORS "https://github.com/imquangthang/pbl4-backend/blob/main/src/Middleware/CORS.js"
    click JWTMW "https://github.com/imquangthang/pbl4-backend/blob/main/src/Middleware/JWTActions.js"
    click WebRoutes "https://github.com/imquangthang/pbl4-backend/blob/main/src/Routes/Web.js"
    click ApiRoutes "https://github.com/imquangthang/pbl4-backend/blob/main/src/Routes/Api.js"
    click APIController "https://github.com/imquangthang/pbl4-backend/blob/main/src/Controllers/apiController.js"
    click DoctorController "https://github.com/imquangthang/pbl4-backend/blob/main/src/Controllers/doctorController.js"
    click HospitalController "https://github.com/imquangthang/pbl4-backend/blob/main/src/Controllers/hospitalController.js"
    click MessageController "https://github.com/imquangthang/pbl4-backend/blob/main/src/Controllers/messageController.js"
    click RoleController "https://github.com/imquangthang/pbl4-backend/blob/main/src/Controllers/roleController.js"
    click UserController "https://github.com/imquangthang/pbl4-backend/blob/main/src/Controllers/userController.js"
    click JWTService "https://github.com/imquangthang/pbl4-backend/blob/main/src/Services/JWTService.js"
    click AuthService "https://github.com/imquangthang/pbl4-backend/blob/main/src/Services/loginRegisterService.js"
    click UserService "https://github.com/imquangthang/pbl4-backend/blob/main/src/Services/userApiService.js"
    click DoctorService "https://github.com/imquangthang/pbl4-backend/blob/main/src/Services/doctorApiService.js"
    click HospitalService "https://github.com/imquangthang/pbl4-backend/blob/main/src/Services/hospitalApiServirce.js"
    click RoleService "https://github.com/imquangthang/pbl4-backend/blob/main/src/Services/roleApiService.js"
    click StaffService "https://github.com/imquangthang/pbl4-backend/blob/main/src/Services/staffApiService.js"
    click MessageService "https://github.com/imquangthang/pbl4-backend/blob/main/src/Services/messageService.js"
    click DBConn "https://github.com/imquangthang/pbl4-backend/blob/main/src/Models/ConnectDB.js"
    click TableInit "https://github.com/imquangthang/pbl4-backend/blob/main/src/Models/CreateTable.js"
    click Accounts "https://github.com/imquangthang/pbl4-backend/blob/main/src/Models/Accounts.js"
    click Appointment "https://github.com/imquangthang/pbl4-backend/blob/main/src/Models/Appointment.js"
    click Faculty "https://github.com/imquangthang/pbl4-backend/blob/main/src/Models/Faculty.js"
    click Groups "https://github.com/imquangthang/pbl4-backend/blob/main/src/Models/Groups.js"
    click GroupRole "https://github.com/imquangthang/pbl4-backend/blob/main/src/Models/Group_Role.js"
    click HealthRecord "https://github.com/imquangthang/pbl4-backend/blob/main/src/Models/HealthRecord.js"
    click MedicalRecord "https://github.com/imquangthang/pbl4-backend/blob/main/src/Models/MedicalRecord.js"
    click MessageModel "https://github.com/imquangthang/pbl4-backend/blob/main/src/Models/Message.js"
    click Roles "https://github.com/imquangthang/pbl4-backend/blob/main/src/Models/Roles.js"
    click ModelsIndex "https://github.com/imquangthang/pbl4-backend/blob/main/src/Models/index.js"

    %% Styles
    classDef client fill:#EFEFFF,stroke:#AAAaff,stroke-width:1px;
    classDef server fill:#FFEFEF,stroke:#ffaaaa,stroke-width:1px;
    classDef config fill:#F0FFF0,stroke:#aaffaa,stroke-width:1px;
    classDef middleware fill:#FFF0F0,stroke:#ffaaff,stroke-width:1px;
    classDef routing fill:#F0F0FF,stroke:#aaaaff,stroke-width:1px;
    classDef controller fill:#FFF8DC,stroke:#eedd82,stroke-width:1px;
    classDef service fill:#F5F5F5,stroke:#888888,stroke-width:1px;
    classDef model fill:#F0FFFF,stroke:#66CCCC,stroke-width:1px;
    classDef database fill:#FFFFE0,stroke:#CCCC66,stroke-width:1px;
```
