diff --git a/src/dashboards/Corporate.tsx b/src/dashboards/Corporate.tsx index 3de8527b..23a7c45a 100644 --- a/src/dashboards/Corporate.tsx +++ b/src/dashboards/Corporate.tsx @@ -207,64 +207,6 @@ export default function CorporateDashboard({user, linkedCorporate}: Props) { ); - const StudentsList = () => { - const filter = (x: User) => - x.type === "student" && - (!!selectedUser - ? groups - .filter((g) => g.admin === selectedUser.id) - .flatMap((g) => g.participants) - .includes(x.id) || false - : groups.flatMap((g) => g.participants).includes(x.id)); - - return ( - ( -
-
router.push("/")} - className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300"> - - Back -
-

Students ({total})

-
- )} - /> - ); - }; - - const TeachersList = () => { - const filter = (x: User) => - x.type === "teacher" && - (!!selectedUser - ? groups - .filter((g) => g.admin === selectedUser.id) - .flatMap((g) => g.participants) - .includes(x.id) || false - : groups.flatMap((g) => g.participants).includes(x.id)); - - return ( - ( -
-
router.push("/")} - className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300"> - - Back -
-

Teachers ({total})

-
- )} - /> - ); - }; - const GroupsList = () => { const filter = (x: Group) => x.admin === user.id || x.participants.includes(user.id); @@ -518,8 +460,40 @@ export default function CorporateDashboard({user, linkedCorporate}: Props) { )} - {router.asPath === "/#students" && } - {router.asPath === "/#teachers" && } + {router.asPath === "/#students" && ( + x.type === "student"]} + renderHeader={(total) => ( +
+
router.push("/")} + className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300"> + + Back +
+

Students ({total})

+
+ )} + /> + )} + {router.asPath === "/#teachers" && ( + x.type === "teacher"]} + renderHeader={(total) => ( +
+
router.push("/")} + className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300"> + + Back +
+

Teachers ({total})

+
+ )} + /> + )} {router.asPath === "/#groups" && } {router.asPath === "/#assignments" && ( user.type === "student" && corporateUserGroups.includes(user.id); - const teacherFilter = (user: User) => user.type === "teacher" && corporateUserGroups.includes(user.id); + const studentFilter = (user: User) => user.type === "student"; + const teacherFilter = (user: User) => user.type === "teacher"; const getStatsByStudent = (user: User) => stats.filter((s) => s.user === user.id); const UserDisplay = (displayUser: User) => ( @@ -365,74 +365,7 @@ export default function MasterCorporateDashboard({user}: Props) { ); - const StudentsList = () => { - const filter = (x: User) => - x.type === "student" && (!!selectedUser ? corporateUserGroups.includes(x.id) || false : corporateUserGroups.includes(x.id)); - - return ( - ( -
-
router.push("/")} - className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300"> - - Back -
-

Students ({total})

-
- )} - /> - ); - }; - - const TeachersList = () => { - const filter = (x: User) => - x.type === "teacher" && (!!selectedUser ? corporateUserGroups.includes(x.id) || false : corporateUserGroups.includes(x.id)); - - return ( - ( -
-
router.push("/")} - className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300"> - - Back -
-

Teachers ({total})

-
- )} - /> - ); - }; - - const corporateUserFilter = (x: User) => - x.type === "corporate" && (!!selectedUser ? masterCorporateUserGroups.includes(x.id) || false : masterCorporateUserGroups.includes(x.id)); - - const CorporateList = () => { - return ( - ( -
-
router.push("/")} - className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300"> - - Back -
-

Corporates ({total})

-
- )} - /> - ); - }; + const corporateUserFilter = (x: User) => x.type === "corporate"; const GroupsList = () => { return ( @@ -704,10 +637,58 @@ export default function MasterCorporateDashboard({user}: Props) { )} - {router.asPath === "/#students" && } - {router.asPath === "/#teachers" && } + {router.asPath === "/#students" && ( + x.type === "student"]} + renderHeader={(total) => ( +
+
router.push("/")} + className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300"> + + Back +
+

Students ({total})

+
+ )} + /> + )} + {router.asPath === "/#teachers" && ( + x.type === "teacher"]} + renderHeader={(total) => ( +
+
router.push("/")} + className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300"> + + Back +
+

Teachers ({total})

+
+ )} + /> + )} {router.asPath === "/#groups" && } - {router.asPath === "/#corporate" && } + {router.asPath === "/#corporate" && ( + x.type === "corporate"]} + renderHeader={(total) => ( +
+
router.push("/")} + className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300"> + + Back +
+

Corporate ({total})

+
+ )} + /> + )} {router.asPath === "/#assignments" && ( state.appendUserFilter); const router = useRouter(); const assignmentsGroups = useMemo(() => groups.filter((x) => x.admin === user.id || x.participants.includes(user.id)), [groups, user.id]); @@ -90,7 +92,7 @@ export default function TeacherDashboard({user, linkedCorporate}: Props) { setShowModal(!!selectedUser && router.asPath === "/#"); }, [selectedUser, router.asPath]); - const studentFilter = (user: User) => user.type === "student" && groups.flatMap((g) => g.participants).includes(user.id); + const studentFilter = (user: User) => user.type === "student"; const getStatsByStudent = (user: User) => stats.filter((s) => s.user === user.id); @@ -106,35 +108,6 @@ export default function TeacherDashboard({user, linkedCorporate}: Props) { ); - const StudentsList = () => { - const filter = (x: User) => - x.type === "student" && - (!!selectedUser - ? groups - .filter((g) => g.admin === selectedUser.id) - .flatMap((g) => g.participants) - .includes(x.id) || false - : groups.flatMap((g) => g.participants).includes(x.id)); - - return ( - ( -
-
router.push("/")} - className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300"> - - Back -
-

Students ({total})

-
- )} - /> - ); - }; - const GroupsList = () => { const filter = (x: Group) => x.admin === user.id; @@ -285,16 +258,68 @@ export default function TeacherDashboard({user, linkedCorporate}: Props) { if (shouldReload) reload(); }} onViewStudents={ - selectedUser.type === "corporate" || selectedUser.type === "teacher" ? () => router.push("/#students") : undefined + selectedUser.type === "corporate" || selectedUser.type === "teacher" + ? () => { + appendUserFilters({ + id: "view-students", + filter: (x: User) => x.type === "student", + }); + appendUserFilters({ + id: "belongs-to-admin", + filter: (x: User) => + groups + .filter((g) => g.admin === selectedUser.id || g.participants.includes(selectedUser.id)) + .flatMap((g) => g.participants) + .includes(x.id), + }); + + router.push("/list/users"); + } + : undefined + } + onViewTeachers={ + selectedUser.type === "corporate" || selectedUser.type === "student" + ? () => { + appendUserFilters({ + id: "view-teachers", + filter: (x: User) => x.type === "teacher", + }); + appendUserFilters({ + id: "belongs-to-admin", + filter: (x: User) => + groups + .filter((g) => g.admin === selectedUser.id || g.participants.includes(selectedUser.id)) + .flatMap((g) => g.participants) + .includes(x.id), + }); + + router.push("/list/users"); + } + : undefined } - onViewTeachers={selectedUser.type === "corporate" ? () => router.push("/#teachers") : undefined} user={selectedUser} /> )} - {router.asPath === "/#students" && } + {router.asPath === "/#students" && ( + x.type === "student"]} + renderHeader={(total) => ( +
+
router.push("/")} + className="flex gap-2 items-center text-mti-purple-light cursor-pointer hover:text-mti-purple-dark transition ease-in-out duration-300"> + + Back +
+

Students ({total})

+
+ )} + /> + )} {router.asPath === "/#groups" && } {router.asPath === "/#assignments" && ( { (async () => { - if (user && users) { - const filterUsers = ["corporate", "teacher", "mastercorporate"].includes(user.type) - ? users.filter((u) => groups.flatMap((g) => g.participants).includes(u.id)) - : users; - - const filteredUsers = filters.reduce((d, f) => d.filter(f), filterUsers); + if (users) { + const filteredUsers = filters.reduce((d, f) => d.filter(f), users); const sortedUsers = await asyncSorter(filteredUsers, sortFunction); setDisplayUsers([...sortedUsers]); } })(); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [user, users, sorter, groups]); + }, [users, sorter]); const deleteAccount = (user: User) => { if (!confirm(`Are you sure you want to delete ${user.name}'s account?`)) return; diff --git a/src/pages/api/users/list.ts b/src/pages/api/users/list.ts index ac569e8d..d5e8c93d 100644 --- a/src/pages/api/users/list.ts +++ b/src/pages/api/users/list.ts @@ -4,6 +4,8 @@ import {app} from "@/firebase"; import {getFirestore, collection, getDocs} from "firebase/firestore"; import {withIronSessionApiRoute} from "iron-session/next"; import {sessionOptions} from "@/lib/session"; +import {getGroupsForUser} from "@/utils/groups.be"; +import {uniq} from "lodash"; const db = getFirestore(app); @@ -16,11 +18,17 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { } const snapshot = await getDocs(collection(db, "users")); + const users = snapshot.docs.map((doc) => ({ + id: doc.id, + ...doc.data(), + })); - res.status(200).json( - snapshot.docs.map((doc) => ({ - id: doc.id, - ...doc.data(), - })), - ); + if (!req.session.user) return res.status(200).json(users); + if (req.session.user.type === "admin" || req.session.user.type === "developer") return res.status(200).json(users); + + const adminGroups = await getGroupsForUser(req.session.user.id); + const groups = await Promise.all(adminGroups.flatMap((x) => x.participants).map(async (x) => await getGroupsForUser(x))); + const participants = uniq([...adminGroups.flatMap((x) => x.participants), ...groups.flat().flatMap((x) => x.participants)]); + + res.status(200).json(users.filter((x) => participants.includes(x.id))); }