|
1 | | -# Claims API (#41) TODO |
2 | | - |
3 | | -## Progress |
4 | | -- [ ] 1. Update package.json (Prisma, JWT, Redis) |
5 | | -- [ ] 2. prisma/schema.prisma (Claim, Vote models) |
6 | | -- [ ] 3. Expand auth/ (JwtStrategy, Guard) |
7 | | -- [ ] 4. claims/ module (controller, service, DTOs) |
8 | | -- [ ] 5. PrismaModule, RedisModule in app.module |
9 | | -- [ ] 6. prisma generate & db push |
10 | | -- [ ] 7. Tests, seed fixtures |
11 | | -- [ ] 8. Uncomment other modules in app.module |
12 | | -- [ ] 9. npm install & test /claims endpoints |
| 1 | +# Claims API Implementation |
13 | 2 |
|
| 3 | +## Overview |
| 4 | +Production-ready Claims REST API for the NiffyInsure Stellar insurance system. |
| 5 | + |
| 6 | +## Endpoints |
| 7 | + |
| 8 | +### GET /api/claims |
| 9 | +List all claims with aggregated data and pagination. |
| 10 | + |
| 11 | +**Query Parameters:** |
| 12 | +- `page` (optional, default: 1) - Page number |
| 13 | +- `limit` (optional, default: 20, max: 100) - Items per page |
| 14 | +- `status` (optional) - Filter by status: `pending`, `approved`, `rejected` |
| 15 | + |
| 16 | +**Response:** |
| 17 | +```json |
| 18 | +{ |
| 19 | + "data": [ |
| 20 | + { |
| 21 | + "metadata": { |
| 22 | + "id": 1, |
| 23 | + "policyId": "policy-123", |
| 24 | + "creatorAddress": "GXXXXXXXXXX", |
| 25 | + "status": "pending", |
| 26 | + "amount": "1000", |
| 27 | + "description": "Insurance claim...", |
| 28 | + "evidenceHash": "QmXXX...", |
| 29 | + "createdAtLedger": 12345678, |
| 30 | + "createdAt": "2024-01-01T00:00:00Z", |
| 31 | + "updatedAt": "2024-01-01T00:00:00Z" |
| 32 | + }, |
| 33 | + "votes": { |
| 34 | + "yesVotes": 3, |
| 35 | + "noVotes": 1, |
| 36 | + "totalVotes": 4 |
| 37 | + }, |
| 38 | + "quorum": { |
| 39 | + "required": 5, |
| 40 | + "current": 4, |
| 41 | + "percentage": 80, |
| 42 | + "reached": false |
| 43 | + }, |
| 44 | + "deadline": { |
| 45 | + "votingDeadlineLedger": 12350000, |
| 46 | + "votingDeadlineTime": "2024-01-02T00:00:00Z", |
| 47 | + "isOpen": true, |
| 48 | + "remainingSeconds": 3600 |
| 49 | + }, |
| 50 | + "evidence": { |
| 51 | + "gatewayUrl": "https://ipfs.io/ipfs/QmXXX...", |
| 52 | + "hash": "QmXXX..." |
| 53 | + }, |
| 54 | + "consistency": { |
| 55 | + "isFinalized": false, |
| 56 | + "indexerLag": 2, |
| 57 | + "lastIndexedLedger": 12345680, |
| 58 | + "isStale": false |
| 59 | + } |
| 60 | + } |
| 61 | + ], |
| 62 | + "pagination": { |
| 63 | + "page": 1, |
| 64 | + "limit": 20, |
| 65 | + "total": 100, |
| 66 | + "totalPages": 5, |
| 67 | + "hasNext": true |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### GET /api/claims/:id |
| 73 | +Get detailed claim view by ID. |
| 74 | + |
| 75 | +### GET /api/claims/needs-my-vote (Authenticated) |
| 76 | +Get claims where the authenticated user has not voted yet. |
| 77 | + |
| 78 | +**Headers:** |
| 79 | +- `Authorization: Bearer <jwt_token>` |
| 80 | + |
| 81 | +## Features |
| 82 | + |
| 83 | +### Aggregation |
| 84 | +- Optimized SQL queries with JOINs and GROUP BY |
| 85 | +- Vote tallies computed in single query (no N+1) |
| 86 | +- Pagination with total count |
| 87 | + |
| 88 | +### Quorum Calculation |
| 89 | +``` |
| 90 | +percentage = (totalVotes / requiredVotes) * 100 |
| 91 | +reached = totalVotes >= requiredVotes |
| 92 | +``` |
| 93 | + |
| 94 | +### Deadline Handling |
| 95 | +- Deadlines based on ledger numbers, not timestamps |
| 96 | +- Stellar: ~5 seconds per ledger |
| 97 | +- Remaining time calculated from current indexed ledger |
| 98 | + |
| 99 | +### Caching Strategy |
| 100 | +- Redis caching with configurable TTL (default: 60s) |
| 101 | +- Cache keys: `claims:list:{page}:{limit}:{status}` and `claims:detail:{id}` |
| 102 | +- Pattern-based invalidation on updates |
| 103 | +- Graceful degradation if Redis unavailable |
| 104 | + |
| 105 | +### Security |
| 106 | +- XSS prevention via HTML entity encoding |
| 107 | +- IPFS hash validation (CID v0/v1 format) |
| 108 | +- Stellar address validation (G... format, 56 chars) |
| 109 | +- Whitelisted evidence URL domains |
| 110 | +- Authorization via JWT with wallet address |
| 111 | + |
| 112 | +### Consistency Model |
| 113 | +- Indexer lag tracking (ledgers behind current) |
| 114 | +- `isStale` flag when lag > 5 ledgers |
| 115 | +- `isFinalized` for on-chain finality |
| 116 | +- Best-effort deadline accuracy based on indexer state |
| 117 | + |
| 118 | +## Database Schema |
| 119 | + |
| 120 | +### Claims |
| 121 | +```sql |
| 122 | +CREATE TABLE claims ( |
| 123 | + id SERIAL PRIMARY KEY, |
| 124 | + policyId VARCHAR NOT NULL, |
| 125 | + creatorAddress VARCHAR NOT NULL, |
| 126 | + amount VARCHAR NOT NULL, |
| 127 | + description TEXT, |
| 128 | + evidenceHash VARCHAR, |
| 129 | + status VARCHAR DEFAULT 'PENDING', |
| 130 | + isFinalized BOOLEAN DEFAULT FALSE, |
| 131 | + createdAtLedger INT NOT NULL, |
| 132 | + createdAt TIMESTAMP DEFAULT NOW(), |
| 133 | + updatedAt TIMESTAMP, |
| 134 | + updatedAtLedger INT DEFAULT 0 |
| 135 | +); |
| 136 | +CREATE INDEX idx_claims_status ON claims(status); |
| 137 | +CREATE INDEX idx_claims_createdAt ON claims(createdAt); |
| 138 | +CREATE INDEX idx_claims_policyId ON claims(policyId); |
| 139 | +``` |
| 140 | + |
| 141 | +### Votes |
| 142 | +```sql |
| 143 | +CREATE TABLE votes ( |
| 144 | + id SERIAL PRIMARY KEY, |
| 145 | + claimId INT REFERENCES claims(id) ON DELETE CASCADE, |
| 146 | + voterAddress VARCHAR NOT NULL, |
| 147 | + vote VARCHAR NOT NULL, -- 'YES' or 'NO' |
| 148 | + votingPower VARCHAR DEFAULT '1', |
| 149 | + txHash VARCHAR, |
| 150 | + votedAtLedger INT NOT NULL, |
| 151 | + createdAt TIMESTAMP DEFAULT NOW(), |
| 152 | + UNIQUE(claimId, voterAddress) |
| 153 | +); |
| 154 | +CREATE INDEX idx_votes_voterAddress ON votes(voterAddress); |
| 155 | +CREATE INDEX idx_votes_claimId ON votes(claimId); |
| 156 | +``` |
| 157 | + |
| 158 | +### Policies |
| 159 | +```sql |
| 160 | +CREATE TABLE policies ( |
| 161 | + id VARCHAR PRIMARY KEY, |
| 162 | + name VARCHAR NOT NULL, |
| 163 | + description TEXT, |
| 164 | + coverageAmount VARCHAR NOT NULL, |
| 165 | + premium VARCHAR NOT NULL, |
| 166 | + durationDays INT NOT NULL, |
| 167 | + requiredVotes INT DEFAULT 5, |
| 168 | + votingPeriodLedgers INT DEFAULT 720, |
| 169 | + votingDeadlineLedger INT NOT NULL, |
| 170 | + votingDeadlineTime TIMESTAMP NOT NULL, |
| 171 | + createdAt TIMESTAMP DEFAULT NOW(), |
| 172 | + updatedAt TIMESTAMP |
| 173 | +); |
| 174 | +``` |
| 175 | + |
| 176 | +### IndexerState |
| 177 | +```sql |
| 178 | +CREATE TABLE indexer_state ( |
| 179 | + id SERIAL PRIMARY KEY, |
| 180 | + lastLedger INT NOT NULL, |
| 181 | + lastCursor VARCHAR, |
| 182 | + updatedAt TIMESTAMP |
| 183 | +); |
| 184 | +``` |
| 185 | + |
| 186 | +## Environment Variables |
| 187 | +- `CACHE_TTL_SECONDS` - Cache TTL in seconds (default: 60) |
| 188 | +- `IPFS_GATEWAY` - IPFS gateway URL (default: https://ipfs.io) |
0 commit comments