Commit Graph

1 Commits

Author SHA1 Message Date
Oleg Proskurin 1ad5b483ef feat: phase 3 part 1 - live scopes database schema and service
Implement foundation for live URL system with database schema, relations,
and comprehensive service layer for live scope management.

**Database Schema (Section 8.4):**
- **liveScopes table** with fields:
  - id (UUID primary key)
  - projectId (foreign key to projects, cascade delete)
  - slug (text, unique within project)
  - allowNewGenerations (boolean, default: true)
  - newGenerationsLimit (integer, default: 30)
  - meta (JSONB for flexible metadata)
  - createdAt, updatedAt timestamps

- **Constraints & Indexes:**
  - Unique constraint on (projectId, slug) combination
  - Index for project lookups
  - Index for project+slug lookups

- **Relations:**
  - projects → liveScopes (one-to-many)
  - liveScopes → project (many-to-one)

**Type Definitions:**
- Added LiveScope, NewLiveScope types from database schema
- Added LiveScopeWithStats interface with computed fields:
  - currentGenerations: number (count of images in scope)
  - lastGeneratedAt: Date | null (latest generation timestamp)
  - images?: Image[] (optional images array)
- Added LiveScopeFilters interface for query filtering

**LiveScopeService:**
- **Core CRUD operations:**
  - create() - Create new scope
  - getById() - Get scope by UUID
  - getBySlug() - Get scope by slug within project
  - getByIdOrThrow() / getBySlugOrThrow() - With error handling
  - update() - Update scope settings
  - delete() - Hard delete scope (images preserved)

- **Statistics & Computed Fields:**
  - getByIdWithStats() - Scope with generation count and last generated date
  - getBySlugWithStats() - Same but lookup by slug
  - list() - Paginated list with stats for each scope

- **Business Logic:**
  - canGenerateNew() - Check if scope allows new generations
  - createOrGet() - Lazy creation with project defaults
  - Uses images.meta field for scope tracking (scope, isLiveUrl)

**Technical Notes:**
- Images track scope via meta.scope field (no schema changes to images table)
- Scope statistics computed from images where meta.scope = slug and meta.isLiveUrl = true
- Project settings (allowNewLiveScopes, newLiveScopesGenerationLimit) already added in Phase 2
- All code compiles with zero new TypeScript errors

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-17 22:50:20 +07:00