Change update semester

This commit is contained in:
Randa Firman Putra
2025-08-21 00:13:35 +07:00
parent 04150b681d
commit f7a7359408
13 changed files with 667 additions and 38 deletions

View File

@@ -42,22 +42,23 @@ export async function POST() {
}
// Calculate current semester based on tahun_angkatan and current date
const yearsSinceEnrollment = currentYear - tahunAngkatan;
let currentSemester = yearsSinceEnrollment * 2; // 2 semesters per year
const years = currentYear - tahunAngkatan;
let currentSemester: number;
// 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;
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 {
// August to January = even semester (2, 4, 6, etc.)
currentSemester += 2;
// Januari: masih semester ganjil dari T.A. sebelumnya
currentSemester = (years - 1) * 2 + 1;
}
// Cap at semester 14 (7 years)
if (currentSemester > 14) {
currentSemester = 14;
}
// Jaga batas
if (currentSemester < 1) currentSemester = 1;
if (currentSemester > 14) currentSemester = 14;
// Update semester if different
if (student.semester !== currentSemester) {

View File

@@ -0,0 +1,96 @@
import { NextResponse } from 'next/server';
import supabase from '@/lib/db';
interface KelompokKeahlianStatus {
tahun_angkatan: number;
status_kuliah: string;
nama_kelompok: string;
jumlah_mahasiswa: number;
}
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const tahunAngkatan = searchParams.get('tahun_angkatan');
const statusKuliah = searchParams.get('status_kuliah');
try {
let query = supabase
.from('mahasiswa')
.select('tahun_angkatan, status_kuliah, id_kelompok_keahlian, kelompok_keahlian!inner(id_kk, nama_kelompok)')
.eq('status_kuliah', statusKuliah);
if (tahunAngkatan && tahunAngkatan !== 'all') {
query = query.eq('tahun_angkatan', parseInt(tahunAngkatan));
}
const { data, error } = await query;
if (error) {
console.error('Error fetching kelompok keahlian status:', error);
return NextResponse.json(
{ error: 'Failed to fetch kelompok keahlian status data' },
{
status: 500,
headers: {
'Cache-Control': 'public, max-age=60, stale-while-revalidate=30',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
},
}
);
}
// Group by tahun_angkatan, status_kuliah, nama_kelompok
const groupedData = data.reduce((acc, item: any) => {
const tahun_angkatan = item.tahun_angkatan;
const status_kuliah = item.status_kuliah;
const nama_kelompok = item.kelompok_keahlian?.nama_kelompok;
const key = `${tahun_angkatan}-${status_kuliah}-${nama_kelompok}`;
acc[key] = (acc[key] || 0) + 1;
return acc;
}, {} as Record<string, number>);
// Convert to final format and sort
const results: KelompokKeahlianStatus[] = Object.entries(groupedData)
.map(([key, jumlah_mahasiswa]) => {
const [tahun_angkatan, status_kuliah, nama_kelompok] = key.split('-');
return {
tahun_angkatan: parseInt(tahun_angkatan),
status_kuliah,
nama_kelompok,
jumlah_mahasiswa
};
})
.sort((a, b) => {
// Sort by tahun_angkatan ASC, nama_kelompok ASC
if (a.tahun_angkatan !== b.tahun_angkatan) {
return a.tahun_angkatan - b.tahun_angkatan;
}
return a.nama_kelompok.localeCompare(b.nama_kelompok);
});
return NextResponse.json(results, {
headers: {
'Cache-Control': 'public, max-age=60, stale-while-revalidate=30',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
},
});
} catch (error) {
console.error('Error fetching kelompok keahlian status:', error);
return NextResponse.json(
{ error: 'Failed to fetch kelompok keahlian status data' },
{
status: 500,
headers: {
'Cache-Control': 'public, max-age=60, stale-while-revalidate=30',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
},
}
);
}
}