95 lines
3.1 KiB
TypeScript
95 lines
3.1 KiB
TypeScript
import { NextResponse } from "next/server";
|
||
import supabase from '@/lib/db';
|
||
|
||
export async function POST() {
|
||
try {
|
||
// Get current date
|
||
const currentDate = new Date();
|
||
const currentYear = currentDate.getFullYear();
|
||
const currentMonth = currentDate.getMonth() + 1; // getMonth() returns 0-11
|
||
|
||
// Get all active students
|
||
const { data: activeStudents, error: fetchError } = await supabase
|
||
.from('mahasiswa')
|
||
.select('nim, tahun_angkatan, semester')
|
||
.eq('status_kuliah', 'Aktif');
|
||
|
||
if (fetchError) {
|
||
console.error('Error fetching active students:', fetchError);
|
||
return NextResponse.json(
|
||
{ message: "Gagal mengambil data mahasiswa aktif" },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
|
||
if (!activeStudents || activeStudents.length === 0) {
|
||
return NextResponse.json({
|
||
message: "Tidak ada mahasiswa aktif yang ditemukan",
|
||
affectedRows: 0
|
||
});
|
||
}
|
||
|
||
let updatedCount = 0;
|
||
const errors: string[] = [];
|
||
|
||
// Update semester for each active student
|
||
for (const student of activeStudents) {
|
||
try {
|
||
const tahunAngkatan = student.tahun_angkatan;
|
||
if (!tahunAngkatan) {
|
||
errors.push(`Mahasiswa NIM ${student.nim}: Tahun angkatan tidak ditemukan`);
|
||
continue;
|
||
}
|
||
|
||
// Calculate current semester based on tahun_angkatan and current date
|
||
const years = currentYear - tahunAngkatan;
|
||
let currentSemester: number;
|
||
|
||
if (currentMonth >= 8 && currentMonth <= 12) {
|
||
// Agustus–Desember: semester ganjil tahun akademik berjalan
|
||
currentSemester = years * 2 + 1;
|
||
} else if (currentMonth >= 2 && currentMonth <= 7) {
|
||
// Februari–Juli: semester genap tahun akademik berjalan
|
||
currentSemester = years * 2 + 2;
|
||
} else {
|
||
// Januari: masih semester ganjil dari T.A. sebelumnya
|
||
currentSemester = (years - 1) * 2 + 1;
|
||
}
|
||
|
||
// Jaga batas
|
||
if (currentSemester < 1) currentSemester = 1;
|
||
if (currentSemester > 14) currentSemester = 14;
|
||
|
||
// Update semester if different
|
||
if (student.semester !== currentSemester) {
|
||
const { error: updateError } = await supabase
|
||
.from('mahasiswa')
|
||
.update({ semester: currentSemester })
|
||
.eq('nim', student.nim);
|
||
|
||
if (updateError) {
|
||
errors.push(`Mahasiswa NIM ${student.nim}: Gagal memperbarui semester: ${updateError.message}`);
|
||
} else {
|
||
updatedCount++;
|
||
}
|
||
}
|
||
} catch (error) {
|
||
console.error(`Error updating semester for mahasiswa NIM ${student.nim}:`, error);
|
||
errors.push(`Mahasiswa NIM ${student.nim}: Terjadi kesalahan: ${(error as Error).message}`);
|
||
}
|
||
}
|
||
|
||
return NextResponse.json({
|
||
message: `Berhasil memperbarui semester untuk ${updatedCount} mahasiswa`,
|
||
affectedRows: updatedCount,
|
||
errors: errors.length > 0 ? errors : undefined
|
||
});
|
||
|
||
} catch (error) {
|
||
console.error('Error in update semester:', error);
|
||
return NextResponse.json(
|
||
{ message: "Terjadi kesalahan internal server" },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|