fix(api): clamp activity and review pagination#339
Conversation
Greptile SummaryThis PR introduces a shared
Confidence Score: 3/5Safe to merge for the pagination fix itself, but the reviews endpoint now returns a wrong average rating on any paginated request where there are more reviews than one page. The src/app/api/users/[username]/reviews/route.ts needs the average-rating calculation fixed before merging. Important Files Changed
Sequence DiagramsequenceDiagram
participant C as Client
participant R as /api/users/[username]/reviews
participant S as Supabase
C->>R: "GET ?limit=10&offset=0"
R->>R: parsePaginationParam(limit, 10, 1, 50)
R->>R: parsePaginationParam(offset, 0, 0, 100_000)
R->>S: profiles.select("id").eq("username", ...)
S-->>R: profile
R->>S: "reviews.select(*).range(offset, offset+limit-1) [count:exact]"
S-->>R: reviews (page slice) + count (total)
Note over R: ⚠️ average_rating computed from page slice only, not all reviews
R-->>C: "{ data, summary: { average_rating, total_reviews }, pagination }"
Reviews (1): Last reviewed commit: "fix(api): clamp activity and review pagi..." | Re-trigger Greptile |
| // Calculate average rating from all reviews | ||
| const totalReviews = count || 0; | ||
| let averageRating = 0; | ||
| if (allRatings && allRatings.length > 0) { | ||
| const sumRatings = allRatings.reduce((sum, r) => sum + r.rating, 0); | ||
| averageRating = sumRatings / allRatings.length; | ||
| if (reviews && reviews.length > 0) { | ||
| const sumRatings = reviews.reduce((sum, r) => sum + r.rating, 0); | ||
| averageRating = totalReviews > 0 ? sumRatings / reviews.length : 0; | ||
| } |
There was a problem hiding this comment.
average_rating computed from page slice, not all reviews
The old code issued a second Supabase query (allRatings) that fetched every rating row for the reviewee before computing the mean. That query was removed here; reviews is now the page-sized subset returned by .range(offset, offset + limit - 1). For a user with 50 total reviews and the default limit=10, averageRating will reflect only the 10 reviews on the current page, returning a different (and wrong) value on every page. The comment on line 55 still says "from all reviews" but that is no longer true.
Summary
Fixes #338.
Verification