import { NextResponse } from 'next/server'; import pool from '@/lib/db'; import { RowDataPacket } from 'mysql2'; interface AsalDaerah extends RowDataPacket { kabupaten: string; jumlah: number; } export async function GET() { const connection = await pool.getConnection(); try { const [results] = await connection.query(` SELECT kabupaten, COUNT(*) AS jumlah FROM mahasiswa GROUP BY kabupaten ORDER BY kabupaten ASC `); 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 asal daerah:', error); return NextResponse.json( { error: 'Failed to fetch asal daerah data' }, { status: 500, headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type, Authorization', }, } ); } finally { connection.release(); } }