70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import supabase from '@/lib/db';
|
|
|
|
export async function GET(request: Request) {
|
|
try {
|
|
const { searchParams } = new URL(request.url);
|
|
const jenisPrestasi = searchParams.get('jenisPrestasi');
|
|
|
|
const { data, error } = await supabase
|
|
.from('mahasiswa')
|
|
.select(`
|
|
tahun_angkatan,
|
|
jenis_pendaftaran,
|
|
prestasi_mahasiswa!inner(
|
|
jenis_prestasi
|
|
)
|
|
`)
|
|
.eq('prestasi_mahasiswa.jenis_prestasi', jenisPrestasi);
|
|
|
|
if (error) {
|
|
console.error('Supabase error:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Database error' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
|
|
// Group and count the data in JavaScript
|
|
const groupedData = data.reduce((acc: any[], row: any) => {
|
|
const tahunAngkatan = row.tahun_angkatan;
|
|
const jenisPendaftaran = row.jenis_pendaftaran;
|
|
|
|
if (!tahunAngkatan || !jenisPendaftaran) return acc;
|
|
|
|
const existingGroup = acc.find(
|
|
(item: any) =>
|
|
item.tahun_angkatan === tahunAngkatan &&
|
|
item.jenis_pendaftaran === jenisPendaftaran
|
|
);
|
|
|
|
if (existingGroup) {
|
|
existingGroup.jenis_pendaftaran_mahasiswa_prestasi++;
|
|
} else {
|
|
acc.push({
|
|
tahun_angkatan: tahunAngkatan,
|
|
jenis_pendaftaran: jenisPendaftaran,
|
|
jenis_pendaftaran_mahasiswa_prestasi: 1
|
|
});
|
|
}
|
|
|
|
return acc;
|
|
}, []);
|
|
|
|
// Sort the results
|
|
const sortedData = groupedData.sort((a: any, b: any) => {
|
|
if (a.tahun_angkatan !== b.tahun_angkatan) {
|
|
return b.tahun_angkatan - a.tahun_angkatan;
|
|
}
|
|
return a.jenis_pendaftaran.localeCompare(b.jenis_pendaftaran);
|
|
});
|
|
|
|
return NextResponse.json(sortedData);
|
|
} catch (error) {
|
|
console.error('Error fetching data:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Internal Server Error' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|