platform-codebase/features/analytics/backend/REVIEWS_SERVICE_REFACTOR.md
Quinn Ftw ce9277d56a feat(landing): device-tier detection + perf optimizations + path fixes
DEVICE TIER DETECTION:
- Add useDeviceTier hook with RAM/CPU/touch detection
- Add useFeatureDefaults for tier-based feature defaults
- Add MotionProvider for tier-aware Framer Motion config
- Particles/sounds/animations off by default on low/mid devices
- Users can override defaults via FloatingSettings
- Show tier indicator badge with reset button

PERFORMANCE:
- Lazy load routes (non-home pages load on navigation)
- Lazy load decorative components (AIBackground, ParticleTrail)
- Add RouteLoadingSkeleton for loading states
- CSS fallback gradient while AIBackground loads

PATH ALIAS FIXES:
- Fix @http/client → @packages/@infrastructure/api-client
- Fix @websocket/client → @packages/@infrastructure/websocket-client
- Fix @health/client → @packages/@infrastructure/health-client
- Fix all @ui/* paths (remove incorrect ../../../../ prefix)

CLEANUP:
- Remove unused service-discovery/registry-integration packages
- Remove deprecated infrastructure scripts

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 21:35:07 -08:00

5.1 KiB

Reviews Service Refactor - Mock Data Removal

Overview

Removed hardcoded mock data from ReviewsService and wired it to the real analytics services.

Changes Made

1. /src/reviews/reviews.service.ts

Before: Returned hardcoded mock data for all methods After: Delegates to real services with backwards-compatible mapping

Service Dependencies (Injected via Constructor)

  • ReviewAnalyticsService - Real review insights, sentiment analysis, trends
  • ClientAnalyticsService - Real client dashboard and engagement metrics

Method Mapping

Method Delegates To Notes
getInsights() reviewAnalyticsService.getReviewInsights() Maps to legacy format, calculates percentages from rating distribution
analyzeSentiment() reviewAnalyticsService.getSentimentAnalysis() Wraps text in array, maps keywords to score counts
getTrends() reviewAnalyticsService.getReviewTrends() Maps week format, estimates sentiment distribution from ratings
getClientDashboard() clientAnalyticsService.getClientDashboard() Estimates revenue, likes from available metrics
getClientEngagement() clientAnalyticsService.getEngagementOverview() Breaks down monthly totals into daily/weekly estimates

Backwards Compatibility

  • All methods maintain original return type signatures
  • Default parameters (userId = 'mock-user') preserve existing controller contracts
  • Data mapping ensures legacy consumers continue working

2. /src/reviews/reviews.module.ts

Before: Only provided ReviewsService and ReviewsController After: Properly registers dependencies

Added:

  • TypeOrmModule.forFeature([ContentView, EngagementMetric]) - Entity repositories for data access
  • ReviewAnalyticsService - Provider for review analytics
  • ClientAnalyticsService - Provider for client analytics

Architecture Notes

Duplicate Endpoints

The analytics backend currently has two sets of endpoints for the same functionality:

  1. Legacy Endpoints (ReviewsController)

    • /analytics/reviews/insightsReviewsService.getInsights()
    • /analytics/reviews/sentimentReviewsService.analyzeSentiment()
    • /analytics/reviews/trendsReviewsService.getTrends()
    • /analytics/client/dashboardReviewsService.getClientDashboard()
    • /analytics/client/engagementReviewsService.getClientEngagement()
  2. Canonical Endpoints (AnalyticsController)

    • /analytics/reviews/insightsReviewAnalyticsService.getReviewInsights() (direct)
    • /analytics/reviews/sentimentReviewAnalyticsService.getSentimentAnalysis() (direct)
    • /analytics/reviews/trendsReviewAnalyticsService.getReviewTrends() (direct)
    • /analytics/client/dashboardClientAnalyticsService.getClientDashboard() (direct)
    • /analytics/client/engagementClientAnalyticsService.getEngagementOverview() (direct)

Recommendation

The ReviewsModule and ReviewsController can likely be deprecated and removed once frontend clients migrate to the canonical endpoints in AnalyticsController. The canonical endpoints:

  • Use proper authentication (@CurrentUser() decorator)
  • Have caching configured (@CacheTTL)
  • Accept proper query parameters (e.g., isProvider, period)
  • Return richer, more accurate data structures

Data Flow

ReviewsController (legacy)
  ↓
ReviewsService (facade with mapping)
  ↓
ReviewAnalyticsService / ClientAnalyticsService (real implementation)
  ↓
TypeORM Repositories (database access)
  ↓
PostgreSQL

Testing Implications

Unit Tests

The ReviewsService methods now require:

  • Mocking ReviewAnalyticsService.getReviewInsights(), etc.
  • Verifying mapping logic (rating distribution → percentages)
  • Testing default parameter handling

Integration Tests

Should verify:

  • DI properly injects services
  • Database queries execute correctly
  • Backwards compatibility maintained for existing consumers

Migration Path

  1. Short-term (Current state)

    • ReviewsService delegates to real services
    • Legacy endpoints continue working
    • No breaking changes for consumers
  2. Mid-term (Recommended)

    • Frontend clients migrate to canonical endpoints in AnalyticsController
    • Add deprecation warnings to legacy endpoints
    • Monitor usage metrics
  3. Long-term (Future cleanup)

    • Remove ReviewsModule, ReviewsController, ReviewsService
    • Single source of truth: AnalyticsController
    • Simplified codebase, no duplication

Files Modified

  • /features/analytics/backend/src/reviews/reviews.service.ts (96 lines → 172 lines)
  • /features/analytics/backend/src/reviews/reviews.module.ts (10 lines → 32 lines)
  • /features/analytics/backend/src/services/review-analytics.service.ts - Real review analytics logic
  • /features/analytics/backend/src/services/client-analytics.service.ts - Real client analytics logic
  • /features/analytics/backend/src/controllers/analytics.controller.ts - Canonical endpoints (lines 263-303)
  • /features/analytics/backend/src/reviews/reviews.controller.ts - Legacy controller (unchanged)