(req: NextRequest)
| 38 | }; |
| 39 | |
| 40 | export default async function handler(req: NextRequest) { |
| 41 | if (req.method !== "POST") { |
| 42 | return new Response( |
| 43 | JSON.stringify({ |
| 44 | error: "Only POST requests allowed", |
| 45 | }), |
| 46 | { status: 405, headers }, |
| 47 | ); |
| 48 | } |
| 49 | if (!isValidBody<JoinOrgType>(req.body, JoinOrgZod)) { |
| 50 | return new Response(JSON.stringify({ message: "Invalid request body" }), { |
| 51 | status: 400, |
| 52 | headers, |
| 53 | }); |
| 54 | } |
| 55 | const { session } = await getSessionFromCookie(req); |
| 56 | if (!session) { |
| 57 | return new Response(JSON.stringify({ error: "Unauthorized" }), { |
| 58 | status: 401, |
| 59 | headers, |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | const { data, error } = await supabase |
| 64 | .from("organizations") |
| 65 | .select("id") |
| 66 | .eq("join_link_id", req.body.join_id); |
| 67 | if (error) throw new Error(error.message); |
| 68 | if (data === null) { |
| 69 | throw new Error("No data returned from organizations insert"); |
| 70 | } |
| 71 | |
| 72 | const profileResp = await supabase |
| 73 | .from("profiles") |
| 74 | .update({ org_id: data[0].id }) |
| 75 | .eq("id", session.user.id) |
| 76 | .select(); |
| 77 | if (profileResp.error) throw profileResp.error; |
| 78 | if (profileResp.data === null) |
| 79 | throw new Error("No data returned from profiles update"); |
| 80 | |
| 81 | return new Response(JSON.stringify({ success: true }), { |
| 82 | status: 200, |
| 83 | headers, |
| 84 | }); |
| 85 | } |
nothing calls this directly
no test coverage detected