feat: update POST /api/matches to handle eventId and tournament match stats
This commit is contained in:
@@ -23,9 +23,15 @@ export async function POST(request: Request) {
|
|||||||
team1Score,
|
team1Score,
|
||||||
team2Score,
|
team2Score,
|
||||||
playedAt,
|
playedAt,
|
||||||
isCasual = true,
|
eventId,
|
||||||
|
isCasual: isCasualFromRequest,
|
||||||
} = body;
|
} = 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
|
// Validate required fields
|
||||||
if (!team1P1Id || !team1P2Id || !team2P1Id || !team2P2Id) {
|
if (!team1P1Id || !team1P2Id || !team2P1Id || !team2P2Id) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
@@ -93,10 +99,10 @@ export async function POST(request: Request) {
|
|||||||
const player3EloChange = team2EloChange / 2;
|
const player3EloChange = team2EloChange / 2;
|
||||||
const player4EloChange = team2EloChange / 2;
|
const player4EloChange = team2EloChange / 2;
|
||||||
|
|
||||||
// Create match (eventId is null for casual matches)
|
// Create match
|
||||||
const match = await prisma.match.create({
|
const match = await prisma.match.create({
|
||||||
data: {
|
data: {
|
||||||
eventId: null, // No tournament association for casual matches
|
eventId: eventId ? parseInt(eventId) : null,
|
||||||
isCasual: isCasual,
|
isCasual: isCasual,
|
||||||
playedAt: playedAt ? new Date(playedAt) : new Date(),
|
playedAt: playedAt ? new Date(playedAt) : new Date(),
|
||||||
team1P1Id: parseInt(team1P1Id),
|
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)
|
// Update player stats
|
||||||
if (isCasual) {
|
await updatePlayerStats(parseInt(team1P1Id), team1Won, player1EloChange, isTie);
|
||||||
await updatePlayerStats(parseInt(team1P1Id), team1Won, player1EloChange, isTie);
|
await updatePlayerStats(parseInt(team1P2Id), team1Won, player2EloChange, isTie);
|
||||||
await updatePlayerStats(parseInt(team1P2Id), team1Won, player2EloChange, isTie);
|
await updatePlayerStats(parseInt(team2P1Id), team2Won, player3EloChange, isTie);
|
||||||
await updatePlayerStats(parseInt(team2P1Id), team2Won, player3EloChange, isTie);
|
await updatePlayerStats(parseInt(team2P2Id), team2Won, player4EloChange, isTie);
|
||||||
await updatePlayerStats(parseInt(team2P2Id), team2Won, player4EloChange, isTie);
|
|
||||||
|
|
||||||
// Update partnership stats for casual matches
|
// Update partnership stats
|
||||||
await updatePartnershipStats(parseInt(team1P1Id), parseInt(team1P2Id), team1Won, player1EloChange, isTie);
|
await updatePartnershipStats(parseInt(team1P1Id), parseInt(team1P2Id), team1Won, player1EloChange, isTie);
|
||||||
await updatePartnershipStats(parseInt(team2P1Id), parseInt(team2P2Id), team2Won, player3EloChange, isTie);
|
await updatePartnershipStats(parseInt(team2P1Id), parseInt(team2P2Id), team2Won, player3EloChange, isTie);
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user