mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
fixed a bit
This commit is contained in:
1 parent
e9d831c0a2
commit
9326f2845e
8 files changed
+990
-584
No files matched your search
@@ -1,41 +1,101 @@
|
||||
// app/api/teams/[teamId]/route.js
|
||||
import { queryOne } from '../../../../lib/db';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { authOptions } from '../../../api/auth/[...nextauth]/route';
|
||||
import { queryOne } from "../../../../lib/db";
|
||||
import { getServerSession } from "next-auth/next";
|
||||
import { authOptions } from "../../../api/auth/[...nextauth]/route";
|
||||
|
||||
export async function GET(request, { params }) {
|
||||
const session = await getServerSession(authOptions);
|
||||
export async function GET(request, context) {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
if (!session) {
|
||||
return Response.json({ error: 'Unauthorised' }, { status: 401 });
|
||||
if (!session) {
|
||||
return new Response(JSON.stringify({ error: "Unauthorised" }), {
|
||||
status: 401,
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const { teamId } = await context.params; // Await the params
|
||||
|
||||
// Convert teamId to number for proper comparison
|
||||
const requestedTeamId = parseInt(teamId, 10);
|
||||
const userTeamId = parseInt(session.user.teamId, 10);
|
||||
|
||||
// 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 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const { teamId } = params;
|
||||
const team = await queryOne("SELECT * FROM teams WHERE id = ?", [
|
||||
requestedTeamId,
|
||||
]);
|
||||
|
||||
// Convert teamId to number for proper comparison
|
||||
const requestedTeamId = parseInt(teamId, 10);
|
||||
const userTeamId = parseInt(session.user.teamId, 10);
|
||||
|
||||
// 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 Response.json({ error: 'Not authorized to view this team' }, { status: 403 });
|
||||
}
|
||||
|
||||
const team = await queryOne('SELECT * FROM teams WHERE id = ?', [requestedTeamId]);
|
||||
|
||||
if (!team) {
|
||||
return Response.json({ error: 'Team not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json(team);
|
||||
} catch (error) {
|
||||
console.error('Team fetch error:', error);
|
||||
return Response.json({ error: 'Failed to fetch team details' }, { status: 500 });
|
||||
if (!team) {
|
||||
return new Response(JSON.stringify({ error: "Team not found" }), {
|
||||
status: 404,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify(team), { status: 200 });
|
||||
} 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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user