import type { NextApiRequest, NextApiResponse } from 'next'; import { pool } from '@/backend/utils/db'; export default async function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method !== 'GET') { return res.status(405).json({ error: 'Method Not Allowed' }); } try { const [categories] = await pool.query('select idcardtype,cardtypedesc from cardtypes ct where ct.useascategory=1'); const query = ` SELECT cc.creditcardsname, cc.MinimumSalary, cc.AnnualFee, cc.creditcardsimagename FROM creditcards cc INNER JOIN creditcardstypes ct ON ct.idcreditcards = cc.idcreditcards WHERE ct.idcardtype = ? `; const result = await Promise.all( (categories as any[]).map(async (category) => { const [cards] = await pool.query( query, [category.id] ); const cardsWithPoints = await Promise.all( (cards as any[]).map(async (card) => { const [points] = await pool.query( 'select * from creditcardsfeturres cf where cf.idcreditcards = ?', [card.id] ); return { ...card, points: (points as any[]).map(p => p.point) }; }) ); return { title: category.title, cards: cardsWithPoints }; }) ); res.status(200).json(result); } catch (error) { console.error('Error fetching cards:', error); res.status(500).json({ error: 'Internal Server Error' }); } }