mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
also fixing some issues with rerouting api after a user logs in.
This commit is contained in:
1 parent
d204a8e3c4
commit
6b233c90e7
4 files changed
+107
-38
No files matched your search
@@ -360,6 +360,7 @@ export default function TeamPage({ params }) {
|
|||||||
margin: 0;
|
margin: 0;
|
||||||
font-size: 24px;
|
font-size: 24px;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
|
color: white;
|
||||||
}
|
}
|
||||||
|
|
||||||
.team-code {
|
.team-code {
|
||||||
@@ -392,6 +393,7 @@ export default function TeamPage({ params }) {
|
|||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||||
|
border: 2px solid #68543c; /* Add 2px thick border with the same brown color as banner */
|
||||||
}
|
}
|
||||||
|
|
||||||
.member-avatar {
|
.member-avatar {
|
||||||
|
|||||||
@@ -242,12 +242,6 @@ export default function AdminDashboard() {
|
|||||||
return (
|
return (
|
||||||
<div className="admin-page">
|
<div className="admin-page">
|
||||||
<div className="page-header">
|
<div className="page-header">
|
||||||
<div
|
|
||||||
className="back-button"
|
|
||||||
onClick={() => router.push("/team-selection")}
|
|
||||||
>
|
|
||||||
←
|
|
||||||
</div>
|
|
||||||
<div className="page-title">Admin Dashboard</div>
|
<div className="page-title">Admin Dashboard</div>
|
||||||
<div className="placeholder"></div>
|
<div className="placeholder"></div>
|
||||||
</div>
|
</div>
|
||||||
@@ -337,20 +331,13 @@ export default function AdminDashboard() {
|
|||||||
|
|
||||||
.page-header {
|
.page-header {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: space-between;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
padding: 1rem;
|
padding: 1rem;
|
||||||
background-color: #68543c;
|
background-color: #68543c;
|
||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
|
|
||||||
.back-button {
|
|
||||||
font-size: 1.5rem;
|
|
||||||
cursor: pointer;
|
|
||||||
width: 40px;
|
|
||||||
text-align: center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.page-title {
|
.page-title {
|
||||||
font-size: 1.2rem;
|
font-size: 1.2rem;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
@@ -358,6 +345,7 @@ export default function AdminDashboard() {
|
|||||||
|
|
||||||
.placeholder {
|
.placeholder {
|
||||||
width: 40px;
|
width: 40px;
|
||||||
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.admin-content {
|
.admin-content {
|
||||||
|
|||||||
+103
-24
@@ -4,6 +4,37 @@ import { verifyToken } from "@/app/api/auth/[...nextauth]/route";
|
|||||||
|
|
||||||
export const dynamic = "force-dynamic"; // Disable caching
|
export const dynamic = "force-dynamic"; // Disable caching
|
||||||
|
|
||||||
|
// Helper function to ensure the hunt_timer table exists
|
||||||
|
async function ensureTableExists() {
|
||||||
|
try {
|
||||||
|
// Check if the table exists by querying it
|
||||||
|
await queryOne("SELECT name FROM sqlite_master WHERE type='table' AND name='hunt_timer'");
|
||||||
|
|
||||||
|
// If no error was thrown, the table exists
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error checking for hunt_timer table:", error);
|
||||||
|
|
||||||
|
// Try to create the table
|
||||||
|
try {
|
||||||
|
await run(`
|
||||||
|
CREATE TABLE IF NOT EXISTS hunt_timer (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
active INTEGER NOT NULL DEFAULT 0,
|
||||||
|
start_time TEXT NOT NULL,
|
||||||
|
end_time TEXT,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
)
|
||||||
|
`);
|
||||||
|
console.log("hunt_timer table created successfully");
|
||||||
|
return true;
|
||||||
|
} catch (createError) {
|
||||||
|
console.error("Failed to create hunt_timer table:", createError);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GET endpoint to get current timer status
|
// GET endpoint to get current timer status
|
||||||
export async function GET(request) {
|
export async function GET(request) {
|
||||||
try {
|
try {
|
||||||
@@ -20,6 +51,15 @@ export async function GET(request) {
|
|||||||
return Response.json({ error: "Invalid session" }, { status: 401 });
|
return Response.json({ error: "Invalid session" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure the table exists
|
||||||
|
const tableExists = await ensureTableExists();
|
||||||
|
if (!tableExists) {
|
||||||
|
return Response.json(
|
||||||
|
{ error: "Database table not found" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Get the current hunt timer status
|
// Get the current hunt timer status
|
||||||
const timerStatus = await queryOne(
|
const timerStatus = await queryOne(
|
||||||
"SELECT active, start_time, end_time FROM hunt_timer ORDER BY id DESC LIMIT 1"
|
"SELECT active, start_time, end_time FROM hunt_timer ORDER BY id DESC LIMIT 1"
|
||||||
@@ -59,43 +99,80 @@ export async function POST(request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const userData = verifyToken(sessionToken);
|
const userData = verifyToken(sessionToken);
|
||||||
if (!userData || userData.role !== "admin") {
|
if (!userData) {
|
||||||
|
return Response.json({ error: "Invalid session" }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("User role:", userData.role);
|
||||||
|
|
||||||
|
// Check if user is admin (more lenient check)
|
||||||
|
if (!(userData.role === "admin" || userData.isAdmin === true || userData.is_admin === 1)) {
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{ error: "Unauthorized: Admin access required" },
|
{ error: "Unauthorized: Admin access required" },
|
||||||
{ status: 403 }
|
{ status: 403 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure the table exists
|
||||||
|
const tableExists = await ensureTableExists();
|
||||||
|
if (!tableExists) {
|
||||||
|
return Response.json(
|
||||||
|
{ error: "Database table not found" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
const { action, endTime } = body;
|
const { action, endTime } = body;
|
||||||
|
|
||||||
|
console.log("Timer action:", action, "End time:", endTime);
|
||||||
|
|
||||||
if (action === "start") {
|
if (action === "start") {
|
||||||
// Get current time in ISO format
|
try {
|
||||||
const startTime = new Date().toISOString();
|
// Get current time in ISO format
|
||||||
|
const startTime = new Date().toISOString();
|
||||||
|
|
||||||
// Insert new timer record
|
// First, deactivate any existing timers
|
||||||
await run(
|
await run("UPDATE hunt_timer SET active = 0 WHERE active = 1");
|
||||||
"INSERT INTO hunt_timer (active, start_time, end_time) VALUES (?, ?, ?)",
|
|
||||||
[1, startTime, endTime || null]
|
|
||||||
);
|
|
||||||
|
|
||||||
return Response.json({
|
// Insert new timer record
|
||||||
success: true,
|
await run(
|
||||||
message: "Timer started successfully",
|
"INSERT INTO hunt_timer (active, start_time, end_time) VALUES (?, ?, ?)",
|
||||||
startTime,
|
[1, startTime, endTime || null]
|
||||||
endTime: endTime || null,
|
);
|
||||||
});
|
|
||||||
|
return Response.json({
|
||||||
|
success: true,
|
||||||
|
message: "Timer started successfully",
|
||||||
|
startTime,
|
||||||
|
endTime: endTime || null,
|
||||||
|
});
|
||||||
|
} catch (dbError) {
|
||||||
|
console.error("Database error starting timer:", dbError);
|
||||||
|
return Response.json(
|
||||||
|
{ error: "Database error: " + dbError.message },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
} else if (action === "stop") {
|
} else if (action === "stop") {
|
||||||
// Update the most recent timer to inactive
|
try {
|
||||||
await run(
|
// Update the most recent timer to inactive
|
||||||
"UPDATE hunt_timer SET active = 0, end_time = ? WHERE id = (SELECT id FROM hunt_timer ORDER BY id DESC LIMIT 1)",
|
await run(
|
||||||
[new Date().toISOString()]
|
"UPDATE hunt_timer SET active = 0, end_time = ? WHERE active = 1",
|
||||||
);
|
[new Date().toISOString()]
|
||||||
|
);
|
||||||
|
|
||||||
return Response.json({
|
return Response.json({
|
||||||
success: true,
|
success: true,
|
||||||
message: "Timer stopped successfully",
|
message: "Timer stopped successfully",
|
||||||
});
|
});
|
||||||
|
} catch (dbError) {
|
||||||
|
console.error("Database error stopping timer:", dbError);
|
||||||
|
return Response.json(
|
||||||
|
{ error: "Database error: " + dbError.message },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{ error: "Invalid action. Use 'start' or 'stop'." },
|
{ error: "Invalid action. Use 'start' or 'stop'." },
|
||||||
@@ -104,6 +181,8 @@ export async function POST(request) {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error managing timer:", error);
|
console.error("Error managing timer:", error);
|
||||||
return Response.json({ error: "Failed to manage timer" }, { status: 500 });
|
return Response.json({
|
||||||
|
error: "Failed to manage timer: " + error.message
|
||||||
|
}, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Binary file not shown.
Reference in new issue
Block a user