# hung123-alt/ktpm_4_2026 architecture

> Architecture diagram of the GitHub repository hung123-alt/ktpm_4_2026, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hung123-alt/ktpm_4_2026
- Repository: https://github.com/hung123-alt/ktpm_4_2026

## Overview

1. **Repository shape**
   - This is a two-tier monorepo: a **React/Vite client** in `client/` and a **NestJS API server** in `server/`.
   - The architecture is centered on a **REST API** consumed by the frontend; there is no separate backend-for-frontend layer.

2. **Frontend shell and app composition**
   - The client bootstraps through `main.jsx` and `App.jsx`, with global wiring in `AppProviders.jsx`.
   - Core cross-cutting providers include **auth state**, **theme state**, and a **query client** for server state management.
   - Routing is split into **public**, **protected**, and **admin** routes, with dedicated route guards.

3. **Frontend feature organization**
   - The client is organized by domain feature: `auth`, `movies`, `comments`, `interactions`, `notifications`, `reports`, and `admin`.
   - Each feature typically has:
     - `api/` for HTTP calls,
     - `hooks/` for reusable data/mutation logic,
     - `pages/` for route-level screens,
     - `queries.js` or `index.js` for feature exports.
   - This indicates a **feature-first frontend architecture** with React Query-style server-state boundaries.

4. **Frontend presentation boundaries**
   - Shared UI is separated from feature UI:
     - `shared/layout/` for `Header`, `Footer`, `MainLayout`, `AdminLayout`,
     - `shared/components/` for cross-app controls like `ThemeToggle`.
   - `ProtectedRoute` and `AdminRoute` enforce access boundaries in the UI, but they complement server-side authorization rather than replace it.

5. **Frontend data flow**
   - UI components call feature hooks, hooks call feature APIs, and APIs use a shared Axios client in `lib/axiosClient.js`.
   - Central query-client setup suggests cache/invalidation-driven flows for list/detail screens, mutations, and refreshable user state.
   - The main user journeys reflected in pages are:
     - browsing/searching/filtering movies,
     - viewing details and watching,
     - commenting, rating, favoriting, watchlisting, and tracking history,
     - authentication and profile management,
     - admin moderation and catalog management.

6. **Backend core structure**
   - The server is a **NestJS modular monolith**: each business area lives in its own module with controller/service/entity separation.
   - `app.module.ts` composes modules, while `main.ts` is the Nest bootstrap entrypoint.
   - The backend is strongly domain-driven at the module level rather than layered by technical concern.

7. **Backend domain modules**
   - Major server modules include:
     - `auth` for login/register/password reset/refresh token handling,
     - `users` for profile and account management,
     - `movies` and `episodes` for catalog and playback metadata,
     - `genres` and `countries` for catalog taxonomy,
     - `comments`, `ratings`, `favorites`, `watchlist`, and `watch-history` for user interactions,
     - `notifications` for user-facing alerts,
     - `error-reports` for issue reporting,
     - `admin` for privileged operations.
   - This mirrors the frontend feature map, which keeps the client/server contract easy to reason about.

8. **Backend security model**
   - Authentication is JWT-based, with `auth.strategy.ts`, `jwt-auth.guard.ts`, and `roles.guard.ts` enforcing access.
   - Authorization is role-aware through `roles.decorator.ts` and `user-role.enum.ts`; there is a clear boundary between normal users and admins.
   - `current-user.decorator.ts` indicates controllers can directly access the authenticated principal.
   - The presence of `refresh-token.entity.ts` suggests token lifecycle management beyond simple stateless JWTs.

9. **Backend persistence and schema**
   - `server/src/database/` contains the relational schema and seed data, which points to a **SQL database-backed architecture**.
   - The devcontainer includes MSSQL tooling and setup scripts, so local development is designed around **Microsoft SQL Server**.
   - Entities under each module imply the API maps closely to relational tables and joins rather than a document model.

10. **Backend cross-cutting services**
   - `mail/` is a separate infrastructure module, likely used for password recovery and notification delivery.
   - Shared enums, DTOs, and utilities in `common/` keep validation, error typing, role checks, and slug generation consistent across modules.
   - This shows a small set of shared primitives supporting otherwise isolated feature modules.

11. **Interaction and moderation workflows**
   - User-generated and user-specific data is split into distinct modules:
     - `comments`, `ratings`, `favorites`, `watchlist`, `watch-history`, and `notifications`.
   - `error-reports` and the `admin` module support moderation/operations workflows, while frontend admin pages expose management screens for users, comments, movies, genres, countries, and reports.
   - This is a good example of a **user-content + moderation** subsystem boundary.

