Initial commit
This commit is contained in:
50
app/api/admin/field-stats/route.ts
Normal file
50
app/api/admin/field-stats/route.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { executeQuery } from "@/lib/database"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { adminId } = await request.json()
|
||||
|
||||
if (!adminId) {
|
||||
return NextResponse.json({ error: "מזהה מנהל חסר" }, { status: 400 })
|
||||
}
|
||||
|
||||
// Get admin's field
|
||||
const adminData = (await executeQuery("SELECT field FROM users WHERE national_id = ? AND role IS NOT NULL AND role != 'user'", [
|
||||
adminId,
|
||||
])) as any[]
|
||||
|
||||
if (adminData.length === 0) {
|
||||
return NextResponse.json({ error: "מנהל לא נמצא" }, { status: 404 })
|
||||
}
|
||||
|
||||
const adminField = adminData[0].field
|
||||
|
||||
if (!adminField) {
|
||||
return NextResponse.json({ error: "למנהל לא הוגדר תחום" }, { status: 400 })
|
||||
}
|
||||
|
||||
// Get field stats
|
||||
const results = (await executeQuery(
|
||||
`
|
||||
SELECT
|
||||
SUM(CASE WHEN in_shelter IS NULL THEN 1 ELSE 0 END) as no_report,
|
||||
SUM(CASE WHEN in_shelter = 'yes' THEN 1 ELSE 0 END) as in_shelter,
|
||||
SUM(CASE WHEN in_shelter = 'no' THEN 1 ELSE 0 END) as not_in_shelter,
|
||||
SUM(CASE WHEN in_shelter = 'no_alarm' THEN 1 ELSE 0 END) as no_alarm,
|
||||
SUM(CASE WHEN in_shelter = 'safe_after_exit' THEN 1 ELSE 0 END) as safe_after_exit
|
||||
FROM users
|
||||
WHERE field = ?
|
||||
`,
|
||||
[adminField],
|
||||
)) as any[]
|
||||
|
||||
return NextResponse.json({
|
||||
...results[0],
|
||||
field: adminField,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Field stats error:", error)
|
||||
return NextResponse.json({ error: "שגיאה בטעינת סטטיסטיקות התחום" }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user