From a73d4b843efe18d1071a929e60c78f806d2de809 Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Sat, 15 Nov 2025 08:50:14 +0000 Subject: [PATCH] FOUNDATION Mentorships API - List/manage mentorships cgen-14a224f7bc29493d8f9c5d185109f3ec --- api/foundation/mentorships.ts | 108 ++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 api/foundation/mentorships.ts diff --git a/api/foundation/mentorships.ts b/api/foundation/mentorships.ts new file mode 100644 index 00000000..34bd55f3 --- /dev/null +++ b/api/foundation/mentorships.ts @@ -0,0 +1,108 @@ +import type { VercelRequest, VercelResponse } from "@vercel/node"; +import { getAdminClient } from "../_supabase"; + +export default async function handler(req: VercelRequest, res: VercelResponse) { + const admin = getAdminClient(); + + // Only authenticated requests + const authHeader = req.headers.authorization; + if (!authHeader) { + return res.status(401).json({ error: "Unauthorized" }); + } + + const token = authHeader.replace("Bearer ", ""); + const { data: { user }, error: authError } = await admin.auth.getUser(token); + + if (authError || !user) { + return res.status(401).json({ error: "Invalid token" }); + } + + try { + if (req.method === "GET") { + // Get user's mentorships (as mentee or mentor) + const role = req.query.role as string | undefined; // 'mentor' or 'mentee' + + let mentorships; + if (role === "mentor") { + const { data: m } = await admin + .from("foundation_mentorships") + .select(` + *, + mentee:user_profiles!mentee_id(id, full_name, avatar_url, email) + `) + .eq("mentor_id", user.id) + .order("created_at", { ascending: false }); + mentorships = m; + } else if (role === "mentee") { + const { data: m } = await admin + .from("foundation_mentorships") + .select(` + *, + mentor:user_profiles!mentor_id(id, full_name, avatar_url, email) + `) + .eq("mentee_id", user.id) + .order("created_at", { ascending: false }); + mentorships = m; + } else { + // Get both roles + const { data: asMentor } = await admin + .from("foundation_mentorships") + .select( + `*, + mentee:user_profiles!mentee_id(id, full_name, avatar_url, email)` + ) + .eq("mentor_id", user.id); + + const { data: asMentee } = await admin + .from("foundation_mentorships") + .select( + `*, + mentor:user_profiles!mentor_id(id, full_name, avatar_url, email)` + ) + .eq("mentee_id", user.id); + + return res.status(200).json({ + as_mentor: asMentor || [], + as_mentee: asMentee || [], + }); + } + + return res.status(200).json({ + mentorships: mentorships || [], + }); + } + + if (req.method === "POST") { + // Request a mentor + const { mentor_id } = req.body; + + if (!mentor_id) { + return res.status(400).json({ error: "mentor_id required" }); + } + + const { data: mentorship, error: createError } = await admin + .from("foundation_mentorships") + .insert({ + mentor_id, + mentee_id: user.id, + status: "pending", + requested_at: new Date().toISOString(), + }) + .select() + .single(); + + if (createError) { + if (createError.code === "23505") { + return res.status(400).json({ error: "Mentorship already exists" }); + } + return res.status(500).json({ error: createError.message }); + } + + return res.status(201).json(mentorship); + } + + return res.status(405).json({ error: "Method not allowed" }); + } catch (error: any) { + return res.status(500).json({ error: error?.message || "Server error" }); + } +}