12. **Operational and developer tooling**
   - The repo includes a `.devcontainer/` setup with Docker Compose and SQL Server initialization scripts, indicating the project is intended to run consistently in containerized dev environments.
   - The client also has a `vercel.json`, suggesting the frontend is deployable as a static SPA on Vercel.
   - End-to-end test scaffolding exists on the server (`test/app.e2e-spec.ts`), showing the API has integration-test coverage.

13. **How to draw the architecture**
   - The simplest diagram is:
     - **Browser / React client**
       → **NestJS REST API**
       → **SQL Server database**
       plus a side path from the API to **mail delivery**.
   - Add role-based gates on both sides: UI route guards on the client and JWT/role guards on the server.
   - Around the API, show domain modules as separate internal subsystems rather than a single generic backend.

Main entry points: [`server/src/main.ts`](https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/main.ts) (Nest app), [`client/src/routes/AppRoutes.jsx`](https://github.com/hung123-alt/ktpm_4_2026/blob/main/client/src/routes/AppRoutes.jsx) (Routes), [`server/src/database/schema.sql`](https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/database/schema.sql) (SQL schema), [`server/src/common`](https://github.com/hung123-alt/ktpm_4_2026/tree/main/server/src/common) (Shared core)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_client_entry["App shell<br/>react shell<br/>[main.jsx]"]
  node_client_providers["Providers<br/>state wiring<br/>[AppProviders.jsx]"]
  node_client_routes["Routes<br/>route guards<br/>[AppRoutes.jsx]"]
  node_client_shared["Shared UI<br/>layout/components<br/>[MainLayout.jsx]"]
  node_client_auth["Auth feature"]
  node_client_movies["Movies feature"]
  node_client_comments["Comments feature"]
  node_client_interactions["Interactions<br/>feature"]
  node_client_notifications["Notifications<br/>feature"]
  node_client_reports["Reports<br/>feature"]
  node_client_admin["Admin feature"]
end

subgraph group_server["Server"]
  node_server_bootstrap["Nest app<br/>bootstrap<br/>[main.ts]"]
  node_server_security["Security<br/>jwt/roles<br/>[jwt-auth.guard.ts]"]
  node_server_auth["Auth module<br/>domain module<br/>[auth.module.ts]"]
  node_server_catalog["Catalog modules<br/>domain modules<br/>[movies.module.ts]"]
  node_server_interactions["Interaction modules<br/>domain modules<br/>[comments.module.ts]"]
  node_server_ops["Admin/ops<br/>domain module<br/>[admin.module.ts]"]
  node_server_shared["Shared core<br/>common primitives"]
  node_server_db[("SQL schema<br/>database<br/>[schema.sql]")]
  node_server_mail["Mail service<br/>infrastructure<br/>[mail.service.ts]"]
end

subgraph group_infra["Infra"]
  node_infra_dev["Dev container<br/>docker tooling<br/>[docker-compose.yml]"]
end

node_client_entry -->|"wraps"| node_client_providers
node_client_entry -->|"renders"| node_client_routes
node_client_providers -->|"auth state"| node_client_auth
node_client_providers -->|"guards"| node_client_routes
node_client_routes -->|"layouts"| node_client_shared
node_client_routes -->|"public"| node_client_auth
node_client_routes -->|"browse"| node_client_movies
node_client_routes -->|"protected"| node_client_interactions
node_client_routes -->|"admin"| node_client_admin
node_client_movies -->|"REST calls"| node_server_bootstrap
node_client_auth -->|"REST calls"| node_server_bootstrap
node_client_comments -->|"REST calls"| node_server_bootstrap
node_client_interactions -->|"REST calls"| node_server_bootstrap
node_client_notifications -->|"REST calls"| node_server_bootstrap
node_client_reports -->|"REST calls"| node_server_bootstrap
node_client_admin -->|"REST calls"| node_server_bootstrap
node_server_bootstrap -->|"enforces"| node_server_security
node_server_bootstrap -->|"composes"| node_server_auth
node_server_bootstrap -->|"composes"| node_server_catalog
node_server_bootstrap -->|"composes"| node_server_interactions
node_server_bootstrap -->|"composes"| node_server_ops
node_server_auth -->|"persists"| node_server_db
node_server_catalog -->|"reads/writes"| node_server_db
node_server_interactions -->|"reads/writes"| node_server_db
node_server_ops -->|"reads/writes"| node_server_db
node_server_auth -->|"sends mail"| node_server_mail
node_server_ops -->|"sends mail"| node_server_mail
node_server_shared -->|"shared by"| node_server_auth
node_server_shared -->|"shared by"| node_server_catalog
node_server_shared -->|"shared by"| node_server_interactions
node_server_shared -->|"shared by"| node_server_ops
node_infra_dev -->|"provisions"| node_server_db
node_infra_dev -.->|"hosts"| node_client_entry

click node_client_entry "https://github.com/hung123-alt/ktpm_4_2026/blob/main/client/src/main.jsx"
click node_client_providers "https://github.com/hung123-alt/ktpm_4_2026/blob/main/client/src/providers/AppProviders.jsx"
click node_client_routes "https://github.com/hung123-alt/ktpm_4_2026/blob/main/client/src/routes/AppRoutes.jsx"
click node_client_shared "https://github.com/hung123-alt/ktpm_4_2026/blob/main/client/src/shared/layout/MainLayout.jsx"
click node_client_auth "https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/auth"
click node_client_movies "https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/movies"
click node_client_comments "https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/comments"
click node_client_interactions "https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/interactions"
click node_client_notifications "https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/notifications"
click node_client_reports "https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/reports"
click node_client_admin "https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/admin"
click node_server_bootstrap "https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/main.ts"
click node_server_security "https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/common/guards/jwt-auth.guard.ts"
click node_server_auth "https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/modules/auth/auth.module.ts"
click node_server_catalog "https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/modules/movies/movies.module.ts"
click node_server_interactions "https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/modules/comments/comments.module.ts"
click node_server_ops "https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/modules/admin/admin.module.ts"
click node_server_shared "https://github.com/hung123-alt/ktpm_4_2026/tree/main/server/src/common"
click node_server_db "https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/database/schema.sql"
click node_server_mail "https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/mail/mail.service.ts"
click node_infra_dev "https://github.com/hung123-alt/ktpm_4_2026/blob/main/.devcontainer/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_client_entry,node_client_providers,node_client_routes,node_client_shared,node_client_auth,node_client_movies,node_client_comments,node_client_interactions,node_client_notifications,node_client_reports,node_client_admin toneBlue
class node_server_bootstrap,node_server_security,node_server_auth,node_server_catalog,node_server_interactions,node_server_ops,node_server_shared,node_server_db,node_server_mail toneAmber
class node_infra_dev toneMint
```

## Components

### Client

React/Vite frontend

- [App shell](https://github.com/hung123-alt/ktpm_4_2026/blob/main/client/src/main.jsx): react shell · Bootstraps the SPA and composes global providers and routes. · `client/src/main.jsx`
- [Providers](https://github.com/hung123-alt/ktpm_4_2026/blob/main/client/src/providers/AppProviders.jsx): state wiring · Auth, theme, and query-client context used across the app. · `client/src/providers/AppProviders.jsx`
- [Routes](https://github.com/hung123-alt/ktpm_4_2026/blob/main/client/src/routes/AppRoutes.jsx): route guards · Public, protected, and admin routing with access checks. · `client/src/routes/AppRoutes.jsx`
- [Shared UI](https://github.com/hung123-alt/ktpm_4_2026/blob/main/client/src/shared/layout/MainLayout.jsx): layout/components · Reusable layouts and controls shared across features. · `client/src/shared/layout/MainLayout.jsx`
- [Auth feature](https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/auth): feature · Login, registration, profile, and password recovery flows. · `client/src/features/auth`
- [Movies feature](https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/movies): feature · Browse, search, filter, detail, watch, and random movie pages. · `client/src/features/movies`
- [Comments feature](https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/comments): feature · Comment listing and mutation hooks around movie discussions. · `client/src/features/comments`
- [Interactions](https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/interactions): feature · Favorites, watchlist, ratings, comments, and watch history UI. · `client/src/features/interactions`
- [Notifications](https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/notifications): feature · User notification retrieval and display hooks. · `client/src/features/notifications`
- [Reports](https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/reports): feature · Error/report submission and tracking flows. · `client/src/features/reports`
- [Admin feature](https://github.com/hung123-alt/ktpm_4_2026/tree/main/client/src/features/admin): feature · Management screens for users, movies, comments, genres, countries, and reports. · `client/src/features/admin`

### Server

NestJS REST API

- [Nest app](https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/main.ts): bootstrap · NestJS entrypoint and root module composition. · `server/src/main.ts`
- [Security](https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/common/guards/jwt-auth.guard.ts): jwt/roles · JWT auth strategy plus guards and role decorators for access control. · `server/src/common/guards/jwt-auth.guard.ts`
- [Auth module](https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/modules/auth/auth.module.ts): domain module · Login, registration, password reset, refresh tokens, and JWT issuance. · `server/src/modules/auth/auth.module.ts`
- [Catalog modules](https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/modules/movies/movies.module.ts): domain modules · Movies, episodes, genres, and countries for catalog and playback metadata. · `server/src/modules/movies/movies.module.ts`
- [Interaction modules](https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/modules/comments/comments.module.ts): domain modules · Comments, ratings, favorites, watchlist, and watch-history subsystems. · `server/src/modules/comments/comments.module.ts`
- [Admin/ops](https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/modules/admin/admin.module.ts): domain module · Admin actions, notifications, and user-facing error reports. · `server/src/modules/admin/admin.module.ts`
- [Shared core](https://github.com/hung123-alt/ktpm_4_2026/tree/main/server/src/common): common primitives · DTOs, enums, interfaces, slug utility, and current-user/roles helpers. · `server/src/common`
- [SQL schema](https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/database/schema.sql): database · Relational schema and seed data backing the API. · `server/src/database/schema.sql`
- [Mail service](https://github.com/hung123-alt/ktpm_4_2026/blob/main/server/src/mail/mail.service.ts): infrastructure · Email delivery for password recovery and notifications. · `server/src/mail/mail.service.ts`

### Infra

Local dev and deployment support

- [Dev container](https://github.com/hung123-alt/ktpm_4_2026/blob/main/.devcontainer/docker-compose.yml): docker tooling · Containerized local environment with SQL Server setup. · `.devcontainer/docker-compose.yml`

## Connections

- App shell → Providers: wraps · App shell installs global state providers.
- App shell → Routes: renders · Top-level routing is mounted from the app shell.
- Providers → Auth feature: auth state · Auth context is consumed by auth and protected flows.
- Providers → Routes: guards · Route decisions depend on provider state.
- Routes → Shared UI: layouts · Routes compose shared application layouts.
- Routes → Auth feature: public · Authentication screens are exposed through public routes.
- Routes → Movies feature: browse · Core content browsing and playback screens.
- Routes → Interactions: protected · User-specific lists and histories require login.
- Routes → Admin feature: admin · Privileged management screens are separately gated.
- Movies feature → Nest app: REST calls · Movie pages fetch data from the API.
- Auth feature → Nest app: REST calls · Authentication flows call backend endpoints.
- Comments feature → Nest app: REST calls · Comment hooks and pages talk to the API.
- Interactions → Nest app: REST calls · Favorites, ratings, watchlist, and history use API endpoints.
- Notifications → Nest app: REST calls · Notification data is loaded from the API.
- Reports → Nest app: REST calls · Report submission and status flows use the API.
- Admin feature → Nest app: REST calls · Management screens operate against admin endpoints.
- Nest app → Security: enforces · Controllers rely on JWT and role guards.
- Nest app → Auth module: composes · Root module wires authentication into the app.
- Nest app → Catalog modules: composes · Catalog-related modules are registered in the app.
- Nest app → Interaction modules: composes · User interaction modules are registered in the app.
- Nest app → Admin/ops: composes · Admin and operational modules are registered in the app.
- Auth module → SQL schema: persists · Users and refresh tokens are stored in the relational database.
- Catalog modules → SQL schema: reads/writes · Catalog data is backed by relational tables.
- Interaction modules → SQL schema: reads/writes · User interactions are persisted in SQL tables.
- Admin/ops → SQL schema: reads/writes · Admin operations and notifications persist to the database.
- Auth module → Mail service: sends mail · Password recovery and related flows trigger email delivery.
- Admin/ops → Mail service: sends mail · Operational notifications can use mail delivery.
- Shared core → Auth module: shared by · Common decorators and enums support auth.
- Shared core → Catalog modules: shared by · Common utilities and enums are reused across catalog modules.
- Shared core → Interaction modules: shared by · Common primitives support interaction modules.
- Shared core → Admin/ops: shared by · Shared helpers support admin and reporting flows.
- Dev container → SQL schema: provisions · Dev container brings up SQL Server and initialization scripts.
- Dev container → App shell: hosts · Containerized development supports the frontend build environment.
