2026-05-24 23:59:59 -04:00
|
|
|
import { getUserFromRequest } from '../../../lib/permission-middleware';
|
|
|
|
|
import { checkImportRateLimit } from '../../../lib/rate-limit.js';
|
2026-05-27 15:47:36 -04:00
|
|
|
import { importMtgSet } from '../../../lib/card-import/mtg.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;
|
2026-05-27 15:47:36 -04:00
|
|
|
|
2025-07-23 22:26:54 -04:00
|
|
|
if (!setCode) {
|
|
|
|
|
return res.status(400).json({ error: 'Set code is required' });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 15:47:36 -04:00
|
|
|
const result = await importMtgSet(setCode);
|
2025-07-23 22:26:54 -04:00
|
|
|
|
2026-05-27 15:47:36 -04:00
|
|
|
return res.status(200).json({
|
2025-07-23 22:26:54 -04:00
|
|
|
success: true,
|
|
|
|
|
message: `Import completed for set ${setCode}`,
|
2026-05-27 15:47:36 -04:00
|
|
|
...result,
|
2025-07-23 22:26:54 -04:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Card import error:', error);
|
2026-05-27 15:47:36 -04:00
|
|
|
return res.status(500).json({
|
|
|
|
|
error: 'Import failed',
|
|
|
|
|
details: error.message,
|
2025-07-23 22:26:54 -04:00
|
|
|
});
|
|
|
|
|
}
|
2026-05-27 15:47:36 -04:00
|
|
|
}
|