# kgesh/lms-backend architecture

> Architecture diagram of the GitHub repository kgesh/lms-backend, 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/kgesh/lms-backend
- Repository: https://github.com/kgesh/lms-backend

## Overview

##### Main Architectural Layers
- Frontline API Layer (Controllers):
– All the v1 directories (e.g., v1/auth, v1/course, v1/ebook, v1/order, etc.) hold the controllers that expose HTTP endpoints.
- Business Logic / Service Layer:
– Each domain module has associated service files handling domain-specific business logic.
– Additional core services (e.g., app.service.ts, auth.service.ts, promotion service, etc.) are located in both src and domain-specific directories.
- Data Access / Repository Layer:
– Every module has its own repository files (e.g., course.repository.ts, coupon.repository.ts, etc.) indicating a repository pattern.
– The infra/db folder includes Drizzle ORM modules, helpers, and schema definitions.
- Infrastructure & Integration:
– The “infra” directory contains integrations for databases, HTTP client, payment (including Portone API), S3 storage, and SMS providers.
– Included also are configuration and helper modules found in src/configs and src/shared (helpers, types, utils).
- Core Components and Cross‑Cutting Concerns:
– The src/core directory holds base repository classes, decorators, guards, middlewares (such as logger and API guards) and error handling.

##### Representing Components and Their Interactions
– API Gateway / Controllers (group by domain e.g., Auth, Course, Ebook, Order, etc.)
– Business Logic / Service Layer (each module’s services)
– Data Access / Repositories (group all repository modules including Drizzle ORM integration)
– Infrastructure Services (database connections, S3 client, Payment service, SMS provider, HTTP client)
– Shared/Common Utilities (configs, shared/helpers, core decorators and guards)
– Controllers call Services.
– Services interact with Repositories for data access.
– Repositories call the Database (visualized as the DB server component) via Drizzle ORM.
– Infrastructure components (e.g., Payment, S3, SMS) are invoked by the services when needed.
– Configuration modules and shared utilities are used across each layer.

##### Architectural Patterns & Design Principles
- Modular Structure (each v1 domain is self-contained, with its own controller, service, repository, and sometimes additional sub‑modules).
- Separation of Concerns – different folders (e.g., core, infra, shared, configs) indicate strong layering.
- Dependency Injection – evidenced by Nest’s provider setup.
- Use of External Integrations – clearly called out in infra (HTTP client, S3, payment, SMS) suggesting communication with external systems.
- Testing/Quality Assurance – the existence of a dedicated “test” directory with end-to-end tests supports a test‑driven design.

##### Elements
– Domain Modules (v1/auth, v1/course, v1/ebook, etc.)
– Core services (application bootstrap, middleware, logging, error handling)
– Repository/Database Access Layer (Drizzle ORM integration)
– Infrastructure modules (database connection pool, HTTP client, payment service, S3 client, SMS provider)
– External Services (e.g., external payment API, S3 storage service, external HTTP endpoints)
– Indicate that controllers forward incoming requests to services.
– Show that services query repositories and call integration modules.
- Annotations/Legends that describe:
– What each module is responsible for (e.g., “Handles user authentication and session management” in the auth module).

##### Putting It All Together
- Inside, create sub‑sections for different layers:
– Presentation Layer (controllers from v1/*),
– Business Layer (services),
– Data Layer (repositories and ORM integration),
– Infrastructure & Integrations (infra/* modules).
- That all modules (auth, course, ebook, coupon, review, teacher, term, etc.) are represented either as individual blocks or as grouped components if there are common patterns.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "NestJS Application (LMS/E-Commerce Platform)"
        
        subgraph "Presentation Layer"
            ctrl["API Controllers"]:::api
        end

        subgraph "Business Logic Layer"
            svc["Domain Services"]:::service
        end

        subgraph "Data Access Layer"
            repo["Domain Repositories"]:::repo
            baseRepo["Base Repository"]:::repo
            drizzle["Drizzle ORM Integration"]:::db
            database["Database"]:::database
        end

        subgraph "Infrastructure & Integrations"
            payment["Payment Gateway"]:::infra
            s3["S3 Storage"]:::infra
            http["HTTP Client"]:::infra
            sms["SMS Provider"]:::infra
        end

        subgraph "Core & Shared Components"
            core["Core Components"]:::core
            configs["Configurations"]:::core
            shared["Shared Utilities"]:::core
        end
    end

    %% Connections between Layers
    ctrl -->|"calls"| svc
    svc -->|"calls"| repo
    svc -->|"calls"| payment
    svc -->|"calls"| s3
    svc -->|"calls"| http
    svc -->|"calls"| sms

    repo -->|"depends_on"| baseRepo
    repo -->|"data_access"| drizzle
    drizzle -->|"queries"| database

    %% Cross-cutting concerns
    ctrl -->|"uses"| core
    svc -->|"uses"| configs
    svc -->|"uses"| shared
    repo -->|"uses"| core

    %% Click Events
    click ctrl "https://github.com/kgesh/lms-backend/tree/main/src/v1"
    click svc "https://github.com/kgesh/lms-backend/blob/main/src/app.service.ts"
    click baseRepo "https://github.com/kgesh/lms-backend/blob/main/src/core/base.repository.ts"
    click drizzle "https://github.com/kgesh/lms-backend/tree/main/src/infra/db"
    click payment "https://github.com/kgesh/lms-backend/tree/main/src/infra/payment"
    click s3 "https://github.com/kgesh/lms-backend/tree/main/src/infra/s3"
    click http "https://github.com/kgesh/lms-backend/tree/main/src/infra/http"
    click sms "https://github.com/kgesh/lms-backend/tree/main/src/infra/sms"
    click core "https://github.com/kgesh/lms-backend/tree/main/src/core"
    click configs "https://github.com/kgesh/lms-backend/tree/main/src/configs"
    click shared "https://github.com/kgesh/lms-backend/tree/main/src/shared"

    %% Styles
    classDef api fill:#f9c,stroke:#333,stroke-width:2px;
    classDef service fill:#ccf,stroke:#333,stroke-width:2px;
    classDef repo fill:#cfc,stroke:#333,stroke-width:2px;
    classDef db fill:#fcf,stroke:#333,stroke-width:2px;
    classDef database fill:#ff9,stroke:#333,stroke-width:2px;
    classDef infra fill:#cff,stroke:#333,stroke-width:2px;
    classDef core fill:#ffc,stroke:#333,stroke-width:2px;
```
