Files
portaldata/app/api/keloladata/update-semester/route.ts
Randa Firman Putra f7a7359408 Change update semester
2025-08-21 00:13:35 +07:00

95 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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) {
// AgustusDesember: semester ganjil tahun akademik berjalan
currentSemester = years * 2 + 1;
} else if (currentMonth >= 2 && currentMonth <= 7) {
// FebruariJuli: 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 }
);
}
}