# nhatlank23/e-course-sales-system architecture

> Architecture diagram of the GitHub repository nhatlank23/e-course-sales-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-03-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nhatlank23/e-course-sales-system
- Repository: https://github.com/nhatlank23/e-course-sales-system

## Overview

##### Project type & purpose (from README)
This is a **full-stack B2C e-learning platform** (“E-Course Sales System”) with two standout non-trivial capabilities:
- **Real-time consulting** (live sessions between learners and consultants/instructors)
- **Secure video streaming** (protected course video delivery)
Tech explicitly mentioned:
- **Next.js** (web app / frontend, possibly also SSR and/or API routes)
- **Express** (backend API / business logic)
- **Supabase** (managed backend services: Postgres DB, Auth, Storage, Realtime)

##### Web Client (Browser / Mobile Web)
- Interacts with Next.js over HTTPS
- Plays video (HLS/DASH) and participates in consulting sessions (WebRTC or similar)
- Authentication and session handling (likely via Supabase Auth tokens/cookies)

##### Next.js App
- UI pages, SSR/SSG, routing
- May call Express API, and may call Supabase directly (common pattern)
- “Next.js UI (SSR/SSG)” and
- “Next.js API routes (BFF)”
But don’t assume that without code—keep it a single container unless confirmed.

##### Express API Service
- Core business logic: course catalog, orders, enrollments, access control decisions
- Issues “secure video access” artifacts (e.g., signed URLs / entitlement checks)
- Manages “real-time consulting” orchestration if not fully handled by Supabase/3rd parties

##### Supabase Auth
- User registration/login, JWT issuance, session management
- Roles/permissions (e.g., student vs instructor/admin)

##### Supabase Postgres (Database)
- Courses, lessons, pricing, purchases, enrollments, user profiles
- Consulting session scheduling/history
- Access logs (optional)

##### Supabase Storage
- Video assets (or at least some media assets)
- Private buckets + signed URLs, and/or
- A CDN in front of storage, and/or
- Token-gated streaming manifests

##### Supabase Realtime (optional but likely)
- Presence/chat updates, session state, notifications, collaborative features
- Only include if the project truly uses it; otherwise mark as “possible”.

##### Real-time Signaling / Session Service
Real-time consulting usually needs:
- **Signaling** (WebSocket or similar) for WebRTC session negotiation
- **Media path** (WebRTC peer-to-peer or via SFU)

**Option 1 (common):**
- Browser ↔ (WebRTC) ↔ Browser (P2P media)
- Browser ↔ (WebSocket/HTTP) ↔ Express (signaling + auth + session state)

**Option 2 (scales better):**
- Browser ↔ (WebRTC) ↔ **SFU/Media Server** (external component)
- Browser ↔ Express (signaling/auth)

##### External services (don’t invent; include placeholders if product implies them)
E-course sales typically implies payments. Since README doesn’t mention it, handle it like this:

##### Auth & user session
- **Browser → Next.js**: login/signup UI
- **Next.js / Browser → Supabase Auth**: authenticate; receive JWT/session
- **Browser/Next.js → Express**: API calls with Bearer token / cookie
- **Express → Supabase Auth**: token verification / RBAC checks (or uses Supabase JWT verification logic)

##### Core app flows (catalog → purchase/enroll → access)
- **Browser → Next.js**: browse course catalog pages
- **Next.js → Express**: fetch course catalog / pricing / enrollment state
- **Express → Supabase Postgres**: read/write domain data (courses, purchases, enrollments)

- **Browser → Next.js**: open lesson page
- **Browser/Next.js → Express**: “request video playback authorization” (entitlement check)
- **Express → Supabase Postgres**: confirm enrollment/purchase
- **Express → Supabase Storage**: request signed URL / create signed access (or returns a token)
- **Browser → Storage/CDN**: stream video using signed URL (HLS/DASH)

##### Real-time consulting
- **Browser ↔ Express**: signaling (WebSocket/HTTP) + session metadata
- **Browser ↔ Browser or Browser ↔ SFU**: real-time media (WebRTC)
- **Express → Supabase Postgres**: persist session records, schedules, etc.
- (Optional) **Express/Browser ↔ Supabase Realtime**: presence/chat/notifications (only if used)

##### Architectural patterns to highlight (label these
Even at high level, call out these patterns as annotations:
- **Separation of concerns**: Next.js (presentation) vs Express (business/API) vs Supabase (data/auth/storage)
- **BFF vs direct-to-Supabase**: clarify whether Next.js directly queries Supabase or always goes via Express (mark as “verify”)
- **Entitlement-based media access**: “authorize then stream” (signed URLs / private storage)
- **Real-time subsystem split**: signaling/control plane vs media plane

##### Diagramming conventions to enforce (so it’s readable and “principal-level”)
Elements explicitly:

