2026-06-02 13:42:22 -04:00
|
|
|
import { withAdmin } from '../../../lib/permission-middleware';
|
2026-05-28 10:18:01 -04:00
|
|
|
import { checkImportRateLimit } from '../../../lib/rate-limit.js';
|
|
|
|
|
import { runCatalogSync } from '../../../lib/card-import/sync-catalog.js';
|
feat(catalog): Scryfall bulk data import + Tagger community tags
Add full Scryfall bulk data pipeline:
- Migration: 13 new columns on `cards` (oracle_id, illustration_id,
color_identity, keywords, legalities, flavor_text, artist, released_at,
layout, edhrec_rank, reserved, reprint, finishes) with GIN indexes
for JSONB search.
- Migration: `tags` + `card_tags` tables for Tagger community data.
- Script: `bulk-import-scryfall.js` — downloads Oracle Cards bulk file
(168 MB) and upserts all 36k+ MTG cards with rich metadata.
- Script: `import-scryfall-tags.js` — imports oracle tags (4.5k tags,
227k taggings) and art tags (11k tags, 458k taggings).
- Lib: `bulk-sync.js` — runtime bulk sync callable from the admin API.
- Admin UI: mode toggle (incremental vs bulk) on catalog sync panel.
Enables Commander deck validation (color_identity), format legality
checks, keyword search, EDHREC popularity ranking, and functional
card tagging ("removal", "ramp", "draw") for deck building assistance.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-14 08:51:39 -04:00
|
|
|
import { runBulkMtgSync } from '../../../lib/card-import/bulk-sync.js';
|
2026-05-28 10:18:01 -04:00
|
|
|
|
2026-06-02 13:42:22 -04:00
|
|
|
export default withAdmin(async function handler(req, res, user) {
|
2026-05-28 10:18:01 -04:00
|
|
|
if (req.method !== 'POST') {
|
|
|
|
|
return res.status(405).json({ error: 'Method not allowed' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.' });
|
|
|
|
|
}
|
|
|
|
|
|
feat(catalog): Scryfall bulk data import + Tagger community tags
Add full Scryfall bulk data pipeline:
- Migration: 13 new columns on `cards` (oracle_id, illustration_id,
color_identity, keywords, legalities, flavor_text, artist, released_at,
layout, edhrec_rank, reserved, reprint, finishes) with GIN indexes
for JSONB search.
- Migration: `tags` + `card_tags` tables for Tagger community data.
- Script: `bulk-import-scryfall.js` — downloads Oracle Cards bulk file
(168 MB) and upserts all 36k+ MTG cards with rich metadata.
- Script: `import-scryfall-tags.js` — imports oracle tags (4.5k tags,
227k taggings) and art tags (11k tags, 458k taggings).
- Lib: `bulk-sync.js` — runtime bulk sync callable from the admin API.
- Admin UI: mode toggle (incremental vs bulk) on catalog sync panel.
Enables Commander deck validation (color_identity), format legality
checks, keyword search, EDHREC popularity ranking, and functional
card tagging ("removal", "ramp", "draw") for deck building assistance.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-14 08:51:39 -04:00
|
|
|
const mode = req.body?.mode || 'incremental';
|
|
|
|
|
|
2026-05-28 10:18:01 -04:00
|
|
|
try {
|
feat(catalog): Scryfall bulk data import + Tagger community tags
Add full Scryfall bulk data pipeline:
- Migration: 13 new columns on `cards` (oracle_id, illustration_id,
color_identity, keywords, legalities, flavor_text, artist, released_at,
layout, edhrec_rank, reserved, reprint, finishes) with GIN indexes
for JSONB search.
- Migration: `tags` + `card_tags` tables for Tagger community data.
- Script: `bulk-import-scryfall.js` — downloads Oracle Cards bulk file
(168 MB) and upserts all 36k+ MTG cards with rich metadata.
- Script: `import-scryfall-tags.js` — imports oracle tags (4.5k tags,
227k taggings) and art tags (11k tags, 458k taggings).
- Lib: `bulk-sync.js` — runtime bulk sync callable from the admin API.
- Admin UI: mode toggle (incremental vs bulk) on catalog sync panel.
Enables Commander deck validation (color_identity), format legality
checks, keyword search, EDHREC popularity ranking, and functional
card tagging ("removal", "ramp", "draw") for deck building assistance.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-14 08:51:39 -04:00
|
|
|
if (mode === 'bulk') {
|
|
|
|
|
const summary = await runBulkMtgSync();
|
|
|
|
|
return res.status(200).json({ success: true, ...summary });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 10:18:01 -04:00
|
|
|
const summary = await runCatalogSync();
|
|
|
|
|
return res.status(200).json({ success: true, ...summary });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('[POST /api/admin/sync-catalog]', error);
|
|
|
|
|
return res.status(500).json({ error: 'Catalog sync failed', details: error.message });
|
|
|
|
|
}
|
2026-06-02 13:42:22 -04:00
|
|
|
});
|