From ef5d8404c218f633d1482eb58ee75f7ad0523c6b Mon Sep 17 00:00:00 2001 From: David Gwilliam Date: Tue, 31 Mar 2026 03:38:39 -0700 Subject: [PATCH] feat: update recalculate-elo API to support multiple rating systems --- src/app/api/admin/recalculate-elo/route.ts | 37 +++++++++++++++++----- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/app/api/admin/recalculate-elo/route.ts b/src/app/api/admin/recalculate-elo/route.ts index fda1633..85ee49b 100644 --- a/src/app/api/admin/recalculate-elo/route.ts +++ b/src/app/api/admin/recalculate-elo/route.ts @@ -2,16 +2,21 @@ import { NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; import { hasRole } from '@/lib/permissions'; import { recalculateAllElo } from '@/lib/elo-utils'; +import { recalculateAllOpenSkill } from '@/lib/openskill-utils'; +import { recalculateAllGlicko2 } from '@/lib/glicko2-utils'; /** * POST /api/admin/recalculate-elo * - * Triggers a full recalculation of all ELO ratings and player statistics. + * Triggers a full recalculation of all rating systems (Elo, OpenSkill, Glicko2). * This endpoint is idempotent - running it multiple times produces the same result. * * Requires: club_admin role + * + * Query parameters: + * - ratingSystem: 'all' (default), 'elo', 'openskill', 'glicko2' */ -export async function POST() { +export async function POST(request: Request) { try { // Check if user has club_admin role const permission = await hasRole('club_admin'); @@ -22,18 +27,34 @@ export async function POST() { ); } - // Perform the recalculation - const result = await recalculateAllElo(prisma); + // Parse request body to get rating system preference + const body = await request.json().catch(() => ({})); + const ratingSystem = body.ratingSystem || 'all'; + + const results: Record = {}; + + // Perform recalculations based on rating system + if (ratingSystem === 'all' || ratingSystem === 'elo') { + results.elo = await recalculateAllElo(prisma); + } + + if (ratingSystem === 'all' || ratingSystem === 'openskill') { + results.openskill = await recalculateAllOpenSkill(prisma); + } + + if (ratingSystem === 'all' || ratingSystem === 'glicko2') { + results.glicko2 = await recalculateAllGlicko2(prisma); + } return NextResponse.json({ success: true, - message: 'ELO recalculation completed successfully', - data: result, + message: `Rating recalculation completed successfully for: ${ratingSystem}`, + data: results, }); } catch (error) { - console.error('Error during ELO recalculation:', error); + console.error('Error during rating recalculation:', error); return NextResponse.json( - { error: 'Failed to recalculate ELO ratings' }, + { error: 'Failed to recalculate ratings' }, { status: 500 } ); }