97 lines
2.8 KiB
TypeScript
97 lines
2.8 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import supabase from '@/lib/db';
|
|
|
|
interface StatusKuliah {
|
|
tahun_angkatan: number;
|
|
status_kuliah: string;
|
|
jumlah: number;
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
try {
|
|
const { searchParams } = new URL(request.url);
|
|
const tahunAngkatan = searchParams.get('tahun_angkatan');
|
|
|
|
let query = supabase
|
|
.from('mahasiswa')
|
|
.select('tahun_angkatan, status_kuliah')
|
|
.in('status_kuliah', ['Lulus', 'Cuti', 'Aktif', 'Non-Aktif']);
|
|
|
|
if (tahunAngkatan && tahunAngkatan !== 'all') {
|
|
query = query.eq('tahun_angkatan', tahunAngkatan);
|
|
}
|
|
|
|
const { data, error } = await query;
|
|
|
|
if (error) {
|
|
console.error('Supabase error:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch status kuliah data' },
|
|
{
|
|
status: 500,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
|
|
// Group and count the data in JavaScript
|
|
const groupedData = data.reduce((acc: any[], row: any) => {
|
|
const tahunAngkatanValue = row.tahun_angkatan;
|
|
const statusKuliah = row.status_kuliah;
|
|
|
|
if (!tahunAngkatanValue || !statusKuliah) return acc;
|
|
|
|
const existingGroup = acc.find(
|
|
(item: any) =>
|
|
item.tahun_angkatan === tahunAngkatanValue &&
|
|
item.status_kuliah === statusKuliah
|
|
);
|
|
|
|
if (existingGroup) {
|
|
existingGroup.jumlah++;
|
|
} else {
|
|
acc.push({
|
|
tahun_angkatan: tahunAngkatanValue,
|
|
status_kuliah: statusKuliah,
|
|
jumlah: 1
|
|
});
|
|
}
|
|
|
|
return acc;
|
|
}, []);
|
|
|
|
// Sort the results
|
|
const sortedData = groupedData.sort((a: any, b: any) => {
|
|
if (a.tahun_angkatan !== b.tahun_angkatan) {
|
|
return a.tahun_angkatan - b.tahun_angkatan;
|
|
}
|
|
return a.status_kuliah.localeCompare(b.status_kuliah);
|
|
});
|
|
|
|
return NextResponse.json(sortedData, {
|
|
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 status kuliah:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch status kuliah data' },
|
|
{
|
|
status: 500,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
}
|