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,143 @@
|
||||
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 room = await db.communicationRoom.findUnique({ where: { id } });
|
||||
|
||||
if (!room) {
|
||||
return NextResponse.json({ error: 'Room not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const role = session.user?.role;
|
||||
const userId = session.userId;
|
||||
|
||||
// Access control
|
||||
if (role === 'STUDENT' && room.studentId !== userId) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
} else if (role === 'TEACHER' && room.teacherId !== userId) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
} else if (role === 'PARENT') {
|
||||
const parentLink = await db.parentStudentLink.findFirst({
|
||||
where: { parentId: userId, studentId: room.studentId },
|
||||
});
|
||||
if (!parentLink) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
} else if (role === 'SCHOOL_ADMIN' || role === 'VICE_PRINCIPAL') {
|
||||
if (room.schoolId !== session.user?.schoolId) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const page = parseInt(searchParams.get('page') || '1');
|
||||
const limit = parseInt(searchParams.get('limit') || '50');
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const [messages, total] = await Promise.all([
|
||||
db.communicationMessage.findMany({
|
||||
where: { roomId: id },
|
||||
orderBy: { createdAt: 'asc' },
|
||||
skip,
|
||||
take: limit,
|
||||
include: {
|
||||
sender: { select: { id: true, firstName: true, lastName: true, role: true } },
|
||||
},
|
||||
}),
|
||||
db.communicationMessage.count({ where: { roomId: id } }),
|
||||
]);
|
||||
|
||||
// Mark messages as read for the current user
|
||||
await db.communicationMessage.updateMany({
|
||||
where: {
|
||||
roomId: id,
|
||||
senderId: { not: userId },
|
||||
isRead: false,
|
||||
},
|
||||
data: { isRead: true },
|
||||
});
|
||||
|
||||
return NextResponse.json({ messages, total, page, limit });
|
||||
} catch (error) {
|
||||
console.error('CommunicationMessage GET error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
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 room = await db.communicationRoom.findUnique({ where: { id } });
|
||||
|
||||
if (!room) {
|
||||
return NextResponse.json({ error: 'Room not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const role = session.user?.role;
|
||||
const userId = session.userId;
|
||||
|
||||
// Only student and teacher can send messages (not admin, not parent)
|
||||
if (role !== 'STUDENT' && role !== 'TEACHER') {
|
||||
return NextResponse.json({ error: 'Only participants can send messages' }, { status: 403 });
|
||||
}
|
||||
|
||||
if (room.studentId !== userId && room.teacherId !== userId) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
if (room.status !== 'active') {
|
||||
return NextResponse.json({ error: 'Room is not active' }, { status: 400 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { content, messageType, fileUrl, metadata } = body;
|
||||
|
||||
if (!content && !fileUrl) {
|
||||
return NextResponse.json({ error: 'Content or fileUrl is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const message = await db.communicationMessage.create({
|
||||
data: {
|
||||
roomId: id,
|
||||
senderId: userId,
|
||||
content: content || '',
|
||||
messageType: messageType || 'text',
|
||||
fileUrl: fileUrl || null,
|
||||
metadata: metadata || null,
|
||||
},
|
||||
include: {
|
||||
sender: { select: { id: true, firstName: true, lastName: true, role: true } },
|
||||
},
|
||||
});
|
||||
|
||||
// Update room's updatedAt
|
||||
await db.communicationRoom.update({
|
||||
where: { id },
|
||||
data: { updatedAt: new Date() },
|
||||
});
|
||||
|
||||
return NextResponse.json(message, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('CommunicationMessage POST error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
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 room = await db.communicationRoom.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
teacher: { select: { id: true, firstName: true, lastName: true } },
|
||||
members: {
|
||||
include: { user: { select: { id: true, firstName: true, lastName: true, role: true } } },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!room) {
|
||||
return NextResponse.json({ error: 'Room not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const role = session.user?.role;
|
||||
const userId = session.userId;
|
||||
|
||||
// Access control
|
||||
if (role === 'STUDENT' && room.studentId !== userId) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
} else if (role === 'TEACHER' && room.teacherId !== userId) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
} else if (role === 'PARENT') {
|
||||
const parentLink = await db.parentStudentLink.findFirst({
|
||||
where: { parentId: userId, studentId: room.studentId },
|
||||
});
|
||||
if (!parentLink) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
} else if (role === 'SCHOOL_ADMIN' || role === 'VICE_PRINCIPAL') {
|
||||
if (room.schoolId !== session.user?.schoolId) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json(room);
|
||||
} catch (error) {
|
||||
console.error('CommunicationRoom 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 room = await db.communicationRoom.findUnique({ where: { id } });
|
||||
|
||||
if (!room) {
|
||||
return NextResponse.json({ error: 'Room not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const role = session.user?.role;
|
||||
const userId = session.userId;
|
||||
const body = await request.json();
|
||||
|
||||
// Teacher accepts a room
|
||||
if (body.status === 'active' && room.status === 'requested') {
|
||||
if (role !== 'TEACHER' || room.teacherId !== userId) {
|
||||
return NextResponse.json({ error: 'Only the assigned teacher can accept' }, { status: 403 });
|
||||
}
|
||||
|
||||
const updated = await db.communicationRoom.update({
|
||||
where: { id },
|
||||
data: {
|
||||
status: 'active',
|
||||
acceptedAt: new Date(),
|
||||
},
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
teacher: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(updated);
|
||||
}
|
||||
|
||||
// Teacher or admin closes a room
|
||||
if (body.status === 'closed') {
|
||||
if (role !== 'TEACHER' || room.teacherId !== userId) {
|
||||
if (role !== 'SCHOOL_ADMIN' && role !== 'VICE_PRINCIPAL' && role !== 'SUPER_ADMIN') {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await db.communicationRoom.update({
|
||||
where: { id },
|
||||
data: {
|
||||
status: 'closed',
|
||||
closedAt: new Date(),
|
||||
closeReason: body.closeReason || null,
|
||||
},
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
teacher: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(updated);
|
||||
}
|
||||
|
||||
// Teacher rejects a room request
|
||||
if (body.status === 'rejected' && room.status === 'requested') {
|
||||
if (role !== 'TEACHER' || room.teacherId !== userId) {
|
||||
return NextResponse.json({ error: 'Only the assigned teacher can reject' }, { status: 403 });
|
||||
}
|
||||
|
||||
const updated = await db.communicationRoom.update({
|
||||
where: { id },
|
||||
data: {
|
||||
status: 'closed',
|
||||
closedAt: new Date(),
|
||||
closeReason: body.closeReason || 'Request rejected',
|
||||
},
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
teacher: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(updated);
|
||||
}
|
||||
|
||||
return NextResponse.json({ error: 'Invalid status transition' }, { status: 400 });
|
||||
} catch (error) {
|
||||
console.error('CommunicationRoom 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 room = await db.communicationRoom.findUnique({ where: { id } });
|
||||
|
||||
if (!room) {
|
||||
return NextResponse.json({ error: 'Room not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const role = session.user?.role;
|
||||
|
||||
// Only admin can delete rooms
|
||||
if (role !== 'SUPER_ADMIN' && role !== 'SCHOOL_ADMIN' && role !== 'VICE_PRINCIPAL') {
|
||||
return NextResponse.json({ error: 'Only admins can delete rooms' }, { status: 403 });
|
||||
}
|
||||
|
||||
await db.communicationMessage.deleteMany({ where: { roomId: id } });
|
||||
await db.communicationRoomMember.deleteMany({ where: { roomId: id } });
|
||||
await db.communicationRoom.delete({ where: { id } });
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('CommunicationRoom DELETE [id] error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
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 status = searchParams.get('status');
|
||||
|
||||
const role = session.user?.role;
|
||||
const userId = session.userId;
|
||||
|
||||
const where: Record<string, unknown> = {};
|
||||
|
||||
if (role === 'STUDENT') {
|
||||
where.studentId = userId;
|
||||
} else if (role === 'TEACHER') {
|
||||
where.teacherId = userId;
|
||||
} else if (role === 'SCHOOL_ADMIN' || role === 'VICE_PRINCIPAL') {
|
||||
where.schoolId = session.user?.schoolId;
|
||||
} else if (role === 'SUPER_ADMIN') {
|
||||
// Can see all
|
||||
} else if (role === 'PARENT') {
|
||||
// Parents can see rooms for their children
|
||||
const parentLinks = await db.parentStudentLink.findMany({
|
||||
where: { parentId: userId },
|
||||
select: { studentId: true },
|
||||
});
|
||||
const childUserIds: string[] = [];
|
||||
for (const link of parentLinks) {
|
||||
const student = await db.student.findUnique({
|
||||
where: { id: link.studentId },
|
||||
select: { userId: true },
|
||||
});
|
||||
if (student?.userId) childUserIds.push(student.userId);
|
||||
}
|
||||
where.studentId = { in: childUserIds };
|
||||
}
|
||||
|
||||
if (status) where.status = status;
|
||||
|
||||
const rooms = await db.communicationRoom.findMany({
|
||||
where,
|
||||
orderBy: { updatedAt: 'desc' },
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
teacher: { select: { id: true, firstName: true, lastName: true } },
|
||||
messages: {
|
||||
orderBy: { createdAt: 'desc' },
|
||||
take: 1,
|
||||
select: { id: true, content: true, messageType: true, createdAt: true, senderId: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(rooms);
|
||||
} catch (error) {
|
||||
console.error('CommunicationRoom 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;
|
||||
|
||||
// Only students can request conversations
|
||||
if (role !== 'STUDENT') {
|
||||
return NextResponse.json({ error: 'Only students can request conversations' }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { reason } = body;
|
||||
|
||||
// Find the student's class and the responsible teacher
|
||||
const student = await db.student.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ userId },
|
||||
{
|
||||
userId: null,
|
||||
firstName: session.user?.firstName,
|
||||
lastName: session.user?.lastName,
|
||||
schoolId: session.user?.schoolId,
|
||||
},
|
||||
],
|
||||
},
|
||||
include: {
|
||||
enrollments: {
|
||||
where: { endDate: null },
|
||||
take: 1,
|
||||
include: {
|
||||
classGroup: {
|
||||
include: {
|
||||
responsibleTeacher: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!student) {
|
||||
return NextResponse.json({ error: 'Student profile not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const currentEnrollment = student.enrollments[0];
|
||||
if (!currentEnrollment) {
|
||||
return NextResponse.json({ error: 'No active class enrollment found' }, { status: 400 });
|
||||
}
|
||||
|
||||
const teacher = currentEnrollment.classGroup.responsibleTeacher;
|
||||
if (!teacher) {
|
||||
return NextResponse.json({ error: 'No class teacher assigned' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Check if there's already an active/requested room
|
||||
const existingRoom = await db.communicationRoom.findFirst({
|
||||
where: {
|
||||
studentId: userId,
|
||||
teacherId: teacher.id,
|
||||
status: { in: ['requested', 'active'] },
|
||||
},
|
||||
});
|
||||
|
||||
if (existingRoom) {
|
||||
return NextResponse.json({ error: 'Active conversation already exists with this teacher' }, { status: 400 });
|
||||
}
|
||||
|
||||
const room = await db.communicationRoom.create({
|
||||
data: {
|
||||
schoolId: student.schoolId,
|
||||
studentId: userId,
|
||||
teacherId: teacher.id,
|
||||
roomType: 'chat',
|
||||
status: 'requested',
|
||||
requestedBy: userId,
|
||||
},
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
teacher: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
});
|
||||
|
||||
// Create room members
|
||||
await db.communicationRoomMember.createMany({
|
||||
data: [
|
||||
{ roomId: room.id, userId, role: 'member' },
|
||||
{ roomId: room.id, userId: teacher.id, role: 'moderator' },
|
||||
],
|
||||
});
|
||||
|
||||
// If there's a reason, send it as the first message
|
||||
if (reason) {
|
||||
await db.communicationMessage.create({
|
||||
data: {
|
||||
roomId: room.id,
|
||||
senderId: userId,
|
||||
content: reason,
|
||||
messageType: 'text',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json(room, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('CommunicationRoom POST error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user