From f5fabe4159f72c2753786d24de7732161420b385 Mon Sep 17 00:00:00 2001 From: Ezhil Shanmugham Date: Fri, 9 Aug 2024 01:23:03 +0530 Subject: [PATCH] userCount --- app/admin/page.tsx | 23 ++++++++++- app/api/admin/count/route.ts | 56 +++++++++++++++++++++++++ components/ui/card.tsx | 79 ++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 app/api/admin/count/route.ts create mode 100644 components/ui/card.tsx diff --git a/app/admin/page.tsx b/app/admin/page.tsx index d222e52..7dbd9c3 100644 --- a/app/admin/page.tsx +++ b/app/admin/page.tsx @@ -7,7 +7,7 @@ import { redirect } from "next/navigation"; import { Responses, columns } from "./columns"; import { DataTable } from "./data-table"; -import bg from "../assets/bg.jpg"; +import { Card, CardContent } from "@/components/ui/card"; const Admin = () => { const { data: session } = useSession({ @@ -22,6 +22,17 @@ const Admin = () => { } const [data, setData] = useState([]); + const [count, setCount] = useState([0, 0, 0, 0]); + + useEffect(() => { + fetch("/api/admin/count", { + method: "POST", + }) + .then((res) => res.json()) + .then((data) => { + setCount(data.users); + }); + }, []); useEffect(() => { fetch("/api/admin/responses", { @@ -43,6 +54,16 @@ const Admin = () => { Welcome, {session?.user?.name}! + + +

Users

+

Total - {count[0]}

+

Applied - {count[1]}

+

Shortlisted - {count[2]}

+

Selected - {count[3]}

+
+
+
diff --git a/app/api/admin/count/route.ts b/app/api/admin/count/route.ts new file mode 100644 index 0000000..abb618a --- /dev/null +++ b/app/api/admin/count/route.ts @@ -0,0 +1,56 @@ +import prisma from "@/app/lib/db"; +import { getServerSession } from "next-auth"; +import { NextResponse, NextRequest } from "next/server"; +import { options } from "@/app/api/auth/[...nextauth]/options"; + +export const POST = async (request: NextRequest) => { + const session = await getServerSession(options); + + if (!session) { + return NextResponse.redirect("/"); + } + + const { user } = session; + + if (user.role !== "admin") { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + try { + const totalCount = await prisma.user.count({ + where: { + role: "user", + }, + }); + + const appliedCount = await prisma.user.count({ + where: { + isFormSubmitted: true, + role: "user", + }, + }); + + const shortlistedCount = await prisma.user.count({ + where: { + isShortlisted: true, + role: "user", + }, + }); + + const selectedCount = await prisma.user.count({ + where: { + isSelected: true, + role: "user", + }, + }); + + return NextResponse.json({ + users: [totalCount, appliedCount, shortlistedCount, selectedCount], + }); + } catch (error) { + return NextResponse.json( + { error: "Internal Server Error" }, + { status: 500 } + ); + } +}; diff --git a/components/ui/card.tsx b/components/ui/card.tsx new file mode 100644 index 0000000..afa13ec --- /dev/null +++ b/components/ui/card.tsx @@ -0,0 +1,79 @@ +import * as React from "react" + +import { cn } from "@/lib/utils" + +const Card = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +
+)) +Card.displayName = "Card" + +const CardHeader = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +
+)) +CardHeader.displayName = "CardHeader" + +const CardTitle = React.forwardRef< + HTMLParagraphElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +

+)) +CardTitle.displayName = "CardTitle" + +const CardDescription = React.forwardRef< + HTMLParagraphElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +

+)) +CardDescription.displayName = "CardDescription" + +const CardContent = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +

+)) +CardContent.displayName = "CardContent" + +const CardFooter = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +
+)) +CardFooter.displayName = "CardFooter" + +export { Card, CardHeader, CardFooter, CardTitle, CardDescription, CardContent }