add manual addtiona of RH as these sometimes are missed

This commit is contained in:
2025-08-17 15:24:25 +01:00
parent db77e1f0bf
commit da70f6f184
4 changed files with 171 additions and 6 deletions

View File

@@ -0,0 +1,70 @@
import { NextRequest } from 'next/server';
import { promises as fs } from 'fs';
import path from 'path';
const DATA_DIR = path.join(process.cwd(), 'data');
const OVERRIDES_FILE = path.join(DATA_DIR, 'overrides.json');
const CARDS_FILE = path.join(DATA_DIR, 'cards.json');
async function ensureFile(): Promise<void> {
try { await fs.mkdir(DATA_DIR, { recursive: true }); } catch {}
try { await fs.access(OVERRIDES_FILE); }
catch { await fs.writeFile(OVERRIDES_FILE, JSON.stringify({ reverseHolofoil: {} }, null, 2), 'utf-8'); }
}
export async function GET() {
try {
await ensureFile();
const raw = await fs.readFile(OVERRIDES_FILE, 'utf-8');
const data = raw ? JSON.parse(raw) : { reverseHolofoil: {} };
return Response.json({ data }, { status: 200 });
} catch (err) {
console.error('GET /api/magikarp/override error', err);
return Response.json({ data: { reverseHolofoil: {} } }, { status: 200 });
}
}
// Body: { id: string, reverseHolofoil?: boolean }
export async function POST(req: NextRequest) {
try {
const { id, reverseHolofoil } = await req.json();
if (!id || typeof id !== 'string') {
return Response.json({ error: 'Missing id' }, { status: 400 });
}
await ensureFile();
const raw = await fs.readFile(OVERRIDES_FILE, 'utf-8');
const json = raw ? JSON.parse(raw) : { reverseHolofoil: {} };
if (!json.reverseHolofoil || typeof json.reverseHolofoil !== 'object') json.reverseHolofoil = {};
if (reverseHolofoil === false) {
delete json.reverseHolofoil[id];
} else {
json.reverseHolofoil[id] = true;
}
await fs.writeFile(OVERRIDES_FILE, JSON.stringify(json, null, 2), 'utf-8');
// Best-effort: patch cached cards.json so UI reflects without refresh
try {
const rawCards = await fs.readFile(CARDS_FILE, 'utf-8');
if (rawCards) {
const cardsJson = JSON.parse(rawCards);
if (Array.isArray(cardsJson?.data)) {
let mutated = false;
cardsJson.data = cardsJson.data.map((c: any) => {
if (c?.id === id) {
const next = { ...c, variants: { ...(c.variants || {}), reverseHolofoil: reverseHolofoil !== false } };
mutated = true;
return next;
}
return c;
});
if (mutated) {
await fs.writeFile(CARDS_FILE, JSON.stringify(cardsJson, null, 2), 'utf-8');
}
}
}
} catch {}
return Response.json({ ok: true }, { status: 200 });
} catch (err) {
console.error('POST /api/magikarp/override error', err);
return Response.json({ error: 'Write failed' }, { status: 500 });
}
}

View File

@@ -5,6 +5,7 @@ import path from 'path';
const DATA_DIR = path.join(process.cwd(), 'data');
const CARDS_FILE = path.join(DATA_DIR, 'cards.json');
const OVERRIDES_FILE = path.join(DATA_DIR, 'overrides.json');
const TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
async function ensureDataDir() {
@@ -21,12 +22,28 @@ export async function GET(req: NextRequest) {
try {
await ensureDataDir();
// Load overrides best-effort
let overrides: { reverseHolofoil?: Record<string, boolean> } = {};
try {
const rawOv = await fs.readFile(OVERRIDES_FILE, 'utf-8');
overrides = rawOv ? JSON.parse(rawOv) : {};
} catch {}
const reverseMap = overrides?.reverseHolofoil || {};
if (!refresh) {
// Try to serve from cache if exists
try {
const raw = await fs.readFile(CARDS_FILE, 'utf-8');
if (raw) {
const json = JSON.parse(raw);
// Apply overrides on the fly to cached payload
if (Array.isArray(json?.data)) {
json.data = json.data.map((c: any) => {
if (reverseMap[c.id]) {
c.variants = { ...(c.variants || {}), reverseHolofoil: true };
}
return c;
});
}
const ts = json?.updatedAt ? Date.parse(json.updatedAt) : NaN;
const fresh = Number.isFinite(ts) && (Date.now() - ts) < TTL_MS;
if (fresh) {
@@ -38,7 +55,11 @@ export async function GET(req: NextRequest) {
// Fetch fresh from API
const data = await fetchMagikarpCards({ q, page, pageSize });
const payload = { ...data, cached: false, updatedAt: new Date().toISOString() };
// Apply overrides to fresh data
const patched = Array.isArray(data?.data)
? { ...data, data: data.data.map((c: any) => (reverseMap[c.id] ? { ...c, variants: { ...(c.variants || {}), reverseHolofoil: true } } : c)) }
: data;
const payload = { ...patched, cached: false, updatedAt: new Date().toISOString() };
// Write cache best-effort
try { await fs.writeFile(CARDS_FILE, JSON.stringify(payload, null, 2), 'utf-8'); } catch {}
return Response.json(payload, { status: 200 });