# jinjutroy/chieu-studio-be architecture

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

- Interactive diagram: https://gitdiagram.com/jinjutroy/chieu-studio-be
- Repository: https://github.com/jinjutroy/chieu-studio-be

## Overview

#### Purpose and Runtime
LensFlow is a backend-only studio workflow API for customer, album, package, authentication, and lifecycle management. It is a TypeScript NestJS service with a separate maintenance worker. The HTTP entry point is `src/server/main.ts`; application composition is `src/server/app.module.ts`; scheduled/background maintenance begins under `src/worker/maintenance/maintenance.module.ts`.

#### API Contract Boundary
HTTP controllers expose the service surface for authentication, customers/packages, albums, sessions, and health. OpenAPI is maintained as a repository contract and generated/exported from the server integration.
- `src/server/auth/auth.controller.ts`
- `src/server/customers/customers.controller.ts`
- `src/server/albums/albums.controller.ts`
- `openapi.yaml`
- `src/server/openapi.ts`

Shared Zod schemas, permissions, state-machine definitions, error codes, and notification types form a cross-boundary contract consumed by server code and contract tests.
- `src/shared/auth.schemas.ts`
- `src/shared/state-machine.ts`
- `src/shared/permissions.ts`

#### Domain Modules and Business Flow
Nest feature modules use controller → service → persistence flow. Customer and package management feed album workflows, including customer-scoped album access. Auth establishes identities and sessions; guards enforce authenticated, live-session, and role-based access before protected domain operations.
- `src/server/customers/customers.module.ts`
- `src/server/customers/packages.service.ts`
- `src/server/albums/customer-albums.controller.ts`
- `src/server/auth/guards/auth.guard.ts`
- `src/server/auth/guards/roles.guard.ts`

#### Persistence and Audit Boundary
Prisma is the database abstraction and migration authority. The schema/migrations define durable entities and roles; services access it through the Prisma module. Audit is isolated as a dedicated module/service, supporting append-only accountability rather than being embedded in each domain controller.
- `prisma/schema.prisma`
- `prisma/migrations/20260705000001_init/migration.sql`
- `src/server/prisma/prisma.service.ts`
- `src/server/audit/audit.service.ts`

#### Security and Session Lifecycle
Authentication is a distinct subsystem: token issuance, refresh/session endpoints, cookie handling, lockout behavior, password-reset tokens, and request context. Live-session validation makes session revocation/expiry an explicit authorization boundary, not merely a token-verification concern.
- `src/server/auth/auth.service.ts`
- `src/server/auth/token.service.ts`
- `src/server/auth/lockout.service.ts`
- `src/server/auth/sessions.controller.ts`

#### Infrastructure and External Services
PostgreSQL is implied by Prisma migrations; Redis backs the server’s Redis module and queue infrastructure. Storage and mail delivery are adapter-style modules behind services. Docker Compose/Dockerfiles provide containerized operation, while backup scripts implement database backup, restore, health checks, and notifications.
- `src/server/redis/redis.module.ts`
- `src/server/queue/queue.module.ts`
- `src/server/storage/storage.service.ts`
- `src/server/mailer/mailer.service.ts`
- `docker-compose.yml`
- `scripts/backup/backup.sh`

#### Background Maintenance and Operational Controls
The worker maintenance subsystem schedules and processes purge operations independently of request handling. Health endpoints, centralized configuration validation, structured logging, exception normalization, and Zod request validation establish operational and API-safety boundaries.
- `src/worker/maintenance/maintenance.scheduler.ts`
- `src/worker/maintenance/purge.processor.ts`
- `src/server/health/health.controller.ts`
- `src/server/config/env.schema.ts`
- `src/server/common/all-exceptions.filter.ts`

Main entry points: [`src/server/customers/customers.controller.ts`](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/customers/customers.controller.ts) (HTTP controllers), [`src/server/auth/auth.service.ts`](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/auth/auth.service.ts) (Authentication service), [`src/server/prisma/prisma.service.ts`](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/prisma/prisma.service.ts) (Prisma service), [`src/server/app.module.ts`](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/app.module.ts) (Application module)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_server["HTTP Server"]
  node_main["Server entry point<br/>Nest bootstrap<br/>[main.ts]"]
  node_app["Application module<br/>Nest composition<br/>[app.module.ts]"]
  node_controllers["HTTP controllers<br/>API controllers"]
  node_openapi["OpenAPI contract<br/>API specification<br/>[openapi.yaml]"]
  node_openapi_export["OpenAPI export<br/>server integration<br/>[openapi.ts]"]
  node_request_safety["Request safety<br/>validation and errors"]
  node_contracts["Shared contracts<br/>Zod and policy definitions<br/>[auth.schemas.ts]"]
