fixed a bit

This commit is contained in:
bobbert committed 2025-03-11 23:09:05 +00:00
1 parent a0fb2b9970
commit 25fe268ed6
3 files changed
+150 -142

No files matched your search

+10 -85
View File
@@ -1,105 +1,30 @@
// app/api/teams/[teamId]/route.js
// app/(main)/team/[teamId]/route.js
import { queryOne, query } from "../../../../lib/db";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../../../api/auth/[...nextauth]/route";
import { redirect } from "next/navigation";
export async function GET(request, context) {
const session = await getServerSession(authOptions);
if (!session) {
return new Response(JSON.stringify({ error: "Unauthorised" }), {
status: 401,
});
// Redirect to login page instead of returning JSON
return redirect("/login");
}
try {
const { teamId } = await context.params; // Await the params
const { teamId } = context.params;
// Convert teamId to number for proper comparison
const requestedTeamId = parseInt(teamId, 10);
const userTeamId = parseInt(session.user.teamId, 10);
// Instead of returning JSON, we should let the page component handle rendering
// This route.js file should only be used for API endpoints, not page rendering
// Log for debugging
console.log("Requested team:", requestedTeamId);
console.log("User team:", userTeamId);
console.log("User role:", session.user.role);
// Ensure the user is requesting their own team or is an admin
if (userTeamId !== requestedTeamId && session.user.role !== "admin") {
return new Response(
JSON.stringify({ error: "Not authorized to view this team" }),
{ status: 403 }
);
}
const team = await queryOne(
"SELECT id, name, code FROM teams WHERE id = ?",
[requestedTeamId]
);
if (!team) {
return new Response(JSON.stringify({ error: "Team not found" }), {
status: 404,
});
}
const members = await query(
"SELECT id, username FROM users WHERE team_id = ?",
[requestedTeamId]
);
return new Response(JSON.stringify({ team, members }), { status: 200 });
// Redirect to the page component
return redirect(`/team/${teamId}`);
} catch (error) {
console.error("Team fetch error:", error);
return new Response(
JSON.stringify({ error: "Failed to fetch team details" }),
{ status: 500 }
);
}
}
export async function PATCH(request, context) {
const session = await getServerSession(authOptions);
if (!session) {
return new Response(JSON.stringify({ error: "Unauthorised" }), {
status: 401,
});
}
try {
const { teamId } = await context.params; // Await the params
const requestedTeamId = parseInt(teamId, 10);
const { userId } = await request.json(); // Parse the request body
// Log for debugging
console.log("Requested team:", requestedTeamId);
console.log("User role:", session.user.role);
// Ensure the user is an admin or the user themselves
if (session.user.role !== "admin" && session.user.id !== userId) {
return new Response(
JSON.stringify({ error: "Not authorized to change this team" }),
{ status: 403 }
);
}
// Update the user's team
await queryOne("UPDATE users SET teamId = ? WHERE id = ?", [
requestedTeamId,
userId,
]);
// Update the session with the new teamId
session.user.teamId = requestedTeamId;
return new Response(
JSON.stringify({ success: "Team updated successfully" }),
{ status: 200 }
);
} catch (error) {
console.error("Team update error:", error);
return new Response(JSON.stringify({ error: "Failed to update team" }), {
status: 500,
});
}
}