(req: NextRequest)
| 52 | }; |
| 53 | |
| 54 | export default async function handler(req: NextRequest) { |
| 55 | console.log("Create org called"); |
| 56 | if (req.method !== "POST") { |
| 57 | return new Response( |
| 58 | JSON.stringify({ |
| 59 | error: "Only POST requests allowed", |
| 60 | }), |
| 61 | { |
| 62 | headers, |
| 63 | status: 400, |
| 64 | }, |
| 65 | ); |
| 66 | } |
| 67 | const { session } = await getSessionFromCookie(req); |
| 68 | if (!session) { |
| 69 | return new Response(JSON.stringify({ error: "Unauthorized" }), { |
| 70 | status: 401, |
| 71 | headers, |
| 72 | }); |
| 73 | } |
| 74 | if (ratelimit) { |
| 75 | // If over limit, success is false |
| 76 | const { success } = await ratelimit.limit(session.user.id); |
| 77 | if (!success) { |
| 78 | return new Response( |
| 79 | JSON.stringify({ error: "Rate limit hit (1 request/30s)" }), |
| 80 | { |
| 81 | status: 429, |
| 82 | headers, |
| 83 | }, |
| 84 | ); |
| 85 | } |
| 86 | } |
| 87 | const api_key = generateApiKey(); |
| 88 | const { data, error } = await supabase |
| 89 | .from("organizations") |
| 90 | .insert({ |
| 91 | name: "", |
| 92 | api_key, |
| 93 | description: "", |
| 94 | join_link_id: uuidv4(), |
| 95 | model: process.env.NEXT_PUBLIC_FINETUNED_GPT_DEFAULT ?? "gpt-4-0613", |
| 96 | bertie_enabled: true, // Set to v2 by default |
| 97 | }) |
| 98 | .select(); |
| 99 | if (error) throw new Error(error.message); |
| 100 | if (data === null) { |
| 101 | throw new Error("No data returned from organizations insert"); |
| 102 | } |
| 103 | const profileResp = await supabase |
| 104 | .from("profiles") |
| 105 | .update({ org_id: data[0].id }) |
| 106 | .eq("id", session.user.id) |
| 107 | .select(); |
| 108 | if (profileResp.error) throw profileResp.error; |
| 109 | if (profileResp.data === null) |
| 110 | throw new Error("No data returned from profiles update"); |
| 111 |
nothing calls this directly
no test coverage detected