end

subgraph group_domain["Domain Services"]
  node_customer_module["Customer &amp; package module<br/>Nest feature module"]
  node_packages["Package service<br/>domain service"]
  node_customer_albums["Customer album access<br/>album controller"]
end

subgraph group_security["Auth &amp; Access"]
  node_auth_service["Authentication service<br/>identity service<br/>[auth.service.ts]"]
  node_tokens["Token service<br/>token lifecycle<br/>[token.service.ts]"]
  node_access_guards["Access guards<br/>authorization guards<br/>[auth.guard.ts]"]
end

subgraph group_data["Data &amp; Integrations"]
  node_prisma[("Prisma service<br/>database access<br/>[prisma.service.ts]")]
  node_schema["Database schema &amp; migrations<br/>Prisma authority<br/>[schema.prisma]"]
  node_audit["Audit service<br/>append-only audit<br/>[audit.service.ts]"]
  node_redis_queue["Redis queue<br/>queue infrastructure<br/>[queue.module.ts]"]
  node_adapters["Storage &amp; mail adapters<br/>external integrations<br/>[storage.service.ts]"]
end

subgraph group_operations["Operations"]
  node_maintenance["Maintenance worker<br/>scheduled worker"]
  node_purge["Purge processor<br/>background processor<br/>[purge.processor.ts]"]
  node_ops_controls["Operational controls<br/>health and configuration<br/>[backup.sh]"]
end

node_main -->|"bootstraps"| node_app
node_app -->|"mounts"| node_controllers
node_controllers -->|"protects routes with"| node_access_guards
node_controllers -->|"validated and normalized by"| node_request_safety
node_openapi_export -->|"exports"| node_openapi
node_app -->|"integrates"| node_openapi_export
node_controllers -->|"uses"| node_contracts
node_controllers -->|"dispatches customer operations"| node_customer_module
node_controllers -->|"dispatches album operations"| node_customer_albums
node_controllers -->|"dispatches auth and session operations"| node_auth_service
node_customer_module -->|"contains"| node_packages
node_customer_module -->|"persists through"| node_prisma
node_customer_albums -->|"reads and writes through"| node_prisma
node_customer_module -->|"records changes"| node_audit
node_auth_service -->|"issues and refreshes via"| node_tokens
node_access_guards -->|"checks live session state"| node_auth_service
node_auth_service -->|"stores identities and sessions"| node_prisma
node_prisma -->|"implements"| node_schema
node_app -->|"configures"| node_redis_queue
node_auth_service -->|"sends reset mail through"| node_adapters
node_maintenance -->|"schedules"| node_purge
node_purge -->|"processes queued work"| node_redis_queue
node_purge -->|"purges durable records"| node_prisma
node_ops_controls -.->|"monitors and supports"| node_main

