Round 17: QR Codes, Gamification Badges, Auto-Award, Badge Management

This commit is contained in:
Mustafa Cetin committed 2026-07-29 12:19:50 +00:00
1 parent 6174f127f9
commit cba8a8e724
23 files changed
+4028 -1

No files matched your search

+141
View File
@@ -0,0 +1,141 @@
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 record = await db.healthRecord.findFirst({
where: { id, deletedAt: null },
include: {
student: {
select: { id: true, firstName: true, lastName: true },
},
},
});
if (!record) {
return NextResponse.json({ error: 'Not found' }, { status: 404 });
}
if (session.user?.role !== 'SUPER_ADMIN' && session.user?.schoolId !== record.schoolId) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
return NextResponse.json(record);
} catch (error) {
console.error('HealthRecord 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.healthRecord.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 {
bloodType,
allergies,
medications,
conditions,
doctorName,
doctorPhone,
insuranceNumber,
insuranceProvider,
lastCheckup,
notes,
isConfidential,
} = body;
const updated = await db.healthRecord.update({
where: { id },
data: {
...(bloodType !== undefined && { bloodType: bloodType || null }),
...(allergies !== undefined && { allergies: allergies ? JSON.stringify(allergies) : null }),
...(medications !== undefined && { medications: medications ? JSON.stringify(medications) : null }),
...(conditions !== undefined && { conditions: conditions ? JSON.stringify(conditions) : null }),
...(doctorName !== undefined && { doctorName: doctorName || null }),
...(doctorPhone !== undefined && { doctorPhone: doctorPhone || null }),
...(insuranceNumber !== undefined && { insuranceNumber: insuranceNumber || null }),
...(insuranceProvider !== undefined && { insuranceProvider: insuranceProvider || null }),
...(lastCheckup !== undefined && { lastCheckup: lastCheckup ? new Date(lastCheckup) : null }),
...(notes !== undefined && { notes }),
...(isConfidential !== undefined && { isConfidential }),
},
include: {
student: {
select: { id: true, firstName: true, lastName: true },
},
},
});
return NextResponse.json(updated);
} catch (error) {
console.error('HealthRecord 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.healthRecord.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.healthRecord.update({
where: { id },
data: { deletedAt: new Date() },
});
return NextResponse.json({ success: true });
} catch (error) {
console.error('HealthRecord DELETE [id] error:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}
+113
View File
@@ -0,0 +1,113 @@
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 studentId = searchParams.get('studentId');
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 (studentId) where.studentId = studentId;
const records = await db.healthRecord.findMany({
where,
orderBy: [{ createdAt: 'desc' }],
include: {
student: {
select: { id: true, firstName: true, lastName: true },
},
},
});
return NextResponse.json(records);
} catch (error) {
console.error('HealthRecord 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,
studentId,
bloodType,
allergies,
medications,
conditions,
doctorName,
doctorPhone,
insuranceNumber,
insuranceProvider,
lastCheckup,
notes,
isConfidential,
isDemo,
} = body;
if (!schoolId || !studentId) {
return NextResponse.json(
{ error: 'schoolId and studentId are required' },
{ status: 400 }
);
}
if (session.user?.role !== 'SUPER_ADMIN' && session.user?.schoolId !== schoolId) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
}
const record = await db.healthRecord.create({
data: {
schoolId,
studentId,
bloodType: bloodType || null,
allergies: allergies ? JSON.stringify(allergies) : null,
medications: medications ? JSON.stringify(medications) : null,
conditions: conditions ? JSON.stringify(conditions) : null,
doctorName: doctorName || null,
doctorPhone: doctorPhone || null,
insuranceNumber: insuranceNumber || null,
insuranceProvider: insuranceProvider || null,
lastCheckup: lastCheckup ? new Date(lastCheckup) : null,
notes: notes || null,
isConfidential: isConfidential ?? true,
isDemo: isDemo ?? false,
},
include: {
student: {
select: { id: true, firstName: true, lastName: true },
},
},
});
return NextResponse.json(record, { status: 201 });
} catch (error) {
console.error('HealthRecord POST error:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}