Files
football-next/app/api/rounds/[id]/activate/route.ts
2026-05-13 15:46:27 +03:30

33 lines
1.1 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";
import { getApiUser } from "@/lib/apiAuth";
export async function POST(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const apiUser = await getApiUser(req);
if (!apiUser || apiUser.role !== "ADMIN")
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const currentRound = await db.round.findUnique({ where: { id } });
if (!currentRound) {
return NextResponse.json({ error: "Round not found" }, { status: 404 });
}
// اگه فعاله، غیرفعالش کن
if (currentRound.isActive) {
const round = await db.round.update({
where: { id },
data: { isActive: false }
});
return NextResponse.json(round);
}
// اگه غیرفعاله، همه رو غیرفعال کن و این رو فعال کن
await db.round.updateMany({ data: { isActive: false } });
const round = await db.round.update({
where: { id },
data: { isActive: true }
});
return NextResponse.json(round);
}