Round 13: PWA Offline Mode, Rate Limiting, Behavior Analytics, Interventions, Audit Logging
This commit is contained in:
1 parent
ceea8fb591
commit
ff4dd670d4
33 files changed
+6327
-509
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
// GET /api/districts/[id]
|
||||
export async function GET(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const district = await db.schoolDistrict.findFirst({
|
||||
where: { id, deletedAt: null },
|
||||
include: {
|
||||
schools: {
|
||||
where: { deletedAt: null },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
schoolType: true,
|
||||
country: true,
|
||||
_count: {
|
||||
select: { students: true, classGroups: true, users: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!district) {
|
||||
return NextResponse.json({ error: 'District not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json(district);
|
||||
} catch (error) {
|
||||
console.error('[districts/[id]] GET error:', error);
|
||||
return NextResponse.json({ error: 'Failed to fetch district' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// PUT /api/districts/[id]
|
||||
export async function PUT(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const body = await req.json();
|
||||
const { name, code, region, country, adminEmail, isActive } = body;
|
||||
|
||||
const data: Record<string, unknown> = {};
|
||||
if (name !== undefined) data.name = name;
|
||||
if (code !== undefined) data.code = code || null;
|
||||
if (region !== undefined) data.region = region || null;
|
||||
if (country !== undefined) data.country = country;
|
||||
if (adminEmail !== undefined) data.adminEmail = adminEmail || null;
|
||||
if (isActive !== undefined) data.isActive = isActive;
|
||||
|
||||
const district = await db.schoolDistrict.update({
|
||||
where: { id },
|
||||
data,
|
||||
include: {
|
||||
schools: { select: { id: true, name: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(district);
|
||||
} catch (error) {
|
||||
console.error('[districts/[id]] PUT error:', error);
|
||||
return NextResponse.json({ error: 'Failed to update district' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE /api/districts/[id]
|
||||
export async function DELETE(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
// Remove district from schools first
|
||||
await db.school.updateMany({
|
||||
where: { districtId: id },
|
||||
data: { districtId: null },
|
||||
});
|
||||
|
||||
await db.schoolDistrict.update({
|
||||
where: { id },
|
||||
data: { deletedAt: new Date() },
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('[districts/[id]] DELETE error:', error);
|
||||
return NextResponse.json({ error: 'Failed to delete district' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
// GET /api/districts/[id]/schools — schools in district
|
||||
export async function GET(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const district = await db.schoolDistrict.findFirst({
|
||||
where: { id, deletedAt: null },
|
||||
});
|
||||
|
||||
if (!district) {
|
||||
return NextResponse.json({ error: 'District not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const schools = await db.school.findMany({
|
||||
where: { districtId: id, deletedAt: null },
|
||||
include: {
|
||||
_count: {
|
||||
select: { students: true, classGroups: true, users: true },
|
||||
},
|
||||
},
|
||||
orderBy: { name: 'asc' },
|
||||
});
|
||||
|
||||
return NextResponse.json(schools);
|
||||
} catch (error) {
|
||||
console.error('[districts/[id]/schools] GET error:', error);
|
||||
return NextResponse.json({ error: 'Failed to fetch district schools' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// POST /api/districts/[id]/schools — assign school to district
|
||||
export async function POST(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const body = await req.json();
|
||||
const { schoolId } = body;
|
||||
|
||||
if (!schoolId) {
|
||||
return NextResponse.json({ error: 'schoolId is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
await db.school.update({
|
||||
where: { id: schoolId },
|
||||
data: { districtId: id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('[districts/[id]/schools] POST error:', error);
|
||||
return NextResponse.json({ error: 'Failed to assign school to district' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
// GET /api/districts
|
||||
export async function GET() {
|
||||
try {
|
||||
const districts = await db.schoolDistrict.findMany({
|
||||
where: { deletedAt: null },
|
||||
include: {
|
||||
schools: {
|
||||
where: { deletedAt: null },
|
||||
select: { id: true, name: true, schoolType: true, country: true },
|
||||
},
|
||||
},
|
||||
orderBy: { name: 'asc' },
|
||||
});
|
||||
|
||||
// Add computed stats
|
||||
const result = districts.map((d) => ({
|
||||
...d,
|
||||
schoolCount: d.schools.length,
|
||||
totalStudents: 0, // Would need aggregation query in production
|
||||
}));
|
||||
|
||||
return NextResponse.json(result);
|
||||
} catch (error) {
|
||||
console.error('[districts] GET error:', error);
|
||||
return NextResponse.json({ error: 'Failed to fetch districts' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// POST /api/districts
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const body = await req.json();
|
||||
const { name, code, region, country, adminEmail } = body;
|
||||
|
||||
if (!name) {
|
||||
return NextResponse.json({ error: 'name is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const district = await db.schoolDistrict.create({
|
||||
data: {
|
||||
name,
|
||||
code: code || null,
|
||||
region: region || null,
|
||||
country: country || 'DE',
|
||||
adminEmail: adminEmail || null,
|
||||
},
|
||||
include: {
|
||||
schools: { select: { id: true, name: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(district, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('[districts] POST error:', error);
|
||||
return NextResponse.json({ error: 'Failed to create district' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user