reports fully implemented
This commit is contained in:
parent
06973ccfb2
commit
e586662c19
Binary file not shown.
@ -44,48 +44,87 @@ export function ReportFilters({
|
|||||||
try {
|
try {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
// Fetch current user info
|
|
||||||
const userResponse = await fetch("/api/users/me");
|
const userResponse = await fetch("/api/users/me");
|
||||||
if (userResponse.ok) {
|
if (!userResponse.ok) {
|
||||||
const userData = await userResponse.json();
|
setLoading(false);
|
||||||
setCurrentUser(userData.user);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Determine which clients to show based on role
|
const userData = await userResponse.json();
|
||||||
if (userData.user.role === "client") {
|
setCurrentUser(userData.user);
|
||||||
// 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);
|
|
||||||
|
|
||||||
if (assignedClientIds.length > 0) {
|
const currentRole = userData.user.role;
|
||||||
// Fetch assigned clients
|
let allUsers: (User & { client?: Client | null })[] = [];
|
||||||
const clientsRes = await fetch(
|
|
||||||
`/api/users?role=client&ids=${assignedClientIds.join(",")}`,
|
if (currentRole === "client") {
|
||||||
);
|
setUsers([userData.user]);
|
||||||
if (clientsRes.ok) {
|
onUserChange(userData.user.id);
|
||||||
const clientsData = await clientsRes.json();
|
setLoading(false);
|
||||||
setUsers(clientsData.data?.users || []);
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
setUsers([]);
|
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
|
setUsers(allUsers);
|
||||||
const clientsRes = await fetch("/api/users?role=client");
|
setLoading(false);
|
||||||
if (clientsRes.ok) {
|
return;
|
||||||
const clientsData = await clientsRes.json();
|
}
|
||||||
setUsers(clientsData.data?.users || []);
|
|
||||||
|
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) {
|
} catch (error) {
|
||||||
log.error("Failed to fetch users:", error);
|
log.error("Failed to fetch users:", error);
|
||||||
} finally {
|
} finally {
|
||||||
@ -120,7 +159,7 @@ export function ReportFilters({
|
|||||||
<SelectContent>
|
<SelectContent>
|
||||||
{users.map((user) => (
|
{users.map((user) => (
|
||||||
<SelectItem key={user.id} value={user.id}>
|
<SelectItem key={user.id} value={user.id}>
|
||||||
{user.firstName} {user.lastName}
|
{user.firstName} {user.lastName} ({user.role})
|
||||||
</SelectItem>
|
</SelectItem>
|
||||||
))}
|
))}
|
||||||
</SelectContent>
|
</SelectContent>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user