- Report Card Generator: Professional report cards with PDF export - Subject grades table with competency levels - Competency radar chart, teacher comments - Template management, batch generation - Status workflow: draft → review → published → archived - Seating Chart View: Visual classroom layout - Grid-based classroom with desk positions - Multiple layout templates (rows, groups, U-shape, circle) - Click-to-assign student placement - Randomize and smart arrange features - Student info popover - School Library View: Book lending system - Book catalog with search and filter - Checkout system with due dates and renewals - Overdue tracking with notifications - Reservation queue system - Statistics dashboard with charts - Enhanced Parent Portal: Comprehensive dashboard - Child overview with multiple children selector - Academic progress with competency radar chart - Attendance & illness with approval - Communication, schedule, and reports tabs - Bug fixes: i18n breadcrumb keys for new views - Styling: Gradient headers, animations, hover effects across all new views - 200+ i18n keys added, 15+ API routes, 3+ Prisma models
176 lines
5.4 KiB
TypeScript
Executable File
176 lines
5.4 KiB
TypeScript
Executable File
import { NextResponse } from 'next/server';
|
|
import { db } from '@/lib/db';
|
|
import { getSession } from '@/lib/auth';
|
|
|
|
/**
|
|
* Check if a student belongs to the current user (STUDENT role).
|
|
* Uses userId (direct link) with name-matching fallback for legacy data.
|
|
*/
|
|
function isOwnStudent(
|
|
student: { userId: string | null; firstName: string; lastName: string; schoolId: string | null },
|
|
user: { id: string; firstName: string; lastName: string; schoolId: string | null }
|
|
): boolean {
|
|
if (student.userId === user.id) return true;
|
|
// Fallback: match by name + school for legacy records without userId
|
|
if (
|
|
student.userId === null &&
|
|
student.firstName === user.firstName &&
|
|
student.lastName === user.lastName &&
|
|
student.schoolId === user.schoolId
|
|
) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
export async function GET(
|
|
_request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const session = await getSession();
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
|
|
}
|
|
|
|
const { id } = await params;
|
|
const goal = await db.studentGoal.findUnique({
|
|
where: { id },
|
|
include: {
|
|
student: {
|
|
select: { id: true, firstName: true, lastName: true, schoolId: true, userId: true },
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!goal) {
|
|
return NextResponse.json({ error: 'Goal not found' }, { status: 404 });
|
|
}
|
|
|
|
// Verify access
|
|
if (session.user?.role === 'STUDENT') {
|
|
if (!isOwnStudent(goal.student, session.user)) {
|
|
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
|
}
|
|
} else if (session.user?.role !== 'SUPER_ADMIN') {
|
|
if (goal.student.schoolId !== session.user?.schoolId) {
|
|
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
|
}
|
|
}
|
|
|
|
return NextResponse.json(goal);
|
|
} catch (error) {
|
|
console.error('StudentGoal GET by ID error:', error);
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function PUT(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const session = await getSession();
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
|
|
}
|
|
|
|
const { id } = await params;
|
|
const body = await request.json();
|
|
const { title, description, targetDate, progress, status } = body;
|
|
|
|
const existingGoal = await db.studentGoal.findUnique({
|
|
where: { id },
|
|
include: {
|
|
student: {
|
|
select: { id: true, firstName: true, lastName: true, schoolId: true, userId: true },
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!existingGoal) {
|
|
return NextResponse.json({ error: 'Goal not found' }, { status: 404 });
|
|
}
|
|
|
|
// Verify access
|
|
if (session.user?.role === 'STUDENT') {
|
|
if (!isOwnStudent(existingGoal.student, session.user)) {
|
|
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
|
}
|
|
} else if (session.user?.role !== 'SUPER_ADMIN') {
|
|
if (existingGoal.student.schoolId !== session.user?.schoolId) {
|
|
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
|
}
|
|
}
|
|
|
|
const updateData: Record<string, unknown> = {};
|
|
if (title !== undefined) updateData.title = title;
|
|
if (description !== undefined) updateData.description = description;
|
|
if (targetDate !== undefined) updateData.targetDate = targetDate ? new Date(targetDate) : null;
|
|
if (progress !== undefined) updateData.progress = Math.min(100, Math.max(0, progress));
|
|
if (status !== undefined) updateData.status = status;
|
|
|
|
// Auto-complete if progress reaches 100
|
|
if (progress !== undefined && progress >= 100) {
|
|
updateData.status = 'completed';
|
|
updateData.progress = 100;
|
|
}
|
|
|
|
const goal = await db.studentGoal.update({
|
|
where: { id },
|
|
data: updateData,
|
|
});
|
|
|
|
return NextResponse.json(goal);
|
|
} catch (error) {
|
|
console.error('StudentGoal PUT error:', error);
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(
|
|
_request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const session = await getSession();
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
|
|
}
|
|
|
|
const { id } = await params;
|
|
const existingGoal = await db.studentGoal.findUnique({
|
|
where: { id },
|
|
include: {
|
|
student: {
|
|
select: { id: true, firstName: true, lastName: true, schoolId: true, userId: true },
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!existingGoal) {
|
|
return NextResponse.json({ error: 'Goal not found' }, { status: 404 });
|
|
}
|
|
|
|
// Verify access
|
|
if (session.user?.role === 'STUDENT') {
|
|
if (!isOwnStudent(existingGoal.student, session.user)) {
|
|
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
|
}
|
|
} else if (session.user?.role !== 'SUPER_ADMIN') {
|
|
if (existingGoal.student.schoolId !== session.user?.schoolId) {
|
|
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
|
}
|
|
}
|
|
|
|
await db.studentGoal.delete({
|
|
where: { id },
|
|
});
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error('StudentGoal DELETE error:', error);
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
}
|