File size: 9,004 Bytes
281395f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# Service Professional Ratings API - Quick Reference

## Base URL
```
/api/v1/ratings
```

## Authentication
Most endpoints require JWT Bearer token:
```
Authorization: Bearer <your_jwt_token>
```

---

## Endpoints Summary

| Method | Endpoint | Auth | Description |
|--------|----------|------|-------------|
| POST | `/` | βœ… | Create rating |
| GET | `/{rating_id}` | ❌ | Get rating by ID |
| PUT | `/{rating_id}` | βœ… | Update rating |
| DELETE | `/{rating_id}` | βœ… | Delete rating |
| POST | `/list` | ❌ | List ratings with filters |
| GET | `/stats/{professional_id}` | ❌ | Get rating statistics |
| POST | `/{rating_id}/responses` | βœ… | Add response to rating |
| POST | `/{rating_id}/helpful` | ❌ | Mark rating as helpful |

---

## 1. Create Rating

**POST** `/api/v1/ratings/`

Create a new rating for a service professional after order completion.

### Request
```json
{
  "order_id": "550e8400-e29b-41d4-a716-446655440000",
  "service_professional_id": "prof_123",
  "service_id": "svc_456",
  "rating_score": 4.5,
  "review_title": "Excellent service!",
  "review_text": "Very professional and skilled. Highly recommend!",
  "is_anonymous": false
}
```

### Response (201)
```json
{
  "rating_id": "660e8400-e29b-41d4-a716-446655440001",
  "order_id": "550e8400-e29b-41d4-a716-446655440000",
  "service_professional_id": "prof_123",
  "customer_id": "cust_456",
  "merchant_id": "merchant_789",
  "service_id": "svc_456",
  "rating_score": 4.5,
  "review_title": "Excellent service!",
  "review_text": "Very professional and skilled. Highly recommend!",
  "is_verified_purchase": true,
  "is_anonymous": false,
  "helpful_count": 0,
  "status": "active",
  "created_at": "2024-02-27T10:30:00",
  "updated_at": "2024-02-27T10:30:00",
  "responses": []
}
```

### Notes
- Rating score must be 1.0-5.0 in 0.5 increments (1.0, 1.5, 2.0, etc.)
- Customer ID extracted from JWT token
- Prevents duplicate ratings for same order/professional
- Order must exist and be completed

---

## 2. Get Rating by ID

**GET** `/api/v1/ratings/{rating_id}`

Retrieve a specific rating with all responses.

### Response (200)
```json
{
  "rating_id": "660e8400-e29b-41d4-a716-446655440001",
  "order_id": "550e8400-e29b-41d4-a716-446655440000",
  "service_professional_id": "prof_123",
  "customer_id": "cust_456",
  "rating_score": 4.5,
  "review_title": "Excellent service!",
  "review_text": "Very professional and skilled.",
  "is_verified_purchase": true,
  "is_anonymous": false,
  "helpful_count": 5,
  "status": "active",
  "created_at": "2024-02-27T10:30:00",
  "updated_at": "2024-02-27T10:30:00",
  "responses": [
    {
      "response_id": "770e8400-e29b-41d4-a716-446655440002",
      "rating_id": "660e8400-e29b-41d4-a716-446655440001",
      "responder_id": "prof_123",
      "responder_type": "professional",
      "response_text": "Thank you for your feedback!",
      "created_at": "2024-02-27T11:00:00",
      "updated_at": "2024-02-27T11:00:00"
    }
  ]
}
```

---

## 3. Update Rating

**PUT** `/api/v1/ratings/{rating_id}`

Update an existing rating (only by customer who created it).

### Request
```json
{
  "rating_score": 5.0,
  "review_text": "Updated review - even better than I thought!",
  "review_title": "Outstanding!"
}
```

### Response (200)
Same as Get Rating response with updated fields.

### Notes
- Only the customer who created the rating can update it
- All fields are optional
- Cannot change order_id or service_professional_id

---

## 4. Delete Rating

**DELETE** `/api/v1/ratings/{rating_id}`

Soft delete a rating (sets status to 'deleted').

### Response (204)
No content

### Notes
- Only the customer who created the rating can delete it
- Soft delete - data remains in database with status='deleted'
- Deleted ratings don't appear in public lists

---

## 5. List Ratings

**POST** `/api/v1/ratings/list`

List ratings with filters and pagination.

### Request
```json
{
  "service_professional_id": "prof_123",
  "min_rating": 4.0,
  "max_rating": 5.0,
  "status": "active",
  "limit": 20,
  "offset": 0
}
```

