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:
Mustafa Cetin committed 2026-07-29 05:46:10 +00:00
commit 0c1f739989
156 files changed
+51090

No files matched your search

@@ -0,0 +1,241 @@
import { NextResponse } from 'next/server';
import { z } from 'zod';
import { db } from '@/lib/db';
import { getSession } from '@/lib/auth';
const severityEnum = z.enum(['minor', 'moderate', 'major']);
const updateIncidentSchema = z.object({
studentId: z.string().min(1).optional(),
classGroupId: z.string().min(1).optional().nullable(),
categoryId: z.string().min(1).optional(),
date: z.string().min(1).optional(),
severity: severityEnum.optional(),
description: z.string().min(1).max(2000).optional(),
location: z.string().max(200).optional().nullable(),
followUpAction: z.string().max(200).optional().nullable(),
resolved: z.boolean().optional(),
});
function isTeacherOrAdmin(role: string | undefined): boolean {
return (
role === 'TEACHER' ||
role === 'SCHOOL_ADMIN' ||
role === 'SUPER_ADMIN'
);
}
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 });
}
if (!isTeacherOrAdmin(session.user?.role)) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
const { id } = await params;
const incident = await db.behaviorIncident.findUnique({
where: { id },
include: {
student: { select: { id: true, firstName: true, lastName: true, deletedAt: true } },
teacher: { select: { id: true, firstName: true, lastName: true } },
classGroup: { select: { id: true, name: true, gradeLevel: true } },
category: { select: { id: true, name: true, color: true, valence: true, icon: true } },
resolvedBy: { select: { id: true, firstName: true, lastName: true } },
},
});
if (!incident) {
return NextResponse.json({ error: 'Incident not found' }, { status: 404 });
}
if (
session.user?.role === 'SCHOOL_ADMIN' &&
session.user.schoolId &&
incident.schoolId !== session.user.schoolId
) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
return NextResponse.json(incident);
} catch (error) {
console.error('BehaviorIncident GET 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 });
}
if (!isTeacherOrAdmin(session.user?.role)) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
const { id } = await params;
const body = await request.json();
const parsed = updateIncidentSchema.safeParse(body);
if (!parsed.success) {
return NextResponse.json(
{ error: 'Validation failed', details: parsed.error.issues },
{ status: 400 }
);
}
const existing = await db.behaviorIncident.findUnique({ where: { id } });
if (!existing) {
return NextResponse.json({ error: 'Incident not found' }, { status: 404 });
}
if (
session.user?.role === 'SCHOOL_ADMIN' &&
session.user.schoolId &&
existing.schoolId !== session.user.schoolId
) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
// Validate references when they're being changed
if (parsed.data.studentId && parsed.data.studentId !== existing.studentId) {
const student = await db.student.findUnique({
where: { id: parsed.data.studentId },
select: { id: true, schoolId: true, deletedAt: true },
});
if (!student || student.schoolId !== existing.schoolId) {
return NextResponse.json({ error: 'Student not found in this school' }, { status: 404 });
}
if (student.deletedAt) {
return NextResponse.json({ error: 'Student is deleted' }, { status: 400 });
}
}
if (parsed.data.categoryId && parsed.data.categoryId !== existing.categoryId) {
const category = await db.behaviorCategory.findUnique({
where: { id: parsed.data.categoryId },
select: { id: true, schoolId: true },
});
if (!category || category.schoolId !== existing.schoolId) {
return NextResponse.json({ error: 'Category not found in this school' }, { status: 404 });
}
}
if (parsed.data.classGroupId) {
const classGroup = await db.classGroup.findUnique({
where: { id: parsed.data.classGroupId },
select: { id: true, schoolId: true },
});
if (!classGroup || classGroup.schoolId !== existing.schoolId) {
return NextResponse.json({ error: 'Class group not found in this school' }, { status: 404 });
}
}
const data: Record<string, unknown> = {};
if (parsed.data.studentId !== undefined) data.studentId = parsed.data.studentId;
if (parsed.data.classGroupId !== undefined) data.classGroupId = parsed.data.classGroupId || null;
if (parsed.data.categoryId !== undefined) data.categoryId = parsed.data.categoryId;
if (parsed.data.date !== undefined) data.date = new Date(parsed.data.date);
if (parsed.data.severity !== undefined) data.severity = parsed.data.severity;
if (parsed.data.description !== undefined) data.description = parsed.data.description;
if (parsed.data.location !== undefined) data.location = parsed.data.location ?? null;
if (parsed.data.followUpAction !== undefined) data.followUpAction = parsed.data.followUpAction ?? null;
// Handle resolved transitions
if (parsed.data.resolved !== undefined && parsed.data.resolved !== existing.resolved) {
data.resolved = parsed.data.resolved;
if (parsed.data.resolved) {
data.resolvedAt = new Date();
data.resolvedById = session.userId;
} else {
data.resolvedAt = null;
data.resolvedById = null;
}
}
const updated = await db.behaviorIncident.update({
where: { id },
data,
include: {
student: { select: { id: true, firstName: true, lastName: true, deletedAt: true } },
teacher: { select: { id: true, firstName: true, lastName: true } },
classGroup: { select: { id: true, name: true, gradeLevel: true } },
category: { select: { id: true, name: true, color: true, valence: true, icon: true } },
resolvedBy: { select: { id: true, firstName: true, lastName: true } },
},
});
await db.auditLog.create({
data: {
userId: session.userId,
schoolId: existing.schoolId,
action: 'UPDATE',
entityType: 'BehaviorIncident',
entityId: id,
metadata: JSON.stringify({ updatedFields: Object.keys(data) }),
},
});
return NextResponse.json(updated);
} catch (error) {
console.error('BehaviorIncident 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 (!isTeacherOrAdmin(session.user?.role)) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
const { id } = await params;
const existing = await db.behaviorIncident.findUnique({ where: { id } });
if (!existing) {
return NextResponse.json({ error: 'Incident not found' }, { status: 404 });
}
if (
session.user?.role === 'SCHOOL_ADMIN' &&
session.user.schoolId &&
existing.schoolId !== session.user.schoolId
) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
await db.behaviorIncident.delete({ where: { id } });
await db.auditLog.create({
data: {
userId: session.userId,
schoolId: existing.schoolId,
action: 'DELETE',
entityType: 'BehaviorIncident',
entityId: id,
metadata: JSON.stringify({
studentId: existing.studentId,
categoryId: existing.categoryId,
date: existing.date.toISOString(),
}),
},
});
return NextResponse.json({ success: true });
} catch (error) {
console.error('BehaviorIncident DELETE error:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}
+224
View File
@@ -0,0 +1,224 @@
import { NextResponse } from 'next/server';
import { z } from 'zod';
import { db } from '@/lib/db';
import { getSession } from '@/lib/auth';
const severityEnum = z.enum(['minor', 'moderate', 'major']);
const createIncidentSchema = z.object({
studentId: z.string().min(1),
classGroupId: z.string().min(1).optional().nullable(),
schoolId: z.string().min(1),
categoryId: z.string().min(1),
date: z.string().min(1),
severity: severityEnum,
description: z.string().min(1).max(2000),
location: z.string().max(200).optional().nullable(),
followUpAction: z.string().max(200).optional().nullable(),
resolved: z.boolean().optional(),
});
function isTeacherOrAdmin(role: string | undefined): boolean {
return (
role === 'TEACHER' ||
role === 'SCHOOL_ADMIN' ||
role === 'SUPER_ADMIN'
);
}
export async function GET(request: Request) {
try {
const session = await getSession();
if (!session) {
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
}
if (!isTeacherOrAdmin(session.user?.role)) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
const { searchParams } = new URL(request.url);
const studentId = searchParams.get('studentId');
const classGroupId = searchParams.get('classGroupId');
const schoolIdParam = searchParams.get('schoolId');
const dateFrom = searchParams.get('dateFrom');
const dateTo = searchParams.get('dateTo');
const categoryId = searchParams.get('categoryId');
const severity = searchParams.get('severity');
const resolved = searchParams.get('resolved');
const teacherId = searchParams.get('teacherId');
// Determine effective schoolId
let schoolId: string | undefined;
if (session.user?.role === 'SCHOOL_ADMIN') {
schoolId = session.user.schoolId ?? undefined;
} else {
schoolId = schoolIdParam ?? session.user?.schoolId ?? undefined;
}
if (!schoolId) {
return NextResponse.json([]);
}
const where: Record<string, unknown> = { schoolId };
if (studentId) where.studentId = studentId;
if (classGroupId && classGroupId !== 'all') where.classGroupId = classGroupId;
if (categoryId && categoryId !== 'all') where.categoryId = categoryId;
if (severity && severity !== 'all') where.severity = severity;
if (teacherId) where.teacherId = teacherId;
if (resolved === 'true') where.resolved = true;
if (resolved === 'false') where.resolved = false;
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;
}
const incidents = await db.behaviorIncident.findMany({
where,
orderBy: { date: 'desc' },
include: {
student: {
select: {
id: true,
firstName: true,
lastName: true,
deletedAt: true,
},
},
teacher: {
select: { id: true, firstName: true, lastName: true },
},
classGroup: {
select: { id: true, name: true, gradeLevel: true },
},
category: {
select: { id: true, name: true, color: true, valence: true, icon: true },
},
resolvedBy: {
select: { id: true, firstName: true, lastName: true },
},
},
});
return NextResponse.json(incidents);
} catch (error) {
console.error('BehaviorIncidents 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 (!isTeacherOrAdmin(session.user?.role)) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
const body = await request.json();
const parsed = createIncidentSchema.safeParse(body);
if (!parsed.success) {
return NextResponse.json(
{ error: 'Validation failed', details: parsed.error.issues },
{ status: 400 }
);
}
const { schoolId, studentId, categoryId, classGroupId, date, ...rest } = parsed.data;
// Verify school
const school = await db.school.findUnique({ where: { id: schoolId } });
if (!school) {
return NextResponse.json({ error: 'School not found' }, { status: 404 });
}
if (
session.user?.role === 'SCHOOL_ADMIN' &&
session.user.schoolId &&
school.id !== session.user.schoolId
) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
// Verify student belongs to same school and is not deleted
const student = await db.student.findUnique({
where: { id: studentId },
select: { id: true, schoolId: true, deletedAt: true },
});
if (!student || student.schoolId !== schoolId) {
return NextResponse.json({ error: 'Student not found in this school' }, { status: 404 });
}
if (student.deletedAt) {
return NextResponse.json({ error: 'Student is deleted' }, { status: 400 });
}
// Verify category belongs to same school
const category = await db.behaviorCategory.findUnique({
where: { id: categoryId },
select: { id: true, schoolId: true },
});
if (!category || category.schoolId !== schoolId) {
return NextResponse.json({ error: 'Category not found in this school' }, { status: 404 });
}
// Verify classGroup belongs to same school (if provided)
if (classGroupId) {
const classGroup = await db.classGroup.findUnique({
where: { id: classGroupId },
select: { id: true, schoolId: true },
});
if (!classGroup || classGroup.schoolId !== schoolId) {
return NextResponse.json({ error: 'Class group not found in this school' }, { status: 404 });
}
}
const resolved = parsed.data.resolved ?? false;
const incident = await db.behaviorIncident.create({
data: {
studentId,
teacherId: session.userId,
classGroupId: classGroupId || null,
schoolId,
categoryId,
date: new Date(date),
severity: parsed.data.severity,
description: parsed.data.description,
location: parsed.data.location ?? null,
followUpAction: parsed.data.followUpAction ?? null,
resolved,
resolvedAt: resolved ? new Date() : null,
resolvedById: resolved ? session.userId : null,
},
include: {
student: { select: { id: true, firstName: true, lastName: true, deletedAt: true } },
teacher: { select: { id: true, firstName: true, lastName: true } },
classGroup: { select: { id: true, name: true, gradeLevel: true } },
category: { select: { id: true, name: true, color: true, valence: true, icon: true } },
resolvedBy: { select: { id: true, firstName: true, lastName: true } },
},
});
await db.auditLog.create({
data: {
userId: session.userId,
schoolId,
action: 'CREATE',
entityType: 'BehaviorIncident',
entityId: incident.id,
metadata: JSON.stringify({
studentId,
categoryId,
severity: parsed.data.severity,
date,
}),
},
});
return NextResponse.json(incident, { status: 201 });
} catch (error) {
console.error('BehaviorIncidents POST error:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}