Spaces:
Build error

0Mart00 commited on
Commit
55152f0
Β·
1 Parent(s): a3ef353

Add user-dashboard feature architecture refactor proposal

Browse files
ARCHITECTURE_REFACTOR_USER_DASHBOARD.md ADDED
@@ -0,0 +1,437 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Feature: User Dashboard - Architecture Refactor Proposal
2
+
3
+ ## Overview
4
+
5
+ This document outlines the refactored architecture for the `feature/user-dashboard` module, which includes:
6
+ 1. **Subscription Status Display** - Show premium status, renewal dates, upgrade options
7
+ 2. **Custom Deck Creation** - AI-generated word pools for personalized games
8
+ 3. **Invite Link Generator** - Create shareable room invite links
9
+
10
+ The architecture strictly enforces modularity, separation of concerns, and scalability.
11
+
12
+ ---
13
+
14
+ ## Recommended File Tree Structure
15
+
16
+ ```
17
+ wordGame/
18
+ β”œβ”€β”€ ... (existing files)
19
+ └── features/
20
+ └── user_dashboard/
21
+ β”œβ”€β”€ __init__.py # Package initialization, router registration
22
+ β”œβ”€β”€ router.py # API route definitions (max 50 lines)
23
+ β”‚
24
+ β”œβ”€β”€ services/ # Business logic layer
25
+ β”‚ β”œβ”€β”€ __init__.py
26
+ β”‚ β”œβ”€β”€ subscription_service.py # Subscription status, billing info
27
+ β”‚ β”œβ”€β”€ deck_service.py # AI deck generation logic
28
+ β”‚ └── invite_service.py # Invite link creation & validation
29
+ β”‚
30
+ β”œβ”€β”€ repositories/ # Data access layer
31
+ β”‚ β”œβ”€β”€ __init__.py
32
+ β”‚ β”œβ”€β”€ subscription_repo.py # DB queries for subscriptions
33
+ β”‚ β”œβ”€β”€ deck_repo.py # DB queries for decks
34
+ β”‚ └── invite_repo.py # DB queries for invites
35
+ β”‚
36
+ β”œβ”€β”€ schemas/ # Pydantic models for validation
37
+ β”‚ β”œβ”€β”€ __init__.py
38
+ β”‚ β”œβ”€β”€ subscription_schemas.py # Request/Response models
39
+ β”‚ β”œβ”€β”€ deck_schemas.py
40
+ β”‚ └── invite_schemas.py
41
+ β”‚
42
+ β”œβ”€β”€ utils/ # Shared utilities
43
+ β”‚ β”œβ”€β”€ __init__.py
44
+ β”‚ β”œβ”€β”€ link_generator.py # Secure token/link generation
45
+ β”‚ β”œβ”€β”€ ai_prompt_builder.py # AI prompt construction
46
+ β”‚ └── validators.py # Input validation helpers
47
+ β”‚
48
+ └── templates/ # Feature-specific templates
49
+ β”œβ”€β”€ __init__.py # (optional, for template config)
50
+ β”œβ”€β”€ dashboard.html # Main dashboard layout
51
+ β”œβ”€β”€ subscription_panel.html # Subscription status component
52
+ β”œβ”€β”€ deck_creator.html # Deck creation UI
53
+ └── invite_generator.html # Invite link generator UI
54
+ ```
55
+
56
+ ---
57
+
58
+ ## File Responsibilities & Line Limits
59
+
60
+ ### Core Module Files
61
+
62
+ #### `__init__.py` (max 30 lines)
63
+ ```python
64
+ # Purpose: Package initialization and router registration
65
+ # Responsibilities:
66
+ # - Import and expose the router
67
+ # - Define module metadata
68
+ # - Optional: Register middleware specific to this feature
69
+ from .router import router
70
+
71
+ __all__ = ["router"]
72
+ ```
73
+
74
+ #### `router.py` (max 50 lines)
75
+ ```python
76
+ # Purpose: API endpoint definitions
77
+ # Responsibilities:
78
+ # - Define all HTTP routes for the dashboard
79
+ # - Delegate to service layer (NO business logic)
80
+ # - Handle request validation via schemas
81
+ # - Return appropriate HTTP responses
82
+ # Routes:
83
+ # GET /dashboard - Main dashboard page
84
+ # GET /dashboard/subscription - Subscription status
85
+ # POST /dashboard/deck - Create new AI deck
86
+ # GET /dashboard/deck/{id} - Get deck details
87
+ # POST /dashboard/invite - Generate invite link
88
+ # GET /dashboard/invite/{code} - Validate/redeem invite
89
+ ```
90
+
91
+ ---
92
+
93
+ ### Services Layer (`services/`)
94
+
95
+ #### `subscription_service.py` (max 150 lines)
96
+ ```python
97
+ # Purpose: Subscription business logic
98
+ # Responsibilities:
99
+ # - Check subscription status (active, expired, trial)
100
+ # - Calculate renewal dates
101
+ # - Determine upgrade/downgrade eligibility
102
+ # - Format subscription data for display
103
+ # - Handle subscription cancellation logic
104
+ # Dependencies: subscription_repo, utils/validators
105
+ # Does NOT: Direct DB access (uses repo), HTML generation
106
+ ```
107
+
108
+ #### `deck_service.py` (max 180 lines)
109
+ ```python
110
+ # Purpose: AI deck generation business logic
111
+ # Responsibilities:
112
+ # - Build AI prompts based on user preferences
113
+ # - Process AI API responses
114
+ # - Validate generated word pools
115
+ # - Manage deck lifecycle (create, update, delete)
116
+ # - Enforce deck limits based on subscription tier
117
+ # Dependencies: deck_repo, ai_prompt_builder, validators
118
+ # Does NOT: Direct DB access, AI API calls (abstracted), HTML generation
119
+ ```
120
+
121
+ #### `invite_service.py` (max 120 lines)
122
+ ```python
123
+ # Purpose: Invite link business logic
124
+ # Responsibilities:
125
+ # - Generate unique, secure invite codes
126
+ # - Set expiration policies
127
+ # - Track invite usage statistics
128
+ # - Validate invite codes
129
+ # - Handle invite redemption logic
130
+ # Dependencies: invite_repo, link_generator, validators
131
+ # Does NOT: Direct DB access, HTML generation
132
+ ```
133
+
134
+ ---
135
+
136
+ ### Repositories Layer (`repositories/`)
137
+
138
+ #### `subscription_repo.py` (max 100 lines)
139
+ ```python
140
+ # Purpose: Subscription data access
141
+ # Responsibilities:
142
+ # - Query user subscription from Supabase
143
+ # - Update subscription status
144
+ # - Cache subscription data (optional)
145
+ # - Handle data transformation from DB format
146
+ # Dependencies: database.py (supabase client)
147
+ # Does NOT: Business logic, validation beyond DB constraints
148
+ ```
149
+
150
+ #### `deck_repo.py` (max 120 lines)
151
+ ```python
152
+ # Purpose: Deck data access
153
+ # Responsibilities:
154
+ # - CRUD operations for custom decks
155
+ # - Query decks by user, category, date
156
+ # - Store AI-generated word pools
157
+ # - Handle deck-word relationships
158
+ # Dependencies: database.py
159
+ # Does NOT: Business logic, AI processing
160
+ ```
161
+
162
+ #### `invite_repo.py` (max 80 lines)
163
+ ```python
164
+ # Purpose: Invite data access
165
+ # Responsibilities:
166
+ # - Store invite codes with metadata
167
+ # - Track usage (used_by, used_at)
168
+ # - Query active/expired invites
169
+ # - Soft-delete expired invites
170
+ # Dependencies: database.py
171
+ # Does NOT: Business logic, code generation
172
+ ```
173
+
174
+ ---
175
+
176
+ ### Schemas Layer (`schemas/`)
177
+
178
+ #### `subscription_schemas.py` (max 80 lines)
179
+ ```python
180
+ # Purpose: Subscription data validation
181
+ # Models:
182
+ # - SubscriptionResponse (id, status, renewal_date, tier)
183
+ # - UpgradeRequest (target_tier)
184
+ # - CancelSubscriptionResponse
185
+ ```
186
+
187
+ #### `deck_schemas.py` (max 100 lines)
188
+ ```python
189
+ # Purpose: Deck data validation
190
+ # Models:
191
+ # - DeckCreateRequest (category, difficulty, word_count, ai_params)
192
+ # - DeckResponse (id, name, words, created_at, word_count)
193
+ # - DeckListResponse (decks: list[DeckResponse], total)
194
+ ```
195
+
196
+ #### `invite_schemas.py` (max 60 lines)
197
+ ```python
198
+ # Purpose: Invite data validation
199
+ # Models:
200
+ # - InviteCreateRequest (room_id, expires_in_hours)
201
+ # - InviteResponse (code, url, expires_at, max_uses)
202
+ # - InviteValidateResponse (valid, room_info, creator)
203
+ ```
204
+
205
+ ---
206
+
207
+ ### Utilities Layer (`utils/`)
208
+
209
+ #### `link_generator.py` (max 80 lines)
210
+ ```python
211
+ # Purpose: Secure link/code generation
212
+ # Responsibilities:
213
+ # - Generate cryptographically secure tokens
214
+ # - Create URL-safe invite codes
215
+ # - Encode/decode invite payloads
216
+ # - Validate code format
217
+ # Does NOT: Database operations, business rules
218
+ ```
219
+
220
+ #### `ai_prompt_builder.py` (max 100 lines)
221
+ ```python
222
+ # Purpose: AI prompt construction
223
+ # Responsibilities:
224
+ # - Build structured prompts for word generation
225
+ # - Include category-specific constraints
226
+ # - Format user preferences into prompt context
227
+ # - Parse and validate AI responses
228
+ # Does NOT: API calls, database operations
229
+ ```
230
+
231
+ #### `validators.py` (max 100 lines)
232
+ ```python
233
+ # Purpose: Shared validation utilities
234
+ # Responsibilities:
235
+ # - Validate user IDs, room codes
236
+ # - Check date ranges, expiration
237
+ # - Validate word format (length, characters)
238
+ # - Common input sanitization
239
+ # Does NOT: Business logic, DB queries
240
+ ```
241
+
242
+ ---
243
+
244
+ ### Templates Layer (`templates/`)
245
+
246
+ #### `dashboard.html` (max 100 lines)
247
+ ```html
248
+ <!-- Purpose: Main dashboard layout -->
249
+ <!-- Includes: Navigation, user greeting, feature summary cards -->
250
+ <!-- Loads: subscription_panel, deck_creator, invite_generator via HTMX -->
251
+ ```
252
+
253
+ #### `subscription_panel.html` (max 80 lines)
254
+ ```html
255
+ <!-- Purpose: Subscription status display -->
256
+ <!-- Shows: Current tier, renewal date, upgrade button, usage stats -->
257
+ <!-- Actions: Upgrade, cancel, view billing -->
258
+ ```
259
+
260
+ #### `deck_creator.html` (max 120 lines)
261
+ ```html
262
+ <!-- Purpose: AI deck creation UI -->
263
+ <!-- Form: Category selection, difficulty, word count, custom params -->
264
+ <!-- Display: Generated deck preview, save button -->
265
+ ```
266
+
267
+ #### `invite_generator.html` (max 80 lines)
268
+ ```html
269
+ <!-- Purpose: Invite link generation UI -->
270
+ <!-- Form: Room selection, expiration settings -->
271
+ <!-- Display: Generated link, copy button, usage stats -->
272
+ ```
273
+
274
+ ---
275
+
276
+ ## Data Flow Diagrams
277
+
278
+ ### Subscription Status Flow
279
+ ```
280
+ User Request β†’ router.py (GET /dashboard/subscription)
281
+ β†’ subscription_service.get_status(user_id)
282
+ β†’ subscription_repo.fetch_subscription(user_id)
283
+ β†’ Supabase DB
284
+ ← subscription_service.format_response(data)
285
+ ← router.py returns JSON/HTML
286
+ ```
287
+
288
+ ### Deck Creation Flow
289
+ ```
290
+ User Request β†’ router.py (POST /dashboard/deck)
291
+ β†’ deck_schemas.DeckCreateRequest.validate(data)
292
+ β†’ deck_service.create_deck(user_id, params)
293
+ β†’ ai_prompt_builder.build_prompt(params)
294
+ β†’ [AI API Call - external service]
295
+ β†’ deck_service.validate_words(ai_response)
296
+ β†’ deck_repo.save_deck(user_id, words)
297
+ β†’ Supabase DB
298
+ ← router.py returns DeckResponse
299
+ ```
300
+
301
+ ### Invite Generation Flow
302
+ ```
303
+ User Request β†’ router.py (POST /dashboard/invite)
304
+ β†’ invite_schemas.InviteCreateRequest.validate(data)
305
+ β†’ invite_service.generate_invite(user_id, room_id, expires)
306
+ β†’ link_generator.create_secure_code()
307
+ β†’ invite_repo.save_invite(code, metadata)
308
+ β†’ Supabase DB
309
+ β†’ invite_service.build_invite_url(code)
310
+ ← router.py returns InviteResponse
311
+ ```
312
+
313
+ ---
314
+
315
+ ## Scalability Considerations
316
+
317
+ ### Adding New Pool Types
318
+ 1. Create new schema in `schemas/deck_schemas.py` (e.g., `CustomPoolParams`)
319
+ 2. Extend `ai_prompt_builder.py` with new prompt templates
320
+ 3. Add validation rules in `utils/validators.py`
321
+ 4. **No changes needed** to existing services or repos
322
+
323
+ ### Adding Analytics
324
+ 1. Create `services/analytics_service.py`
325
+ 2. Create `repositories/analytics_repo.py`
326
+ 3. Create `schemas/analytics_schemas.py`
327
+ 4. Add routes to `router.py`
328
+ 5. **No changes needed** to existing services
329
+
330
+ ### Adding New Subscription Tiers
331
+ 1. Update `schemas/subscription_schemas.py` with new tier enum
332
+ 2. Update `services/subscription_service.py` tier logic
333
+ 3. **No changes needed** to repos or routers
334
+
335
+ ---
336
+
337
+ ## File Size Enforcement Strategy
338
+
339
+ If any file approaches 200 lines:
340
+
341
+ ### For Services
342
+ - Extract helper functions to `utils/`
343
+ - Split by aggregate root (e.g., `deck_service.py` β†’ `deck_create_service.py`, `deck_manage_service.py`)
344
+ - Move complex validation to `utils/validators.py`
345
+
346
+ ### For Repositories
347
+ - Split by query complexity (simple queries vs. complex joins)
348
+ - Extract query builders to separate modules
349
+
350
+ ### For Templates
351
+ - Extract reusable components to `templates/components/`
352
+ - Use HTMX to load partial views
353
+
354
+ ---
355
+
356
+ ## Integration with Existing Codebase
357
+
358
+ ### Main Application (`main.py`)
359
+ ```python
360
+ # Add to imports
361
+ from features.user_dashboard import router as dashboard_router
362
+
363
+ # Add to app setup
364
+ app.include_router(dashboard_router, prefix="/dashboard")
365
+ ```
366
+
367
+ ### Database (`database.py`)
368
+ - No changes needed - existing supabase client is reused
369
+
370
+ ### Authentication
371
+ - Reuse existing `get_current_user` from `main.py`
372
+ - Add as dependency to dashboard routes
373
+
374
+ ---
375
+
376
+ ## Testing Strategy
377
+
378
+ ```
379
+ tests/
380
+ └── features/
381
+ └── user_dashboard/
382
+ β”œβ”€β”€ test_subscription_service.py
383
+ β”œβ”€β”€ test_deck_service.py
384
+ β”œβ”€β”€ test_invite_service.py
385
+ β”œβ”€β”€ test_link_generator.py
386
+ └── test_ai_prompt_builder.py
387
+ ```
388
+
389
+ **Key Testing Principles:**
390
+ - Unit tests for services (mock repos)
391
+ - Integration tests for repos (test DB queries)
392
+ - Schema validation tests
393
+ - Utility function tests
394
+
395
+ ---
396
+
397
+ ## Migration Plan
398
+
399
+ ### Phase 1: Foundation
400
+ 1. Create directory structure
401
+ 2. Implement `schemas/` layer
402
+ 3. Implement `utils/` layer
403
+ 4. Set up basic `router.py` skeleton
404
+
405
+ ### Phase 2: Data Layer
406
+ 1. Implement `repositories/` layer
407
+ 2. Create database tables (if needed)
408
+ 3. Write repository tests
409
+
410
+ ### Phase 3: Business Logic
411
+ 1. Implement `services/` layer
412
+ 2. Write service tests
413
+ 3. Integrate with existing auth
414
+
415
+ ### Phase 4: UI Layer
416
+ 1. Create templates
417
+ 2. Wire up HTMX interactions
418
+ 3. Test user flows
419
+
420
+ ### Phase 5: Integration
421
+ 1. Register router in `main.py`
422
+ 2. End-to-end testing
423
+ 3. Deploy to staging
424
+
425
+ ---
426
+
427
+ ## Summary
428
+
429
+ This architecture provides:
430
+ - βœ… **Separation of Concerns**: Clear layers (routes β†’ services β†’ repos β†’ DB)
431
+ - βœ… **Single Responsibility**: Each file has one well-defined purpose
432
+ - βœ… **Scalability**: Easy to add new features without modifying existing code
433
+ - βœ… **Maintainability**: Files stay under 200 lines through logical splitting
434
+ - βœ… **Testability**: Each layer can be tested in isolation
435
+ - βœ… **Reusability**: Utilities can be shared across features
436
+
437
+ The structure follows clean architecture principles while remaining pragmatic for a FastAPI application.