Feat: Add an stats api endpoint to monitor users, decks and Bsets
This commit is contained in:
parent
82928ea6df
commit
78cd7df1c1
1 changed files with 41 additions and 0 deletions
41
app/app/api/stats/route.ts
Normal file
41
app/app/api/stats/route.ts
Normal 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,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue