feat(api): add auth, user role, match upload, and tournament routes

This commit is contained in:
2026-03-29 19:25:31 -07:00
parent eb8fcd3cf4
commit ebc8352ae1
4 changed files with 377 additions and 245 deletions
+73 -48
View File
@@ -1,49 +1,14 @@
import { NextRequest, NextResponse } from "next/server"
import { prisma } from "@/lib/prisma"
import { auth } from "@/lib/auth"
export async function POST(request: NextRequest) {
const session = await auth()
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
}
try {
const body = await request.json()
const { name, description, eventDate, format, maxParticipants } = body
if (!name) {
return NextResponse.json(
{ error: "Tournament name is required" },
{ status: 400 }
)
}
const tournament = await prisma.event.create({
data: {
name,
description: description || "",
eventDate: eventDate ? new Date(eventDate) : null,
format: format || "round_robin",
status: "planned",
maxParticipants: maxParticipants || null,
},
})
return NextResponse.json({ success: true, tournament })
} catch (error) {
console.error("Error creating tournament:", error)
return NextResponse.json(
{ error: "Failed to create tournament" },
{ status: 500 }
)
}
}
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { getTournamentStatus } from "@/lib/tournamentUtils";
import { canCreateTournaments } from "@/lib/permissions";
import { getSession } from "@/lib/auth-simple";
export async function GET() {
try {
const tournaments = await prisma.event.findMany({
where: { eventType: "tournament" },
orderBy: { createdAt: "desc" },
include: {
participants: true,
teams: {
@@ -52,17 +17,77 @@ export async function GET() {
player2: true,
},
},
rounds: true,
},
orderBy: { createdAt: "desc" },
})
});
return NextResponse.json({ tournaments })
// Update tournament statuses based on event date
const updatedTournaments = await Promise.all(
tournaments.map(async (tournament) => {
const calculatedStatus = getTournamentStatus(tournament.eventDate);
// Only update if the calculated status differs from the stored status
if (tournament.status !== calculatedStatus) {
await prisma.event.update({
where: { id: tournament.id },
data: { status: calculatedStatus },
});
return { ...tournament, status: calculatedStatus };
}
return tournament;
})
);
return NextResponse.json({ tournaments: updatedTournaments });
} catch (error) {
console.error("Error fetching tournaments:", error)
console.error("Failed to fetch tournaments:", error);
return NextResponse.json(
{ error: "Failed to fetch tournaments" },
{ status: 500 }
)
);
}
}
export async function POST(request: Request) {
try {
// Check if user has permission to create tournaments
const permission = await canCreateTournaments();
if (!permission.allowed) {
return NextResponse.json(
{ error: permission.reason || 'Insufficient permissions to create tournaments' },
{ status: 403 }
);
}
// Get the current user to assign as owner
const session = await getSession();
if (!session?.user?.id) {
return NextResponse.json(
{ error: 'User not authenticated' },
{ status: 401 }
);
}
const body = await request.json();
const { name, format, eventDate } = body;
const tournament = await prisma.event.create({
data: {
name,
format: format || "round_robin",
eventDate: eventDate ? new Date(eventDate) : null,
eventType: "tournament",
status: "planned",
ownerId: session.user.id, // Assign ownership to the creator
},
});
return NextResponse.json({ tournament }, { status: 201 });
} catch (error) {
console.error("Failed to create tournament:", error);
return NextResponse.json(
{ error: "Failed to create tournament" },
{ status: 500 }
);
}
}