Convert admin API to Next.js Pages API format

This commit is contained in:
Randall Stillwell 2025-07-23 08:28:42 -05:00
parent 2cc332e0a0
commit e49ada09ba

View file

@ -1,4 +1,3 @@
import { NextResponse } from 'next/server';
import { sql } from '@vercel/postgres'; import { sql } from '@vercel/postgres';
import { verifyToken, isAdmin } from '../auth-utils.js'; import { verifyToken, isAdmin } from '../auth-utils.js';
@ -102,17 +101,12 @@ async function loadPokemonCards() {
try { try {
let loadedCount = 0; let loadedCount = 0;
let page = 1; let page = 1;
const pageSize = 250; // Max allowed by API const pageSize = 250; // API limit
while (loadedCount < 1000) { // Limit to 1000 for now while (loadedCount < 1000) { // Limit to 1000 for now
await waitForRateLimit('pokemon'); await waitForRateLimit('pokemon');
const response = await fetch(`https://api.pokemontcg.io/v2/cards?page=${page}&pageSize=${pageSize}`, { const response = await fetch(`https://api.pokemontcg.io/v2/cards?page=${page}&pageSize=${pageSize}`);
headers: {
'X-Api-Key': process.env.POKEMON_API_KEY || ''
}
});
const data = await response.json(); const data = await response.json();
if (!data.data || data.data.length === 0) break; if (!data.data || data.data.length === 0) break;
@ -123,9 +117,9 @@ async function loadPokemonCards() {
INSERT INTO cards ( INSERT INTO cards (
name, set_name, set_code, card_number, rarity, game, mana_cost, cmc, name, set_name, set_code, card_number, rarity, game, mana_cost, cmc,
card_type, colors, oracle_text, power, toughness, image_url, card_type, colors, oracle_text, power, toughness, image_url,
stock_image_url, current_price, market_price, tcg_player_id, verified stock_image_url, current_price, market_price, scryfall_id, verified
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19) ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19)
ON CONFLICT (tcg_player_id) DO NOTHING ON CONFLICT (scryfall_id) DO NOTHING
`, [ `, [
card.name, card.name,
card.set.name, card.set.name,
@ -133,17 +127,17 @@ async function loadPokemonCards() {
card.number, card.number,
card.rarity, card.rarity,
'POKEMON', 'POKEMON',
card.convertedRetreatCost?.toString() || null, null, // No mana cost in Pokémon
card.convertedRetreatCost, null, // No CMC in Pokémon
card.supertype + (card.subtypes ? ' - ' + card.subtypes.join(', ') : ''), card.supertype,
JSON.stringify(card.types || []), JSON.stringify(card.types || []),
card.flavorText || card.rules?.join(' ') || '', card.rules?.join(' ') || '',
card.attacks?.[0]?.damage || null, card.nationalPokedexNumbers?.[0] || null,
card.hp || null, null, // No toughness in Pokémon
card.images?.large, card.images?.large,
card.images?.small, card.images?.small,
card.cardmarket?.prices?.averageSellPrice ? parseFloat(card.cardmarket.prices.averageSellPrice) : null, card.cardmarket?.prices?.averageSellPrice || null,
card.cardmarket?.prices?.lowPrice ? parseFloat(card.cardmarket.prices.lowPrice) : null, card.cardmarket?.prices?.lowPrice || null,
card.id, card.id,
true true
]); ]);
@ -166,110 +160,59 @@ async function loadPokemonCards() {
} }
} }
// Load Lorcana cards from multiple sources // Load Lorcana cards from Lorcana API
async function loadLorcanaCards() { async function loadLorcanaCards() {
console.log('🏰 Loading Lorcana cards from multiple sources...'); console.log('🏰 Loading Lorcana cards from Lorcana API...');
try { try {
let loadedCount = 0; let loadedCount = 0;
// Try Lorcana API first // Get all cards from Lorcana API
try { await waitForRateLimit('lorcana');
await waitForRateLimit('lorcana'); const response = await fetch('https://lorcana-api.com/api/v1/cards');
const response = await fetch('https://api.lorcana-api.com/cards/fetch?pagesize=1000'); const data = await response.json();
const data = await response.json();
if (data.cards && data.cards.length > 0) { if (!data.data || data.data.length === 0) {
console.log(`📊 Found ${data.cards.length} cards from Lorcana API`); console.log('❌ No Lorcana cards found');
return 0;
for (const card of data.cards) {
try {
await sql.query(`
INSERT INTO cards (
name, set_name, set_code, card_number, rarity, game, mana_cost, cmc,
card_type, colors, oracle_text, power, toughness, image_url,
stock_image_url, current_price, market_price, verified
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)
ON CONFLICT (name, set_code, card_number) DO NOTHING
`, [
card.name || card.card_name || card.title || '',
card.set?.name || card.set_name || '',
card.set?.code || card.set_code || '',
card.number || card.card_number || card.card_num || '',
card.rarity || card.rarity_name || '',
'LORCANA',
card.cost?.toString() || card.mana_cost || card.ink_cost?.toString() || '',
card.cost || card.cmc || card.ink_cost || 0,
card.type || card.card_type || card.type_name || '',
JSON.stringify(card.colors || card.ink || []),
card.text || card.oracle_text || card.description || card.effect || '',
card.strength?.toString() || card.power || card.attack?.toString() || '',
card.willpower?.toString() || card.toughness || card.defense?.toString() || '',
card.image_url || card.images?.small || card.images?.png || card.image || '',
card.image_url || card.images?.small || card.images?.png || card.image || '',
card.price?.market || card.current_price || null,
card.price?.low || card.market_price || null,
true
]);
loadedCount++;
} catch (error) {
console.error(`❌ Error loading Lorcana card ${card.name}:`, error);
}
}
}
} catch (error) {
console.log('❌ Lorcana API failed, trying Lorcast...');
} }
// Try Lorcast API as fallback console.log(`📊 Found ${data.data.length} Lorcana cards to load`);
if (loadedCount === 0) {
for (const card of data.data) {
try { try {
await waitForRateLimit('lorcana'); await sql.query(`
const response = await fetch('https://api.lorcast.com/v0/cards'); INSERT INTO cards (
const data = await response.json(); name, set_name, set_code, card_number, rarity, game, mana_cost, cmc,
card_type, colors, oracle_text, power, toughness, image_url,
stock_image_url, current_price, market_price, scryfall_id, verified
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19)
ON CONFLICT (scryfall_id) DO NOTHING
`, [
card.name,
card.set?.name || 'Unknown Set',
card.set?.id || 'UNK',
card.number || '0',
card.rarity || 'Common',
'LORCANA',
null, // No mana cost in Lorcana
card.cost || null,
card.type || 'Character',
JSON.stringify(card.colors || []),
card.text || '',
card.strength || null,
card.willpower || null,
card.images?.full || card.images?.large,
card.images?.small,
null, // No price data available
null, // No price data available
card.id,
true
]);
if (data.cards && data.cards.length > 0) { loadedCount++;
console.log(`📊 Found ${data.cards.length} cards from Lorcast API`);
for (const card of data.cards) {
try {
await sql.query(`
INSERT INTO cards (
name, set_name, set_code, card_number, rarity, game, mana_cost, cmc,
card_type, colors, oracle_text, power, toughness, image_url,
stock_image_url, current_price, market_price, verified
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)
ON CONFLICT (name, set_code, card_number) DO NOTHING
`, [
card.name || card.card_name || card.title || '',
card.set?.name || card.set_name || '',
card.set?.code || card.set_code || '',
card.number || card.card_number || card.card_num || '',
card.rarity || card.rarity_name || '',
'LORCANA',
card.cost?.toString() || card.mana_cost || card.ink_cost?.toString() || '',
card.cost || card.cmc || card.ink_cost || 0,
card.type || card.card_type || card.type_name || '',
JSON.stringify(card.colors || card.ink || []),
card.text || card.oracle_text || card.description || card.effect || '',
card.strength?.toString() || card.power || card.attack?.toString() || '',
card.willpower?.toString() || card.toughness || card.defense?.toString() || '',
card.image_url || card.images?.small || card.images?.png || card.image || '',
card.image_url || card.images?.small || card.images?.png || card.image || '',
card.price?.market || card.current_price || null,
card.price?.low || card.market_price || null,
true
]);
loadedCount++;
} catch (error) {
console.error(`❌ Error loading Lorcana card ${card.name}:`, error);
}
}
}
} catch (error) { } catch (error) {
console.error('❌ Lorcast API also failed:', error); console.error(`❌ Error loading Lorcana card ${card.name}:`, error);
} }
} }
@ -281,196 +224,186 @@ async function loadLorcanaCards() {
} }
} }
// GET /api/admin - Get admin data (card counts, user stats) // GET /api/admin - Get admin data
export async function GET(request) { export default async function handler(req, res) {
// Set CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// Handle preflight requests
if (req.method === 'OPTIONS') {
res.status(200).end();
return;
}
if (req.method !== 'GET' && req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
try { try {
// Temporarily bypass auth for testing // Temporarily bypass auth for testing
// const token = request.headers.get('authorization')?.replace('Bearer ', ''); // const token = req.headers.authorization?.replace('Bearer ', '');
// const user = await verifyToken(token); // const user = await verifyToken(token);
// if (!user) { // if (!user) {
// return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); // return res.status(401).json({ error: 'Unauthorized' });
// } // }
// if (!isAdmin(user)) { // if (!isAdmin(user)) {
// return NextResponse.json({ error: 'Admin access required' }, { status: 403 }); // return res.status(403).json({ error: 'Admin access required' });
// } // }
const { searchParams } = new URL(request.url); const { action } = req.query;
const action = searchParams.get('action');
// Simple test endpoint // Simple test endpoint
if (action === 'test') { if (action === 'test') {
return NextResponse.json({ return res.status(200).json({
success: true, success: true,
message: 'Admin API is working!', message: 'Admin API is working!',
timestamp: new Date().toISOString() timestamp: new Date().toISOString()
}); });
} }
if (action === 'card-counts') { if (req.method === 'GET') {
try { if (action === 'card-counts') {
// First check if the cards table exists try {
const tableCheck = await sql.query(` // First check if the cards table exists
SELECT EXISTS ( const tableCheck = await sql.query(`
SELECT FROM information_schema.tables SELECT EXISTS (
WHERE table_schema = 'public' SELECT FROM information_schema.tables
AND table_name = 'cards' WHERE table_schema = 'public'
); AND table_name = 'cards'
);
`);
if (!tableCheck.rows[0].exists) {
return res.status(200).json({
success: true,
counts: {},
total: 0,
message: 'Cards table does not exist yet'
});
}
// Get card counts from database
const result = await sql.query(`
SELECT
game,
COUNT(*) as count
FROM cards
GROUP BY game
`);
const counts = {};
result.rows.forEach(row => {
counts[row.game] = parseInt(row.count);
});
return res.status(200).json({
success: true,
counts,
total: Object.values(counts).reduce((sum, count) => sum + count, 0)
});
} catch (dbError) {
console.error('❌ Database error:', dbError);
return res.status(500).json({
success: false,
error: 'Database error',
details: dbError.message
});
}
}
if (action === 'user-stats') {
// Get user statistics
const result = await sql.query(`
SELECT
COUNT(*) as total_users,
COUNT(CASE WHEN created_at >= NOW() - INTERVAL '7 days' THEN 1 END) as new_users_7d,
COUNT(CASE WHEN created_at >= NOW() - INTERVAL '30 days' THEN 1 END) as new_users_30d
FROM user_preferences
`); `);
if (!tableCheck.rows[0].exists) { return res.status(200).json({
return NextResponse.json({ success: true,
stats: result.rows[0]
});
}
return res.status(400).json({ error: 'Invalid action' });
}
if (req.method === 'POST') {
if (action === 'load-cards') {
const { game } = req.body;
console.log(`🚀 Starting card loading process for game: ${game}`);
let results = {};
if (game === 'MTG' || game === 'ALL') {
results.mtg = await loadMTGCards();
}
if (game === 'POKEMON' || game === 'ALL') {
results.pokemon = await loadPokemonCards();
}
if (game === 'LORCANA' || game === 'ALL') {
results.lorcana = await loadLorcanaCards();
}
const totalLoaded = Object.values(results).reduce((sum, count) => sum + count, 0);
console.log(`🎉 Card loading completed! Total loaded: ${totalLoaded}`);
return res.status(200).json({
success: true,
message: `Successfully loaded ${totalLoaded} cards`,
results
});
}
if (action === 'manage-users') {
const { operation, userId, data } = req.body;
if (operation === 'promote') {
await sql.query(`
UPDATE user_preferences
SET roles = array_append(roles, 'admin')
WHERE user_id = $1
`, [userId]);
return res.status(200).json({
success: true, success: true,
counts: {}, message: 'User promoted to admin'
total: 0,
message: 'Cards table does not exist yet'
}); });
} }
// Get card counts from database if (operation === 'demote') {
const result = await sql.query(` await sql.query(`
SELECT UPDATE user_preferences
game, SET roles = array_remove(roles, 'admin')
COUNT(*) as count WHERE user_id = $1
FROM cards `, [userId]);
GROUP BY game
`);
const counts = {}; return res.status(200).json({
result.rows.forEach(row => { success: true,
counts[row.game] = parseInt(row.count); message: 'User demoted from admin'
}); });
}
return NextResponse.json({ return res.status(400).json({ error: 'Invalid operation' });
success: true,
counts,
total: Object.values(counts).reduce((sum, count) => sum + count, 0)
});
} catch (dbError) {
console.error('❌ Database error:', dbError);
return NextResponse.json({
success: false,
error: 'Database error',
details: dbError.message
}, { status: 500 });
} }
return res.status(400).json({ error: 'Invalid action' });
} }
if (action === 'user-stats') {
// Get user statistics
const result = await sql.query(`
SELECT
COUNT(*) as total_users,
COUNT(CASE WHEN created_at >= NOW() - INTERVAL '7 days' THEN 1 END) as new_users_7d,
COUNT(CASE WHEN created_at >= NOW() - INTERVAL '30 days' THEN 1 END) as new_users_30d
FROM user_preferences
`);
return NextResponse.json({
success: true,
stats: result.rows[0]
});
}
return NextResponse.json({ error: 'Invalid action' }, { status: 400 });
} catch (error) { } catch (error) {
console.error('❌ Error in admin GET:', error); console.error('❌ Error in admin API:', error);
return NextResponse.json( return res.status(500).json(
{ error: 'Failed to get admin data', details: error.message }, { error: 'Failed to process admin request', details: error.message }
{ status: 500 }
);
}
}
// POST /api/admin - Load cards or manage users
export async function POST(request) {
try {
// Temporarily bypass auth for testing
// const token = request.headers.get('authorization')?.replace('Bearer ', '');
// const user = await verifyToken(token);
// if (!user) {
// return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
// }
// if (!isAdmin(user)) {
// return NextResponse.json({ error: 'Admin access required' }, { status: 403 });
// }
const { searchParams } = new URL(request.url);
const action = searchParams.get('action');
if (action === 'load-cards') {
const { game } = await request.json();
console.log(`🚀 Starting card loading process for game: ${game}`);
let results = {};
if (game === 'MTG' || game === 'ALL') {
results.mtg = await loadMTGCards();
}
if (game === 'POKEMON' || game === 'ALL') {
results.pokemon = await loadPokemonCards();
}
if (game === 'LORCANA' || game === 'ALL') {
results.lorcana = await loadLorcanaCards();
}
const totalLoaded = Object.values(results).reduce((sum, count) => sum + count, 0);
console.log(`🎉 Card loading completed! Total loaded: ${totalLoaded}`);
return NextResponse.json({
success: true,
message: `Successfully loaded ${totalLoaded} cards`,
results
});
}
if (action === 'manage-users') {
const { operation, userId, data } = await request.json();
if (operation === 'promote') {
await sql.query(`
UPDATE user_preferences
SET roles = array_append(roles, 'admin')
WHERE user_id = $1
`, [userId]);
return NextResponse.json({
success: true,
message: 'User promoted to admin'
});
}
if (operation === 'demote') {
await sql.query(`
UPDATE user_preferences
SET roles = array_remove(roles, 'admin')
WHERE user_id = $1
`, [userId]);
return NextResponse.json({
success: true,
message: 'User demoted from admin'
});
}
return NextResponse.json({ error: 'Invalid operation' }, { status: 400 });
}
return NextResponse.json({ error: 'Invalid action' }, { status: 400 });
} catch (error) {
console.error('❌ Error in admin POST:', error);
return NextResponse.json(
{ error: 'Failed to process admin action', details: error.message },
{ status: 500 }
); );
} }
} }