click node_main "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/main.ts"
click node_app "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/app.module.ts"
click node_controllers "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/customers/customers.controller.ts"
click node_openapi "https://github.com/jinjutroy/chieu-studio-be/blob/main/openapi.yaml"
click node_openapi_export "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/openapi.ts"
click node_request_safety "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/common/all-exceptions.filter.ts"
click node_contracts "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/shared/auth.schemas.ts"
click node_customer_module "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/customers/customers.module.ts"
click node_packages "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/customers/packages.service.ts"
click node_customer_albums "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/albums/customer-albums.controller.ts"
click node_auth_service "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/auth/auth.service.ts"
click node_tokens "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/auth/token.service.ts"
click node_access_guards "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/auth/guards/auth.guard.ts"
click node_prisma "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/prisma/prisma.service.ts"
click node_schema "https://github.com/jinjutroy/chieu-studio-be/blob/main/prisma/schema.prisma"
click node_audit "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/audit/audit.service.ts"
click node_redis_queue "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/queue/queue.module.ts"
click node_adapters "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/storage/storage.service.ts"
click node_maintenance "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/worker/maintenance/maintenance.module.ts"
click node_purge "https://github.com/jinjutroy/chieu-studio-be/blob/main/src/worker/maintenance/purge.processor.ts"
click node_ops_controls "https://github.com/jinjutroy/chieu-studio-be/blob/main/scripts/backup/backup.sh"

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_main,node_app,node_controllers,node_openapi,node_openapi_export,node_request_safety,node_contracts toneBlue
class node_customer_module,node_packages,node_customer_albums toneAmber
class node_auth_service,node_tokens,node_access_guards toneMint
class node_prisma,node_schema,node_audit,node_redis_queue,node_adapters toneRose
class node_maintenance,node_purge,node_ops_controls toneIndigo
```

## Components

### HTTP Server

NestJS request runtime and API boundary.

- [Server entry point](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/main.ts): Nest bootstrap · `src/server/main.ts`
- [Application module](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/app.module.ts): Nest composition · Composes HTTP feature and infrastructure modules. · `src/server/app.module.ts`
- [HTTP controllers](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/customers/customers.controller.ts): API controllers · Auth, customer, album, session, and health endpoints. · `src/server/customers/customers.controller.ts`
- [OpenAPI contract](https://github.com/jinjutroy/chieu-studio-be/blob/main/openapi.yaml): API specification · Published repository API contract. · `openapi.yaml`
- [OpenAPI export](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/openapi.ts): server integration · `src/server/openapi.ts`
- [Request safety](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/common/all-exceptions.filter.ts): validation and errors · Zod validation and normalized exception responses. · `src/server/common/all-exceptions.filter.ts`
- [Shared contracts](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/shared/auth.schemas.ts): Zod and policy definitions · Schemas, permissions, and lifecycle state definitions. · `src/shared/auth.schemas.ts`

### Domain Services

Customer, package, and album workflows.

- [Customer & package module](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/customers/customers.module.ts): Nest feature module · Manages customers and packages used by album workflows. · `src/server/customers/customers.module.ts`
- [Package service](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/customers/packages.service.ts): domain service · `src/server/customers/packages.service.ts`
- [Customer album access](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/albums/customer-albums.controller.ts): album controller · Exposes customer-scoped album operations. · `src/server/albums/customer-albums.controller.ts`

### Auth & Access

Identity, live-session, and authorization controls.

- [Authentication service](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/auth/auth.service.ts): identity service · Handles authentication, refresh, reset, cookies, and sessions. · `src/server/auth/auth.service.ts`
- [Token service](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/auth/token.service.ts): token lifecycle · `src/server/auth/token.service.ts`
- [Access guards](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/auth/guards/auth.guard.ts): authorization guards · Enforces authentication, live sessions, and roles. · `src/server/auth/guards/auth.guard.ts`

### Data & Integrations

Durable storage, audit, and external adapters.

- [Prisma service](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/prisma/prisma.service.ts): database access · `src/server/prisma/prisma.service.ts`
- [Database schema & migrations](https://github.com/jinjutroy/chieu-studio-be/blob/main/prisma/schema.prisma): Prisma authority · Defines durable entities, roles, and migration history. · `prisma/schema.prisma`
- [Audit service](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/audit/audit.service.ts): append-only audit · Records accountability separately from controllers. · `src/server/audit/audit.service.ts`
- [Redis queue](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/queue/queue.module.ts): queue infrastructure · Redis-backed queue infrastructure. · `src/server/queue/queue.module.ts`
- [Storage & mail adapters](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/server/storage/storage.service.ts): external integrations · Adapter services for file storage and mail delivery. · `src/server/storage/storage.service.ts`

### Operations

Independent maintenance and operational safeguards.

- [Maintenance worker](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/worker/maintenance/maintenance.module.ts): scheduled worker · Runs purge maintenance outside the HTTP request path. · `src/worker/maintenance/maintenance.module.ts`
- [Purge processor](https://github.com/jinjutroy/chieu-studio-be/blob/main/src/worker/maintenance/purge.processor.ts): background processor · `src/worker/maintenance/purge.processor.ts`
- [Operational controls](https://github.com/jinjutroy/chieu-studio-be/blob/main/scripts/backup/backup.sh): health and configuration · Health API, validated environment, logging, and backups. · `scripts/backup/backup.sh`

## Connections

- Server entry point → Application module: bootstraps
- Application module → HTTP controllers: mounts
- HTTP controllers → Access guards: protects routes with
- HTTP controllers → Request safety: validated and normalized by
- OpenAPI export → OpenAPI contract: exports
- Application module → OpenAPI export: integrates
- HTTP controllers → Shared contracts: uses
- HTTP controllers → Customer & package module: dispatches customer operations
- HTTP controllers → Customer album access: dispatches album operations
- HTTP controllers → Authentication service: dispatches auth and session operations
- Customer & package module → Package service: contains
- Customer & package module → Prisma service: persists through
- Customer album access → Prisma service: reads and writes through
- Customer & package module → Audit service: records changes
- Authentication service → Token service: issues and refreshes via
- Access guards → Authentication service: checks live session state
- Authentication service → Prisma service: stores identities and sessions
- Prisma service → Database schema & migrations: implements
- Application module → Redis queue: configures
- Authentication service → Storage & mail adapters: sends reset mail through
- Maintenance worker → Purge processor: schedules
- Purge processor → Redis queue: processes queued work
- Purge processor → Prisma service: purges durable records
- Operational controls → Server entry point: monitors and supports
