deckhearth/pages/api/cron/sync-catalog.js
varutasu 0a47362103
feat(catalog): weekly Vercel Cron sync for MTG and Pokémon sets (#48)
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:59:59 -05:00

41 lines
1.3 KiB
JavaScript

import { runCatalogSync } from '../../../lib/card-import/sync-catalog.js';
function authorizeCron(req) {
const secret = process.env.CRON_SECRET;
if (!secret) {
if (process.env.NODE_ENV === 'production' || process.env.VERCEL_ENV === 'production') {
return { ok: false, status: 503, error: 'CRON_SECRET is not configured' };
}
console.warn('[GET /api/cron/sync-catalog] CRON_SECRET unset — allowing in dev');
return { ok: true };
}
const authHeader = req.headers.authorization || '';
const token = authHeader.startsWith('Bearer ') ? authHeader.slice('Bearer '.length) : '';
if (!token || token !== secret) {
return { ok: false, status: 401, error: 'Unauthorized' };
}
return { ok: true };
}
export default async function handler(req, res) {
if (req.method !== 'GET') {
return res.status(405).json({ error: 'Method not allowed' });
}
const auth = authorizeCron(req);
if (!auth.ok) {
return res.status(auth.status).json({ error: auth.error });
}
try {
const summary = await runCatalogSync();
return res.status(200).json({ success: true, ...summary });
} catch (error) {
console.error('[GET /api/cron/sync-catalog]', error);
return res.status(500).json({ error: 'Catalog sync failed', details: error.message });
}
}