diff --git a/app/(pages)/admin/users/page.tsx b/app/(pages)/admin/users/page.tsx deleted file mode 100644 index 987f203..0000000 --- a/app/(pages)/admin/users/page.tsx +++ /dev/null @@ -1,207 +0,0 @@ -"use client"; - -import React, { useState, useEffect } from "react"; -import { - Table, - Button, - Input, - Dropdown, - TableHeader, - TableColumn, - TableBody, - TableRow, - TableCell, - Pagination, - User, - Card, - DropdownMenu, - DropdownItem, -} from "@nextui-org/react"; -import { - AiOutlineSearch as SearchIcon, - AiOutlinePlus as PlusIcon, - AiOutlineStop as BlockIcon, -} from "react-icons/ai"; -import SpinnerLoader from "@/components/SpinnerLoader"; - -interface UserData { - id: string; - name: string; - role: string; - username: string; - profile: string; - gender: string; - contact: string; - city: string; - state: string; -} - -interface PaginationMetadata { - currentPage: number; - pageSize: number; - totalPages: number; - totalCount: number; -} - -const UserManagement: React.FC = () => { - const [users, setUsers] = useState([]); - const [loading, setLoading] = useState(false); - const [searchTerm, setSearchTerm] = useState(""); - const [selectedRole, setSelectedRole] = useState("All"); - - const [currentUser, setCurrentUser] = useState(null); - const [pagination, setPagination] = useState({ - currentPage: 1, - pageSize: 10, - totalPages: 1, - totalCount: 0, - }); - - useEffect(() => { - fetchUsers(); - }, [pagination.currentPage]); - - const fetchUsers = async () => { - try { - setLoading(true); - const response = await fetch( - `/api/admin/users?page=${pagination.currentPage}&limit=${pagination.pageSize}` - ); - if (!response.ok) throw new Error("Failed to fetch users"); - const data = await response.json(); - setUsers(data.users); - setPagination(data.pagination); - } catch (error) { - console.error("Error fetching users:", error); - } finally { - setLoading(false); - } - }; - - const filteredUsers = users.filter( - (user) => - user.name.toLowerCase().includes(searchTerm.toLowerCase()) && - (selectedRole === "All" || user.role === selectedRole) - ); - - const handleBlockUser = async (userId: string) => { - console.log("Block user:", userId); - await fetchUsers(); - }; - - return ( -
- - {/*

Users

*/} - -
-
- ) => - setSearchTerm(e.target.value) - } - startContent={} - className="w-full md:w-64" - /> - - - setSelectedRole(key as string)} - selectedKeys={[selectedRole]} - selectionMode="single" - > - All - Patient - Doctor - Receptionist - Hospital - Admin - - -
- -
- - {loading ? ( -
- -
- ) : ( -
- - - User - Role - Username - Gender - Contact - Location - Actions - - - {filteredUsers.map((user) => ( - - - - - {user.role} - {user.username} - {user.gender} - {user.contact} - {`${user.city}, ${user.state}`} - - - - - ))} - -
-
- )} - -
- - setPagination({ ...pagination, currentPage: page }) - } - showControls - showShadow={false} - page={pagination.currentPage} - classNames={{ - cursor: "bg-foreground text-background", - }} - /> -
-
-
- ); -}; - -export default UserManagement; diff --git a/app/api/admin/users/route.ts b/app/api/admin/users/route.ts deleted file mode 100644 index 225d6cb..0000000 --- a/app/api/admin/users/route.ts +++ /dev/null @@ -1,132 +0,0 @@ -import dbConfig from "@utils/db"; -import { decrypt } from "@sessions/sessionUtils"; -import { Admin, Patient, Receptionist, Hospital, Doctor } from "@models/index"; - -export interface UserDetails { - id: string; - profile: string; - name: string; - role: string; - username: string; - gender: string; - contact: string; - city: string; - state: string; -} - -export async function GET(request: Request) { - // const session = request.headers.get("Authorization"); - // if (!session) { - // return Response.json({ error: "Unauthorized" }, { status: 401 }); - // } - - try { - // const token = session.split("Bearer ")[1]; - // const decryptedUser = await decrypt(token); - // const email = decryptedUser.user.email; - - // await dbConfig(); - - // const adminData = await Admin.findOne({ email }); - - // if (!adminData) { - // return Response.json({ error: "Admin not found" }, { status: 404 }); - // } - - // parse query parameters for pagination - const url = new URL(request.url); - const page = parseInt(url.searchParams.get("page") || "1"); - const limit = parseInt(url.searchParams.get("limit") || "10"); - const skip = (page - 1) * limit; - - // base aggregation pipeline - const baseAggregationPipeline = [ - { - $project: { - profile: 1, - name: { $concat: ["$firstname", " ", "$lastname"] }, - username: 1, - gender: 1, - contact: 1, - "address.city": 1, - "address.state": 1, - }, - }, - { $skip: skip }, - { $limit: limit }, - ]; - - // create role-specific pipeline - const createPipelineWithRole = (pipeline: any[], role: string) => [ - ...pipeline.slice(0, 1), - { $addFields: { role: role } }, - ...pipeline.slice(1), - ]; - - // create role-specific pipelines - const patientsPipeline = createPipelineWithRole( - baseAggregationPipeline, - "Patient" - ); - const receptionistsPipeline = createPipelineWithRole( - baseAggregationPipeline, - "Receptionist" - ); - const doctorsPipeline = createPipelineWithRole( - baseAggregationPipeline, - "Doctor" - ); - const hospitalsPipeline = createPipelineWithRole( - baseAggregationPipeline, - "Hospital" - ); - - // fetch users from each model - const [patients, receptionists, doctors, hospitals] = await Promise.all([ - Patient.aggregate(patientsPipeline), - Receptionist.aggregate(receptionistsPipeline), - Doctor.aggregate(doctorsPipeline), - Hospital.aggregate(hospitalsPipeline), - ]); - - // combine and map results - const allUsers = [...patients, ...receptionists, ...doctors, ...hospitals]; - const userDetails: UserDetails[] = allUsers.map((user) => ({ - id: user._id.toString(), - profile: user.profile || "", - name: user.name, - role: user.role, - username: user.username, - gender: user.gender || "", - contact: user.contact || "", - city: user.address?.city || "", - state: user.address?.state || "", - })); - - // pagination count - const [patientCount, receptionistCount, doctorCount, hospitalCount] = - await Promise.all([ - Patient.countDocuments(), - Receptionist.countDocuments(), - Doctor.countDocuments(), - Hospital.countDocuments(), - ]); - const totalUsers = - patientCount + receptionistCount + doctorCount + hospitalCount; - - const paginationMetadata = { - currentPage: page, - pageSize: limit, - totalPages: Math.ceil(totalUsers / limit), - totalCount: totalUsers, - }; - - return Response.json({ - users: userDetails, - pagination: paginationMetadata, - }); - } catch (error) { - console.error("Error fetching Users data:", error); - return Response.json({ error: "Internal Server Error" }, { status: 500 }); - } -}