forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.ts
More file actions
51 lines (44 loc) · 1.64 KB
/
Copy pathlist.ts
File metadata and controls
51 lines (44 loc) · 1.64 KB
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
/**
* Review List Endpoint
*
* Returns public reviews for a specific prompt, sorted by most recent first.
* Filters out hidden reviews and strips internal moderation metadata.
*/
import { getPublicReviews } from "../../src/lib/reviews/reviewStore";
export default async function handler(req: any, res: any) {
if (req.method !== "GET") {
res.status(405).json({ error: "Method not allowed" });
return;
}
const { promptId } = req.query;
if (!promptId) {
res.status(400).json({ error: "promptId query parameter is required" });
return;
}
try {
// Get public reviews (excludes status === "hidden" and strips moderation metadata)
const publicReviews = getPublicReviews(String(promptId));
// Calculate stats based on visible reviews
const averageRating = publicReviews.length > 0
? publicReviews.reduce((sum, r) => sum + r.rating, 0) / publicReviews.length
: 0;
res.status(200).json({
reviews: publicReviews,
stats: {
total: publicReviews.length,
averageRating: Math.round(averageRating * 10) / 10,
distribution: {
5: publicReviews.filter((r) => r.rating === 5).length,
4: publicReviews.filter((r) => r.rating === 4).length,
3: publicReviews.filter((r) => r.rating === 3).length,
2: publicReviews.filter((r) => r.rating === 2).length,
1: publicReviews.filter((r) => r.rating === 1).length,
},
},
});
} catch (error) {
const message = error instanceof Error ? error.message : "Failed to fetch reviews";
console.error("Review fetch error:", message);
res.status(500).json({ error: message });
}
}