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:
@@ -19,8 +19,8 @@ export const getAssignmentsByAssigner = async (id: string, startDate?: Date, end
|
||||
return await db.collection("assignments").find<Assignment>(query).toArray();
|
||||
};
|
||||
|
||||
export const getAssignments = async () => {
|
||||
return await db.collection("assignments").find<Assignment>({}).toArray();
|
||||
export const getAssignments = async (projection = {}) => {
|
||||
return await db.collection("assignments").find<Assignment>({}, { projection }).toArray();
|
||||
};
|
||||
|
||||
export const getAssignment = async (id: string) => {
|
||||
|
||||
@@ -9,7 +9,6 @@ const db = client.db(process.env.MONGODB_DB);
|
||||
export const getEntityWithRoles = async (id: string): Promise<EntityWithRoles | undefined> => {
|
||||
const entity = await getEntity(id);
|
||||
if (!entity) return undefined;
|
||||
|
||||
const roles = await getRolesByEntity(id);
|
||||
return { ...entity, roles };
|
||||
};
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { collection, getDocs, query, where, setDoc, doc, Firestore, getDoc, and } from "firebase/firestore";
|
||||
import { groupBy, shuffle } from "lodash";
|
||||
import { CEFRLevels, Difficulty, Exam, InstructorGender, SpeakingExam, Variant, WritingExam } from "@/interfaces/exam";
|
||||
import { CEFRLevels, Exam, InstructorGender, SpeakingExam, Variant, WritingExam } from "@/interfaces/exam";
|
||||
import { DeveloperUser, Stat, StudentUser, User } from "@/interfaces/user";
|
||||
import { Module } from "@/interfaces";
|
||||
import { getCorporateUser } from "@/resources/user";
|
||||
import { getUserCorporate } from "./groups.be";
|
||||
import { Db, ObjectId } from "mongodb";
|
||||
import { Db } from "mongodb";
|
||||
import client from "@/lib/mongodb";
|
||||
import { MODULE_ARRAY } from "./moduleUtils";
|
||||
import { mapBy } from ".";
|
||||
|
||||
@@ -3,10 +3,10 @@ import client from "@/lib/mongodb";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export const getSessionsByUser = async (id: string, limit = 0, filter = {}) =>
|
||||
export const getSessionsByUser = async (id: string, limit = 0, filter = {}, projection = {}) =>
|
||||
await db
|
||||
.collection("sessions")
|
||||
.find<Session>({ user: id, ...filter })
|
||||
.find<Session>({ user: id, ...filter }, { projection })
|
||||
.limit(limit || 0)
|
||||
.toArray();
|
||||
|
||||
|
||||
@@ -129,19 +129,19 @@ export async function getUser(id: string, projection = {}): Promise<User | undef
|
||||
return !!user ? user : undefined;
|
||||
}
|
||||
|
||||
export async function getSpecificUsers(ids: string[]) {
|
||||
export async function getSpecificUsers(ids: string[], projection = {}) {
|
||||
if (ids.length === 0) return [];
|
||||
|
||||
return await db
|
||||
.collection("users")
|
||||
.find<User>({ id: { $in: ids } }, { projection: { _id: 0 } })
|
||||
.find<User>({ id: { $in: ids } }, { projection: { _id: 0, ...projection } })
|
||||
.toArray();
|
||||
}
|
||||
|
||||
export async function getEntityUsers(id: string, limit?: number, filter?: object) {
|
||||
export async function getEntityUsers(id: string, limit?: number, filter?: object, projection = {}) {
|
||||
return await db
|
||||
.collection("users")
|
||||
.find<User>({ "entities.id": id, ...(filter || {}) })
|
||||
.find<User>({ "entities.id": id, ...(filter || {}) }, { projection: { _id: 0, ...projection } })
|
||||
.limit(limit || 0)
|
||||
.toArray();
|
||||
}
|
||||
@@ -231,8 +231,8 @@ export async function getUserBalance(user: User) {
|
||||
);
|
||||
}
|
||||
|
||||
export const filterAllowedUsers = async (user: User, entities: EntityWithRoles[]) => {
|
||||
|
||||
export const filterAllowedUsers = async (user: User, entities: EntityWithRoles[], projection = {}) => {
|
||||
|
||||
const {
|
||||
["view_students"]: allowedStudentEntities,
|
||||
["view_teachers"]: allowedTeacherEntities,
|
||||
@@ -244,12 +244,12 @@ export const filterAllowedUsers = async (user: User, entities: EntityWithRoles[]
|
||||
'view_corporates',
|
||||
'view_mastercorporates',
|
||||
]);
|
||||
|
||||
|
||||
const students = await getEntitiesUsers(mapBy(allowedStudentEntities, 'id'), { type: "student" })
|
||||
const teachers = await getEntitiesUsers(mapBy(allowedTeacherEntities, 'id'), { type: "teacher" })
|
||||
const corporates = await getEntitiesUsers(mapBy(allowedCorporateEntities, 'id'), { type: "corporate" })
|
||||
const masterCorporates = await getEntitiesUsers(mapBy(allowedMasterCorporateEntities, 'id'), { type: "mastercorporate" })
|
||||
const [students, teachers, corporates, masterCorporates] = await Promise.all([
|
||||
getEntitiesUsers(mapBy(allowedStudentEntities, 'id'), { type: "student" }, 0, projection),
|
||||
getEntitiesUsers(mapBy(allowedTeacherEntities, 'id'), { type: "teacher" }, 0, projection),
|
||||
getEntitiesUsers(mapBy(allowedCorporateEntities, 'id'), { type: "corporate" }, 0, projection),
|
||||
getEntitiesUsers(mapBy(allowedMasterCorporateEntities, 'id'), { type: "mastercorporate" }, 0, projection),
|
||||
])
|
||||
|
||||
return [...students, ...teachers, ...corporates, ...masterCorporates]
|
||||
}
|
||||
@@ -266,11 +266,12 @@ export const countAllowedUsers = async (user: User, entities: EntityWithRoles[])
|
||||
'view_corporates',
|
||||
'view_mastercorporates',
|
||||
]);
|
||||
|
||||
const student = await countEntitiesUsers(mapBy(allowedStudentEntities, 'id'), { type: "student" })
|
||||
const teacher = await countEntitiesUsers(mapBy(allowedTeacherEntities, 'id'), { type: "teacher" })
|
||||
const corporate = await countEntitiesUsers(mapBy(allowedCorporateEntities, 'id'), { type: "corporate" })
|
||||
const mastercorporate = await countEntitiesUsers(mapBy(allowedMasterCorporateEntities, 'id'), { type: "mastercorporate" })
|
||||
const [student, teacher, corporate, mastercorporate] = await Promise.all([
|
||||
countEntitiesUsers(mapBy(allowedStudentEntities, 'id'), { type: "student" }),
|
||||
countEntitiesUsers(mapBy(allowedTeacherEntities, 'id'), { type: "teacher" }),
|
||||
countEntitiesUsers(mapBy(allowedCorporateEntities, 'id'), { type: "corporate" }),
|
||||
countEntitiesUsers(mapBy(allowedMasterCorporateEntities, 'id'), { type: "mastercorporate" }),
|
||||
])
|
||||
|
||||
return { student, teacher, corporate, mastercorporate }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user