mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
106 lines
3.3 KiB
JavaScript
106 lines
3.3 KiB
JavaScript
import { queryOne, run } from "@/lib/db";
|
|
import { cookies } from "next/headers";
|
|
import { verifyToken } from "@/app/api/auth/[...nextauth]/route";
|
|
|
|
export const dynamic = "force-dynamic"; // Disable caching
|
|
|
|
// Helper function to ensure the hunt_status table exists
|
|
async function ensureHuntStatusTableExists() {
|
|
try {
|
|
// Create hunt_status table if it doesn't exist
|
|
await run(`
|
|
CREATE TABLE IF NOT EXISTS hunt_status (
|
|
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
is_active BOOLEAN DEFAULT 0,
|
|
start_time TEXT,
|
|
end_time TEXT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Check if we need to insert the initial record
|
|
const status = await queryOne("SELECT * FROM hunt_status LIMIT 1");
|
|
if (!status) {
|
|
await run(
|
|
"INSERT INTO hunt_status (id, is_active, start_time) VALUES (1, 0, NULL)"
|
|
);
|
|
console.log("Initialized hunt_status table with default record");
|
|
}
|
|
return true;
|
|
} catch (error) {
|
|
console.error("Error ensuring hunt_status table:", error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// GET endpoint to check if the hunt is active
|
|
export async function GET(request) {
|
|
try {
|
|
// First ensure the table exists
|
|
await ensureHuntStatusTableExists();
|
|
|
|
// Get the token from cookies - properly awaited
|
|
const cookieStore = await cookies();
|
|
const sessionToken = cookieStore.get("next-auth.session-token")?.value;
|
|
|
|
// Check hunt status even if user is not authenticated
|
|
// to prevent auth errors from interfering with status checks
|
|
|
|
// First check the hunt_status table for direct status
|
|
const huntStatus = await queryOne(
|
|
"SELECT is_active, start_time FROM hunt_status LIMIT 1"
|
|
).catch((err) => {
|
|
console.warn("Error querying hunt_status:", err.message);
|
|
return null;
|
|
});
|
|
|
|
// Then check the settings table as a backup
|
|
const huntActiveSetting = await queryOne(
|
|
"SELECT value FROM settings WHERE key = 'hunt_active'"
|
|
).catch((err) => {
|
|
console.warn("Error querying settings:", err.message);
|
|
return null;
|
|
});
|
|
|
|
// Also check the hunts table if it exists (for backward compatibility)
|
|
const oldHuntRecord = await queryOne(
|
|
"SELECT active, start_time FROM hunts ORDER BY id DESC LIMIT 1"
|
|
).catch((err) => {
|
|
// Ignore errors if table doesn't exist
|
|
return null;
|
|
});
|
|
|
|
// Determine active status from all available sources
|
|
let isActive = false;
|
|
let startTime = null;
|
|
|
|
// First priority: hunt_status.is_active
|
|
if (huntStatus && huntStatus.is_active !== null) {
|
|
isActive = Boolean(huntStatus.is_active);
|
|
startTime = huntStatus.start_time;
|
|
}
|
|
// Second priority: settings table
|
|
else if (huntActiveSetting) {
|
|
isActive = huntActiveSetting.value === "true";
|
|
}
|
|
// Third priority: old hunts table
|
|
else if (oldHuntRecord) {
|
|
isActive = Boolean(oldHuntRecord.active);
|
|
startTime = oldHuntRecord.start_time;
|
|
}
|
|
|
|
console.log("Hunt status check:", { isActive, startTime });
|
|
|
|
return Response.json({
|
|
active: isActive,
|
|
startTime: startTime || null,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error checking hunt status:", error);
|
|
return Response.json(
|
|
{ error: "Failed to check hunt status", active: false },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|