115 lines
4.0 KiB
JavaScript
115 lines
4.0 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// List of routes to convert
|
|
const routes = [
|
|
'app/api/mahasiswa/asal-daerah-angkatan/route.ts',
|
|
'app/api/mahasiswa/asal-daerah-beasiswa/route.ts',
|
|
'app/api/mahasiswa/asal-daerah-lulus/route.ts',
|
|
'app/api/mahasiswa/asal-daerah-prestasi/route.ts',
|
|
'app/api/mahasiswa/asal-daerah-status/route.ts',
|
|
'app/api/mahasiswa/gender-per-angkatan/route.ts',
|
|
'app/api/mahasiswa/ipk-beasiswa/route.ts',
|
|
'app/api/mahasiswa/ipk-jenis-kelamin/route.ts',
|
|
'app/api/mahasiswa/ipk-lulus-tepat/route.ts',
|
|
'app/api/mahasiswa/ipk-prestasi/route.ts',
|
|
'app/api/mahasiswa/ipk-status/route.ts',
|
|
'app/api/mahasiswa/jenis-beasiswa/route.ts',
|
|
'app/api/mahasiswa/jenis-pendaftaran-beasiswa/route.ts',
|
|
'app/api/mahasiswa/jenis-pendaftaran-lulus/route.ts',
|
|
'app/api/mahasiswa/jenis-pendaftaran-prestasi/route.ts',
|
|
'app/api/mahasiswa/jenis-pendaftaran-status/route.ts',
|
|
'app/api/mahasiswa/jenis-prestasi/route.ts',
|
|
'app/api/mahasiswa/lulus-tepat-waktu/route.ts',
|
|
'app/api/mahasiswa/nama-beasiswa/route.ts',
|
|
'app/api/mahasiswa/profile/route.ts',
|
|
'app/api/mahasiswa/statistik/route.ts',
|
|
'app/api/mahasiswa/status-kuliah/route.ts',
|
|
'app/api/mahasiswa/status-mahasiswa/route.ts',
|
|
'app/api/mahasiswa/tahun-angkatan/route.ts',
|
|
'app/api/mahasiswa/tingkat-prestasi/route.ts',
|
|
'app/api/mahasiswa/total-beasiswa/route.ts',
|
|
'app/api/mahasiswa/total-prestasi/route.ts',
|
|
'app/api/auth/check/route.ts',
|
|
'app/api/auth/login/route.ts',
|
|
'app/api/auth/register/route.ts'
|
|
];
|
|
|
|
// Template for simple count queries
|
|
const simpleCountTemplate = (tableName, groupBy = null, whereClause = null) => {
|
|
let query = `supabase.from('${tableName}').select('*', { count: 'exact', head: true })`;
|
|
|
|
if (whereClause) {
|
|
query += whereClause;
|
|
}
|
|
|
|
return `
|
|
import { NextResponse } from 'next/server';
|
|
import supabase from '@/lib/db';
|
|
|
|
export async function GET() {
|
|
try {
|
|
const { count } = await ${query};
|
|
|
|
return NextResponse.json({ count: count || 0 }, {
|
|
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 data:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch data' },
|
|
{
|
|
status: 500,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
}
|
|
);
|
|
}
|
|
}`;
|
|
};
|
|
|
|
// Convert each route
|
|
routes.forEach(routePath => {
|
|
try {
|
|
const content = fs.readFileSync(routePath, 'utf8');
|
|
|
|
// Remove MySQL imports
|
|
let newContent = content
|
|
.replace(/import.*mysql2.*\n/g, '')
|
|
.replace(/import.*pool.*from.*@\/lib\/db.*\n/g, 'import supabase from \'@/lib/db\';\n')
|
|
.replace(/import.*db.*from.*@\/lib\/db.*\n/g, 'import supabase from \'@/lib/db\';\n');
|
|
|
|
// Remove RowDataPacket extensions
|
|
newContent = newContent.replace(/extends RowDataPacket/g, '');
|
|
|
|
// Remove connection management
|
|
newContent = newContent
|
|
.replace(/const connection = await pool\.getConnection\(\);\s*\n\s*try\s*{/g, 'try {')
|
|
.replace(/} finally {\s*\n\s*connection\.release\(\);\s*\n\s*}/g, '}');
|
|
|
|
// Replace simple queries with Supabase equivalents
|
|
if (newContent.includes('COUNT(*)')) {
|
|
// This is a simple count query
|
|
const tableMatch = newContent.match(/FROM (\w+)/);
|
|
if (tableMatch) {
|
|
const tableName = tableMatch[1];
|
|
newContent = simpleCountTemplate(tableName);
|
|
}
|
|
}
|
|
|
|
fs.writeFileSync(routePath, newContent);
|
|
console.log(`✅ Converted: ${routePath}`);
|
|
} catch (error) {
|
|
console.error(`❌ Error converting ${routePath}:`, error.message);
|
|
}
|
|
});
|
|
|
|
console.log('🎉 Route conversion completed!');
|