70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import supabase from '@/lib/db';
|
|
|
|
interface IPKData {
|
|
tahun_angkatan: number;
|
|
rata_rata_ipk: number;
|
|
}
|
|
|
|
export async function GET() {
|
|
try {
|
|
const { data, error } = await supabase
|
|
.from('mahasiswa')
|
|
.select('tahun_angkatan, ipk')
|
|
.not('ipk', 'is', null);
|
|
|
|
if (error) {
|
|
console.error('Error fetching IPK data:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch IPK data' },
|
|
{
|
|
status: 500,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
|
|
// Group by tahun_angkatan and calculate average IPK
|
|
const groupedData = data.reduce((acc, item) => {
|
|
const tahun = item.tahun_angkatan;
|
|
if (!acc[tahun]) {
|
|
acc[tahun] = { sum: 0, count: 0 };
|
|
}
|
|
acc[tahun].sum += item.ipk || 0;
|
|
acc[tahun].count += 1;
|
|
return acc;
|
|
}, {} as Record<number, { sum: number; count: number }>);
|
|
|
|
// Convert to final format
|
|
const results: IPKData[] = Object.entries(groupedData).map(([tahun, data]) => ({
|
|
tahun_angkatan: parseInt(tahun),
|
|
rata_rata_ipk: Math.round((data.sum / data.count) * 100) / 100
|
|
}));
|
|
|
|
return NextResponse.json(results, {
|
|
headers: {
|
|
'Cache-Control': 'public, max-age=60, stale-while-revalidate=30',
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error('Error fetching IPK data:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch IPK data' },
|
|
{
|
|
status: 500,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
}
|