Improved some of the querying for the assignments

This commit is contained in:
Tiago Ribeiro
2024-09-09 00:02:34 +01:00
parent 9f0ba418e5
commit 1c61d50a5c
4 changed files with 52 additions and 69 deletions

View File

@@ -1,18 +1,18 @@
import client from "@/lib/mongodb";
import { Assignment } from "@/interfaces/results";
import { getAllAssignersByCorporate } from "@/utils/groups.be";
import {Assignment} from "@/interfaces/results";
import {getAllAssignersByCorporate} from "@/utils/groups.be";
const db = client.db(process.env.MONGODB_DB);
export const getAssignmentsByAssigner = async (id: string, startDate?: Date, endDate?: Date) => {
let query: any = { assigner: id };
let query: any = {assigner: id};
if (startDate) {
query.startDate = { $gte: startDate.toISOString() };
query.startDate = {$gte: startDate.toISOString()};
}
if (endDate) {
query.endDate = { $lte: endDate.toISOString() };
query.endDate = {$lte: endDate.toISOString()};
}
return await db.collection("assignments").find<Assignment>(query).toArray();
@@ -26,7 +26,14 @@ export const getAssignmentsByAssignerBetweenDates = async (id: string, startDate
};
export const getAssignmentsByAssigners = async (ids: string[], startDate?: Date, endDate?: Date) => {
return (await Promise.all(ids.map((id) => getAssignmentsByAssigner(id, startDate, endDate)))).flat();
return await db
.collection("assignments")
.find<Assignment>({
assigner: {$in: ids},
...(!!startDate ? {startDate: {$gte: startDate.toISOString()}} : {}),
...(!!endDate ? {endDate: {$lte: endDate.toISOString()}} : {}),
})
.toArray();
};
export const getAssignmentsForCorporates = async (idsList: string[], startDate?: Date, endDate?: Date) => {
@@ -57,4 +64,4 @@ export const getAssignmentsForCorporates = async (idsList: string[], startDate?:
);
return assignments.flat();
}
};

View File

@@ -1,8 +1,9 @@
import {app} from "@/firebase";
import {Assignment} from "@/interfaces/results";
import {CorporateUser, Group, MasterCorporateUser, StudentUser, TeacherUser, User} from "@/interfaces/user";
import client from "@/lib/mongodb";
import moment from "moment";
import {getUser} from "./users.be";
import {getLinkedUsers, getUser} from "./users.be";
import {getSpecificUsers} from "./users.be";
const db = client.db(process.env.MONGODB_DB);
@@ -71,17 +72,10 @@ export const getUsersGroups = async (ids: string[]) => {
};
export const getAllAssignersByCorporate = async (corporateID: string): Promise<string[]> => {
const groups = await getUserGroups(corporateID);
const groupUsers = (await Promise.all(groups.map(async (g) => await Promise.all(g.participants.map(getUser)))))
.flat()
.filter((x) => !!x) as User[];
const teacherPromises = await Promise.all(
groupUsers.map(async (u) =>
u.type === "teacher" ? u.id : u.type === "corporate" ? [...(await getAllAssignersByCorporate(u.id)), u.id] : undefined,
),
);
const linkedTeachers = await getLinkedUsers(corporateID, "mastercorporate", "teacher");
const linkedCorporates = await getLinkedUsers(corporateID, "mastercorporate", "corporate");
return teacherPromises.filter((x) => !!x).flat() as string[];
return [...linkedTeachers.users.map((x) => x.id), ...linkedCorporates.users.map((x) => x.id)];
};
export const getGroupsForUser = async (admin?: string, participant?: string) => {