105 lines
3.2 KiB
TypeScript
105 lines
3.2 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import supabase from '@/lib/db';
|
|
|
|
interface MasaStudiAktifData {
|
|
tahun_angkatan: number;
|
|
rata_rata_masa_studi_aktif_tahun: number;
|
|
rata_rata_masa_studi_lulus_tahun: number;
|
|
}
|
|
|
|
export async function GET(req: Request) {
|
|
try {
|
|
const { searchParams } = new URL(req.url);
|
|
const tahun_angkatan = searchParams.get('tahun_angkatan');
|
|
|
|
let query = supabase
|
|
.from('mahasiswa')
|
|
.select('tahun_angkatan, status_kuliah, semester')
|
|
.not('semester', 'is', null)
|
|
.in('status_kuliah', ['Aktif', 'Lulus']);
|
|
|
|
if (tahun_angkatan && tahun_angkatan !== 'all') {
|
|
query = query.eq('tahun_angkatan', tahun_angkatan);
|
|
}
|
|
|
|
const { data, error } = await query;
|
|
|
|
if (error) {
|
|
console.error('Error fetching masa studi aktif data:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch masa studi aktif 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, separate active and graduated students
|
|
const groupedData = data.reduce((acc, item) => {
|
|
const tahun = item.tahun_angkatan;
|
|
if (!acc[tahun]) {
|
|
acc[tahun] = {
|
|
tahun_angkatan: tahun,
|
|
aktif: { sum: 0, count: 0 },
|
|
lulus: { sum: 0, count: 0 }
|
|
};
|
|
}
|
|
|
|
if (item.status_kuliah === 'Aktif') {
|
|
acc[tahun].aktif.sum += item.semester || 0;
|
|
acc[tahun].aktif.count += 1;
|
|
} else if (item.status_kuliah === 'Lulus') {
|
|
acc[tahun].lulus.sum += item.semester || 0;
|
|
acc[tahun].lulus.count += 1;
|
|
}
|
|
|
|
return acc;
|
|
}, {} as Record<number, {
|
|
tahun_angkatan: number;
|
|
aktif: { sum: number; count: number };
|
|
lulus: { sum: number; count: number };
|
|
}>);
|
|
|
|
// Convert to final format
|
|
const results: MasaStudiAktifData[] = Object.values(groupedData).map((data) => ({
|
|
tahun_angkatan: data.tahun_angkatan,
|
|
rata_rata_masa_studi_aktif_tahun: data.aktif.count > 0
|
|
? Math.round(((data.aktif.sum / data.aktif.count) / 2) * 10) / 10
|
|
: 0,
|
|
rata_rata_masa_studi_lulus_tahun: data.lulus.count > 0
|
|
? Math.round(((data.lulus.sum / data.lulus.count) / 2) * 10) / 10
|
|
: 0,
|
|
}));
|
|
|
|
// Sort by tahun_angkatan
|
|
results.sort((a, b) => a.tahun_angkatan - b.tahun_angkatan);
|
|
|
|
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 masa studi aktif data:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch masa studi aktif data' },
|
|
{
|
|
status: 500,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
}
|