feat: add recalculateAllElo function for idempotent ELO recalculation

This commit is contained in:
2026-03-30 21:28:09 -07:00
parent 04a42c903b
commit 3a78f354ad
7 changed files with 594 additions and 61 deletions
@@ -0,0 +1,47 @@
import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { hasRole } from '@/lib/permissions';
import { recalculateAllElo } from '@/lib/elo-utils';
/**
* POST /api/admin/recalculate-elo
*
* Triggers a full recalculation of all ELO ratings and player statistics.
* This endpoint is idempotent - running it multiple times produces the same result.
*
* Requires: club_admin role
*/
export async function POST() {
try {
// Check if user has club_admin role
const permission = await hasRole('club_admin');
if (!permission.allowed) {
return NextResponse.json(
{ error: permission.reason || 'Unauthorized' },
{ status: 403 }
);
}
// Perform the recalculation
const result = await recalculateAllElo(prisma);
return NextResponse.json({
success: true,
message: 'ELO recalculation completed successfully',
data: result,
});
} catch (error) {
console.error('Error during ELO recalculation:', error);
return NextResponse.json(
{ error: 'Failed to recalculate ELO ratings' },
{ status: 500 }
);
}
}
export async function GET() {
return NextResponse.json(
{ error: 'Method not allowed. Use POST to trigger recalculation.' },
{ status: 405 }
);
}