Files
portaldata/app/api/keloladata/data-mahasiswa/upload/route.ts
Randa Firman Putra 6d86e1ca2f Change Alur Aplikasi
2025-07-14 15:07:33 +07:00

360 lines
12 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import supabase from '@/lib/db';
import * as XLSX from 'xlsx';
export async function POST(request: NextRequest) {
try {
// Get form data from request
const formData = await request.formData();
const file = formData.get('file') as File;
if (!file) {
return NextResponse.json({ message: 'No file uploaded' }, { status: 400 });
}
// Read file content as array buffer
const fileBuffer = await file.arrayBuffer();
// Process file data based on file type
let validData = [];
let errors: string[] = [];
if (file.name.endsWith('.csv') || file.type === 'text/csv') {
// Process as CSV
const fileContent = await file.text();
const result = await processCSVData(fileContent);
validData = result.validData;
errors = result.errors;
} else {
// Process as Excel
const result = await processExcelData(fileBuffer);
validData = result.validData;
errors = result.errors;
}
if (validData.length === 0) {
return NextResponse.json({
message: 'No valid data found in the file',
errors
}, { status: 400 });
}
// Insert valid data into the database
const { insertedCount, errorCount } = await insertDataToDatabase(validData);
return NextResponse.json({
message: 'File processed successfully',
insertedCount,
errorCount,
errors: errors.length > 0 ? errors : undefined
});
} catch (error) {
console.error('Error processing file upload:', error);
return NextResponse.json({
message: 'Error processing file upload',
error: (error as Error).message
}, { status: 500 });
}
}
// Function to process Excel data
async function processExcelData(fileBuffer: ArrayBuffer) {
try {
// Parse Excel file
const workbook = XLSX.read(fileBuffer, { type: 'array' });
// Get first sheet
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
// Convert to JSON with proper typing
const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }) as any[][];
if (jsonData.length === 0) {
return { validData: [], errors: ['Excel file is empty'] };
}
// Convert Excel data to CSV-like format for processing
const headers = jsonData[0].map(h => String(h).toLowerCase());
const rows = jsonData.slice(1);
// Process the data using the common function
return processData(headers, rows);
} catch (error) {
console.error('Error processing Excel data:', error);
return { validData: [], errors: [(error as Error).message] };
}
}
// Function to process CSV data
async function processCSVData(fileContent: string) {
const lines = fileContent.split(/\r?\n/).filter(line => line.trim() !== '');
if (lines.length === 0) {
return { validData: [], errors: ['CSV file is empty'] };
}
// Get headers from first line
const headerLine = lines[0].toLowerCase();
const headers = headerLine.split(',').map(h => h.trim());
// Process data rows
const rows = lines.slice(1).map(line => line.split(',').map(v => v.trim()));
return processData(headers, rows);
}
// Common function to process data regardless of source format
function processData(headers: string[], rows: any[][]) {
// Define expected headers and their possible variations
const expectedHeaderMap = {
nim: ['nim'],
nama: ['nama', 'name'],
jenis_kelamin: ['jenis_kelamin', 'jk', 'gender'],
agama: ['agama', 'religion'],
kabupaten: ['kabupaten', 'kota', 'city'],
provinsi: ['provinsi', 'province'],
jenis_pendaftaran: ['jenis_pendaftaran', 'jalur_masuk', 'admission_type'],
tahun_angkatan: ['tahun_angkatan', 'angkatan', 'tahun', 'year'],
ipk: ['ipk', 'gpa'],
kelompok_keahlian: ['kelompok_keahlian', 'kk', 'keahlian', 'id_kk'],
status_kuliah: ['status_kuliah', 'status', 'status_mahasiswa'],
semester: ['semester', 'sem']
};
// Map actual headers to expected headers
const headerMap: { [key: string]: number } = {};
for (const [expectedHeader, variations] of Object.entries(expectedHeaderMap)) {
const index = headers.findIndex(h => variations.includes(h));
if (index !== -1) {
headerMap[expectedHeader] = index;
}
}
// Check required headers
const requiredHeaders = ['nim', 'nama', 'jenis_kelamin', 'tahun_angkatan'];
const missingHeaders = requiredHeaders.filter(h => headerMap[h] === undefined);
if (missingHeaders.length > 0) {
return {
validData: [],
errors: [`Missing required headers: ${missingHeaders.join(', ')}`]
};
}
const validData = [];
const errors = [];
// Process data rows
for (let i = 0; i < rows.length; i++) {
const values = rows[i];
if (!values || values.length === 0) continue;
try {
// Extract values using header map
const nim = String(values[headerMap.nim] || '');
const nama = String(values[headerMap.nama] || '');
const jenis_kelamin = mapGender(String(values[headerMap.jenis_kelamin] || ''));
const tahun_angkatan = String(values[headerMap.tahun_angkatan] || '');
// Optional fields
const agama = headerMap.agama !== undefined ? String(values[headerMap.agama] || '') || null : null;
const kabupaten = headerMap.kabupaten !== undefined ? String(values[headerMap.kabupaten] || '') || null : null;
const provinsi = headerMap.provinsi !== undefined ? String(values[headerMap.provinsi] || '') || null : null;
const jenis_pendaftaran = headerMap.jenis_pendaftaran !== undefined ? String(values[headerMap.jenis_pendaftaran] || '') || null : null;
// Handle IPK (could be number or string)
let ipk = null;
if (headerMap.ipk !== undefined && values[headerMap.ipk] !== undefined && values[headerMap.ipk] !== null) {
const ipkValue = values[headerMap.ipk];
const ipkStr = typeof ipkValue === 'number' ? ipkValue.toString() : String(ipkValue);
ipk = ipkStr ? parseFloat(ipkStr) : null;
}
// Handle kelompok_keahlian (could be number or string)
let kelompok_keahlian_id = null;
if (headerMap.kelompok_keahlian !== undefined && values[headerMap.kelompok_keahlian] !== undefined) {
const kkValue = values[headerMap.kelompok_keahlian];
if (kkValue !== null && kkValue !== '') {
kelompok_keahlian_id = typeof kkValue === 'number' ? kkValue : parseInt(String(kkValue));
}
}
// Handle status_kuliah
let status_kuliah = 'Aktif'; // Default value
if (headerMap.status_kuliah !== undefined && values[headerMap.status_kuliah] !== undefined) {
const statusValue = String(values[headerMap.status_kuliah] || '').trim();
if (statusValue) {
const mappedStatus = mapStatus(statusValue);
if (mappedStatus) {
status_kuliah = mappedStatus;
}
}
}
// Handle semester (could be number or string)
let semester = 1; // Default value
if (headerMap.semester !== undefined && values[headerMap.semester] !== undefined) {
const semesterValue = values[headerMap.semester];
if (semesterValue !== null && semesterValue !== '') {
const semesterNum = typeof semesterValue === 'number' ? semesterValue : parseInt(String(semesterValue));
if (!isNaN(semesterNum) && semesterNum >= 1 && semesterNum <= 14) {
semester = semesterNum;
}
}
}
// Validate required fields
if (!nim || !nama || !jenis_kelamin || !tahun_angkatan) {
errors.push(`Row ${i+1}: Missing required fields`);
continue;
}
// Validate NIM format (should be alphanumeric and proper length)
if (!/^[A-Za-z0-9]{8,12}$/.test(nim)) {
errors.push(`Row ${i+1}: Invalid NIM format - ${nim}`);
continue;
}
// Validate tahun_angkatan format (should be 4 digits)
if (!/^\d{4}$/.test(tahun_angkatan)) {
errors.push(`Row ${i+1}: Invalid tahun_angkatan format - ${tahun_angkatan}`);
continue;
}
// Validate IPK if provided
if (ipk !== null && (isNaN(ipk) || ipk < 0 || ipk > 4)) {
errors.push(`Row ${i+1}: Invalid IPK value - ${ipk}`);
continue;
}
// Validate semester if provided
if (semester < 1 || semester > 14) {
errors.push(`Row ${i+1}: Invalid semester value - ${semester} (must be between 1-14)`);
continue;
}
// Add to valid data
validData.push({
nim,
nama,
jk: jenis_kelamin,
agama,
kabupaten,
provinsi,
jenis_pendaftaran,
tahun_angkatan,
ipk,
kelompok_keahlian_id,
status_kuliah,
semester
});
} catch (error) {
errors.push(`Row ${i+1}: Error processing row - ${(error as Error).message}`);
}
}
return { validData, errors };
}
// Function to map gender values to standardized format
function mapGender(value: string): 'Pria' | 'Wanita' | null {
if (!value) return null;
const lowerValue = value.toLowerCase();
if (['pria', 'laki-laki', 'laki', 'l', 'male', 'm', 'p'].includes(lowerValue)) {
return 'Pria';
}
if (['wanita', 'perempuan', 'w', 'female', 'f', 'woman', 'w'].includes(lowerValue)) {
return 'Wanita';
}
return null;
}
// Function to map status values to standardized format
function mapStatus(value: string): 'Aktif' | 'Cuti' | 'Lulus' | 'Non-Aktif' | null {
if (!value) return null;
const lowerValue = value.toLowerCase();
if (['aktif', 'active', 'a'].includes(lowerValue)) {
return 'Aktif';
}
if (['cuti', 'leave', 'c'].includes(lowerValue)) {
return 'Cuti';
}
if (['lulus', 'graduated', 'graduate', 'l'].includes(lowerValue)) {
return 'Lulus';
}
if (['non-aktif', 'non aktif', 'nonaktif', 'non-aktif', 'non aktif', 'nonaktif', 'n'].includes(lowerValue)) {
return 'Non-Aktif';
}
return null;
}
// Function to insert data into database
async function insertDataToDatabase(data: any[]) {
let insertedCount = 0;
let errorCount = 0;
for (const item of data) {
try {
// Check if mahasiswa already exists
const { data: existingData } = await supabase
.from('mahasiswa')
.select('nim')
.eq('nim', item.nim)
.single();
const mahasiswaData = {
nama: item.nama,
jk: item.jk,
agama: item.agama,
kabupaten: item.kabupaten,
provinsi: item.provinsi,
jenis_pendaftaran: item.jenis_pendaftaran,
tahun_angkatan: item.tahun_angkatan,
ipk: item.ipk,
id_kelompok_keahlian: item.kelompok_keahlian_id,
status_kuliah: item.status_kuliah,
semester: item.semester
};
if (existingData) {
// Update existing record
const { error } = await supabase
.from('mahasiswa')
.update(mahasiswaData)
.eq('nim', item.nim);
if (error) throw error;
} else {
// Insert new record
const { error } = await supabase
.from('mahasiswa')
.insert({
nim: item.nim,
...mahasiswaData
});
if (error) throw error;
}
insertedCount++;
} catch (error) {
console.error(`Error inserting/updating record for NIM ${item.nim}:`, error);
errorCount++;
}
}
return { insertedCount, errorCount };
}