66 lines
1.9 KiB
TypeScript
66 lines
1.9 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import supabase from '@/lib/db';
|
|
|
|
interface AsalDaerah {
|
|
kabupaten: string;
|
|
jumlah: number;
|
|
}
|
|
|
|
export async function GET() {
|
|
try {
|
|
const { data, error } = await supabase
|
|
.from('mahasiswa')
|
|
.select('kabupaten')
|
|
.order('kabupaten', { ascending: true });
|
|
|
|
if (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',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
|
|
// Group by kabupaten and count
|
|
const groupedData = data.reduce((acc, item) => {
|
|
const kabupaten = item.kabupaten;
|
|
acc[kabupaten] = (acc[kabupaten] || 0) + 1;
|
|
return acc;
|
|
}, {} as Record<string, number>);
|
|
|
|
// Convert to array format
|
|
const results: AsalDaerah[] = Object.entries(groupedData).map(([kabupaten, jumlah]) => ({
|
|
kabupaten,
|
|
jumlah
|
|
}));
|
|
|
|
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',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
}
|