AeThex-OS/temp-forge-extract/aethex-forge-main/api/foundation/courses.ts
MrPiglr b3c308b2c8 Add functional marketplace modules, bottom nav bar, root terminal, arcade games
- ModuleManager: Central tracking for installed marketplace modules
- DataAnalyzerWidget: Real-time CPU/RAM/Battery/Storage widget (unlocked by Data Analyzer module)
- BottomNavBar: Navigation bar for Projects/Chat/Marketplace/Settings
- RootShell: Real root command execution utility
- TerminalActivity: Full root shell with neofetch, sysinfo, real Linux commands
- Terminal Pro module: Adds aliases (ll, la, h), command history
- ArcadeActivity + SnakeGame: Pixel Arcade module unlocks retro games
- fade_in/fade_out animations for smooth transitions
2026-02-18 22:03:50 -07:00

100 lines
2.8 KiB
TypeScript

import type { VercelRequest, VercelResponse } from "@vercel/node";
import { getAdminClient } from "../_supabase.js";
export default async function handler(req: VercelRequest, res: VercelResponse) {
const admin = getAdminClient();
// Only authenticated requests for enrollment
const authHeader = req.headers.authorization;
let userId: string | null = null;
if (authHeader) {
const token = authHeader.replace("Bearer ", "");
const {
data: { user },
error: authError,
} = await admin.auth.getUser(token);
if (!authError && user) {
userId = user.id;
}
}
try {
if (req.method === "GET") {
// List all published courses
const { data: courses, error: coursesError } = await admin
.from("foundation_courses")
.select(
`
*,
instructor:user_profiles(id, full_name, avatar_url)
`,
)
.eq("is_published", true)
.order("order_index", { ascending: true });
if (coursesError) {
return res.status(500).json({ error: coursesError.message });
}
// If user is authenticated, get their enrollment status
let enrollments: any = {};
if (userId) {
const { data: userEnrollments } = await admin
.from("foundation_enrollments")
.select("course_id, progress_percent, status, completed_at")
.eq("user_id", userId);
if (userEnrollments) {
enrollments = Object.fromEntries(
userEnrollments.map((e: any) => [e.course_id, e]),
);
}
}
const coursesWithStatus = (courses || []).map((course: any) => ({
...course,
userEnrollment: enrollments[course.id] || null,
}));
return res.status(200).json(coursesWithStatus);
}
if (req.method === "POST") {
// Enroll in a course
if (!userId) {
return res.status(401).json({ error: "Unauthorized" });
}
const { course_id } = req.body;
if (!course_id) {
return res.status(400).json({ error: "course_id required" });
}
const { data: enrollment, error: enrollError } = await admin
.from("foundation_enrollments")
.upsert(
{
user_id: userId,
course_id,
progress_percent: 0,
status: "in_progress",
enrolled_at: new Date().toISOString(),
},
{ onConflict: "user_id,course_id" },
)
.select()
.single();
if (enrollError) {
return res.status(500).json({ error: enrollError.message });
}
return res.status(200).json(enrollment);
}
return res.status(405).json({ error: "Method not allowed" });
} catch (error: any) {
return res.status(500).json({ error: error?.message || "Server error" });
}
}