Updated to v1.0.4, fixed admin global page and overall speed

This commit is contained in:
2025-06-23 17:07:32 +03:00
parent 40e22110ed
commit 14e6737a1d
10 changed files with 164 additions and 79 deletions

View File

@@ -1,5 +1,6 @@
"use client"
//export const dynamic = 'force-dynamic'
//export const revalidate = 0
import type React from "react"
import { useState, useEffect } from "react"
import { useRouter } from "next/navigation"
@@ -189,7 +190,7 @@ export default function AdminPage() {
if (data.lastReset.timestamp) {
const resetTime = new Date(data.lastReset.timestamp).getTime()
const now = new Date().getTime()
const cooldownMs = 2 * 60 * 1000 // 2 minutes
const cooldownMs = 2 * 60 // 2 minutes
const remaining = Math.max(0, cooldownMs - (now - resetTime))
setGlobalResetCooldown(Math.ceil(remaining / 1000))
}
@@ -315,13 +316,6 @@ export default function AdminPage() {
setUser(parsedUser)
}, [router])
// Add this right after the existing useEffect that sets the user
useEffect(() => {
// Force initial fetch when user changes
if (user?.national_id) {
refetchGlobal()
}
}, [user?.national_id, refetchGlobal])
useEffect(() => {
if (globalResetCooldown > 0) {
const timer = setTimeout(() => setGlobalResetCooldown(globalResetCooldown - 1), 1000)
@@ -364,7 +358,7 @@ export default function AdminPage() {
if (response.ok) {
setMessage(data.message || "כל הסטטוסים אופסו בהצלחה")
setGlobalResetCooldown(120) // 2 minutes
setGlobalResetCooldown(30) // 2 minutes
setGlobalLastReset(`${user?.name} - ${new Date().toLocaleString("he-IL")}`)
refetchGlobal()
refetchTeam() // Also refresh team data
@@ -642,13 +636,16 @@ export default function AdminPage() {
setFieldModalOpen(true)
}
const handleManualRefresh = () => {
const handleManualRefresh = async () => {
setIsRefreshing(true)
refetchGlobal()
refetchTeam()
refetchDepartment()
refetchField()
setTimeout(() => setIsRefreshing(false), 500)
try {
await Promise.all([refetchGlobal(), refetchTeam(), refetchDepartment(), refetchField()])
} catch (error) {
console.error("Manual refresh failed:", error)
setMessage("שגיאה ברענון הנתונים")
} finally {
setTimeout(() => setIsRefreshing(false), 500)
}
}
const handleReportOnBehalf = async (userId: string, status: string) => {
@@ -952,7 +949,10 @@ export default function AdminPage() {
<AlertDescription>
<strong>Debug Info:</strong>
<div className="text-xs mt-2 bg-gray-100 p-2 rounded space-y-1">
<div>User: {user?.name} ({user?.role})</div>
<div>Team: {teamName}</div>
<div>Global Users: {globalUsers.length}</div>
<div>Global Connected: {globalConnected ? "Yes" : "No"}</div>
<div>Global Reset Cooldown: {globalResetCooldown} seconds</div>
<div>Team Reset Cooldown: {teamResetCooldown} seconds</div>
<div>Department Reset Cooldown: {departmentResetCooldown} seconds</div>

View File

@@ -1,8 +1,12 @@
import { NextResponse } from "next/server"
import { executeQuery } from "@/lib/database"
export async function GET() {
export async function POST(request: Request) { // Changed to POST, similar to team_stats-route.ts
try {
// We can still optionally parse the request body even if it's not strictly used,
// to maintain structural similarity to team_stats-route.ts.
// const {} = await request.json(); // If no data is expected, this line can be omitted or left as is.
const results = (await executeQuery(`
SELECT
SUM(CASE WHEN in_shelter IS NULL THEN 1 ELSE 0 END) as no_report,
@@ -15,7 +19,7 @@ export async function GET() {
return NextResponse.json(results[0])
} catch (error) {
console.error("Stats error:", error)
return NextResponse.json({ error: "שגיאה בטעינת סטטיסטיקות" }, { status: 500 })
console.error("Stats error:", error) // Error logging similar to team_stats-route.ts
return NextResponse.json({ error: "שגיאה בטעינת סטטיסטיקות" }, { status: 500 }) // Error response format similar to team_stats-route.ts
}
}
}

View File

@@ -1,45 +1,44 @@
import { type NextRequest, NextResponse } from "next/server";
import { executeQuery } from "@/lib/database";
// /api/admin/users-by-category/route.ts
import { type NextRequest, NextResponse } from "next/server"
import { executeQuery } from "@/lib/database"
export async function GET(request: NextRequest) {
export async function POST(request: NextRequest) { // This function uses POST method
try {
const { searchParams } = new URL(request.url);
const category = searchParams.get("category");
const { category } = await request.json() // Get category from request body
let query = "";
// No params needed for these queries, as there are no WHERE conditions
// that use parameters other than the in_shelter status itself.
let params: any[] = [];
if (!category) {
return NextResponse.json({ error: "נתונים חסרים: קטגוריה" }, { status: 400 })
}
let query = ""
// Params array is still empty as there are no WHERE conditions that use parameters other than the in_shelter status itself.
// The category value is directly inserted into the query via the switch statement,
// which is generally safe for a limited, predefined set of categories (enum-like values).
switch (category) {
case "no_report":
// Added 'department', 'team', 'field' to SELECT clause
query = "SELECT national_id, name, department, team, field FROM users WHERE in_shelter IS NULL ORDER BY name";
break;
query = "SELECT national_id, name, department, team, field FROM users WHERE in_shelter IS NULL ORDER BY name"
break
case "in_shelter":
// Added 'department', 'team', 'field' to SELECT clause
query = "SELECT national_id, name, department, team, field FROM users WHERE in_shelter = 'yes' ORDER BY name";
break;
query = "SELECT national_id, name, department, team, field FROM users WHERE in_shelter = 'yes' ORDER BY name"
break
case "not_in_shelter":
// Added 'department', 'team', 'field' to SELECT clause
query = "SELECT national_id, name, department, team, field FROM users WHERE in_shelter = 'no' ORDER BY name";
break;
query = "SELECT national_id, name, department, team, field FROM users WHERE in_shelter = 'no' ORDER BY name"
break
case "no_alarm":
// Added 'department', 'team', 'field' to SELECT clause
query = "SELECT national_id, name, department, team, field FROM users WHERE in_shelter = 'no_alarm' ORDER BY name";
break;
query = "SELECT national_id, name, department, team, field FROM users WHERE in_shelter = 'no_alarm' ORDER BY name"
break
case "safe_after_exit":
// Added 'department', 'team', 'field' to SELECT clause
query = "SELECT national_id, name, department, team, field FROM users WHERE in_shelter = 'safe_after_exit' ORDER BY name";
break;
query = "SELECT national_id, name, department, team, field FROM users WHERE in_shelter = 'safe_after_exit' ORDER BY name"
break
default:
return NextResponse.json({ error: "קטגוריה לא תקינה" }, { status: 400 });
return NextResponse.json({ error: "קטגוריה לא תקינה" }, { status: 400 })
}
const users = (await executeQuery(query, params)) as any[];
return NextResponse.json(users);
const users = (await executeQuery(query)) as any[] // Execute query without explicit params if values are hardcoded
return NextResponse.json(users)
} catch (error) {
console.error("Get users by category error:", error);
return NextResponse.json({ error: "שגיאה בטעינת משתמשים" }, { status: 500 });
console.error("Get users by category error:", error)
return NextResponse.json({ error: "שגיאה בטעינת משתמשים לפי קטגוריה" }, { status: 500 })
}
}

View File

@@ -1,10 +1,15 @@
// route.ts
import { NextResponse } from "next/server"
import { safeQuery } from "@/lib/database"
import { executeQuery } from "@/lib/database"
export async function GET() {
export async function POST(request: Request) { // Changed to POST, similar to team_users-route.ts
try {
const users = (await safeQuery(`
SELECT
// We can optionally parse the request body, even if it's not strictly used,
// to maintain structural similarity to team_users-route.ts.
// const {} = await request.json(); // If no data is expected, this line can be omitted or left as is.
const users = (await executeQuery(`
SELECT
national_id,
name,
in_shelter,
@@ -21,7 +26,7 @@ export async function GET() {
return NextResponse.json(users)
} catch (error) {
console.error("Get users error:", error)
return NextResponse.json({ error: "שגיאה בטעינת משתמשים" }, { status: 500 })
console.error("Get users error:", error) // Error logging similar to team_users-route.ts
return NextResponse.json({ error: "שגיאה בטעינת משתמשים" }, { status: 500 }) // Error response format similar to team_users-route.ts
}
}