Files
wm-projekt/app/api/matches/route.ts
2026-06-22 15:32:32 -05:00

45 lines
1.5 KiB
TypeScript

import { NextResponse } from "next/server";
import { fetchMatchesAndTeams, fetchOdds, attachOdds } from "@/lib/feeds";
import { computeGroupTables, computeThirdPlaceTable } from "@/lib/standings";
import { qualifiedThirdGroups, resolveAnnexC } from "@/lib/bracket";
// Diese Route wird vom Frontend gepollt. Sie ist der einzige Ort, der die
// Upstream-Feeds berührt — gecacht, damit Rate Limits eingehalten werden.
export const dynamic = "force-dynamic";
export async function GET() {
try {
const { matches: rawMatches, teams } = await fetchMatchesAndTeams();
// Odds sind optional: fällt der Polymarket-Call aus, liefern wir trotzdem.
let matches = rawMatches;
try {
const odds = await fetchOdds();
matches = attachOdds(rawMatches, teams, odds);
} catch {
// still ohne Wahrscheinlichkeiten
}
const groupTables = computeGroupTables(teams, matches);
const thirdTable = computeThirdPlaceTable(groupTables);
const qGroups = qualifiedThirdGroups(thirdTable);
const annex = qGroups.length === 8 ? resolveAnnexC(qGroups) : null;
return NextResponse.json({
updatedAt: new Date().toISOString(),
teams,
matches,
groupTables,
thirdTable,
annexAssignment: annex,
annexResolved: annex != null,
});
} catch (err) {
const message = err instanceof Error ? err.message : "unbekannter Fehler";
return NextResponse.json(
{ error: "Feed nicht erreichbar", detail: message },
{ status: 502 },
);
}
}