|
| 1 | +# PR: Add REST Route - Get Current User Usage and Stats |
| 2 | + |
| 3 | +## Summary |
| 4 | +Fixes #29 - Implements GET /api/usage endpoint that returns usage events and statistics for the authenticated user. |
| 5 | + |
| 6 | +## Changes Made |
| 7 | + |
| 8 | +### 🔧 Repository Extensions |
| 9 | +- **Extended `UsageEventsRepository`**: |
| 10 | + - Added `UserUsageEventQuery` interface for user-specific queries |
| 11 | + - Implemented `findByUser()` method to retrieve usage events for a specific user |
| 12 | + - Added `aggregateByUser()` method to calculate total usage statistics with breakdown by API |
| 13 | + |
| 14 | +### 🚀 New Authenticated Endpoint |
| 15 | +- **Implemented `GET /api/usage`** with JWT authentication |
| 16 | +- **Query Parameters**: |
| 17 | + - `from`/`to`: Date range filtering (ISO format) |
| 18 | + - `limit`: Pagination (non-negative integer) |
| 19 | + - `apiId`: Filter by specific API |
| 20 | +- **Smart Defaults**: Last 30 days when no dates provided |
| 21 | +- **Comprehensive Validation**: Input validation with clear error messages |
| 22 | + |
| 23 | +### 📊 Response Format |
| 24 | +```json |
| 25 | +{ |
| 26 | + "events": [ |
| 27 | + { |
| 28 | + "id": "event-id", |
| 29 | + "apiId": "api-id", |
| 30 | + "endpoint": "/api/endpoint", |
| 31 | + "occurredAt": "2024-01-15T10:00:00.000Z", |
| 32 | + "revenue": "1000000" |
| 33 | + } |
| 34 | + ], |
| 35 | + "stats": { |
| 36 | + "totalCalls": 10, |
| 37 | + "totalSpent": "4500000", |
| 38 | + "breakdownByApi": [ |
| 39 | + { |
| 40 | + "apiId": "api1", |
| 41 | + "calls": 7, |
| 42 | + "revenue": "3000000" |
| 43 | + } |
| 44 | + ] |
| 45 | + }, |
| 46 | + "period": { |
| 47 | + "from": "2024-01-15T00:00:00.000Z", |
| 48 | + "to": "2024-02-15T00:00:00.000Z" |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### 🧪 Comprehensive Testing |
| 54 | +- **12 test cases** covering: |
| 55 | + - Authentication requirements |
| 56 | + - Parameter validation |
| 57 | + - Date range filtering |
| 58 | + - API filtering and pagination |
| 59 | + - Edge cases and error handling |
| 60 | + - Response format validation |
| 61 | + |
| 62 | +## ✅ Requirements Satisfied |
| 63 | + |
| 64 | +- ✅ **Requires wallet auth (JWT)** - Uses existing `requireAuth` middleware |
| 65 | +- ✅ **Default period: last 30 days** - Smart default handling |
| 66 | +- ✅ **Query params: from, to, limit** - Full parameter support with validation |
| 67 | +- ✅ **Returns usage events for current user** - User-scoped data retrieval |
| 68 | +- ✅ **Returns total spent in period** - Aggregated statistics |
| 69 | +- ✅ **Optional breakdown by API** - Detailed usage breakdown |
| 70 | +- ✅ **Uses usage_events repository** - Leverages existing data layer |
| 71 | +- ✅ **Includes requireAuth middleware** - Proper authentication |
| 72 | + |
| 73 | +## 🔒 Security Features |
| 74 | + |
| 75 | +- JWT authentication with existing middleware |
| 76 | +- Input validation prevents injection attacks |
| 77 | +- Users can only access their own usage data |
| 78 | +- No sensitive information exposure |
| 79 | + |
| 80 | +## 📁 Files Modified |
| 81 | + |
| 82 | +- `src/repositories/usageEventsRepository.ts` - Extended repository interface and implementation |
| 83 | +- `src/app.ts` - Implemented authenticated route |
| 84 | +- `src/__tests__/userUsage.test.ts` - Added comprehensive test suite |
| 85 | + |
| 86 | +## 🚀 Usage Examples |
| 87 | + |
| 88 | +```bash |
| 89 | +# Get usage for last 30 days (default) |
| 90 | +GET /api/usage |
| 91 | +Authorization: Bearer <jwt-token> |
| 92 | + |
| 93 | +# Get usage for custom date range |
| 94 | +GET /api/usage?from=2024-01-01T00:00:00Z&to=2024-01-31T23:59:59Z |
| 95 | +Authorization: Bearer <jwt-token> |
| 96 | + |
| 97 | +# Get usage for specific API with limit |
| 98 | +GET /api/usage?apiId=api1&limit=10 |
| 99 | +Authorization: Bearer <jwt-token> |
| 100 | +``` |
| 101 | + |
| 102 | +## 🧪 Testing |
| 103 | + |
| 104 | +The implementation includes comprehensive test coverage with 12 test cases that verify: |
| 105 | +- Authentication requirements |
| 106 | +- Parameter validation and error handling |
| 107 | +- Data filtering and pagination |
| 108 | +- Response format and structure |
| 109 | +- Edge cases and boundary conditions |
| 110 | + |
| 111 | +All tests pass and the endpoint is ready for production use. |
0 commit comments