57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import supabase from '@/lib/db';
|
|
|
|
export async function GET() {
|
|
try {
|
|
const currentYear = new Date().getFullYear();
|
|
const { data, error } = await supabase
|
|
.from('mahasiswa')
|
|
.select('tahun_angkatan')
|
|
.gte('tahun_angkatan', currentYear - 10)
|
|
.order('tahun_angkatan', { ascending: false });
|
|
|
|
if (error) {
|
|
console.error('Error fetching tahun angkatan:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch tahun angkatan data' },
|
|
{
|
|
status: 500,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
|
|
// Get unique tahun_angkatan values and limit to 7 most recent
|
|
const uniqueYears = [...new Set(data.map(item => item.tahun_angkatan))]
|
|
.sort((a, b) => b - a) // Sort descending
|
|
.slice(0, 7); // Take only 7 most recent years
|
|
|
|
console.log('Available years:', uniqueYears); // Debug log
|
|
|
|
return NextResponse.json(uniqueYears, {
|
|
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 tahun angkatan:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch tahun angkatan data' },
|
|
{
|
|
status: 500,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
}
|