99 lines
3.4 KiB
TypeScript
99 lines
3.4 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import supabase from '@/lib/db';
|
|
|
|
interface JenisPendaftaranStatus {
|
|
jenis_pendaftaran: string;
|
|
tahun_angkatan: number;
|
|
status_kuliah: string;
|
|
total_mahasiswa: number;
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams } = new URL(request.url);
|
|
const tahunAngkatan = searchParams.get('tahun_angkatan');
|
|
const statusKuliah = searchParams.get('status_kuliah');
|
|
|
|
try {
|
|
let query = supabase
|
|
.from('status_mahasiswa')
|
|
.select('status_kuliah, mahasiswa!inner(jenis_pendaftaran, tahun_angkatan, nim)')
|
|
.eq('status_kuliah', statusKuliah);
|
|
|
|
if (tahunAngkatan && tahunAngkatan !== 'all') {
|
|
query = query.eq('mahasiswa.tahun_angkatan', parseInt(tahunAngkatan));
|
|
}
|
|
|
|
const { data, error } = await query;
|
|
|
|
if (error) {
|
|
console.error('Error fetching jenis pendaftaran status:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch jenis pendaftaran status data' },
|
|
{
|
|
status: 500,
|
|
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',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
|
|
// Group by jenis_pendaftaran, tahun_angkatan, status_kuliah
|
|
const groupedData = data.reduce((acc, item: any) => {
|
|
const jenis_pendaftaran = item.mahasiswa.jenis_pendaftaran;
|
|
const tahun_angkatan = item.mahasiswa.tahun_angkatan;
|
|
const status_kuliah = item.status_kuliah;
|
|
const key = `${jenis_pendaftaran}-${tahun_angkatan}-${status_kuliah}`;
|
|
acc[key] = (acc[key] || 0) + 1;
|
|
return acc;
|
|
}, {} as Record<string, number>);
|
|
|
|
// Convert to final format and sort
|
|
const results: JenisPendaftaranStatus[] = Object.entries(groupedData)
|
|
.map(([key, total_mahasiswa]) => {
|
|
const [jenis_pendaftaran, tahun_angkatan, status_kuliah] = key.split('-');
|
|
return {
|
|
jenis_pendaftaran,
|
|
tahun_angkatan: parseInt(tahun_angkatan),
|
|
status_kuliah,
|
|
total_mahasiswa
|
|
};
|
|
})
|
|
.sort((a, b) => {
|
|
// Sort by tahun_angkatan DESC, jenis_pendaftaran ASC, status_kuliah ASC
|
|
if (a.tahun_angkatan !== b.tahun_angkatan) {
|
|
return b.tahun_angkatan - a.tahun_angkatan;
|
|
}
|
|
if (a.jenis_pendaftaran !== b.jenis_pendaftaran) {
|
|
return a.jenis_pendaftaran.localeCompare(b.jenis_pendaftaran);
|
|
}
|
|
return a.status_kuliah.localeCompare(b.status_kuliah);
|
|
});
|
|
|
|
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 jenis pendaftaran status:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch jenis pendaftaran status data' },
|
|
{
|
|
status: 500,
|
|
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',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
}
|