commit part sekian
This commit is contained in:
91
app/api/mahasiswa/kk-dashboard/route.ts
Normal file
91
app/api/mahasiswa/kk-dashboard/route.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import supabase from '@/lib/db';
|
||||
|
||||
interface KelompokKeahlianStatus {
|
||||
tahun_angkatan: number;
|
||||
nama_kelompok: string;
|
||||
jumlah_mahasiswa: number;
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tahunAngkatan = searchParams.get('tahun_angkatan');
|
||||
|
||||
try {
|
||||
let query = supabase
|
||||
.from('mahasiswa')
|
||||
.select('tahun_angkatan, id_kelompok_keahlian, kelompok_keahlian!inner(id_kk, nama_kelompok)');
|
||||
|
||||
if (tahunAngkatan && tahunAngkatan !== 'all') {
|
||||
query = query.eq('tahun_angkatan', parseInt(tahunAngkatan));
|
||||
}
|
||||
|
||||
const { data, error } = await query;
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching kelompok keahlian status:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch kelompok keahlian 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 tahun_angkatan, nama_kelompok
|
||||
const groupedData = data.reduce((acc, item: any) => {
|
||||
const tahun_angkatan = item.tahun_angkatan;
|
||||
const nama_kelompok = item.kelompok_keahlian?.nama_kelompok;
|
||||
const key = `${tahun_angkatan}-${nama_kelompok}`;
|
||||
acc[key] = (acc[key] || 0) + 1;
|
||||
return acc;
|
||||
}, {} as Record<string, number>);
|
||||
|
||||
// Convert to final format and sort
|
||||
const results: KelompokKeahlianStatus[] = Object.entries(groupedData)
|
||||
.map(([key, jumlah_mahasiswa]) => {
|
||||
const [tahun_angkatan, nama_kelompok] = key.split('-');
|
||||
return {
|
||||
tahun_angkatan: parseInt(tahun_angkatan),
|
||||
nama_kelompok,
|
||||
jumlah_mahasiswa
|
||||
};
|
||||
})
|
||||
.sort((a, b) => {
|
||||
// Sort by tahun_angkatan ASC, nama_kelompok ASC
|
||||
if (a.tahun_angkatan !== b.tahun_angkatan) {
|
||||
return a.tahun_angkatan - b.tahun_angkatan;
|
||||
}
|
||||
return a.nama_kelompok.localeCompare(b.nama_kelompok);
|
||||
});
|
||||
|
||||
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 kelompok keahlian status:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch kelompok keahlian 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',
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user