Files
Randa Firman Putra 60211ae829 testing yuk
2025-09-14 16:59:31 +07:00

95 lines
3.1 KiB
TypeScript
Raw Permalink 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 {
// Ambil tanggal saat ini
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
const currentMonth = currentDate.getMonth() + 1; // getMonth() returns 0-11
// Ambil semua mahasiswa aktif
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 untuk setiap mahasiswa aktif
for (const student of activeStudents) {
try {
const tahunAngkatan = student.tahun_angkatan;
if (!tahunAngkatan) {
errors.push(`Mahasiswa NIM ${student.nim}: Tahun angkatan tidak ditemukan`);
continue;
}
// Hitung semester saat ini berdasarkan tahun_angkatan dan tanggal saat ini
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 jika berbeda
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 }
);
}
}