From 6f83080fca6f93ec8f9a02fdb414d682f77595e5 Mon Sep 17 00:00:00 2001 From: David Gwilliam Date: Mon, 30 Mar 2026 22:58:25 -0700 Subject: [PATCH] feat: update POST /api/matches to handle eventId and tournament match stats --- src/app/api/matches/route.ts | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/app/api/matches/route.ts b/src/app/api/matches/route.ts index 92095b5..dbd5c90 100644 --- a/src/app/api/matches/route.ts +++ b/src/app/api/matches/route.ts @@ -23,9 +23,15 @@ export async function POST(request: Request) { team1Score, team2Score, playedAt, - isCasual = true, + eventId, + isCasual: isCasualFromRequest, } = body; + // Determine if match is casual + // If eventId is provided, it's a tournament match (not casual by default) + // If eventId is null, it's a casual match (casual by default) + const isCasual = isCasualFromRequest !== undefined ? isCasualFromRequest : (eventId ? false : true); + // Validate required fields if (!team1P1Id || !team1P2Id || !team2P1Id || !team2P2Id) { return NextResponse.json( @@ -93,10 +99,10 @@ export async function POST(request: Request) { const player3EloChange = team2EloChange / 2; const player4EloChange = team2EloChange / 2; - // Create match (eventId is null for casual matches) + // Create match const match = await prisma.match.create({ data: { - eventId: null, // No tournament association for casual matches + eventId: eventId ? parseInt(eventId) : null, isCasual: isCasual, playedAt: playedAt ? new Date(playedAt) : new Date(), team1P1Id: parseInt(team1P1Id), @@ -110,17 +116,15 @@ export async function POST(request: Request) { }, }); - // Update player stats (only if it's a casual match - tournament matches update via their own flow) - if (isCasual) { - await updatePlayerStats(parseInt(team1P1Id), team1Won, player1EloChange, isTie); - await updatePlayerStats(parseInt(team1P2Id), team1Won, player2EloChange, isTie); - await updatePlayerStats(parseInt(team2P1Id), team2Won, player3EloChange, isTie); - await updatePlayerStats(parseInt(team2P2Id), team2Won, player4EloChange, isTie); + // Update player stats + await updatePlayerStats(parseInt(team1P1Id), team1Won, player1EloChange, isTie); + await updatePlayerStats(parseInt(team1P2Id), team1Won, player2EloChange, isTie); + await updatePlayerStats(parseInt(team2P1Id), team2Won, player3EloChange, isTie); + await updatePlayerStats(parseInt(team2P2Id), team2Won, player4EloChange, isTie); - // Update partnership stats for casual matches - await updatePartnershipStats(parseInt(team1P1Id), parseInt(team1P2Id), team1Won, player1EloChange, isTie); - await updatePartnershipStats(parseInt(team2P1Id), parseInt(team2P2Id), team2Won, player3EloChange, isTie); - } + // Update partnership stats + await updatePartnershipStats(parseInt(team1P1Id), parseInt(team1P2Id), team1Won, player1EloChange, isTie); + await updatePartnershipStats(parseInt(team2P1Id), parseInt(team2P2Id), team2Won, player3EloChange, isTie); return NextResponse.json({ success: true,