deckhearth/pages/api/cards/import-mtg.js
Randall Stillwell 3849f54d05 feat(catalog): weekly Vercel Cron sync for MTG and Pokémon sets
Extract shared import logic into lib/card-import, discover missing sets via
Scryfall/Pokémon TCG APIs, and expose GET /api/cron/sync-catalog protected
by CRON_SECRET (max 3 sets/run, paced imports).

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-27 14:47:36 -05:00

45 lines
1.3 KiB
JavaScript

import { getUserFromRequest } from '../../../lib/permission-middleware';
import { checkImportRateLimit } from '../../../lib/rate-limit.js';
import { importMtgSet } from '../../../lib/card-import/mtg.js';
export default async function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
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.' });
}
try {
const { setCode } = req.body;
if (!setCode) {
return res.status(400).json({ error: 'Set code is required' });
}
const result = await importMtgSet(setCode);
return res.status(200).json({
success: true,
message: `Import completed for set ${setCode}`,
...result,
});
} catch (error) {
console.error('Card import error:', error);
return res.status(500).json({
error: 'Import failed',
details: error.message,
});
}
}