Feat: Add an stats api endpoint to monitor users, decks and Bsets

This commit is contained in:
zuma 2025-02-16 20:22:51 +01:00
parent 82928ea6df
commit 78cd7df1c1

View file

@ -0,0 +1,41 @@
import { NextResponse, NextRequest } from 'next/server'
import { validateToken, decryptToken } from '@/lib/jwt'
import { db } from "@/lib/db"
export async function GET(req: NextRequest) {
try {
const token = req?.headers.get("authorization")?.split(" ")[1]
if (token == undefined || !validateToken(token)) {
return NextResponse.json({"message": "You did not provide token or it is in the wrong format"},{
status: 401,
});
}
const tokenData = decryptToken(token)
if (!tokenData.admin) {
return NextResponse.json({"message": "You need to be an admin to use this endpoint."},{
status: 401,
});
}
const decks = await db.deck.findMany()
const bsets = await db.bset.findMany()
const users = await db.utilisateurice.findMany()
return NextResponse.json({"data": { "decks": decks.length, "bsets": bsets.length, "users": users.length },"message": "Here are the stats"},{
status: 200,
});
} catch (error) {
console.log(error)
return NextResponse.json(
{ error: "Failed, check console" },
{
status: 500,
}
);
}
}