diff --git a/apps/admin/data/fitai.db b/apps/admin/data/fitai.db index ccd543a..77add3c 100644 Binary files a/apps/admin/data/fitai.db and b/apps/admin/data/fitai.db differ diff --git a/apps/admin/src/components/reports/ReportFilters.tsx b/apps/admin/src/components/reports/ReportFilters.tsx index 441877c..651b617 100644 --- a/apps/admin/src/components/reports/ReportFilters.tsx +++ b/apps/admin/src/components/reports/ReportFilters.tsx @@ -44,48 +44,87 @@ export function ReportFilters({ try { setLoading(true); - // Fetch current user info const userResponse = await fetch("/api/users/me"); - if (userResponse.ok) { - const userData = await userResponse.json(); - setCurrentUser(userData.user); + if (!userResponse.ok) { + setLoading(false); + return; + } - // Determine which clients to show based on role - if (userData.user.role === "client") { - // Regular users can only view their own report - setUsers([userData.user]); - onUserChange(userData.user.id); - } else if (userData.user.role === "trainer") { - // Trainers can only view their assigned clients - const assignmentsRes = await fetch("/api/trainer-client"); - if (assignmentsRes.ok) { - const assignmentsData = await assignmentsRes.json(); - const assignedClientIds = (assignmentsData.assignments || []) - .filter((a: any) => a.isActive) - .map((a: any) => a.clientId); + const userData = await userResponse.json(); + setCurrentUser(userData.user); - if (assignedClientIds.length > 0) { - // Fetch assigned clients - const clientsRes = await fetch( - `/api/users?role=client&ids=${assignedClientIds.join(",")}`, - ); - if (clientsRes.ok) { - const clientsData = await clientsRes.json(); - setUsers(clientsData.data?.users || []); - } - } else { - setUsers([]); + const currentRole = userData.user.role; + let allUsers: (User & { client?: Client | null })[] = []; + + if (currentRole === "client") { + setUsers([userData.user]); + onUserChange(userData.user.id); + setLoading(false); + return; + } + + if (currentRole === "trainer") { + allUsers.push(userData.user); + const assignmentsRes = await fetch("/api/trainer-client"); + if (assignmentsRes.ok) { + const assignmentsData = await assignmentsRes.json(); + const assignedClientIds = (assignmentsData.assignments || []) + .filter((a: any) => a.isActive) + .map((a: any) => a.clientId); + + if (assignedClientIds.length > 0) { + const clientsRes = await fetch( + `/api/users?role=client&ids=${assignedClientIds.join(",")}`, + ); + if (clientsRes.ok) { + const clientsData = await clientsRes.json(); + allUsers = [...allUsers, ...(clientsData.data?.users || [])]; } } - } else { - // Admins and superadmins can view all clients - const clientsRes = await fetch("/api/users?role=client"); - if (clientsRes.ok) { - const clientsData = await clientsRes.json(); - setUsers(clientsData.data?.users || []); + } + setUsers(allUsers); + setLoading(false); + return; + } + + if (currentRole === "admin") { + allUsers.push(userData.user); + } + + if (currentRole === "superAdmin" || currentRole === "admin") { + const [adminsRes, trainersRes, clientsRes] = await Promise.all([ + fetch("/api/users?role=admin"), + fetch("/api/users?role=trainer"), + fetch("/api/users?role=client"), + ]); + + const [adminsData, trainersData, clientsData] = await Promise.all([ + adminsRes.ok ? adminsRes.json() : { data: { users: [] } }, + trainersRes.ok ? trainersRes.json() : { data: { users: [] } }, + clientsRes.ok ? clientsRes.json() : { data: { users: [] } }, + ]); + + allUsers = [ + ...allUsers, + ...(adminsData.data?.users || []), + ...(trainersData.data?.users || []), + ...(clientsData.data?.users || []), + ]; + + if (currentRole === "superAdmin") { + const superAdminsRes = await fetch("/api/users?role=superAdmin"); + if (superAdminsRes.ok) { + const superAdminsData = await superAdminsRes.json(); + allUsers = [...(superAdminsData.data?.users || []), ...allUsers]; } } + + setUsers(allUsers); + setLoading(false); + return; } + + setUsers(allUsers); } catch (error) { log.error("Failed to fetch users:", error); } finally { @@ -120,7 +159,7 @@ export function ReportFilters({ {users.map((user) => ( - {user.firstName} {user.lastName} + {user.firstName} {user.lastName} ({user.role}) ))}