import { NextRequest, NextResponse } from "next/server"; import { prisma } from "@/lib/prisma"; function generateCode(length = 6) { const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; let code = ""; for (let i = 0; i < length; i++) code += chars.charAt(Math.floor(Math.random() * chars.length)); return code; } export async function GET(req: NextRequest) { const { searchParams } = new URL(req.url); const classId = searchParams.get("classId"); const establishmentId = searchParams.get("establishmentId"); const where: any = {}; if (classId) where.classId = classId; if (establishmentId) { const classes = await prisma.class.findMany({ where: { establishmentId }, select: { id: true } }); where.classId = { in: classes.map((c) => c.id) }; } const students = await prisma.student.findMany({ where, include: { class: true, nodes: true }, orderBy: { createdAt: "desc" }, }); return NextResponse.json(students); } export async function POST(req: NextRequest) { const body = await req.json(); const { classId, firstName, lastName, email } = body; const student = await prisma.student.create({ data: { classId, firstName, lastName, email, activationCode: generateCode(), }, }); return NextResponse.json(student, { status: 201 }); }