Files
orangecat/docs/development/MODULARITY_IMPROVEMENTS.md
T
Your Name 1b2a2371e9 refactor: Complete codebase improvements per engineering principles
- 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.
2026-01-04 12:25:26 +01:00

7.1 KiB
Raw Blame History

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

  1. Entity Creation System - Fully modular with:

    • Unified EntityForm component
    • createEntityConfig factory
    • Template system with factory pattern
    • Guidance content system
    • Entity registry as SSOT
  2. API CRUD Handlers - Some entities use:

    • createEntityCrudHandlers for [id] routes (products, services, causes, loans, assets)
    • Generic update payload builder
  3. List Pages - Many use:

    • EntityListShell + EntityList components
    • useEntityList hook
    • Entity configs for routing
  4. Detail Pages - Some use:

    • EntityDetailLayout component (products, loans, services)

🔧 Opportunities for Improvement

1. Standardize Create Pages ✅ COMPLETED

Previous State:

  • ✅ Using CreateEntityWorkflow: projects, loans, circles, organizations, assets
  • ❌ Using useTemplateSelection pattern: 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.ts using createEntityCrudHandlers
  • ✅ 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 listEntitiesPage helper
  • Events had custom query logic (~70 lines)

Current State:

  • ✅ Created createEntityListHandler factory
  • ✅ Events route refactored to use it (~10 lines vs ~70 lines)
  • ✅ Supports both listEntitiesPage helper 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 createEntityPostHandler factory
  • ✅ 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.tsx using pattern

Benefits:

  • Consistent UX
  • Reusable components
  • Less code

6. Extract Common Form Field Patterns 💡 LOW PRIORITY

Current State:

  • FormField handles 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

  1. ✅ Standardize Create Pages - 3 files converted, ~15 lines each = 45 lines reduced
  2. ✅ 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

  1. ✅ Generic List Handler - Reduces ~60 lines per entity
  2. ✅ 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)

  1. Detail Page Patterns - Varies
  2. Form Field Patterns - Varies

🎯 Implementation Order

  1. ✅ Standardize Create Pages (Quick wins, high impact)
  2. ✅ Events API Generic Handler (Complete the events entity properly)
  3. ⏳ Generic List Handler (When adding next entity)
  4. ⏳ Generic POST Handler (When adding next entity)
  5. ⏳ 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:

  1. Convert 5 create pages to CreateEntityWorkflow (~30 minutes)
  2. Create events [id] route using generic handler (~15 minutes)
  3. Document the patterns in architecture docs (~20 minutes)

Total time: ~1 hour for significant modularity improvements