Round 16: Peer Assessment, Emergency Contacts, School Events, School Branding, Email Templates, Performance, Styling Polish, Onboarding 24 Steps
This commit is contained in:
1 parent
d45ba030f5
commit
6174f127f9
30 files changed
+4835
-142
No files matched your search
@@ -0,0 +1,113 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { getSession } from '@/lib/auth';
|
||||
|
||||
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 event = await db.schoolEvent.findFirst({
|
||||
where: { id, deletedAt: null },
|
||||
});
|
||||
|
||||
if (!event) {
|
||||
return NextResponse.json({ error: 'Event not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
if (!event.requiresRegistration) {
|
||||
return NextResponse.json({ error: 'Event does not require registration' }, { status: 400 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { userId, notes } = body;
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: 'userId is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Check if already registered
|
||||
const existing = await db.eventRegistration.findUnique({
|
||||
where: { eventId_userId: { eventId: id, userId } },
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
if (existing.status === 'cancelled') {
|
||||
// Re-register
|
||||
const updated = await db.eventRegistration.update({
|
||||
where: { id: existing.id },
|
||||
data: { status: 'registered', notes: notes || null },
|
||||
});
|
||||
return NextResponse.json(updated);
|
||||
}
|
||||
return NextResponse.json({ error: 'Already registered' }, { status: 409 });
|
||||
}
|
||||
|
||||
// Check max participants
|
||||
if (event.maxParticipants) {
|
||||
const count = await db.eventRegistration.count({
|
||||
where: { eventId: id, status: 'registered' },
|
||||
});
|
||||
if (count >= event.maxParticipants) {
|
||||
return NextResponse.json({ error: 'Event is full' }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
const registration = await db.eventRegistration.create({
|
||||
data: {
|
||||
eventId: id,
|
||||
userId,
|
||||
notes: notes || null,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(registration, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('EventRegistration POST 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 body = await request.json();
|
||||
const { userId, status } = body;
|
||||
|
||||
if (!userId || !status) {
|
||||
return NextResponse.json({ error: 'userId and status are required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const registration = await db.eventRegistration.findUnique({
|
||||
where: { eventId_userId: { eventId: id, userId } },
|
||||
});
|
||||
|
||||
if (!registration) {
|
||||
return NextResponse.json({ error: 'Registration not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const updated = await db.eventRegistration.update({
|
||||
where: { id: registration.id },
|
||||
data: { status },
|
||||
});
|
||||
|
||||
return NextResponse.json(updated);
|
||||
} catch (error) {
|
||||
console.error('EventRegistration PUT error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
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 schoolId = searchParams.get('schoolId');
|
||||
const eventType = searchParams.get('eventType');
|
||||
const classGroupId = searchParams.get('classGroupId');
|
||||
const upcoming = searchParams.get('upcoming');
|
||||
|
||||
if (!schoolId) {
|
||||
return NextResponse.json({ error: 'schoolId is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
if (session.user?.role !== 'SUPER_ADMIN' && session.user?.schoolId !== schoolId) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
const where: Record<string, unknown> = {
|
||||
schoolId,
|
||||
deletedAt: null,
|
||||
};
|
||||
|
||||
if (eventType) where.eventType = eventType;
|
||||
if (classGroupId) where.classGroupId = classGroupId;
|
||||
|
||||
if (upcoming === 'true') {
|
||||
where.startDate = { gte: new Date() };
|
||||
}
|
||||
|
||||
const events = await db.schoolEvent.findMany({
|
||||
where,
|
||||
orderBy: { startDate: 'asc' },
|
||||
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(events);
|
||||
} catch (error) {
|
||||
console.error('SchoolEvent 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 body = await request.json();
|
||||
const {
|
||||
schoolId,
|
||||
title,
|
||||
description,
|
||||
eventType,
|
||||
startDate,
|
||||
endDate,
|
||||
location,
|
||||
organizerId,
|
||||
classGroupId,
|
||||
isAllSchool,
|
||||
isPublic,
|
||||
requiresRegistration,
|
||||
maxParticipants,
|
||||
notes,
|
||||
isDemo,
|
||||
} = body;
|
||||
|
||||
if (!schoolId || !title || !eventType || !startDate) {
|
||||
return NextResponse.json(
|
||||
{ error: 'schoolId, title, eventType, and startDate are required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (session.user?.role !== 'SUPER_ADMIN' && session.user?.schoolId !== schoolId) {
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
|
||||
}
|
||||
|
||||
const event = await db.schoolEvent.create({
|
||||
data: {
|
||||
schoolId,
|
||||
title,
|
||||
description: description || null,
|
||||
eventType,
|
||||
startDate: new Date(startDate),
|
||||
endDate: endDate ? new Date(endDate) : null,
|
||||
location: location || null,
|
||||
organizerId: organizerId || null,
|
||||
classGroupId: classGroupId || null,
|
||||
isAllSchool: isAllSchool ?? true,
|
||||
isPublic: isPublic ?? true,
|
||||
requiresRegistration: requiresRegistration ?? false,
|
||||
maxParticipants: maxParticipants || null,
|
||||
notes: notes || null,
|
||||
isDemo: isDemo ?? false,
|
||||
},
|
||||
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(event, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('SchoolEvent POST error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user