0Mart00
Verbumix rebrand: emojis removed, multi-jurisdictional legal docs, cookie consent, footer links
9d4e305 |
Download ARCHITECTURE_REFACTOR_USER_DASHBOARD.md from humut/WordGame: direct link, hf CLI and curl.
- Browser
- Download file 13.3 kB
-
https://huggingface.co/spaces/humut/WordGame/resolve/main/ARCHITECTURE_REFACTOR_USER_DASHBOARD.md
- Command line
-
hf download hf://spaces/humut/WordGame/ARCHITECTURE_REFACTOR_USER_DASHBOARD.md
-
curl -L -o ARCHITECTURE_REFACTOR_USER_DASHBOARD.md https://huggingface.co/spaces/humut/WordGame/resolve/main/ARCHITECTURE_REFACTOR_USER_DASHBOARD.md
13.3 kB
| # Feature: User Dashboard - Architecture Refactor Proposal | |
| ## Overview | |
| This document outlines the refactored architecture for the `feature/user-dashboard` module, which includes: | |
| 1. **Subscription Status Display** - Show premium status, renewal dates, upgrade options | |
| 2. **Custom Deck Creation** - AI-generated word pools for personalized games | |
| 3. **Invite Link Generator** - Create shareable room invite links | |
| The architecture strictly enforces modularity, separation of concerns, and scalability. | |
| --- | |
| ## Recommended File Tree Structure | |
| ``` | |
| wordGame/ | |
| βββ ... (existing files) | |
| βββ features/ | |
| βββ user_dashboard/ | |
| βββ __init__.py # Package initialization, router registration | |
| βββ router.py # API route definitions (max 50 lines) | |
| β | |
| βββ services/ # Business logic layer | |
| β βββ __init__.py | |
| β βββ subscription_service.py # Subscription status, billing info | |
| β βββ deck_service.py # AI deck generation logic | |
| β βββ invite_service.py # Invite link creation & validation | |
| β | |
| βββ repositories/ # Data access layer | |
| β βββ __init__.py | |
| β βββ subscription_repo.py # DB queries for subscriptions | |
| β βββ deck_repo.py # DB queries for decks | |
| β βββ invite_repo.py # DB queries for invites | |
| β | |
| βββ schemas/ # Pydantic models for validation | |
| β βββ __init__.py | |
| β βββ subscription_schemas.py # Request/Response models | |
| β βββ deck_schemas.py | |
| β βββ invite_schemas.py | |
| β | |
| βββ utils/ # Shared utilities | |
| β βββ __init__.py | |
| β βββ link_generator.py # Secure token/link generation | |
| β βββ ai_prompt_builder.py # AI prompt construction | |
| β βββ validators.py # Input validation helpers | |
| β | |
| βββ templates/ # Feature-specific templates | |
| βββ __init__.py # (optional, for template config) | |
| βββ dashboard.html # Main dashboard layout | |
| βββ subscription_panel.html # Subscription status component | |
| βββ deck_creator.html # Deck creation UI | |
| βββ invite_generator.html # Invite link generator UI | |
| ``` | |
| --- | |
| ## File Responsibilities & Line Limits | |
| ### Core Module Files | |
| #### `__init__.py` (max 30 lines) | |
| ```python | |
| # Purpose: Package initialization and router registration | |
| # Responsibilities: | |
| # - Import and expose the router | |
| # - Define module metadata | |
| # - Optional: Register middleware specific to this feature | |
| from .router import router | |
| __all__ = ["router"] | |
| ``` | |
| #### `router.py` (max 50 lines) | |
| ```python | |
| # Purpose: API endpoint definitions | |
| # Responsibilities: | |
| # - Define all HTTP routes for the dashboard | |
| # - Delegate to service layer (NO business logic) | |
| # - Handle request validation via schemas | |
| # - Return appropriate HTTP responses | |
| # Routes: | |
| # GET /dashboard - Main dashboard page | |
| # GET /dashboard/subscription - Subscription status | |
| # POST /dashboard/deck - Create new AI deck | |
| # GET /dashboard/deck/{id} - Get deck details | |
| # POST /dashboard/invite - Generate invite link | |
| # GET /dashboard/invite/{code} - Validate/redeem invite | |
| ``` | |
| --- | |
| ### Services Layer (`services/`) | |
| #### `subscription_service.py` (max 150 lines) | |
| ```python | |
| # Purpose: Subscription business logic | |
| # Responsibilities: | |
| # - Check subscription status (active, expired, trial) | |
| # - Calculate renewal dates | |
| # - Determine upgrade/downgrade eligibility | |
| # - Format subscription data for display | |
| # - Handle subscription cancellation logic | |
| # Dependencies: subscription_repo, utils/validators | |
| # Does NOT: Direct DB access (uses repo), HTML generation | |
| ``` | |
| #### `deck_service.py` (max 180 lines) | |
| ```python | |
| # Purpose: AI deck generation business logic | |
| # Responsibilities: | |
| # - Build AI prompts based on user preferences | |
| # - Process AI API responses | |
| # - Validate generated word pools | |
| # - Manage deck lifecycle (create, update, delete) | |
| # - Enforce deck limits based on subscription tier | |
| # Dependencies: deck_repo, ai_prompt_builder, validators | |
| # Does NOT: Direct DB access, AI API calls (abstracted), HTML generation | |
| ``` | |
| #### `invite_service.py` (max 120 lines) | |
| ```python | |
| # Purpose: Invite link business logic | |
| # Responsibilities: | |
| # - Generate unique, secure invite codes | |
| # - Set expiration policies | |
| # - Track invite usage statistics | |
| # - Validate invite codes | |
| # - Handle invite redemption logic | |
| # Dependencies: invite_repo, link_generator, validators | |
| # Does NOT: Direct DB access, HTML generation | |
| ``` | |
| --- | |
| ### Repositories Layer (`repositories/`) | |
| #### `subscription_repo.py` (max 100 lines) | |
| ```python | |
| # Purpose: Subscription data access | |
| # Responsibilities: | |
| # - Query user subscription from Supabase | |
| # - Update subscription status | |
| # - Cache subscription data (optional) | |
| # - Handle data transformation from DB format | |
| # Dependencies: database.py (supabase client) | |
| # Does NOT: Business logic, validation beyond DB constraints | |
| ``` | |
| #### `deck_repo.py` (max 120 lines) | |
| ```python | |
| # Purpose: Deck data access | |
| # Responsibilities: | |
| # - CRUD operations for custom decks | |
| # - Query decks by user, category, date | |
| # - Store AI-generated word pools | |
| # - Handle deck-word relationships | |
| # Dependencies: database.py | |
| # Does NOT: Business logic, AI processing | |
| ``` | |
| #### `invite_repo.py` (max 80 lines) | |
| ```python | |
| # Purpose: Invite data access | |
| # Responsibilities: | |
| # - Store invite codes with metadata | |
| # - Track usage (used_by, used_at) | |
| # - Query active/expired invites | |
| # - Soft-delete expired invites | |
| # Dependencies: database.py | |
| # Does NOT: Business logic, code generation | |
| ``` | |
| --- | |
| ### Schemas Layer (`schemas/`) | |
| #### `subscription_schemas.py` (max 80 lines) | |
| ```python | |
| # Purpose: Subscription data validation | |
| # Models: | |
| # - SubscriptionResponse (id, status, renewal_date, tier) | |
| # - UpgradeRequest (target_tier) | |
| # - CancelSubscriptionResponse | |
| ``` | |
| #### `deck_schemas.py` (max 100 lines) | |
| ```python | |
| # Purpose: Deck data validation | |
| # Models: | |
| # - DeckCreateRequest (category, difficulty, word_count, ai_params) | |
| # - DeckResponse (id, name, words, created_at, word_count) | |
| # - DeckListResponse (decks: list[DeckResponse], total) | |
| ``` | |
| #### `invite_schemas.py` (max 60 lines) | |
| ```python | |
| # Purpose: Invite data validation | |
| # Models: | |
| # - InviteCreateRequest (room_id, expires_in_hours) | |
| # - InviteResponse (code, url, expires_at, max_uses) | |
| # - InviteValidateResponse (valid, room_info, creator) | |
| ``` | |
| --- | |
| ### Utilities Layer (`utils/`) | |
| #### `link_generator.py` (max 80 lines) | |
| ```python | |
| # Purpose: Secure link/code generation | |
| # Responsibilities: | |
| # - Generate cryptographically secure tokens | |
| # - Create URL-safe invite codes | |
| # - Encode/decode invite payloads | |
| # - Validate code format | |
| # Does NOT: Database operations, business rules | |
| ``` | |
| #### `ai_prompt_builder.py` (max 100 lines) | |
| ```python | |
| # Purpose: AI prompt construction | |
| # Responsibilities: | |
| # - Build structured prompts for word generation | |
| # - Include category-specific constraints | |
| # - Format user preferences into prompt context | |
| # - Parse and validate AI responses | |
| # Does NOT: API calls, database operations | |
| ``` | |
| #### `validators.py` (max 100 lines) | |
| ```python | |
| # Purpose: Shared validation utilities | |
| # Responsibilities: | |
| # - Validate user IDs, room codes | |
| # - Check date ranges, expiration | |
| # - Validate word format (length, characters) | |
| # - Common input sanitization | |
| # Does NOT: Business logic, DB queries | |
| ``` | |
| --- | |
| ### Templates Layer (`templates/`) | |
| #### `dashboard.html` (max 100 lines) | |
| ```html | |
| <!-- Purpose: Main dashboard layout --> | |
| <!-- Includes: Navigation, user greeting, feature summary cards --> | |
| <!-- Loads: subscription_panel, deck_creator, invite_generator via HTMX --> | |
| ``` | |
| #### `subscription_panel.html` (max 80 lines) | |
| ```html | |
| <!-- Purpose: Subscription status display --> | |
| <!-- Shows: Current tier, renewal date, upgrade button, usage stats --> | |
| <!-- Actions: Upgrade, cancel, view billing --> | |
| ``` | |
| #### `deck_creator.html` (max 120 lines) | |
| ```html | |
| <!-- Purpose: AI deck creation UI --> | |
| <!-- Form: Category selection, difficulty, word count, custom params --> | |
| <!-- Display: Generated deck preview, save button --> | |
| ``` | |
| #### `invite_generator.html` (max 80 lines) | |
| ```html | |
| <!-- Purpose: Invite link generation UI --> | |
| <!-- Form: Room selection, expiration settings --> | |
| <!-- Display: Generated link, copy button, usage stats --> | |
| ``` | |
| --- | |
| ## Data Flow Diagrams | |
| ### Subscription Status Flow | |
| ``` | |
| User Request router.py (GET /dashboard/subscription) | |
| subscription_service.get_status(user_id) | |
| subscription_repo.fetch_subscription(user_id) | |
| Supabase DB | |
| subscription_service.format_response(data) | |
| router.py returns JSON/HTML | |
| ``` | |
| ### Deck Creation Flow | |
| ``` | |
| User Request router.py (POST /dashboard/deck) | |
| deck_schemas.DeckCreateRequest.validate(data) | |
| deck_service.create_deck(user_id, params) | |
| ai_prompt_builder.build_prompt(params) | |
| [AI API Call - external service] | |
| deck_service.validate_words(ai_response) | |
| deck_repo.save_deck(user_id, words) | |
| Supabase DB | |
| router.py returns DeckResponse | |
| ``` | |
| ### Invite Generation Flow | |
| ``` | |
| User Request router.py (POST /dashboard/invite) | |
| invite_schemas.InviteCreateRequest.validate(data) | |
| invite_service.generate_invite(user_id, room_id, expires) | |
| link_generator.create_secure_code() | |
| invite_repo.save_invite(code, metadata) | |
| Supabase DB | |
| invite_service.build_invite_url(code) | |
| router.py returns InviteResponse | |
| ``` | |
| --- | |
| ## Scalability Considerations | |
| ### Adding New Pool Types | |
| 1. Create new schema in `schemas/deck_schemas.py` (e.g., `CustomPoolParams`) | |
| 2. Extend `ai_prompt_builder.py` with new prompt templates | |
| 3. Add validation rules in `utils/validators.py` | |
| 4. **No changes needed** to existing services or repos | |
| ### Adding Analytics | |
| 1. Create `services/analytics_service.py` | |
| 2. Create `repositories/analytics_repo.py` | |
| 3. Create `schemas/analytics_schemas.py` | |
| 4. Add routes to `router.py` | |
| 5. **No changes needed** to existing services | |
| ### Adding New Subscription Tiers | |
| 1. Update `schemas/subscription_schemas.py` with new tier enum | |
| 2. Update `services/subscription_service.py` tier logic | |
| 3. **No changes needed** to repos or routers | |
| --- | |
| ## File Size Enforcement Strategy | |
| If any file approaches 200 lines: | |
| ### For Services | |
| - Extract helper functions to `utils/` | |
| - Split by aggregate root (e.g., `deck_service.py` `deck_create_service.py`, `deck_manage_service.py`) | |
| - Move complex validation to `utils/validators.py` | |
| ### For Repositories | |
| - Split by query complexity (simple queries vs. complex joins) | |
| - Extract query builders to separate modules | |
| ### For Templates | |
| - Extract reusable components to `templates/components/` | |
| - Use HTMX to load partial views | |
| --- | |
| ## Integration with Existing Codebase | |
| ### Main Application (`main.py`) | |
| ```python | |
| # Add to imports | |
| from features.user_dashboard import router as dashboard_router | |
| # Add to app setup | |
| app.include_router(dashboard_router, prefix="/dashboard") | |
| ``` | |
| ### Database (`database.py`) | |
| - No changes needed - existing supabase client is reused | |
| ### Authentication | |
| - Reuse existing `get_current_user` from `main.py` | |
| - Add as dependency to dashboard routes | |
| --- | |
| ## Testing Strategy | |
| ``` | |
| tests/ | |
| βββ features/ | |
| βββ user_dashboard/ | |
| βββ test_subscription_service.py | |
| βββ test_deck_service.py | |
| βββ test_invite_service.py | |
| βββ test_link_generator.py | |
| βββ test_ai_prompt_builder.py | |
| ``` | |
| **Key Testing Principles:** | |
| - Unit tests for services (mock repos) | |
| - Integration tests for repos (test DB queries) | |
| - Schema validation tests | |
| - Utility function tests | |
| --- | |
| ## Migration Plan | |
| ### Phase 1: Foundation | |
| 1. Create directory structure | |
| 2. Implement `schemas/` layer | |
| 3. Implement `utils/` layer | |
| 4. Set up basic `router.py` skeleton | |
| ### Phase 2: Data Layer | |
| 1. Implement `repositories/` layer | |
| 2. Create database tables (if needed) | |
| 3. Write repository tests | |
| ### Phase 3: Business Logic | |
| 1. Implement `services/` layer | |
| 2. Write service tests | |
| 3. Integrate with existing auth | |
| ### Phase 4: UI Layer | |
| 1. Create templates | |
| 2. Wire up HTMX interactions | |
| 3. Test user flows | |
| ### Phase 5: Integration | |
| 1. Register router in `main.py` | |
| 2. End-to-end testing | |
| 3. Deploy to staging | |
| --- | |
| ## Summary | |
| This architecture provides: | |
| - **Separation of Concerns**: Clear layers (routes services repos DB) | |
| - **Single Responsibility**: Each file has one well-defined purpose | |
| - **Scalability**: Easy to add new features without modifying existing code | |
| - **Maintainability**: Files stay under 200 lines through logical splitting | |
| - **Testability**: Each layer can be tested in isolation | |
| - **Reusability**: Utilities can be shared across features | |
| The structure follows clean architecture principles while remaining pragmatic for a FastAPI application. |