Files
peworkshop/app/api/hunt-status/route.js
T
2025-03-15 16:30:04 +00:00

125 lines
4.0 KiB
JavaScript

import { queryOne, run } from "@/lib/db";
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 - allow unauthenticated access for easier status checks
export async function GET(request) {
try {
// First ensure the table exists
await ensureHuntStatusTableExists();
// NO TOKEN CHECK - Allow anonymous status checks
// This is critical for the waiting page to work reliably
// Query all possible sources
const huntStatus = await queryOne(
"SELECT is_active, start_time FROM hunt_status LIMIT 1"
).catch(() => null);
const timerStatus = await queryOne(
"SELECT active, start_time, end_time FROM hunt_timer WHERE active = 1 ORDER BY id DESC LIMIT 1"
).catch(() => null);
const huntActiveSetting = await queryOne(
"SELECT value FROM settings WHERE key = 'hunt_active'"
).catch(() => null);
// Also check the traditional hunts table
const oldHuntRecord = await queryOne(
"SELECT active, start_time FROM hunts ORDER BY id DESC LIMIT 1"
).catch(() => null);
// Determine active status from all possible sources
let isActive = false;
let startTime = null;
let endTime = null;
let source = "none";
// Check all possible active indicators in priority order
if (timerStatus && Number(timerStatus.active) === 1) {
console.log("Hunt active from timer table");
isActive = true;
startTime = timerStatus.start_time;
endTime = timerStatus.end_time;
source = "timer";
} else if (huntStatus && huntStatus.is_active) {
console.log("Hunt active from hunt_status table");
isActive = true;
startTime = huntStatus.start_time;
endTime = huntStatus.end_time;
source = "hunt_status";
} else if (huntActiveSetting && huntActiveSetting.value === "true") {
console.log("Hunt active from settings table");
isActive = true;
// If we have no start time but hunt is active, use current time
if (!startTime) {
startTime = new Date().toISOString();
}
source = "settings";
} else if (oldHuntRecord && oldHuntRecord.active) {
console.log("Hunt active from hunts table");
isActive = true;
startTime = oldHuntRecord.start_time;
endTime = oldHuntRecord.end_time;
source = "hunts";
}
console.log("Hunt status check:", { isActive, startTime, endTime, source });
// Always include all timing data for consistency
return new Response(
JSON.stringify({
active: isActive,
startTime: startTime || null,
endTime: endTime || null,
source, // For debugging
timestamp: new Date().toISOString(), // For cache-busting
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
"Cache-Control": "no-store, no-cache, must-revalidate, max-age=0",
Pragma: "no-cache",
Expires: "0",
},
}
);
} catch (error) {
console.error("Error checking hunt status:", error);
return Response.json(
{ error: "Failed to check hunt status", active: false },
{ status: 500 }
);
}
}