nextjs-rewrite (#5)
Reviewed-on: #5 Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
This commit was merged in pull request #5.
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
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: {
|
||||
include: {
|
||||
player1: true,
|
||||
player2: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// 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("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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user