2026-06-02 13:42:22 -04:00
|
|
|
import { withAdmin } from '../../../lib/permission-middleware';
|
2026-05-24 23:59:59 -04:00
|
|
|
import { checkImportRateLimit } from '../../../lib/rate-limit.js';
|
2026-05-27 15:59:59 -04:00
|
|
|
import { importPokemonSet } from '../../../lib/card-import/pokemon.js';
|
2025-07-24 11:30:21 -04:00
|
|
|
|
2026-06-02 13:42:22 -04:00
|
|
|
export default withAdmin(async function handler(req, res, user) {
|
2025-07-23 22:26:54 -04:00
|
|
|
if (req.method !== 'POST') {
|
|
|
|
|
return res.status(405).json({ error: 'Method not allowed' });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 23:59:59 -04:00
|
|
|
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:59:59 -04:00
|
|
|
|
2025-07-23 22:26:54 -04:00
|
|
|
if (!setCode) {
|
|
|
|
|
return res.status(400).json({ error: 'Set code is required' });
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 11:30:21 -04:00
|
|
|
console.log(`Starting import for Pokemon set: ${setCode}`);
|
2026-05-27 15:59:59 -04:00
|
|
|
const result = await importPokemonSet(setCode);
|
|
|
|
|
console.log(
|
|
|
|
|
`Import completed for set ${setCode}: ${result.imported} imported, ${result.skipped} skipped`
|
|
|
|
|
);
|
2025-07-23 22:26:54 -04:00
|
|
|
|
2026-05-27 15:59:59 -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:59:59 -04:00
|
|
|
...result,
|
2025-07-23 22:26:54 -04:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Card import error:', error);
|
2026-05-27 15:59:59 -04:00
|
|
|
return res.status(500).json({
|
|
|
|
|
error: 'Import failed',
|
|
|
|
|
details: error.message,
|
2025-07-23 22:26:54 -04:00
|
|
|
});
|
|
|
|
|
}
|
2026-06-02 13:42:22 -04:00
|
|
|
});
|