(req: NextRequest)
| 96 | const headers = { "Content-Type": "application/json" }; |
| 97 | |
| 98 | export default async function handler(req: NextRequest) { |
| 99 | try { |
| 100 | console.log("/api/v3/generate-answer-description called!"); |
| 101 | if (req.method === "OPTIONS") { |
| 102 | // Handle CORS preflight request |
| 103 | return new Response(undefined, { status: 200 }); |
| 104 | } |
| 105 | if (req.method !== "POST") { |
| 106 | // Handle non-POST requests |
| 107 | return new Response( |
| 108 | JSON.stringify({ |
| 109 | error: "Only POST requests allowed", |
| 110 | }), |
| 111 | { status: 405, headers }, |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | // Validate that the request body is of the correct format |
| 116 | const requestData = await req.json(); |
| 117 | if ( |
| 118 | !isValidBody<GenerateAnswerOfflineType>( |
| 119 | requestData, |
| 120 | GenerateDescriptionZod, |
| 121 | ) |
| 122 | ) { |
| 123 | return new Response(JSON.stringify({ message: "Invalid request body" }), { |
| 124 | status: 400, |
| 125 | headers, |
| 126 | }); |
| 127 | } |
| 128 | console.log("Req body", requestData); |
| 129 | |
| 130 | let supabase = serviceLevelSupabase; |
| 131 | let { session, supabase: localSupabase } = await getSessionFromCookie(req); |
| 132 | if (localSupabase) supabase = localSupabase; |
| 133 | if (!session) { |
| 134 | const authToken = req.headers.get("Authorization"); |
| 135 | if ( |
| 136 | authToken && |
| 137 | // If the user is authenticated with the service level key, allow access |
| 138 | authToken.includes(process.env.SERVICE_LEVEL_KEY_SUPABASE ?? "") |
| 139 | ) { |
| 140 | const answerData = await serviceLevelSupabase |
| 141 | .from("approval_answers") |
| 142 | .select("org_id, organizations(*, profiles(id))") |
| 143 | .eq("id", requestData.answer_id) |
| 144 | .single(); |
| 145 | if (answerData.error) throw new Error(answerData.error.message); |
| 146 | if ( |
| 147 | !answerData.data.organizations || |
| 148 | !answerData.data.organizations.profiles |
| 149 | ) { |
| 150 | return new Response(JSON.stringify({ error: "Unauthorized" }), { |
| 151 | status: 401, |
| 152 | headers, |
| 153 | }); |
| 154 | } |
| 155 | session = { |
nothing calls this directly
no test coverage detected