|
| 1 | +# Enhanced Churn Risk Prediction System |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This implementation provides a comprehensive churn risk prediction system that analyzes subscriber behavior patterns to identify users at risk of canceling their subscriptions due to payment failures (insufficient funds). The system uses machine learning-inspired rule-based logic to detect "just-in-time" top-up patterns and provides actionable insights for merchants. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +### 🎯 Core Functionality |
| 10 | +- **Just-in-Time Detection**: Identifies users who consistently top up their wallets right before payment failures |
| 11 | +- **Multi-Factor Analysis**: Analyzes balance history, missed payment streaks, and spending patterns |
| 12 | +- **Risk Scoring**: Provides 0-100 risk scores with categorized levels (Low, Medium, High, Critical) |
| 13 | +- **Historical Analysis**: Examines patterns over the last 6 billing cycles |
| 14 | + |
| 15 | +### 📊 Analytics & Insights |
| 16 | +- **Actionable Recommendations**: Suggests retention strategies based on risk patterns |
| 17 | +- **Real-time Analysis**: On-demand risk assessment via API |
| 18 | +- **Daily Updates**: Automated background processing for all merchants |
| 19 | +- **Performance Monitoring**: Built-in metrics and performance tracking |
| 20 | + |
| 21 | +### 🚀 Performance Optimizations |
| 22 | +- **Batch Processing**: Handles thousands of users efficiently |
| 23 | +- **Database Indexing**: Optimized queries for large-scale scanning |
| 24 | +- **Memory Management**: Streaming processing for large datasets |
| 25 | +- **Error Resilience**: Graceful failure handling and retry logic |
| 26 | + |
| 27 | +## Architecture |
| 28 | + |
| 29 | +### Database Schema |
| 30 | + |
| 31 | +#### Core Tables |
| 32 | +- **`risk_metrics`**: Stores risk analysis results and prediction factors |
| 33 | +- **`balance_history`**: Tracks wallet balance changes over time |
| 34 | +- **`payment_attempts`**: Records all payment attempts and failures |
| 35 | +- **`churn_risk_worker_metrics`**: Monitors worker performance |
| 36 | + |
| 37 | +#### Key Functions |
| 38 | +- **`detect_just_in_time_topups()`**: Identifies just-in-time top-up patterns |
| 39 | +- **`calculate_missed_payment_streak()`**: Calculates consecutive missed payments |
| 40 | + |
| 41 | +#### Optimized Views |
| 42 | +- **`high_risk_subscribers`**: Real-time view of high-risk users |
| 43 | +- **`churn_worker_performance_summary`**: Performance monitoring dashboard |
| 44 | + |
| 45 | +### Services |
| 46 | + |
| 47 | +#### EnhancedChurnRiskService |
| 48 | +- **Purpose**: Core risk analysis logic |
| 49 | +- **Key Methods**: |
| 50 | + - `analyzeMerchantChurnRisk(merchantId)`: Full merchant analysis |
| 51 | + - `analyzeSubscriberRisk(merchantId, userWallet)`: Individual user analysis |
| 52 | + - `getMerchantRiskAnalysis(merchantId, options)`: API data retrieval |
| 53 | + |
| 54 | +#### EnhancedChurnRiskWorker |
| 55 | +- **Purpose**: Background processing and daily updates |
| 56 | +- **Features**: |
| 57 | + - Configurable run intervals and batch sizes |
| 58 | + - Performance monitoring and error handling |
| 59 | + - Graceful shutdown and retry logic |
| 60 | + |
| 61 | +### API Endpoints |
| 62 | + |
| 63 | +#### GET `/api/v1/merchants/:id/risk-analysis` |
| 64 | +**Purpose**: Retrieve churn risk analysis for a merchant's subscribers |
| 65 | + |
| 66 | +**Query Parameters**: |
| 67 | +- `riskLevel`: Filter by risk level (Low, Medium, High, Critical) |
| 68 | +- `limit`: Number of results to return (default: 100) |
| 69 | +- `offset`: Pagination offset (default: 0) |
| 70 | +- `includeFactors`: Include detailed prediction factors (default: true) |
| 71 | +- `triggerAnalysis`: Force real-time analysis (default: false) |
| 72 | + |
| 73 | +**Response Structure**: |
| 74 | +```json |
| 75 | +{ |
| 76 | + "success": true, |
| 77 | + "data": { |
| 78 | + "merchantId": "uuid", |
| 79 | + "summary": { |
| 80 | + "totalSubscribers": 1000, |
| 81 | + "highRiskCount": 50, |
| 82 | + "mediumRiskCount": 200, |
| 83 | + "lowRiskCount": 750, |
| 84 | + "averageRiskScore": 35.5 |
| 85 | + }, |
| 86 | + "subscribers": [...], |
| 87 | + "actionableInsights": [...], |
| 88 | + "webhookConfig": { |
| 89 | + "highRiskWebhook": "/api/v1/webhooks/merchants/:id/high-risk-churn", |
| 90 | + "retentionSuggestions": [...] |
| 91 | + } |
| 92 | + }, |
| 93 | + "timestamp": "2026-04-28T20:00:00.000Z", |
| 94 | + "message": "Risk analysis retrieved successfully" |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## Risk Scoring Algorithm |
| 99 | + |
| 100 | +### Primary Factors (100 points total) |
| 101 | + |
| 102 | +1. **Just-in-Time Top-ups (40 points)** |
| 103 | + - 3+ just-in-time top-ups in last 3 cycles: 40 points (High Risk) |
| 104 | + - 1-2 just-in-time top-ups: 10-20 points |
| 105 | + - Detection window: 24 hours before/after failed payment |
| 106 | + |
| 107 | +2. **Missed Payment Streak (30 points)** |
| 108 | + - Each consecutive missed payment: 10 points |
| 109 | + - Maximum: 30 points (3+ missed payments) |
| 110 | + |
| 111 | +3. **Balance Trend (20 points)** |
| 112 | + - Critical decline (>30%): 20 points |
| 113 | + - Decreasing (10-30%): 15 points |
| 114 | + - Stable: 5 points |
| 115 | + - Increasing: 0 points |
| 116 | + |
| 117 | +4. **Days Until Balance Exhausted (10 points)** |
| 118 | + - ≤3 days: 10 points |
| 119 | + - ≤7 days: 7 points |
| 120 | + - ≤14 days: 3 points |
| 121 | + - >14 days: 0 points |
| 122 | +
|
| 123 | +### Risk Levels |
| 124 | +- **Critical**: 85-100 points |
| 125 | +- **High**: 70-84 points |
| 126 | +- **Medium**: 40-69 points |
| 127 | +- **Low**: 0-39 points |
| 128 | + |
| 129 | +## Installation & Setup |
| 130 | + |
| 131 | +### 1. Database Migration |
| 132 | +Run the migration scripts to create the required tables: |
| 133 | + |
| 134 | +```bash |
| 135 | +# Run migrations |
| 136 | +npm run migrate |
| 137 | + |
| 138 | +# Or run specific files |
| 139 | +psql -d your_database -f migrations/009_create_risk_metrics_table.sql |
| 140 | +psql -d your_database -f migrations/010_create_churn_risk_worker_metrics.sql |
| 141 | +``` |
| 142 | + |
| 143 | +### 2. Environment Configuration |
| 144 | +Add these environment variables to your `.env` file: |
| 145 | + |
| 146 | +```env |
| 147 | +# Churn Risk Worker Configuration |
| 148 | +CHURN_RISK_DEBUG=true |
| 149 | +CHURN_RISK_BATCH_SIZE=1000 |
| 150 | +CHURN_RISK_MERCHANT_BATCH_SIZE=10 |
| 151 | +CHURN_RISK_RUN_INTERVAL=86400000 # 24 hours in milliseconds |
| 152 | +CHURN_RISK_INITIAL_DELAY=600000 # 10 minutes in milliseconds |
| 153 | +``` |
| 154 | + |
| 155 | +### 3. Worker Integration |
| 156 | +The churn risk worker is automatically started with the main worker process: |
| 157 | + |
| 158 | +```bash |
| 159 | +# Start the worker (includes churn risk analysis) |
| 160 | +npm run worker |
| 161 | + |
| 162 | +# Or run in development mode |
| 163 | +npm run worker:dev |
| 164 | +``` |
| 165 | + |
| 166 | +### 4. API Integration |
| 167 | +The risk analysis endpoint is automatically available when the main application starts: |
| 168 | + |
| 169 | +```bash |
| 170 | +# Start the main application |
| 171 | +npm start |
| 172 | + |
| 173 | +# Access the API |
| 174 | +curl -X GET "http://localhost:3000/api/v1/merchants/{merchant_id}/risk-analysis" |
| 175 | +``` |
| 176 | + |
| 177 | +## Usage Examples |
| 178 | + |
| 179 | +### Basic Risk Analysis |
| 180 | +```javascript |
| 181 | +const { EnhancedChurnRiskService } = require('./src/services/enhancedChurnRiskService'); |
| 182 | + |
| 183 | +const service = new EnhancedChurnRiskService(); |
| 184 | + |
| 185 | +// Analyze all subscribers for a merchant |
| 186 | +const analysis = await service.analyzeMerchantChurnRisk('merchant-uuid'); |
| 187 | +console.log(`Found ${analysis.highRiskCount} high-risk subscribers`); |
| 188 | + |
| 189 | +// Get API-formatted data |
| 190 | +const apiData = await service.getMerchantRiskAnalysis('merchant-uuid', { |
| 191 | + riskLevel: 'High', |
| 192 | + limit: 50 |
| 193 | +}); |
| 194 | +``` |
| 195 | + |
| 196 | +### Manual Worker Trigger |
| 197 | +```javascript |
| 198 | +const { EnhancedChurnRiskWorker } = require('./src/services/enhancedChurnRiskWorker'); |
| 199 | + |
| 200 | +const worker = new EnhancedChurnRiskWorker(); |
| 201 | + |
| 202 | +// Trigger manual analysis for specific merchants |
| 203 | +const results = await worker.triggerManualAnalysis(['merchant-1', 'merchant-2']); |
| 204 | + |
| 205 | +// Get worker statistics |
| 206 | +const stats = worker.getStats(); |
| 207 | +console.log('Worker performance:', stats); |
| 208 | +``` |
| 209 | + |
| 210 | +### API Integration |
| 211 | +```bash |
| 212 | +# Get risk analysis for a merchant |
| 213 | +curl -X GET \ |
| 214 | + "http://localhost:3000/api/v1/merchants/{merchant_id}/risk-analysis?riskLevel=High&limit=20" \ |
| 215 | + -H "Authorization: Bearer {token}" |
| 216 | + |
| 217 | +# Trigger real-time analysis |
| 218 | +curl -X GET \ |
| 219 | + "http://localhost:3000/api/v1/merchants/{merchant_id}/risk-analysis?triggerAnalysis=true" \ |
| 220 | + -H "Authorization: Bearer {token}" |
| 221 | +``` |
| 222 | + |
| 223 | +## Performance Considerations |
| 224 | + |
| 225 | +### Database Optimization |
| 226 | +- **Indexes**: All queries use optimized indexes for sub-100ms response times |
| 227 | +- **Batching**: Large datasets are processed in configurable batches |
| 228 | +- **Connection Pooling**: Efficient database connection management |
| 229 | + |
| 230 | +### Memory Management |
| 231 | +- **Streaming**: Large result sets are processed in streams |
| 232 | +- **Garbage Collection**: Regular cleanup of temporary objects |
| 233 | +- **Error Boundaries**: Isolated error handling prevents memory leaks |
| 234 | + |
| 235 | +### Scalability |
| 236 | +- **Horizontal Scaling**: Multiple worker instances can run in parallel |
| 237 | +- **Queue Processing**: Redis-based queue for distributed processing |
| 238 | +- **Load Balancing**: Intelligent distribution of processing load |
| 239 | + |
| 240 | +## Monitoring & Maintenance |
| 241 | + |
| 242 | +### Performance Metrics |
| 243 | +The system automatically tracks: |
| 244 | +- Processing time per merchant |
| 245 | +- Total subscribers processed |
| 246 | +- High-risk identification rate |
| 247 | +- Error rates and retry attempts |
| 248 | + |
| 249 | +### Health Checks |
| 250 | +```bash |
| 251 | +# Check worker status |
| 252 | +curl -X GET "http://localhost:3000/health/worker" |
| 253 | + |
| 254 | +# Check database connectivity |
| 255 | +curl -X GET "http://localhost:3000/health/database" |
| 256 | +``` |
| 257 | + |
| 258 | +### Log Analysis |
| 259 | +```bash |
| 260 | +# View worker logs |
| 261 | +tail -f logs/churn-risk-worker.log |
| 262 | + |
| 263 | +# View performance metrics |
| 264 | +grep "Performance" logs/churn-risk-worker.log |
| 265 | +``` |
| 266 | + |
| 267 | +## Testing |
| 268 | + |
| 269 | +### Run Test Suite |
| 270 | +```bash |
| 271 | +# Run comprehensive tests |
| 272 | +node test_churn_risk_system.js |
| 273 | + |
| 274 | +# Run with debug output |
| 275 | +CHURN_RISK_DEBUG=true node test_churn_risk_system.js |
| 276 | +``` |
| 277 | + |
| 278 | +### Test Coverage |
| 279 | +- ✅ Database schema validation |
| 280 | +- ✅ Just-in-time detection logic |
| 281 | +- ✅ Risk score calculation |
| 282 | +- ✅ API endpoint functionality |
| 283 | +- ✅ Performance optimization |
| 284 | +- ✅ Background worker operations |
| 285 | + |
| 286 | +## Acceptance Criteria Verification |
| 287 | + |
| 288 | +### ✅ 1. Actionable Risk Scores |
| 289 | +- **Implementation**: Comprehensive risk analysis with detailed factors |
| 290 | +- **API**: `/api/v1/merchants/:id/risk-analysis` endpoint |
| 291 | +- **Output**: Structured risk scores with actionable insights |
| 292 | + |
| 293 | +### ✅ 2. Performance Optimization |
| 294 | +- **Batch Processing**: Configurable batch sizes for large datasets |
| 295 | +- **Database Indexing**: Optimized queries for thousands of users |
| 296 | +- **Memory Efficiency**: Streaming processing and garbage collection |
| 297 | + |
| 298 | +### ✅ 3. Retention Triggers |
| 299 | +- **Webhooks**: Configurable endpoints for high-risk alerts |
| 300 | +- **Email Integration**: Automated retention email suggestions |
| 301 | +- **API Integration**: Real-time risk data for external systems |
| 302 | + |
| 303 | +## Troubleshooting |
| 304 | + |
| 305 | +### Common Issues |
| 306 | + |
| 307 | +#### Worker Not Starting |
| 308 | +```bash |
| 309 | +# Check environment variables |
| 310 | +echo $CHURN_RISK_DEBUG |
| 311 | + |
| 312 | +# Verify database connection |
| 313 | +npm run health-check |
| 314 | +``` |
| 315 | + |
| 316 | +#### Slow Performance |
| 317 | +```bash |
| 318 | +# Check database indexes |
| 319 | +psql -d your_database -c "\d risk_metrics" |
| 320 | + |
| 321 | +# Monitor worker stats |
| 322 | +curl -X GET "http://localhost:3000/api/v1/workers/churn-risk/stats" |
| 323 | +``` |
| 324 | + |
| 325 | +#### Inaccurate Risk Scores |
| 326 | +```bash |
| 327 | +# Verify data quality |
| 328 | +psql -d your_database -c "SELECT COUNT(*) FROM balance_history WHERE merchant_id = 'uuid'" |
| 329 | + |
| 330 | +# Check analysis logs |
| 331 | +grep "risk_score" logs/churn-risk-worker.log | tail -20 |
| 332 | +``` |
| 333 | + |
| 334 | +### Debug Mode |
| 335 | +Enable detailed logging: |
| 336 | +```bash |
| 337 | +CHURN_RISK_DEBUG=true npm run worker:dev |
| 338 | +``` |
| 339 | + |
| 340 | +## Future Enhancements |
| 341 | + |
| 342 | +### Planned Features |
| 343 | +- **Machine Learning**: Replace rule-based logic with ML models |
| 344 | +- **Real-time Streaming**: Kafka integration for live risk updates |
| 345 | +- **Advanced Analytics**: Predictive modeling for churn prevention |
| 346 | +- **Multi-tenant**: Enhanced multi-merchant isolation |
| 347 | + |
| 348 | +### API Extensions |
| 349 | +- **Webhook Management**: CRUD operations for retention webhooks |
| 350 | +- **Custom Thresholds**: Per-merchant risk configuration |
| 351 | +- **Historical Trends**: Long-term risk pattern analysis |
| 352 | + |
| 353 | +## Support |
| 354 | + |
| 355 | +For issues or questions: |
| 356 | +1. Check the troubleshooting section above |
| 357 | +2. Review the test suite for expected behavior |
| 358 | +3. Enable debug mode for detailed logging |
| 359 | +4. Check the health endpoints for system status |
| 360 | + |
| 361 | +--- |
| 362 | + |
| 363 | +**Implementation Date**: April 28, 2026 |
| 364 | +**Version**: 1.0.0 |
| 365 | +**Compatibility**: Node.js 20.11.0+, PostgreSQL 12+ |
0 commit comments