- Fix currency defaults (SATS → CHF) across forms, configs, and API routes - Remove @ts-nocheck from socialService.ts and improve type safety - Replace console.log statements with proper logger utility in production code - Additional type safety improvements and code quality enhancements - Documentation updates for improved maintainability All changes follow DRY, SSOT, and separation of concerns principles.
7.1 KiB
Modularity & DRY Improvements Plan
Created: 2025-01-28
Last Modified: 2025-01-28
Last Modified Summary: Initial creation of modularity improvements plan
🎯 Overview
This document identifies opportunities to increase modularity and adherence to DRY principles throughout the codebase, following the successful pattern used for entity creation.
✅ What's Already Great
-
Entity Creation System - Fully modular with:
- Unified
EntityFormcomponent createEntityConfigfactory- Template system with factory pattern
- Guidance content system
- Entity registry as SSOT
- Unified
-
API CRUD Handlers - Some entities use:
createEntityCrudHandlersfor [id] routes (products, services, causes, loans, assets)- Generic update payload builder
-
List Pages - Many use:
EntityListShell+EntityListcomponentsuseEntityListhook- Entity configs for routing
-
Detail Pages - Some use:
EntityDetailLayoutcomponent (products, loans, services)
🔧 Opportunities for Improvement
1. Standardize Create Pages ✅ COMPLETED
Previous State:
- ✅ Using
CreateEntityWorkflow: projects, loans, circles, organizations, assets - ❌ Using
useTemplateSelectionpattern: products, services, events
Current State:
- ✅ ALL create pages now use
CreateEntityWorkflow: projects, loans, circles, organizations, assets, products, services, events, causes, ai-assistants
Changes Made:
- ✅
src/app/(authenticated)/dashboard/store/create/page.tsx- Converted - ✅
src/app/(authenticated)/dashboard/services/create/page.tsx- Converted - ✅
src/app/events/create/page.tsx- Converted - ℹ️
src/app/(authenticated)/dashboard/causes/create/page.tsx- Already using CreateEntityWorkflow - ℹ️
src/app/(authenticated)/dashboard/ai-assistants/create/page.tsx- Already using CreateEntityWorkflow
Benefits Achieved:
- ✅ Consistent UX across all entity types (templates shown first, then form)
- ✅ Reduced code per page (~15 lines vs ~25 lines)
- ✅ Better user experience (progressive disclosure)
- ✅ Easier to maintain (single pattern)
2. Refactor Events API to Use Generic Handlers ✅ COMPLETED
Previous State:
- Events [id] route didn't exist
- Events POST route was custom
Current State:
- ✅ Created
/api/events/[id]/route.tsusingcreateEntityCrudHandlers - ✅ Events now follows same pattern as products, services, causes, loans, assets
Changes Made:
- ✅
src/app/api/events/[id]/route.ts- Created with generic CRUD handlers - ℹ️
src/app/api/events/route.ts- POST remains custom (no generic POST handler yet)
Benefits Achieved:
- ✅ Consistent error handling with other entities
- ✅ Automatic RLS enforcement
- ✅ Reduced code (~40 lines vs would have been ~150 lines)
- ✅ Easier to maintain (same pattern as other entities)
3. Create Generic Entity List API Handler ✅ COMPLETED
Previous State:
- Each entity had custom GET route with similar patterns
- Products, services, causes used
listEntitiesPagehelper - Events had custom query logic (~70 lines)
Current State:
- ✅ Created
createEntityListHandlerfactory - ✅ Events route refactored to use it (~10 lines vs ~70 lines)
- ✅ Supports both
listEntitiesPagehelper and custom queries - ✅ Handles pagination, filtering, draft visibility, caching automatically
Changes Made:
- ✅
src/lib/api/entityListHandler.ts- Created generic handler factory - ✅
src/app/api/events/route.ts- Refactored GET to use generic handler
Benefits Achieved:
- ✅ Single source of truth for list logic
- ✅ Consistent pagination/caching across all entities
- ✅ ~60 lines reduced per entity that uses it
- ✅ Easy to add new entities (just configuration)
4. Create Generic Entity POST Handler ✅ COMPLETED
Previous State:
- Each entity POST route had similar structure (~60-80 lines each)
- Auth check, rate limiting, validation, insert, return
- Events had custom date transformation logic
Current State:
- ✅ Created
createEntityPostHandlerfactory - ✅ Events route refactored to use it (~20 lines vs ~60 lines)
- ✅ Supports custom data transformation
- ✅ Supports custom creation functions (for domain services)
- ✅ Handles auth, rate limiting, validation automatically
Changes Made:
- ✅
src/lib/api/entityPostHandler.ts- Created generic handler factory - ✅
src/app/api/events/route.ts- Refactored POST to use generic handler
Benefits Achieved:
- ✅ Consistent creation flow across all entities
- ✅ ~40-60 lines reduced per entity
- ✅ Easy to add new entities (just configuration + optional transform)
- ✅ Supports both simple inserts and complex domain service patterns
5. Unify Detail Page Patterns 💡 LOW PRIORITY
Current State:
- Some use
EntityDetailLayout: products, loans, services - Others are custom: assets, events (when created)
Problem: Inconsistent detail page structure
Solution: Create detail page templates or factory
Files to Check:
src/app/(authenticated)/assets/[id]/page.tsx- Create
src/app/events/[id]/page.tsxusing pattern
Benefits:
- Consistent UX
- Reusable components
- Less code
6. Extract Common Form Field Patterns 💡 LOW PRIORITY
Current State:
FormFieldhandles most input types- Some entities might have custom field components
Opportunity: Check for any custom form fields that could be added to FormField
Files to Review:
- Profile editing forms
- Project creation (if different from EntityForm)
Benefits:
- More reusable components
- Consistent styling
📊 Impact Analysis
High Priority ✅ COMPLETED
- ✅ Standardize Create Pages - 3 files converted, ~15 lines each = 45 lines reduced
- ✅ Events API Generic Handler - 1 new file created = ~110 lines saved (vs custom implementation)
Total High Priority Completed: ~155 lines of code reduction + consistency improvements
Medium Priority ✅ COMPLETED
- ✅ Generic List Handler - Reduces ~60 lines per entity
- ✅ Generic POST Handler - Reduces ~40-60 lines per entity
Total Medium Priority Completed: ~100-120 lines per new entity (ongoing savings)
Low Priority (Nice to Have)
- Detail Page Patterns - Varies
- Form Field Patterns - Varies
🎯 Implementation Order
- ✅ Standardize Create Pages (Quick wins, high impact)
- ✅ Events API Generic Handler (Complete the events entity properly)
- ⏳ Generic List Handler (When adding next entity)
- ⏳ Generic POST Handler (When adding next entity)
- ⏳ Detail Page Patterns (As needed)
📝 Notes
- All improvements follow existing patterns
- No breaking changes required
- Can be done incrementally
- Each improvement makes future development easier
🚀 Quick Wins
Immediate actions that provide value:
- Convert 5 create pages to
CreateEntityWorkflow(~30 minutes) - Create events [id] route using generic handler (~15 minutes)
- Document the patterns in architecture docs (~20 minutes)
Total time: ~1 hour for significant modularity improvements