### Response (200)
```json
{
  "ratings": [
    {
      "rating_id": "660e8400-e29b-41d4-a716-446655440001",
      "service_professional_id": "prof_123",
      "rating_score": 4.5,
      "review_title": "Excellent service!",
      "review_text": "Very professional and skilled.",
      "helpful_count": 5,
      "status": "active",
      "created_at": "2024-02-27T10:30:00",
      "responses": []
    }
  ],
  "pagination": {
    "limit": 20,
    "offset": 0,
    "total": 150
  }
}
```

### Filter Options
- `service_professional_id` - Filter by professional
- `customer_id` - Filter by customer
- `merchant_id` - Filter by merchant
- `service_id` - Filter by service
- `min_rating` - Minimum rating score (1.0-5.0)
- `max_rating` - Maximum rating score (1.0-5.0)
- `status` - Filter by status (defaults to 'active')
- `limit` - Results per page (1-100, default 20)
- `offset` - Pagination offset (default 0)

---

## 6. Get Rating Statistics

**GET** `/api/v1/ratings/stats/{service_professional_id}`

Get aggregated rating statistics for a service professional.

### Response (200)
```json
{
  "service_professional_id": "prof_123",
  "total_ratings": 150,
  "average_rating": 4.3,
  "rating_distribution": {
    "5": 80,
    "4": 45,
    "3": 15,
    "2": 7,
    "1": 3
  },
  "verified_purchase_count": 145,
  "total_reviews_with_text": 120
}
```

### Notes
- Only includes active ratings
- Average rating rounded to 2 decimal places
- Distribution shows count per star rating (1-5)
- Useful for displaying professional profiles

---

## 7. Create Response to Rating

**POST** `/api/v1/ratings/{rating_id}/responses`

Service professional, merchant, or admin can respond to a rating.

### Request
```json
{
  "response_text": "Thank you for your feedback! We're glad you enjoyed our service and look forward to serving you again."
}
```

### Response (201)
```json
{
  "response_id": "770e8400-e29b-41d4-a716-446655440002",
  "rating_id": "660e8400-e29b-41d4-a716-446655440001",
  "responder_id": "prof_123",
  "responder_type": "professional",
  "response_text": "Thank you for your feedback!",
  "created_at": "2024-02-27T11:00:00",
  "updated_at": "2024-02-27T11:00:00"
}
```

### Authorization Rules
- **Professional**: Can respond to their own ratings
- **Merchant**: Can respond to ratings in their establishment
- **Admin**: Can respond to any rating

### Notes
- Response text must be 10-1000 characters
- Multiple responses allowed per rating
- Responder type determined from JWT role

---

## 8. Mark Rating as Helpful

**POST** `/api/v1/ratings/{rating_id}/helpful`

Increment the helpful count for a rating.

### Response (200)
Returns the updated rating with incremented `helpful_count`.

### Notes
- No authentication required
- Can be called multiple times (no duplicate prevention)
- Consider implementing client-side tracking to prevent abuse

---

## Error Responses

### 400 Bad Request
```json
{
  "detail": "Rating already exists for this order and professional"
}
```

### 401 Unauthorized
```json
{
  "detail": "Not authenticated"
}
```

### 403 Forbidden
```json
{
  "detail": "Not authorized to update this rating"
}
```

### 404 Not Found
```json
{
  "detail": "Rating not found"
}
```

### 500 Internal Server Error
```json
{
  "detail": "Failed to create rating: <error message>"
}
```

---

## Common Use Cases

### Display Professional Profile with Ratings
1. GET `/api/v1/ratings/stats/{professional_id}` - Get overall stats
2. POST `/api/v1/ratings/list` with filters - Get recent reviews

### Customer Leaves Review After Service
1. POST `/api/v1/ratings/` - Create rating
2. Customer receives confirmation

### Professional Responds to Review
1. GET `/api/v1/ratings/{rating_id}` - View rating
2. POST `/api/v1/ratings/{rating_id}/responses` - Add response

### Browse All Reviews for a Professional
1. POST `/api/v1/ratings/list` with `service_professional_id` filter
2. Paginate through results

---

## Rate Limiting Recommendations

Consider implementing rate limits:
- Rating creation: 5 per hour per customer
- Helpful marking: 20 per hour per IP
- Response creation: 10 per hour per professional
- List queries: 100 per minute per IP

---

## Testing with cURL

### Create Rating
```bash
curl -X POST "http://localhost:8000/api/v1/ratings/" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "order_id": "550e8400-e29b-41d4-a716-446655440000",
    "service_professional_id": "prof_123",
    "rating_score": 4.5,
    "review_title": "Great service!",
    "review_text": "Highly recommend!"
  }'
```

### Get Statistics
```bash
curl -X GET "http://localhost:8000/api/v1/ratings/stats/prof_123"
```

### List Ratings
```bash
curl -X POST "http://localhost:8000/api/v1/ratings/list" \
  -H "Content-Type: application/json" \
  -d '{
    "service_professional_id": "prof_123",
    "limit": 10
  }'
```

---

**Last Updated**: February 27, 2024