94 lines
3.0 KiB
TypeScript
94 lines
3.0 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 yearsSinceEnrollment = currentYear - tahunAngkatan;
|
|
let currentSemester = yearsSinceEnrollment * 2; // 2 semesters per year
|
|
|
|
// Adjust for current month (odd months = odd semesters, even months = even semesters)
|
|
if (currentMonth >= 2 && currentMonth <= 7) {
|
|
// February to July = odd semester (1, 3, 5, etc.)
|
|
currentSemester += 1;
|
|
} else {
|
|
// August to January = even semester (2, 4, 6, etc.)
|
|
currentSemester += 2;
|
|
}
|
|
|
|
// Cap at semester 14 (7 years)
|
|
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 }
|
|
);
|
|
}
|
|
}
|