feat: update recalculate-elo API to support multiple rating systems

This commit is contained in:
2026-03-31 03:38:39 -07:00
parent e3f9935d68
commit ef5d8404c2
+29 -8
View File
@@ -2,16 +2,21 @@ import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma'; import { prisma } from '@/lib/prisma';
import { hasRole } from '@/lib/permissions'; import { hasRole } from '@/lib/permissions';
import { recalculateAllElo } from '@/lib/elo-utils'; import { recalculateAllElo } from '@/lib/elo-utils';
import { recalculateAllOpenSkill } from '@/lib/openskill-utils';
import { recalculateAllGlicko2 } from '@/lib/glicko2-utils';
/** /**
* POST /api/admin/recalculate-elo * 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. * This endpoint is idempotent - running it multiple times produces the same result.
* *
* Requires: club_admin role * Requires: club_admin role
*
* Query parameters:
* - ratingSystem: 'all' (default), 'elo', 'openskill', 'glicko2'
*/ */
export async function POST() { export async function POST(request: Request) {
try { try {
// Check if user has club_admin role // Check if user has club_admin role
const permission = await hasRole('club_admin'); const permission = await hasRole('club_admin');
@@ -22,18 +27,34 @@ export async function POST() {
); );
} }
// Perform the recalculation // Parse request body to get rating system preference
const result = await recalculateAllElo(prisma); const body = await request.json().catch(() => ({}));
const ratingSystem = body.ratingSystem || 'all';
const results: Record<string, any> = {};
// 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({ return NextResponse.json({
success: true, success: true,
message: 'ELO recalculation completed successfully', message: `Rating recalculation completed successfully for: ${ratingSystem}`,
data: result, data: results,
}); });
} catch (error) { } catch (error) {
console.error('Error during ELO recalculation:', error); console.error('Error during rating recalculation:', error);
return NextResponse.json( return NextResponse.json(
{ error: 'Failed to recalculate ELO ratings' }, { error: 'Failed to recalculate ratings' },
{ status: 500 } { status: 500 }
); );
} }