91 lines
2.1 KiB
TypeScript
91 lines
2.1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import pool from '@/lib/db';
|
|
import { RowDataPacket } from 'mysql2';
|
|
import { cookies } from 'next/headers';
|
|
import { jwtVerify } from 'jose';
|
|
|
|
interface MahasiswaProfile extends RowDataPacket {
|
|
nim: string;
|
|
nama: string;
|
|
jk: 'Pria' | 'Wanita';
|
|
agama: string;
|
|
kabupaten: string;
|
|
provinsi: string;
|
|
jenis_pendaftaran: string;
|
|
status_beasiswa: 'YA' | 'TIDAK';
|
|
tahun_angkatan: string;
|
|
ipk: number | null;
|
|
prestasi: 'YA' | 'TIDAK';
|
|
status_kuliah: string;
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
let connection;
|
|
try {
|
|
// Get token from cookies
|
|
const cookieStore = await cookies();
|
|
const token = cookieStore.get('token')?.value;
|
|
|
|
if (!token) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Verify JWT token
|
|
const { payload } = await jwtVerify(
|
|
token,
|
|
new TextEncoder().encode(process.env.JWT_SECRET || 'your-secret-key')
|
|
);
|
|
|
|
const nim = payload.nim as string;
|
|
|
|
// Get connection from pool
|
|
connection = await pool.getConnection();
|
|
|
|
const query = `
|
|
SELECT
|
|
m.nim,
|
|
m.nama,
|
|
m.jk,
|
|
m.agama,
|
|
m.kabupaten,
|
|
m.provinsi,
|
|
m.jenis_pendaftaran,
|
|
m.status_beasiswa,
|
|
m.tahun_angkatan,
|
|
m.ipk,
|
|
m.prestasi,
|
|
s.status_kuliah
|
|
FROM
|
|
mahasiswa m
|
|
LEFT JOIN
|
|
status_mahasiswa s ON m.nim = s.nim
|
|
WHERE
|
|
m.nim = ?
|
|
`;
|
|
|
|
const [rows] = await connection.query<MahasiswaProfile[]>(query, [nim]);
|
|
|
|
if (rows.length === 0) {
|
|
connection.release();
|
|
return NextResponse.json(
|
|
{ error: 'Data mahasiswa tidak ditemukan' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
connection.release();
|
|
return NextResponse.json(rows[0]);
|
|
} catch (error) {
|
|
if (connection) {
|
|
connection.release();
|
|
}
|
|
console.error('Error fetching profile data:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Internal Server Error' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|