2025-07-23 22:26:54 -04:00
|
|
|
import { sql } from '@vercel/postgres';
|
2026-05-24 23:59:59 -04:00
|
|
|
import { getUserFromRequest } from '../../../lib/permission-middleware';
|
|
|
|
|
import { checkImportRateLimit } from '../../../lib/rate-limit.js';
|
2025-07-23 22:26:54 -04:00
|
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
|
|
|
|
if (req.method !== 'POST') {
|
|
|
|
|
return res.status(405).json({ error: 'Method not allowed' });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 23:59:59 -04:00
|
|
|
const user = await getUserFromRequest(req);
|
|
|
|
|
if (!user) {
|
|
|
|
|
return res.status(401).json({ error: 'Authentication required' });
|
|
|
|
|
}
|
|
|
|
|
if (user.role !== 'admin') {
|
|
|
|
|
return res.status(403).json({ error: 'Admin access required' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { allowed, reset } = await checkImportRateLimit(req, user.userId);
|
|
|
|
|
if (!allowed) {
|
|
|
|
|
res.setHeader('Retry-After', Math.ceil((reset - Date.now()) / 1000));
|
|
|
|
|
return res.status(429).json({ error: 'Too many attempts. Try again later.' });
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 22:26:54 -04:00
|
|
|
try {
|
|
|
|
|
const { setCode } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!setCode) {
|
|
|
|
|
return res.status(400).json({ error: 'Set code is required' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch cards from Scryfall API
|
|
|
|
|
const response = await fetch(`https://api.scryfall.com/cards/search?q=set:${setCode}`);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`Scryfall API error: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
const cards = data.data || [];
|
|
|
|
|
|
|
|
|
|
let importedCount = 0;
|
|
|
|
|
let skippedCount = 0;
|
|
|
|
|
|
|
|
|
|
for (const card of cards) {
|
|
|
|
|
try {
|
|
|
|
|
// Check if card already exists
|
|
|
|
|
const existingCard = await sql`
|
|
|
|
|
SELECT id FROM cards WHERE scryfall_id = ${card.id}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
if (existingCard.rows.length > 0) {
|
|
|
|
|
skippedCount++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract price data
|
|
|
|
|
let currentPrice = null;
|
|
|
|
|
let marketPrice = null;
|
|
|
|
|
|
|
|
|
|
if (card.prices) {
|
|
|
|
|
currentPrice = card.prices.usd ? parseFloat(card.prices.usd) : null;
|
|
|
|
|
marketPrice = card.prices.usd_foil ? parseFloat(card.prices.usd_foil) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert card into database
|
|
|
|
|
await sql`
|
|
|
|
|
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, scryfall_id, verified
|
|
|
|
|
) VALUES (
|
|
|
|
|
${card.name}, ${card.set_name}, ${card.set}, ${card.collector_number},
|
|
|
|
|
${card.rarity}, 'MTG', ${card.mana_cost || null}, ${card.cmc || null},
|
|
|
|
|
${card.type_line}, ${JSON.stringify(card.colors || [])},
|
|
|
|
|
${card.oracle_text || null}, ${card.power || null}, ${card.toughness || null},
|
|
|
|
|
${card.image_uris?.normal || null}, ${card.image_uris?.art_crop || null},
|
|
|
|
|
${currentPrice}, ${marketPrice}, ${card.id}, true
|
|
|
|
|
)
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
importedCount++;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`Error importing card ${card.name}:`, error);
|
|
|
|
|
skippedCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
message: `Import completed for set ${setCode}`,
|
|
|
|
|
imported: importedCount,
|
|
|
|
|
skipped: skippedCount,
|
|
|
|
|
total: cards.length
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Card import error:', error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
error: 'Import failed',
|
|
|
|
|
details: error.message
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|