testing yuk
This commit is contained in:
@@ -4,7 +4,7 @@ import * as XLSX from 'xlsx';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
// Get form data from request
|
||||
// Ambil form data dari request
|
||||
const formData = await request.formData();
|
||||
const file = formData.get('file') as File;
|
||||
|
||||
@@ -12,21 +12,21 @@ export async function POST(request: NextRequest) {
|
||||
return NextResponse.json({ message: 'No file uploaded' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Read file content as array buffer
|
||||
// Baca konten file sebagai array buffer
|
||||
const fileBuffer = await file.arrayBuffer();
|
||||
|
||||
// Process file data based on file type
|
||||
// Proses data file berdasarkan tipe file
|
||||
let validData = [];
|
||||
let errors: string[] = [];
|
||||
|
||||
if (file.name.endsWith('.csv') || file.type === 'text/csv') {
|
||||
// Process as CSV
|
||||
// Proses sebagai CSV
|
||||
const fileContent = await file.text();
|
||||
const result = await processCSVData(fileContent);
|
||||
validData = result.validData;
|
||||
errors = result.errors;
|
||||
} else {
|
||||
// Process as Excel
|
||||
// Proses sebagai Excel
|
||||
const result = await processExcelData(fileBuffer);
|
||||
validData = result.validData;
|
||||
errors = result.errors;
|
||||
@@ -39,7 +39,7 @@ export async function POST(request: NextRequest) {
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// Insert valid data into the database
|
||||
// Insert data valid ke database
|
||||
const { insertedCount, errorCount } = await insertDataToDatabase(validData);
|
||||
|
||||
return NextResponse.json({
|
||||
@@ -58,7 +58,7 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
// Function to process Excel data
|
||||
// Fungsi untuk memproses data Excel
|
||||
async function processExcelData(fileBuffer: ArrayBuffer) {
|
||||
try {
|
||||
// Parse Excel file
|
||||
@@ -87,7 +87,7 @@ async function processExcelData(fileBuffer: ArrayBuffer) {
|
||||
}
|
||||
}
|
||||
|
||||
// Function to process CSV data
|
||||
// Fungsi untuk memproses data CSV
|
||||
async function processCSVData(fileContent: string) {
|
||||
const lines = fileContent.split(/\r?\n/).filter(line => line.trim() !== '');
|
||||
|
||||
@@ -301,14 +301,14 @@ function mapStatus(value: string): 'Aktif' | 'Cuti' | 'Lulus' | 'Non-Aktif' | nu
|
||||
return null;
|
||||
}
|
||||
|
||||
// Function to insert data into database
|
||||
// Fungsi untuk insert data ke database
|
||||
async function insertDataToDatabase(data: any[]) {
|
||||
let insertedCount = 0;
|
||||
let errorCount = 0;
|
||||
|
||||
for (const item of data) {
|
||||
try {
|
||||
// Check if mahasiswa already exists
|
||||
// Cek apakah mahasiswa sudah ada
|
||||
const { data: existingData } = await supabase
|
||||
.from('mahasiswa')
|
||||
.select('nim')
|
||||
@@ -330,7 +330,7 @@ async function insertDataToDatabase(data: any[]) {
|
||||
};
|
||||
|
||||
if (existingData) {
|
||||
// Update existing record
|
||||
// Update record yang sudah ada
|
||||
const { error } = await supabase
|
||||
.from('mahasiswa')
|
||||
.update(mahasiswaData)
|
||||
@@ -338,7 +338,7 @@ async function insertDataToDatabase(data: any[]) {
|
||||
|
||||
if (error) throw error;
|
||||
} else {
|
||||
// Insert new record
|
||||
// Insert record baru
|
||||
const { error } = await supabase
|
||||
.from('mahasiswa')
|
||||
.insert({
|
||||
|
||||
Reference in New Issue
Block a user