import { NextResponse } from 'next/server'; import supabase from '@/lib/db'; export async function GET(request: Request) { try { // Get all mahasiswa data and process in JavaScript const { data: mahasiswaData, error } = await supabase .from('mahasiswa') .select('provinsi'); if (error) { console.error('Supabase error:', error); return NextResponse.json( { error: 'Database error' }, { status: 500 } ); } // Process the data in JavaScript let kalimantanBarat = 0; let luarKalimantanBarat = 0; mahasiswaData.forEach((mahasiswa) => { const provinsi = mahasiswa.provinsi?.toLowerCase() || ''; if (provinsi.includes('kalimantan barat') || provinsi.includes('kalbar')) { kalimantanBarat++; } else if (mahasiswa.provinsi) { luarKalimantanBarat++; } }); // Transform the data to match the expected format const result = [ { provinsi: 'Kalimantan Barat', jumlah_mahasiswa: kalimantanBarat }, { provinsi: 'Luar Kalimantan Barat', jumlah_mahasiswa: luarKalimantanBarat } ]; return NextResponse.json(result, { 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 provinsi data:', error); return NextResponse.json( { error: 'Internal Server Error' }, { status: 500 } ); } }