| 14 | const VALID_CATEGORIES = ["feature", "improvement", "praise", "other"] |
| 15 | |
| 16 | export async function POST(request: NextRequest) { |
| 17 | try { |
| 18 | const ip = request.headers.get("x-forwarded-for") || request.headers.get("x-real-ip") || "unknown" |
| 19 | |
| 20 | const { success } = await ratelimit.limit(ip) |
| 21 | if (!success) { |
| 22 | return new Response(JSON.stringify({ error: "Rate limit exceeded" }), { |
| 23 | status: 429, |
| 24 | headers: { "Content-Type": "application/json" }, |
| 25 | }) |
| 26 | } |
| 27 | |
| 28 | const body = await request.json() |
| 29 | |
| 30 | if (!body.feedback || typeof body.feedback !== "string") { |
| 31 | return new Response(JSON.stringify({ error: "Feedback is required" }), { |
| 32 | status: 400, |
| 33 | headers: { "Content-Type": "application/json" }, |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | if (body.feedback.length < 10 || body.feedback.length > 2000) { |
| 38 | return new Response(JSON.stringify({ error: "Feedback must be between 10 and 2000 characters" }), { |
| 39 | status: 400, |
| 40 | headers: { "Content-Type": "application/json" }, |
| 41 | }) |
| 42 | } |
| 43 | |
| 44 | if (!body.category || !VALID_CATEGORIES.includes(body.category)) { |
| 45 | return new Response(JSON.stringify({ error: "Invalid category" }), { |
| 46 | status: 400, |
| 47 | headers: { "Content-Type": "application/json" }, |
| 48 | }) |
| 49 | } |
| 50 | |
| 51 | const { feedback, category, version, deviceId } = body |
| 52 | const timestamp = Date.now() |
| 53 | const date = new Date().toISOString().split("T")[0] |
| 54 | |
| 55 | const feedbackId = `feedback:${date}:${timestamp}:${Math.random().toString(36).slice(2, 8)}` |
| 56 | await redis.set( |
| 57 | feedbackId, |
| 58 | { |
| 59 | feedback, |
| 60 | category, |
| 61 | version: version?.slice(0, 50), |
| 62 | deviceId: deviceId?.slice(0, 50), |
| 63 | timestamp, |
| 64 | ip: ip.slice(0, 100), |
| 65 | }, |
| 66 | { ex: 60 * 60 * 24 * 90 }, |
| 67 | ) |
| 68 | |
| 69 | await redis.sadd(`feedback:${date}`, feedbackId) |
| 70 | |
| 71 | await redis.hincrby(`feedback_categories:${date}`, category, 1) |
| 72 | |
| 73 | return new Response(JSON.stringify({ success: true }), { |