|
| 1 | +## 📋 Summary |
| 2 | + |
| 3 | +Added optional `userId` query parameter to POST `/api/routes/calculate` endpoint for user activity tracking in logs. |
| 4 | + |
| 5 | +## 🎯 Motivation |
| 6 | + |
| 7 | +Enable tracking of which users are searching for routes without adding database complexity. This provides valuable insights through logs for analytics and debugging purposes. |
| 8 | + |
| 9 | +## 🔧 Changes Implemented |
| 10 | + |
| 11 | +### Modified Files |
| 12 | +- `catalog-service/src/main/java/com/busconnect/catalogservice/controller/RouteController.java` |
| 13 | + |
| 14 | +### Technical Details |
| 15 | + |
| 16 | +**Endpoint signature:** |
| 17 | +```java |
| 18 | +@PostMapping("/calculate") |
| 19 | +public Mono<ResponseEntity<RouteResultResponse>> calculateRoute( |
| 20 | + @Valid @RequestBody CalculateRouteRequest request, |
| 21 | + @RequestParam(required = false) Long userId |
| 22 | +) |
| 23 | +``` |
| 24 | + |
| 25 | +**Logging behavior:** |
| 26 | +- If `userId` is provided: `User {id} requested route: {origin} -> {destination}` |
| 27 | +- If `userId` is null: `Route calculation requested: {origin} -> {destination}` |
| 28 | + |
| 29 | +## 📝 Usage Examples |
| 30 | + |
| 31 | +**With userId:** |
| 32 | +```bash |
| 33 | +curl -X POST "http://localhost:8083/api/routes/calculate?userId=2" \ |
| 34 | + -H "Content-Type: application/json" \ |
| 35 | + -d '{"originMunicipality":"Barcelona","destinationMunicipality":"Girona"}' |
| 36 | +``` |
| 37 | + |
| 38 | +**Without userId (backwards compatible):** |
| 39 | +```bash |
| 40 | +curl -X POST "http://localhost:8083/api/routes/calculate" \ |
| 41 | + -H "Content-Type: application/json" \ |
| 42 | + -d '{"originMunicipality":"Barcelona","destinationMunicipality":"Girona"}' |
| 43 | +``` |
| 44 | + |
| 45 | +## 🔍 Log Output Example |
| 46 | + |
| 47 | +``` |
| 48 | +2026-01-18 13:28:18 - c.b.c.controller.RouteController - User 2 requested route: Barcelona -> Tarragona |
| 49 | +``` |
| 50 | + |
| 51 | +## ✅ Benefits |
| 52 | + |
| 53 | +- ✅ **No database changes required** - Pure logging solution |
| 54 | +- ✅ **Backwards compatible** - Parameter is optional |
| 55 | +- ✅ **Easy to implement** - Minimal code changes |
| 56 | +- ✅ **Immediate insights** - Track user behavior in real-time |
| 57 | +- ✅ **Frontend ready** - Can be integrated with user session context |
| 58 | + |
| 59 | +## 🚀 Next Steps (Frontend) |
| 60 | + |
| 61 | +This backend change enables the following frontend features: |
| 62 | +1. User session context with localStorage |
| 63 | +2. Search history tracking per user |
| 64 | +3. Personalized dashboard with recent searches |
| 65 | +4. User-specific analytics |
| 66 | + |
| 67 | +## 🧪 Testing |
| 68 | + |
| 69 | +Tested with: |
| 70 | +- User ID 2 (Joan García): ✅ Logged correctly |
| 71 | +- No user ID: ✅ Falls back to generic log |
| 72 | +- API response: ✅ Unchanged, fully backwards compatible |
| 73 | + |
| 74 | +## 📦 Deployment Status |
| 75 | + |
| 76 | +- [x] Code changes implemented |
| 77 | +- [x] Service rebuilt and redeployed |
| 78 | +- [x] Manual testing completed |
| 79 | +- [x] Logs verified |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +**Implementado por:** @Irina-Ichim |
| 84 | +**Fecha:** 2026-01-18 |
| 85 | +**Servicio afectado:** catalog-service |
| 86 | +**Puerto:** 8083 |
0 commit comments