Initial commit: ExaMetra v1.0.0 — Free & open-source competence & grading platform
Features: - 15+ views: Dashboard, Classes, Progress, Flower, Matrix, Assessments, Grading, Reports, Attendance, Calendar, Lesson Plans, Analytics, Parent Communication, Behavior Tracking, Settings - 34+ API endpoints - 30+ Prisma models - 700+ i18n keys (German + English) - Keyboard shortcuts (Cmd+K, Cmd+1-9, Cmd+/) - Batch deletion, drag-and-drop seating, custom color palette - Demo accounts (admin, teacher, student, parent) - Dark mode, responsive design, print styles - CSV/PDF export, notification system, onboarding tour - Zero emojis — all replaced with Lucide icons - MIT License
This commit is contained in:
commit
0c1f739989
156 files changed
+51090
No files matched your search
@@ -0,0 +1,146 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import { db } from '@/lib/db';
|
||||
import { getSession } from '@/lib/auth';
|
||||
|
||||
const statusEnum = z.enum(['PRESENT', 'ABSENT', 'EXCUSED', 'LATE']);
|
||||
|
||||
const updateRecordSchema = z.object({
|
||||
status: statusEnum.optional(),
|
||||
arrivalTime: z.string().optional(),
|
||||
comment: z.string().optional(),
|
||||
});
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
if (session.user?.role !== 'TEACHER' && session.user?.role !== 'SCHOOL_ADMIN' && session.user?.role !== 'SUPER_ADMIN') {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
const parsed = updateRecordSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Validation failed', details: parsed.error.issues },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const existing = await db.attendanceRecord.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
session: {
|
||||
include: { classGroup: { select: { schoolId: true } } },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return NextResponse.json({ error: 'Record not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const isTeacher = existing.session.teacherId === session.userId;
|
||||
const isSchoolAdmin =
|
||||
session.user?.role === 'SCHOOL_ADMIN' &&
|
||||
session.user.schoolId &&
|
||||
existing.session.classGroup.schoolId === session.user.schoolId;
|
||||
const isSuperAdmin = session.user?.role === 'SUPER_ADMIN';
|
||||
|
||||
if (!isTeacher && !isSchoolAdmin && !isSuperAdmin) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
const data: Record<string, unknown> = {};
|
||||
if (parsed.data.status !== undefined) data.status = parsed.data.status;
|
||||
if (parsed.data.arrivalTime !== undefined) data.arrivalTime = parsed.data.arrivalTime;
|
||||
if (parsed.data.comment !== undefined) data.comment = parsed.data.comment;
|
||||
|
||||
const record = await db.attendanceRecord.update({
|
||||
where: { id },
|
||||
data,
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(record);
|
||||
} catch (error) {
|
||||
console.error('AttendanceRecord 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 });
|
||||
}
|
||||
|
||||
if (session.user?.role !== 'TEACHER' && session.user?.role !== 'SCHOOL_ADMIN' && session.user?.role !== 'SUPER_ADMIN') {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
const { id } = await params;
|
||||
|
||||
const existing = await db.attendanceSession.findUnique({
|
||||
where: { id },
|
||||
include: { classGroup: { select: { schoolId: true } } },
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return NextResponse.json({ error: 'Session not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const isTeacher = existing.teacherId === session.userId;
|
||||
const isSchoolAdmin =
|
||||
session.user?.role === 'SCHOOL_ADMIN' &&
|
||||
session.user.schoolId &&
|
||||
existing.classGroup.schoolId === session.user.schoolId;
|
||||
const isSuperAdmin = session.user?.role === 'SUPER_ADMIN';
|
||||
|
||||
if (!isTeacher && !isSchoolAdmin && !isSuperAdmin) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
// Delete records first, then session
|
||||
await db.attendanceRecord.deleteMany({
|
||||
where: { sessionId: id },
|
||||
});
|
||||
|
||||
await db.attendanceSession.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
await db.auditLog.create({
|
||||
data: {
|
||||
userId: session.userId,
|
||||
schoolId: existing.classGroup.schoolId,
|
||||
action: 'DELETE',
|
||||
entityType: 'AttendanceSession',
|
||||
entityId: id,
|
||||
metadata: JSON.stringify({
|
||||
classGroupId: existing.classGroupId,
|
||||
date: existing.date.toISOString(),
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('AttendanceSession DELETE error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,298 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import { db } from '@/lib/db';
|
||||
import { getSession } from '@/lib/auth';
|
||||
|
||||
const statusEnum = z.enum(['PRESENT', 'ABSENT', 'EXCUSED', 'LATE']);
|
||||
|
||||
const createSessionSchema = z.object({
|
||||
classGroupId: z.string().min(1),
|
||||
date: z.string().min(1),
|
||||
subjectId: z.string().optional(),
|
||||
period: z.string().optional(),
|
||||
});
|
||||
|
||||
const updateSessionSchema = z.object({
|
||||
id: z.string().min(1),
|
||||
status: z.enum(['OPEN', 'COMPLETED']).optional(),
|
||||
records: z.array(z.object({
|
||||
id: z.string().min(1),
|
||||
status: statusEnum.optional(),
|
||||
arrivalTime: z.string().optional(),
|
||||
comment: z.string().optional(),
|
||||
})).optional(),
|
||||
});
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const session = await getSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
|
||||
}
|
||||
|
||||
if (session.user?.role !== 'TEACHER' && session.user?.role !== 'SCHOOL_ADMIN' && session.user?.role !== 'SUPER_ADMIN') {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const classGroupId = searchParams.get('classGroupId');
|
||||
const dateFrom = searchParams.get('dateFrom');
|
||||
const dateTo = searchParams.get('dateTo');
|
||||
const status = searchParams.get('status');
|
||||
|
||||
if (!classGroupId) {
|
||||
return NextResponse.json({ error: 'classGroupId is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Verify class belongs to user's school
|
||||
const classGroup = await db.classGroup.findUnique({
|
||||
where: { id: classGroupId },
|
||||
select: { id: true, schoolId: true },
|
||||
});
|
||||
|
||||
if (!classGroup) {
|
||||
return NextResponse.json({ error: 'Class not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
if (
|
||||
session.user?.role === 'SCHOOL_ADMIN' &&
|
||||
session.user.schoolId &&
|
||||
classGroup.schoolId !== session.user.schoolId
|
||||
) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
const where: Record<string, unknown> = { classGroupId };
|
||||
if (dateFrom || dateTo) {
|
||||
const dateFilter: Record<string, unknown> = {};
|
||||
if (dateFrom) dateFilter.gte = new Date(dateFrom);
|
||||
if (dateTo) dateFilter.lte = new Date(dateTo);
|
||||
where.date = dateFilter;
|
||||
}
|
||||
if (status) {
|
||||
where.status = status;
|
||||
}
|
||||
|
||||
const sessions = await db.attendanceSession.findMany({
|
||||
where,
|
||||
orderBy: { date: 'desc' },
|
||||
include: {
|
||||
subject: { select: { id: true, name: true } },
|
||||
teacher: { select: { id: true, firstName: true, lastName: true } },
|
||||
records: {
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
orderBy: { student: { lastName: 'asc' } },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(sessions);
|
||||
} catch (error) {
|
||||
console.error('Attendance 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 });
|
||||
}
|
||||
|
||||
if (session.user?.role !== 'TEACHER' && session.user?.role !== 'SCHOOL_ADMIN' && session.user?.role !== 'SUPER_ADMIN') {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const parsed = createSessionSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Validation failed', details: parsed.error.issues },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const { classGroupId, date, subjectId, period } = parsed.data;
|
||||
|
||||
// Verify class exists
|
||||
const classGroup = await db.classGroup.findUnique({
|
||||
where: { id: classGroupId },
|
||||
select: { id: true, schoolId: true },
|
||||
});
|
||||
|
||||
if (!classGroup) {
|
||||
return NextResponse.json({ error: 'Class not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
if (
|
||||
session.user?.role === 'SCHOOL_ADMIN' &&
|
||||
session.user.schoolId &&
|
||||
classGroup.schoolId !== session.user.schoolId
|
||||
) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
// Get all enrolled students for the class
|
||||
const enrollments = await db.enrollment.findMany({
|
||||
where: {
|
||||
classGroupId,
|
||||
endDate: null,
|
||||
},
|
||||
select: { studentId: true },
|
||||
});
|
||||
|
||||
// Create the session with records for all students
|
||||
const attendanceSession = await db.attendanceSession.create({
|
||||
data: {
|
||||
classGroupId,
|
||||
teacherId: session.userId,
|
||||
date: new Date(date),
|
||||
subjectId: subjectId || null,
|
||||
period: period || null,
|
||||
status: 'OPEN',
|
||||
records: {
|
||||
create: enrollments.map((e) => ({
|
||||
studentId: e.studentId,
|
||||
status: 'PRESENT',
|
||||
})),
|
||||
},
|
||||
},
|
||||
include: {
|
||||
subject: { select: { id: true, name: true } },
|
||||
teacher: { select: { id: true, firstName: true, lastName: true } },
|
||||
records: {
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
orderBy: { student: { lastName: 'asc' } },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await db.auditLog.create({
|
||||
data: {
|
||||
userId: session.userId,
|
||||
schoolId: classGroup.schoolId,
|
||||
action: 'CREATE',
|
||||
entityType: 'AttendanceSession',
|
||||
entityId: attendanceSession.id,
|
||||
metadata: JSON.stringify({
|
||||
classGroupId,
|
||||
date,
|
||||
studentCount: enrollments.length,
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(attendanceSession, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Attendance POST error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: Request) {
|
||||
try {
|
||||
const session = await getSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
|
||||
}
|
||||
|
||||
if (session.user?.role !== 'TEACHER' && session.user?.role !== 'SCHOOL_ADMIN' && session.user?.role !== 'SUPER_ADMIN') {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const parsed = updateSessionSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Validation failed', details: parsed.error.issues },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const { id } = parsed.data;
|
||||
|
||||
const existing = await db.attendanceSession.findUnique({
|
||||
where: { id },
|
||||
include: { classGroup: { select: { schoolId: true } } },
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return NextResponse.json({ error: 'Session not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const isTeacher = existing.teacherId === session.userId;
|
||||
const isSchoolAdmin =
|
||||
session.user?.role === 'SCHOOL_ADMIN' &&
|
||||
session.user.schoolId &&
|
||||
existing.classGroup.schoolId === session.user.schoolId;
|
||||
const isSuperAdmin = session.user?.role === 'SUPER_ADMIN';
|
||||
|
||||
if (!isTeacher && !isSchoolAdmin && !isSuperAdmin) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
// Update session status if provided
|
||||
if (parsed.data.status !== undefined) {
|
||||
await db.attendanceSession.update({
|
||||
where: { id },
|
||||
data: { status: parsed.data.status },
|
||||
});
|
||||
}
|
||||
|
||||
// Update records if provided
|
||||
if (parsed.data.records && parsed.data.records.length > 0) {
|
||||
for (const record of parsed.data.records) {
|
||||
const updateData: Record<string, unknown> = {};
|
||||
if (record.status !== undefined) updateData.status = record.status;
|
||||
if (record.arrivalTime !== undefined) updateData.arrivalTime = record.arrivalTime;
|
||||
if (record.comment !== undefined) updateData.comment = record.comment;
|
||||
|
||||
if (Object.keys(updateData).length > 0) {
|
||||
await db.attendanceRecord.update({
|
||||
where: { id: record.id },
|
||||
data: updateData,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return updated session
|
||||
const updated = await db.attendanceSession.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
subject: { select: { id: true, name: true } },
|
||||
teacher: { select: { id: true, firstName: true, lastName: true } },
|
||||
records: {
|
||||
include: {
|
||||
student: { select: { id: true, firstName: true, lastName: true } },
|
||||
},
|
||||
orderBy: { student: { lastName: 'asc' } },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await db.auditLog.create({
|
||||
data: {
|
||||
userId: session.userId,
|
||||
schoolId: existing.classGroup.schoolId,
|
||||
action: 'UPDATE',
|
||||
entityType: 'AttendanceSession',
|
||||
entityId: id,
|
||||
metadata: JSON.stringify({
|
||||
status: parsed.data.status,
|
||||
recordsUpdated: parsed.data.records?.length ?? 0,
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(updated);
|
||||
} catch (error) {
|
||||
console.error('Attendance PUT error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user