reports fully implemented
This commit is contained in:
parent
06973ccfb2
commit
e586662c19
Binary file not shown.
@ -44,19 +44,27 @@ export function ReportFilters({
|
||||
try {
|
||||
setLoading(true);
|
||||
|
||||
// Fetch current user info
|
||||
const userResponse = await fetch("/api/users/me");
|
||||
if (userResponse.ok) {
|
||||
if (!userResponse.ok) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const userData = await userResponse.json();
|
||||
setCurrentUser(userData.user);
|
||||
|
||||
// Determine which clients to show based on role
|
||||
if (userData.user.role === "client") {
|
||||
// Regular users can only view their own report
|
||||
const currentRole = userData.user.role;
|
||||
let allUsers: (User & { client?: Client | null })[] = [];
|
||||
|
||||
if (currentRole === "client") {
|
||||
setUsers([userData.user]);
|
||||
onUserChange(userData.user.id);
|
||||
} else if (userData.user.role === "trainer") {
|
||||
// Trainers can only view their assigned clients
|
||||
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();
|
||||
@ -65,27 +73,58 @@ export function ReportFilters({
|
||||
.map((a: any) => a.clientId);
|
||||
|
||||
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([]);
|
||||
}
|
||||
}
|
||||
} 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 || []);
|
||||
allUsers = [...allUsers, ...(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({
|
||||
<SelectContent>
|
||||
{users.map((user) => (
|
||||
<SelectItem key={user.id} value={user.id}>
|
||||
{user.firstName} {user.lastName}
|
||||
{user.firstName} {user.lastName} ({user.role})
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user