Files
portaldata/app/api/keloladata/setting-jenis-pendaftaran/route.ts
Randa Firman Putra 6d86e1ca2f Change Alur Aplikasi
2025-07-14 15:07:33 +07:00

63 lines
2.0 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import supabase from '@/lib/db';
// GET - Fetch all unique jenis_pendaftaran values
export async function GET() {
try {
// Get all unique jenis_pendaftaran values
const { data, error } = await supabase
.from('mahasiswa')
.select('jenis_pendaftaran')
.not('jenis_pendaftaran', 'is', null)
.order('jenis_pendaftaran');
if (error) {
console.error('Error fetching jenis pendaftaran data:', error);
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
}
// Get unique values
const uniqueValues = [...new Set(data.map(item => item.jenis_pendaftaran))];
return NextResponse.json(uniqueValues.map(value => ({ jenis_pendaftaran: value })));
} catch (error) {
console.error('Error fetching jenis pendaftaran data:', error);
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
}
}
// PUT - Update jenis_pendaftaran value
export async function PUT(request: NextRequest) {
try {
const body = await request.json();
const { oldValue, newValue } = body;
// Validate required fields
if (!oldValue || !newValue) {
return NextResponse.json(
{ message: 'Missing required fields: oldValue, newValue' },
{ status: 400 }
);
}
// Update jenis_pendaftaran
const { data, error } = await supabase
.from('mahasiswa')
.update({ jenis_pendaftaran: newValue })
.eq('jenis_pendaftaran', oldValue)
.select();
if (error) {
console.error('Error updating jenis pendaftaran:', error);
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
}
return NextResponse.json({
message: 'Jenis pendaftaran updated successfully',
affectedRows: data?.length || 0
});
} catch (error) {
console.error('Error updating jenis pendaftaran:', error);
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
}
}