##### Clear labels
- Tech in the label, e.g.:
- “Next.js Web App (SSR/SSG)”
- “Express API (REST/GraphQL? verify)”
- “Supabase Auth (JWT)”
- “Supabase Storage (private bucket, signed URLs)”
- “Postgres (Supabase)”

- “HTTPS (JSON)”
- “WebSocket (signaling)”
- “WebRTC (media)”
- “SQL”
- “Signed URL (time-limited)”

##### “Don’t overthink it” but be detailed: split into as many components as possible
Given the minimal repo info, the best approach is:
- **Split real-time consulting into signaling vs media path**
- **Split secure streaming into authorization vs content delivery path**
- “VERIFY:” callouts on anything not explicitly confirmed by README.

- Does Next.js call Supabase directly, or is Express the only backend interface?
- Is Express deployed separately from Next.js, or behind the same domain/reverse proxy?
- For consulting: WebRTC P2P or SFU? What’s used for signaling (WS via Express, Supabase Realtime, or 3rd party)?
- For video: stored in Supabase Storage or elsewhere? Is there a CDN? How are signed URLs generated?
- What handles payments (if any), webhooks, and order finalization?

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% =========================
    %% System Context (C4 - Context)
    %% =========================
    subgraph "System Context (C4) — E‑Course Sales System"
        direction TB

        Learner["Student / Learner"]:::actor
        Instructor["Instructor / Consultant"]:::actor
        Admin["Admin (optional)"]:::actor

        System["E‑Learning Platform\n(Real‑time consulting + Secure video streaming)"]:::system

        SupabasePlatform["Supabase Platform\n(Managed services)"]:::external
        Payment["Payment Provider (TBD)\nVERIFY: Stripe/etc."]:::externalOptional
        CDN["CDN (optional)\nVERIFY: if used for video delivery"]:::externalOptional
        SFU["SFU / Media Server (optional)\nVERIFY: if used (vs P2P)"]:::externalOptional
    end

    Learner -->|"uses"| System
    Instructor -->|"uses"| System
    Admin -->|"manages"| System

    System -->|"auth+data+storage+realtime"| SupabasePlatform
    System -.->|"payments (verify)"| Payment
    System -.->|"video edge caching (verify)"| CDN
    System -.->|"WebRTC routing (verify)"| SFU

    %% =========================
    %% Container Diagram (C4 - Containers)
    %% =========================
    subgraph "Container Diagram (C4) — High‑level Architecture"
        direction TB

        subgraph "Client-facing Layer"
            direction TB
            WebClient["Web Client\n(Browser / Mobile Web)"]:::client
        end

        subgraph "Application Layer"
            direction TB
            NextApp["Next.js Web App\n(UI + SSR/SSG)\nVERIFY: API routes/BFF?"]:::compute
            ExpressAPI["Express API Service\n(Business logic + Entitlements + Signaling)"]:::compute
        end

        subgraph "Supabase (Managed Services)"
            direction TB
            SupaAuth["Supabase Auth\n(JWT + Sessions + RBAC)"]:::managed
            SupaDB[("Supabase Postgres\n(Courses, Orders, Enrollments,\nConsulting sessions)")]:::datastore
            SupaStorage[("Supabase Storage\n(Private media)\nSigned URLs / Gated access (VERIFY)")]:::datastore
            SupaRealtime["Supabase Realtime (optional)\nPresence/Chat/Notifications (VERIFY)"]:::managedOptional
        end

        subgraph "Real-time Consulting Subsystem"
            direction TB
            Signaling["Signaling / Session Control Plane\n(WebSocket/HTTP)\nImplementation: Express or Supabase Realtime (VERIFY)"]:::rt
            MediaPlane["Media Plane\n(WebRTC media)\nP2P or SFU (VERIFY)"]:::rt
            OptionalSFU["Optional: SFU / Media Server\n(if used)"]:::externalOptional
        end

        subgraph "Secure Video Streaming"
            direction TB
            VideoAuth["Playback Authorization\n(Entitlement check)"]:::security
            StreamPath["Direct Video Delivery\n(HLS/DASH bytes)\nClient → Storage/CDN"]:::security
            OptionalCDN["Optional: CDN\n(if used)"]:::externalOptional
        end

        subgraph "External Services (Optional)"
            direction TB
            PayProvider["Payment Provider (TBD)\nVERIFY integration + webhooks"]:::externalOptional
        end
    end

    %% =========================
    %% Container Connections (vertical-first)
    %% =========================
    WebClient -->|"HTTPS"| NextApp

    %% Auth flows
    NextApp -->|"Auth UI/actions"| SupaAuth
    WebClient -->|"Auth (JWT/session)"| SupaAuth
    WebClient -->|"HTTPS (JSON)\nBearer token/cookie"| ExpressAPI
    NextApp -->|"HTTPS (JSON)\nAPI calls"| ExpressAPI
    ExpressAPI -->|"JWT verify / RBAC (VERIFY impl)"| SupaAuth

    %% Core domain data
    ExpressAPI -->|"SQL (via Supabase client/REST)"| SupaDB

    %% Optional direct-to-Supabase from Next.js (marked verify)
    NextApp -.->|"Direct queries (VERIFY)"| SupaDB
    NextApp -.->|"Direct storage access (VERIFY)"| SupaStorage

    %% Secure video streaming (explicit gate; avoid proxying bytes)
    NextApp -->|"Open lesson page"| VideoAuth
    WebClient -->|"Request playback authorization"| VideoAuth
    VideoAuth -->|"Entitlement check"| ExpressAPI
    ExpressAPI -->|"Read enrollment/purchase"| SupaDB
    ExpressAPI -->|"Create signed URL / token (VERIFY)"| SupaStorage
    VideoAuth -->|"Return signed URL (time-limited)"| WebClient
    WebClient -->|"Stream video (HLS/DASH)\nusing signed URL"| StreamPath
    StreamPath -->|"GET video segments"| SupaStorage
    StreamPath -.->|"Optional edge caching"| OptionalCDN
    OptionalCDN -.->|"Origin fetch (if configured)"| SupaStorage

    %% Real-time consulting (signaling vs media)
    WebClient <-->|"WebSocket/HTTP\n(Signaling)"| Signaling
    Signaling -->|"Implemented by"| ExpressAPI
    ExpressAPI -->|"Persist sessions/schedule"| SupaDB

    %% Optional: Realtime for chat/presence
    WebClient -.->|"Presence/Chat (VERIFY)"| SupaRealtime
    ExpressAPI -.->|"Publish/subscribe (VERIFY)"| SupaRealtime
    Signaling -.->|"Alternative: signaling via Supabase Realtime (VERIFY)"| SupaRealtime

    %% Media plane
    WebClient <-->|"WebRTC (media)\nP2P (VERIFY)"| MediaPlane
    MediaPlane -.->|"If SFU used"| OptionalSFU
    WebClient -.->|"WebRTC (media)\nvia SFU (VERIFY)"| OptionalSFU

    %% Payments optional
    ExpressAPI -.->|"Create checkout / verify payment (VERIFY)"| PayProvider
    PayProvider -.->|"Webhook callbacks (VERIFY)"| ExpressAPI
    ExpressAPI -.->|"Update orders/enrollments"| SupaDB

    %% =========================
    %% Notes / Assumptions to confirm
    %% =========================
    subgraph "VERIFY / To Confirm"
        direction TB
        Verify1["VERIFY: Next.js uses direct Supabase access\nvs all domain calls via Express"]:::note
        Verify2["VERIFY: Express deployment vs Next.js domain/reverse proxy"]:::note
        Verify3["VERIFY: Consulting media is WebRTC P2P vs SFU;\nSignaling via Express WS vs Supabase Realtime"]:::note
        Verify4["VERIFY: Video stored in Supabase Storage;\nSigned URLs vs token-gated manifests; CDN usage"]:::note
        Verify5["VERIFY: Payment provider + webhook handling"]:::note
    end

    System --- Verify1
    System --- Verify2
    System --- Verify3
    System --- Verify4
    System --- Verify5

    %% =========================
    %% Click Events (from component_mapping)
    %% =========================
    click System "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click WebClient "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click NextApp "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click ExpressAPI "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click SupaAuth "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click SupaDB "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click SupaStorage "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click SupaRealtime "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click Signaling "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click MediaPlane "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click VideoAuth "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click StreamPath "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click PayProvider "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click OptionalCDN "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"
    click OptionalSFU "https://github.com/nhatlank23/e-course-sales-system/blob/main/README.md"

    %% =========================
    %% Styles
    %% =========================
    classDef actor fill:#e5e7eb,stroke:#6b7280,color:#111827,stroke-width:1px
    classDef client fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:1px
    classDef system fill:#dbeafe,stroke:#2563eb,color:#0f172a,stroke-width:2px
    classDef compute fill:#bfdbfe,stroke:#1d4ed8,color:#0b1220,stroke-width:1.5px
    classDef managed fill:#fed7aa,stroke:#c2410c,color:#111827,stroke-width:1.5px
    classDef managedOptional fill:#ffedd5,stroke:#c2410c,color:#111827,stroke-width:1.5px,stroke-dasharray: 6 4
    classDef datastore fill:#bbf7d0,stroke:#15803d,color:#052e16,stroke-width:1.5px
    classDef rt fill:#fde68a,stroke:#b45309,color:#111827,stroke-width:1.5px
    classDef security fill:#fecaca,stroke:#b91c1c,color:#111827,stroke-width:1.5px
    classDef external fill:#fdba74,stroke:#9a3412,color:#111827,stroke-width:1.5px
    classDef externalOptional fill:#ffedd5,stroke:#9a3412,color:#111827,stroke-width:1.5px,stroke-dasharray: 6 4
    classDef note fill:#fff7ed,stroke:#c2410c,color:#111827,stroke-width:1px,stroke-dasharray: 3 3
```
