- 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
144 lines
4.5 KiB
TypeScript
Executable File
144 lines
4.5 KiB
TypeScript
Executable File
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 });
|
|
}
|
|
}
|