feat: Round 21 - Major feature additions
- Schlaukopf.de clone: Subject Content Management System (7 categories, 416 topics) - AI Integration: Pollination AI, OpenAI, Anthropic (chat, image/video generation) - Virtual Character System: 5 animated characters (Owl, Dragon, Robot, Cat, Wizard) - Illness Reporting with Parent Approval: privacy-first, student reports need parent approval - Student-Teacher Communication Rooms: real-time chat, voice, file sharing - Counseling/Guidance System: 4 types, calendar integration - Disciplinary Committee System: case management, severity levels - AI Test Generation: auto-generate practice tests before exams - Teacher Grading Review: AI-powered grading audit - VICE_PRINCIPAL role with same privileges as SCHOOL_ADMIN - Class responsible teacher (Klassenlehrer) field - 15 new Prisma models (80+ total) - 30+ new API route files - 8 new frontend components - 400+ i18n keys - Professional styling with gradient headers, animated counters, skeleton loading
This commit is contained in:
1 parent
04ae7aa80c
commit
f1776932d7
48 files changed
+13559
-4
No files matched your search
@@ -0,0 +1,205 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { getSession } from '@/lib/auth';
|
||||
|
||||
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 report = await db.illnessReport.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
reporter: { select: { id: true, firstName: true, lastName: true } },
|
||||
approver: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
});
|
||||
|
||||
if (!report) {
|
||||
return NextResponse.json({ error: 'Report not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const role = session.user?.role;
|
||||
const userId = session.userId;
|
||||
|
||||
// Access control
|
||||
if (role === 'STUDENT') {
|
||||
const student = await db.student.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ userId },
|
||||
{ userId: null, firstName: session.user?.firstName, lastName: session.user?.lastName, schoolId: session.user?.schoolId },
|
||||
],
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
if (!student || student.id !== report.studentId) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
} else if (role === 'PARENT') {
|
||||
const parentLink = await db.parentStudentLink.findFirst({
|
||||
where: { parentId: userId, studentId: report.studentId },
|
||||
});
|
||||
if (!parentLink) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
} else if (role === 'TEACHER') {
|
||||
if (!report.isVisibleToTeacher) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
} else if (role === 'SCHOOL_ADMIN' || role === 'VICE_PRINCIPAL') {
|
||||
if (!report.isVisibleToAdmin) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json(report);
|
||||
} catch (error) {
|
||||
console.error('IllnessReport GET [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 report = await db.illnessReport.findUnique({ where: { id } });
|
||||
|
||||
if (!report) {
|
||||
return NextResponse.json({ error: 'Report not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const role = session.user?.role;
|
||||
const userId = session.userId;
|
||||
const body = await request.json();
|
||||
|
||||
// PARENT can approve/reject pending reports
|
||||
if (body.parentApprovalStatus && (role === 'PARENT' || role === 'SCHOOL_ADMIN' || role === 'VICE_PRINCIPAL')) {
|
||||
const newStatus = body.parentApprovalStatus;
|
||||
if (newStatus !== 'approved' && newStatus !== 'rejected') {
|
||||
return NextResponse.json({ error: 'Invalid approval status' }, { status: 400 });
|
||||
}
|
||||
|
||||
if (role === 'PARENT') {
|
||||
const parentLink = await db.parentStudentLink.findFirst({
|
||||
where: { parentId: userId, studentId: report.studentId },
|
||||
});
|
||||
if (!parentLink) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await db.illnessReport.update({
|
||||
where: { id },
|
||||
data: {
|
||||
parentApprovalStatus: newStatus,
|
||||
parentApprovedBy: userId,
|
||||
parentApprovedAt: new Date(),
|
||||
isVisibleToTeacher: newStatus === 'approved',
|
||||
isVisibleToAdmin: newStatus === 'approved',
|
||||
status: newStatus === 'approved' ? 'approved' : 'rejected',
|
||||
},
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
reporter: { select: { id: true, firstName: true, lastName: true } },
|
||||
approver: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(updated);
|
||||
}
|
||||
|
||||
// STUDENT can update own pending reports (before parent approval)
|
||||
if (role === 'STUDENT') {
|
||||
const student = await db.student.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ userId },
|
||||
{ userId: null, firstName: session.user?.firstName, lastName: session.user?.lastName, schoolId: session.user?.schoolId },
|
||||
],
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
if (!student || student.id !== report.studentId) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
if (report.parentApprovalStatus !== 'pending') {
|
||||
return NextResponse.json({ error: 'Cannot update report after parent approval' }, { status: 400 });
|
||||
}
|
||||
|
||||
const updateData: Record<string, unknown> = {};
|
||||
if (body.reason) updateData.reason = body.reason;
|
||||
if (body.description !== undefined) updateData.description = body.description;
|
||||
if (body.startDate) updateData.startDate = new Date(body.startDate);
|
||||
if (body.endDate !== undefined) updateData.endDate = body.endDate ? new Date(body.endDate) : null;
|
||||
if (body.documentUrl !== undefined) updateData.documentUrl = body.documentUrl;
|
||||
|
||||
const updated = await db.illnessReport.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
reporter: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(updated);
|
||||
}
|
||||
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
} catch (error) {
|
||||
console.error('IllnessReport PUT [id] 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 report = await db.illnessReport.findUnique({ where: { id } });
|
||||
|
||||
if (!report) {
|
||||
return NextResponse.json({ error: 'Report not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const role = session.user?.role;
|
||||
const userId = session.userId;
|
||||
|
||||
// Only the reporter or admin can delete
|
||||
if (role !== 'SUPER_ADMIN' && role !== 'SCHOOL_ADMIN' && role !== 'VICE_PRINCIPAL') {
|
||||
if (report.reportedBy !== userId) {
|
||||
return NextResponse.json({ error: 'Only the reporter or admin can delete' }, { status: 403 });
|
||||
}
|
||||
}
|
||||
|
||||
await db.illnessReport.delete({ where: { id } });
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('IllnessReport DELETE [id] error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { getSession } from '@/lib/auth';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const session = await getSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
|
||||
}
|
||||
|
||||
const role = session.user?.role;
|
||||
const userId = session.userId;
|
||||
|
||||
if (role !== 'PARENT' && role !== 'SCHOOL_ADMIN' && role !== 'VICE_PRINCIPAL') {
|
||||
return NextResponse.json({ error: 'Only parents or admins can approve' }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { reportId, action } = body; // action: "approve" or "reject"
|
||||
|
||||
if (!reportId || !action) {
|
||||
return NextResponse.json({ error: 'reportId and action are required' }, { status: 400 });
|
||||
}
|
||||
|
||||
if (action !== 'approve' && action !== 'reject') {
|
||||
return NextResponse.json({ error: 'Action must be "approve" or "reject"' }, { status: 400 });
|
||||
}
|
||||
|
||||
const report = await db.illnessReport.findUnique({ where: { id: reportId } });
|
||||
if (!report) {
|
||||
return NextResponse.json({ error: 'Report not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
if (report.parentApprovalStatus !== 'pending') {
|
||||
return NextResponse.json({ error: 'Report already processed' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Verify parent is linked to this student
|
||||
if (role === 'PARENT') {
|
||||
const parentLink = await db.parentStudentLink.findFirst({
|
||||
where: { parentId: userId, studentId: report.studentId },
|
||||
});
|
||||
if (!parentLink) {
|
||||
return NextResponse.json({ error: 'You can only approve reports for your children' }, { status: 403 });
|
||||
}
|
||||
}
|
||||
|
||||
const newStatus = action === 'approve' ? 'approved' : 'rejected';
|
||||
|
||||
const updated = await db.illnessReport.update({
|
||||
where: { id: reportId },
|
||||
data: {
|
||||
parentApprovalStatus: newStatus,
|
||||
parentApprovedBy: userId,
|
||||
parentApprovedAt: new Date(),
|
||||
isVisibleToTeacher: newStatus === 'approved',
|
||||
isVisibleToAdmin: newStatus === 'approved',
|
||||
status: newStatus === 'approved' ? 'approved' : 'rejected',
|
||||
},
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
reporter: { select: { id: true, firstName: true, lastName: true } },
|
||||
approver: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(updated);
|
||||
} catch (error) {
|
||||
console.error('IllnessReport approve error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { getSession } from '@/lib/auth';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const session = await getSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const studentId = searchParams.get('studentId');
|
||||
const status = searchParams.get('status');
|
||||
const parentApprovalStatus = searchParams.get('parentApprovalStatus');
|
||||
|
||||
const role = session.user?.role;
|
||||
const userId = session.userId;
|
||||
|
||||
const where: Record<string, unknown> = {};
|
||||
|
||||
if (role === 'STUDENT') {
|
||||
// Students can only see their own reports
|
||||
const student = await db.student.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ userId },
|
||||
{
|
||||
userId: null,
|
||||
firstName: session.user?.firstName,
|
||||
lastName: session.user?.lastName,
|
||||
schoolId: session.user?.schoolId,
|
||||
},
|
||||
],
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
if (!student) {
|
||||
return NextResponse.json([]);
|
||||
}
|
||||
where.studentId = student.id;
|
||||
} else if (role === 'PARENT') {
|
||||
// Parents can see reports for their children
|
||||
const parentLinks = await db.parentStudentLink.findMany({
|
||||
where: { parentId: userId },
|
||||
select: { studentId: true },
|
||||
});
|
||||
const childIds = parentLinks.map((l) => l.studentId);
|
||||
if (childIds.length === 0) {
|
||||
return NextResponse.json([]);
|
||||
}
|
||||
where.studentId = { in: childIds };
|
||||
} else if (role === 'TEACHER') {
|
||||
// Teachers can only see approved reports
|
||||
where.isVisibleToTeacher = true;
|
||||
where.schoolId = session.user?.schoolId;
|
||||
} else if (role === 'SCHOOL_ADMIN' || role === 'VICE_PRINCIPAL') {
|
||||
// Admin can see all approved reports
|
||||
where.isVisibleToAdmin = true;
|
||||
where.schoolId = session.user?.schoolId;
|
||||
} else if (role === 'SUPER_ADMIN') {
|
||||
// Super admin can see all
|
||||
}
|
||||
|
||||
if (studentId) where.studentId = studentId;
|
||||
if (status) where.status = status;
|
||||
if (parentApprovalStatus) where.parentApprovalStatus = parentApprovalStatus;
|
||||
|
||||
const reports = await db.illnessReport.findMany({
|
||||
where,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
reporter: { select: { id: true, firstName: true, lastName: true } },
|
||||
approver: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(reports);
|
||||
} catch (error) {
|
||||
console.error('IllnessReport GET error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const session = await getSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
|
||||
}
|
||||
|
||||
const role = session.user?.role;
|
||||
const userId = session.userId;
|
||||
|
||||
if (role !== 'STUDENT' && role !== 'PARENT') {
|
||||
return NextResponse.json({ error: 'Only students and parents can report illness' }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { studentId, reason, description, startDate, endDate, documentUrl } = body;
|
||||
|
||||
if (!studentId || !reason || !startDate) {
|
||||
return NextResponse.json(
|
||||
{ error: 'studentId, reason, and startDate are required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Verify the student exists and belongs to the same school
|
||||
const student = await db.student.findUnique({
|
||||
where: { id: studentId },
|
||||
select: { schoolId: true, userId: true, firstName: true, lastName: true },
|
||||
});
|
||||
|
||||
if (!student) {
|
||||
return NextResponse.json({ error: 'Student not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Authorization checks
|
||||
if (role === 'STUDENT') {
|
||||
const isOwnStudent =
|
||||
student.userId === userId ||
|
||||
(student.userId === null &&
|
||||
student.firstName === session.user?.firstName &&
|
||||
student.lastName === session.user?.lastName &&
|
||||
student.schoolId === session.user?.schoolId);
|
||||
if (!isOwnStudent) {
|
||||
return NextResponse.json({ error: 'You can only report illness for yourself' }, { status: 403 });
|
||||
}
|
||||
} else if (role === 'PARENT') {
|
||||
const parentLink = await db.parentStudentLink.findFirst({
|
||||
where: { parentId: userId, studentId },
|
||||
});
|
||||
if (!parentLink) {
|
||||
return NextResponse.json({ error: 'You can only report illness for your children' }, { status: 403 });
|
||||
}
|
||||
}
|
||||
|
||||
const reporterType = role === 'STUDENT' ? 'student' : 'parent';
|
||||
const isParentReport = reporterType === 'parent';
|
||||
|
||||
// When student reports: needs parent approval, not visible to teacher/admin
|
||||
// When parent reports: auto-approved, visible to teacher/admin
|
||||
const report = await db.illnessReport.create({
|
||||
data: {
|
||||
schoolId: student.schoolId,
|
||||
studentId,
|
||||
reportedBy: userId,
|
||||
reporterType,
|
||||
reason,
|
||||
description: description || null,
|
||||
startDate: new Date(startDate),
|
||||
endDate: endDate ? new Date(endDate) : null,
|
||||
documentUrl: documentUrl || null,
|
||||
parentApprovalStatus: isParentReport ? 'approved' : 'pending',
|
||||
parentApprovedBy: isParentReport ? userId : null,
|
||||
parentApprovedAt: isParentReport ? new Date() : null,
|
||||
isVisibleToTeacher: isParentReport,
|
||||
isVisibleToAdmin: isParentReport,
|
||||
status: isParentReport ? 'approved' : 'pending',
|
||||
},
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
reporter: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(report, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('IllnessReport POST error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user