(req: NextRequest)
| 50 | const headers = { "Content-Type": "application/json" }; |
| 51 | |
| 52 | export default async function handler(req: NextRequest) { |
| 53 | try { |
| 54 | console.log("/api/v1/feedback called!"); |
| 55 | // Handle CORS preflight request |
| 56 | if (req.method === "OPTIONS") { |
| 57 | return new Response(undefined, { status: 200 }); |
| 58 | } |
| 59 | // Handle non-POST requests |
| 60 | if (req.method !== "POST") { |
| 61 | return new Response( |
| 62 | JSON.stringify({ |
| 63 | error: "Only POST requests allowed", |
| 64 | }), |
| 65 | { |
| 66 | status: 405, |
| 67 | headers, |
| 68 | }, |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | // Authenticate that the user is allowed to use this API |
| 73 | let token = req.headers |
| 74 | .get("Authorization") |
| 75 | ?.replace("Bearer ", "") |
| 76 | .replace("bearer ", ""); |
| 77 | |
| 78 | if (!token) { |
| 79 | return new Response(JSON.stringify({ error: "Authentication failed" }), { |
| 80 | status: 401, |
| 81 | headers, |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | let org: OrgJoinIsPaid | null = null; |
| 86 | if (token) { |
| 87 | const authRes = await supabase |
| 88 | .from("organizations") |
| 89 | .select("*, is_paid(*)") |
| 90 | .eq("api_key", token) |
| 91 | .single(); |
| 92 | if (authRes.error) throw new Error(authRes.error.message); |
| 93 | org = authRes.data; |
| 94 | } |
| 95 | if (!org) { |
| 96 | return new Response(JSON.stringify({ error: "Authentication failed" }), { |
| 97 | status: 401, |
| 98 | headers, |
| 99 | }); |
| 100 | } |
| 101 | |
| 102 | // Validate that the request body is of the correct format |
| 103 | const requestData = await req.json(); |
| 104 | if (!isValidBody<FeedbackType>(requestData, FeedbackZod)) { |
| 105 | return new Response(JSON.stringify({ error: "Invalid request body" }), { |
| 106 | status: 400, |
| 107 | headers, |
| 108 | }); |
| 109 | } |
nothing calls this directly
no outgoing calls
no test coverage detected