diff --git a/src/pages/dashboard/mastercorporate.tsx b/src/pages/dashboard/mastercorporate.tsx index 54814d11..229c7ce1 100644 --- a/src/pages/dashboard/mastercorporate.tsx +++ b/src/pages/dashboard/mastercorporate.tsx @@ -2,6 +2,7 @@ import Layout from "@/components/High/Layout"; import UserDisplayList from "@/components/UserDisplayList"; import IconCard from "@/dashboards/IconCard"; +import { useAllowedEntities } from "@/hooks/useEntityPermissions"; import { Module } from "@/interfaces"; import { EntityWithRoles } from "@/interfaces/entity"; import { Assignment } from "@/interfaces/results"; @@ -18,6 +19,7 @@ import { groupByExam } from "@/utils/stats"; import { getStatsByUsers } from "@/utils/stats.be"; import { filterAllowedUsers } from "@/utils/users.be"; import { getEntitiesUsers } from "@/utils/users.be"; +import { clsx } from "clsx"; import { withIronSessionSsr } from "iron-session/next"; import { uniqBy } from "lodash"; import moment from "moment"; @@ -73,30 +75,7 @@ export default function Dashboard({ user, users, entities, assignments, stats, g const router = useRouter(); - const averageLevelCalculator = (studentStats: Stat[]) => { - const formattedStats = studentStats - .map((s) => ({ - focus: students.find((u) => u.id === s.user)?.focus, - score: s.score, - module: s.module, - })) - .filter((f) => !!f.focus); - const bandScores = formattedStats.map((s) => ({ - module: s.module, - level: calculateBandScore(s.score.correct, s.score.total, s.module, s.focus!), - })); - - const levels: { [key in Module]: number } = { - reading: 0, - listening: 0, - writing: 0, - speaking: 0, - level: 0, - }; - bandScores.forEach((b) => (levels[b.module] += b.level)); - - return calculateAverageLevel(levels); - }; + const allowedEntityStatistics = useAllowedEntities(user, entities, 'view_entity_statistics') const UserDisplay = (displayUser: User) => (
@@ -158,17 +137,20 @@ export default function Dashboard({ user, users, entities, assignments, stats, g value={students.length} color="purple" /> - router.push("/statistical")} - label="Entity Statistics" - value={entities.length} - color="purple" - /> + {allowedEntityStatistics.length > 0 && ( + router.push("/statistical")} + label="Entity Statistics" + value={allowedEntityStatistics.length} + color="purple" + /> + )} router.push("/assignments")} label="Assignments" value={assignments.filter((a) => !a.archived).length} + className={clsx(allowedEntityStatistics.length === 0 && "col-span-2")} color="purple" /> { +export const getServerSideProps = withIronSessionSsr(async ({ req, res, params }) => { const user = await requestUser(req, res) if (!user) return redirect("/login") if (shouldRedirectHome(user)) return redirect("/") - const {id, role} = params as {id: string, role: string}; + const { id, role } = params as { id: string, role: string }; if (!mapBy(user.entities, 'id').includes(id) && !["admin", "developer"].includes(user.type)) return redirect("/entities") @@ -130,7 +131,7 @@ interface Props { userCount: number; } -export default function Role({user, entity, role, userCount}: Props) { +export default function Role({ user, entity, role, userCount }: Props) { const [permissions, setPermissions] = useState(role.permissions) const [isLoading, setIsLoading] = useState(false); @@ -148,7 +149,7 @@ export default function Role({user, entity, role, userCount}: Props) { setIsLoading(true); axios - .patch(`/api/roles/${role.id}`, {label}) + .patch(`/api/roles/${role.id}`, { label }) .then(() => { toast.success("The role has been updated successfully!"); router.replace(router.asPath); @@ -185,7 +186,7 @@ export default function Role({user, entity, role, userCount}: Props) { setIsLoading(true); axios - .patch(`/api/roles/${role.id}`, {permissions}) + .patch(`/api/roles/${role.id}`, { permissions }) .then(() => { toast.success("This role has been successfully updated!"); router.replace(router.asPath); @@ -202,7 +203,7 @@ export default function Role({user, entity, role, userCount}: Props) { return ( <> - { role.label } | {entity.label} | EnCoach + {role.label} | {entity.label} | EnCoach -

{role.label} Role ({ userCount } users)

+

{role.label} Role ({userCount} users)

@@ -264,11 +265,11 @@ export default function Role({user, entity, role, userCount}: Props) {
- {USER_MANAGEMENT.map(({label, key}) => ( - togglePermissions(key)}> - { label } - - )) } + {USER_MANAGEMENT.map(({ label, key }) => ( + togglePermissions(key)}> + {label} + + ))}
@@ -284,11 +285,11 @@ export default function Role({user, entity, role, userCount}: Props) {
- {EXAM_MANAGEMENT.map(({label, key}) => ( - togglePermissions(key)}> - { label } - - )) } + {EXAM_MANAGEMENT.map(({ label, key }) => ( + togglePermissions(key)}> + {label} + + ))}
@@ -304,11 +305,11 @@ export default function Role({user, entity, role, userCount}: Props) {
- {CLASSROOM_MANAGEMENT.map(({label, key}) => ( - togglePermissions(key)}> - { label } - - )) } + {CLASSROOM_MANAGEMENT.map(({ label, key }) => ( + togglePermissions(key)}> + {label} + + ))}
@@ -324,11 +325,11 @@ export default function Role({user, entity, role, userCount}: Props) {
- {ENTITY_MANAGEMENT.map(({label, key}) => ( - togglePermissions(key)}> - { label } - - )) } + {ENTITY_MANAGEMENT.map(({ label, key }) => ( + togglePermissions(key)}> + {label} + + ))}
@@ -344,11 +345,11 @@ export default function Role({user, entity, role, userCount}: Props) {
- {ASSIGNMENT_MANAGEMENT.map(({label, key}) => ( - togglePermissions(key)}> - { label } - - )) } + {ASSIGNMENT_MANAGEMENT.map(({ label, key }) => ( + togglePermissions(key)}> + {label} + + ))}
diff --git a/src/pages/statistical.tsx b/src/pages/statistical.tsx index 91eed6b7..0b7b5a6f 100644 --- a/src/pages/statistical.tsx +++ b/src/pages/statistical.tsx @@ -53,6 +53,9 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => { const entityIDS = mapBy(user.entities, "id") || []; const entities = await getEntitiesWithRoles(isAdmin(user) ? undefined : entityIDS); + const allowedEntities = findAllowedEntities(user, entities, 'view_entity_statistics') + + if (allowedEntities.length === 0) return redirect("/") const studentsAllowedEntities = findAllowedEntities(user, entities, 'view_students') const students = await getEntitiesUsers(mapBy(studentsAllowedEntities, 'id'), { type: "student" }) @@ -61,7 +64,7 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => { const sessions = await getSessionsByAssignments(mapBy(assignments, 'id')) const exams = await getExamsByIds(assignments.flatMap(a => a.exams)) - return { props: serialize({ user, students, entities, assignments, sessions, exams }) }; + return { props: serialize({ user, students, entities: allowedEntities, assignments, sessions, exams }) }; }, sessionOptions); interface Item { diff --git a/src/resources/entityPermissions.ts b/src/resources/entityPermissions.ts index 5d431895..e4ea3fce 100644 --- a/src/resources/entityPermissions.ts +++ b/src/resources/entityPermissions.ts @@ -48,7 +48,8 @@ export type RolePermission = "edit_assignment" | "delete_assignment" | "start_assignment" | - "archive_assignment" + "archive_assignment" | + "view_entity_statistics" export const DEFAULT_PERMISSIONS: RolePermission[] = [ "view_students", @@ -109,4 +110,5 @@ export const ADMIN_PERMISSIONS: RolePermission[] = [ "delete_assignment", "start_assignment", "archive_assignment", + "view_entity_statistics" ]