Round 16: Peer Assessment, Emergency Contacts, School Events, School Branding, Email Templates, Performance, Styling Polish, Onboarding 24 Steps

This commit is contained in:
Mustafa Cetin committed 2026-07-29 11:45:24 +00:00
1 parent d45ba030f5
commit 6174f127f9
30 files changed
+4835 -142

No files matched your search

+165
View File
@@ -0,0 +1,165 @@
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 event = await db.schoolEvent.findFirst({
where: { id, deletedAt: null },
include: {
organizer: {
select: { id: true, firstName: true, lastName: true },
},
classGroup: {
select: { id: true, name: true },
},
registrations: {
include: {
user: {
select: { id: true, firstName: true, lastName: true },
},
},
},
},
});
if (!event) {
return NextResponse.json({ error: 'Not found' }, { status: 404 });
}
if (session.user?.role !== 'SUPER_ADMIN' && session.user?.schoolId !== event.schoolId) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
return NextResponse.json(event);
} catch (error) {
console.error('SchoolEvent 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 existing = await db.schoolEvent.findFirst({
where: { id, deletedAt: null },
});
if (!existing) {
return NextResponse.json({ error: 'Not found' }, { status: 404 });
}
if (session.user?.role !== 'SUPER_ADMIN' && session.user?.schoolId !== existing.schoolId) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
const body = await request.json();
const {
title,
description,
eventType,
startDate,
endDate,
location,
organizerId,
classGroupId,
isAllSchool,
isPublic,
requiresRegistration,
maxParticipants,
notes,
} = body;
const updated = await db.schoolEvent.update({
where: { id },
data: {
...(title !== undefined && { title }),
...(description !== undefined && { description }),
...(eventType !== undefined && { eventType }),
...(startDate !== undefined && { startDate: new Date(startDate) }),
...(endDate !== undefined && { endDate: endDate ? new Date(endDate) : null }),
...(location !== undefined && { location }),
...(organizerId !== undefined && { organizerId: organizerId || null }),
...(classGroupId !== undefined && { classGroupId: classGroupId || null }),
...(isAllSchool !== undefined && { isAllSchool }),
...(isPublic !== undefined && { isPublic }),
...(requiresRegistration !== undefined && { requiresRegistration }),
...(maxParticipants !== undefined && { maxParticipants: maxParticipants || null }),
...(notes !== undefined && { notes }),
},
include: {
organizer: {
select: { id: true, firstName: true, lastName: true },
},
classGroup: {
select: { id: true, name: true },
},
registrations: {
include: {
user: {
select: { id: true, firstName: true, lastName: true },
},
},
},
},
});
return NextResponse.json(updated);
} catch (error) {
console.error('SchoolEvent 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 existing = await db.schoolEvent.findFirst({
where: { id, deletedAt: null },
});
if (!existing) {
return NextResponse.json({ error: 'Not found' }, { status: 404 });
}
if (session.user?.role !== 'SUPER_ADMIN' && session.user?.schoolId !== existing.schoolId) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
// Soft delete
await db.schoolEvent.update({
where: { id },
data: { deletedAt: new Date() },
});
return NextResponse.json({ success: true });
} catch (error) {
console.error('SchoolEvent DELETE [id] error:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}