Refactor most getServerProps to fetch independent request in parallel and projected the data only to return the necessary fields and changed some functions
This commit is contained in:
@@ -2,21 +2,16 @@
|
||||
import UserDisplayList from "@/components/UserDisplayList";
|
||||
import IconCard from "@/components/IconCard";
|
||||
import { EntityWithRoles } from "@/interfaces/entity";
|
||||
import { Stat, Type, User } from "@/interfaces/user";
|
||||
import { Stat, Type, User } from "@/interfaces/user";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { filterBy, mapBy, redirect, serialize } from "@/utils";
|
||||
import { requestUser } from "@/utils/api";
|
||||
import {
|
||||
countEntitiesAssignments,
|
||||
} from "@/utils/assignments.be";
|
||||
import { countEntitiesAssignments } from "@/utils/assignments.be";
|
||||
import { getEntities } from "@/utils/entities.be";
|
||||
import { countGroups } from "@/utils/groups.be";
|
||||
import { checkAccess } from "@/utils/permissions";
|
||||
import { groupByExam } from "@/utils/stats";
|
||||
import {
|
||||
countUsersByTypes,
|
||||
getUsers,
|
||||
} from "@/utils/users.be";
|
||||
import { countUsersByTypes, getUsers } from "@/utils/users.be";
|
||||
import { withIronSessionSsr } from "iron-session/next";
|
||||
import Head from "next/head";
|
||||
import { useRouter } from "next/router";
|
||||
@@ -49,45 +44,41 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
|
||||
|
||||
if (!checkAccess(user, ["admin", "developer"])) return redirect("/");
|
||||
|
||||
const students = await getUsers(
|
||||
{ type: "student" },
|
||||
10,
|
||||
{
|
||||
averageLevel: -1,
|
||||
},
|
||||
{ id: 1, name: 1, email: 1, profilePicture: 1 }
|
||||
);
|
||||
|
||||
const usersCount = await countUsersByTypes([
|
||||
"student",
|
||||
"teacher",
|
||||
"corporate",
|
||||
"mastercorporate",
|
||||
const [
|
||||
students,
|
||||
latestStudents,
|
||||
latestTeachers,
|
||||
usersCount,
|
||||
entities,
|
||||
groupsCount,
|
||||
] = await Promise.all([
|
||||
getUsers(
|
||||
{ type: "student" },
|
||||
10,
|
||||
{ averageLevel: -1 },
|
||||
{ _id: 0, id: 1, name: 1, email: 1, profilePicture: 1 }
|
||||
),
|
||||
getUsers(
|
||||
{ type: "student" },
|
||||
10,
|
||||
{ registrationDate: -1 },
|
||||
{ _id: 0, id: 1, name: 1, email: 1, profilePicture: 1 }
|
||||
),
|
||||
getUsers(
|
||||
{ type: "teacher" },
|
||||
10,
|
||||
{ registrationDate: -1 },
|
||||
{ _id: 0, id: 1, name: 1, email: 1, profilePicture: 1 }
|
||||
),
|
||||
countUsersByTypes(["student", "teacher", "corporate", "mastercorporate"]),
|
||||
getEntities(undefined, { _id: 0, id: 1, label: 1 }),
|
||||
countGroups(),
|
||||
]);
|
||||
|
||||
const latestStudents = await getUsers(
|
||||
{ type: "student" },
|
||||
10,
|
||||
{
|
||||
registrationDate: -1,
|
||||
},
|
||||
{id:1, name: 1, email: 1, profilePicture: 1 }
|
||||
);
|
||||
const latestTeachers = await getUsers(
|
||||
{ type: "teacher" },
|
||||
10,
|
||||
{
|
||||
registrationDate: -1,
|
||||
},
|
||||
{ id:1,name: 1, email: 1, profilePicture: 1 }
|
||||
);
|
||||
|
||||
const entities = await getEntities(undefined, { _id: 0, id: 1, label: 1 });
|
||||
const assignmentsCount = await countEntitiesAssignments(
|
||||
mapBy(entities, "id"),
|
||||
{ archived: { $ne: true } }
|
||||
);
|
||||
const groupsCount = await countGroups();
|
||||
|
||||
return {
|
||||
props: serialize({
|
||||
|
||||
Reference in New Issue
Block a user