Solved a problem with the assignees stuff related to the query
This commit is contained in:
@@ -4,34 +4,34 @@ import Layout from "@/components/High/Layout";
|
||||
import Button from "@/components/Low/Button";
|
||||
import Separator from "@/components/Low/Separator";
|
||||
import ProfileSummary from "@/components/ProfileSummary";
|
||||
import {Session} from "@/hooks/useSessions";
|
||||
import {Grading} from "@/interfaces";
|
||||
import {EntityWithRoles} from "@/interfaces/entity";
|
||||
import {Exam} from "@/interfaces/exam";
|
||||
import { Session } from "@/hooks/useSessions";
|
||||
import { Grading } from "@/interfaces";
|
||||
import { EntityWithRoles } from "@/interfaces/entity";
|
||||
import { Exam } from "@/interfaces/exam";
|
||||
import { InviteWithEntity } from "@/interfaces/invite";
|
||||
import {Assignment} from "@/interfaces/results";
|
||||
import {Stat, User} from "@/interfaces/user";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import { Assignment } from "@/interfaces/results";
|
||||
import { Stat, User } from "@/interfaces/user";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import useExamStore from "@/stores/examStore";
|
||||
import {findBy, mapBy, redirect, serialize} from "@/utils";
|
||||
import { findBy, mapBy, redirect, serialize } from "@/utils";
|
||||
import { requestUser } from "@/utils/api";
|
||||
import {activeAssignmentFilter} from "@/utils/assignments";
|
||||
import {getAssignmentsByAssignee} from "@/utils/assignments.be";
|
||||
import {getEntitiesWithRoles} from "@/utils/entities.be";
|
||||
import {getExamsByIds} from "@/utils/exams.be";
|
||||
import {sortByModule} from "@/utils/moduleUtils";
|
||||
import {checkAccess} from "@/utils/permissions";
|
||||
import {getSessionsByUser} from "@/utils/sessions.be";
|
||||
import { activeAssignmentFilter } from "@/utils/assignments";
|
||||
import { getAssignmentsByAssignee } from "@/utils/assignments.be";
|
||||
import { getEntitiesWithRoles } from "@/utils/entities.be";
|
||||
import { getExamsByIds } from "@/utils/exams.be";
|
||||
import { sortByModule } from "@/utils/moduleUtils";
|
||||
import { checkAccess } from "@/utils/permissions";
|
||||
import { getSessionsByUser } from "@/utils/sessions.be";
|
||||
import axios from "axios";
|
||||
import clsx from "clsx";
|
||||
import {withIronSessionSsr} from "iron-session/next";
|
||||
import {uniqBy} from "lodash";
|
||||
import { withIronSessionSsr } from "iron-session/next";
|
||||
import { uniqBy } from "lodash";
|
||||
import moment from "moment";
|
||||
import Head from "next/head";
|
||||
import {useRouter} from "next/router";
|
||||
import { useRouter } from "next/router";
|
||||
import { useMemo, useState } from "react";
|
||||
import {BsArrowRepeat} from "react-icons/bs";
|
||||
import {ToastContainer} from "react-toastify";
|
||||
import { BsArrowRepeat } from "react-icons/bs";
|
||||
import { ToastContainer } from "react-toastify";
|
||||
|
||||
interface Props {
|
||||
user: User;
|
||||
@@ -44,7 +44,7 @@ interface Props {
|
||||
grading: Grading;
|
||||
}
|
||||
|
||||
export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
||||
export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
|
||||
const user = await requestUser(req, res)
|
||||
const destination = Buffer.from(req.url || "/").toString("base64")
|
||||
if (!user) return redirect(`/login?destination=${destination}`)
|
||||
@@ -55,21 +55,21 @@ export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
||||
const entityIDS = mapBy(user.entities, "id") || [];
|
||||
|
||||
const entities = await getEntitiesWithRoles(entityIDS);
|
||||
const assignments = await getAssignmentsByAssignee(user.id, {archived: {$ne: true}});
|
||||
const assignments = await getAssignmentsByAssignee(user.id, { archived: { $ne: true } });
|
||||
const sessions = await getSessionsByUser(user.id, 0, { "assignment.id": { $in: mapBy(assignments, 'id') } });
|
||||
|
||||
const examIDs = uniqBy(
|
||||
assignments.flatMap((a) =>
|
||||
a.exams.filter((e) => e.assignee === user.id).map((e) => ({module: e.module, id: e.id, key: `${e.module}_${e.id}`})),
|
||||
a.exams.filter((e) => e.assignee === user.id).map((e) => ({ module: e.module, id: e.id, key: `${e.module}_${e.id}` })),
|
||||
),
|
||||
"key",
|
||||
);
|
||||
const exams = await getExamsByIds(examIDs);
|
||||
|
||||
return {props: serialize({user, entities, assignments, exams, sessions})};
|
||||
return { props: serialize({ user, entities, assignments, exams, sessions }) };
|
||||
}, sessionOptions);
|
||||
|
||||
export default function OfficialExam({user, entities, assignments, sessions, exams}: Props) {
|
||||
export default function OfficialExam({ user, entities, assignments, sessions, exams }: Props) {
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
|
||||
const router = useRouter();
|
||||
@@ -98,7 +98,7 @@ export default function OfficialExam({user, entities, assignments, sessions, exa
|
||||
};
|
||||
|
||||
const loadSession = async (session: Session) => {
|
||||
state.setShuffles(session.userSolutions.map((x) => ({exerciseID: x.exercise, shuffles: x.shuffleMaps ? x.shuffleMaps : []})));
|
||||
state.setShuffles(session.userSolutions.map((x) => ({ exerciseID: x.exercise, shuffles: x.shuffleMaps ? x.shuffleMaps : [] })));
|
||||
state.setSelectedModules(session.selectedModules);
|
||||
state.setExam(session.exam);
|
||||
state.setExams(session.exams);
|
||||
@@ -115,11 +115,11 @@ export default function OfficialExam({user, entities, assignments, sessions, exa
|
||||
router.push("/exam");
|
||||
};
|
||||
|
||||
const logout = async () => {
|
||||
axios.post("/api/logout").finally(() => {
|
||||
setTimeout(() => router.reload(), 500);
|
||||
});
|
||||
};
|
||||
const logout = async () => {
|
||||
axios.post("/api/logout").finally(() => {
|
||||
setTimeout(() => router.reload(), 500);
|
||||
});
|
||||
};
|
||||
|
||||
const studentAssignments = useMemo(() => assignments.filter(activeAssignmentFilter), [assignments]);
|
||||
const assignmentSessions = useMemo(() => sessions.filter(s => mapBy(studentAssignments, 'id').includes(s.assignment?.id || "")), [sessions, studentAssignments])
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import client from "@/lib/mongodb";
|
||||
import {Assignment} from "@/interfaces/results";
|
||||
import {getAllAssignersByCorporate} from "@/utils/groups.be";
|
||||
import {Type} from "@/interfaces/user";
|
||||
import { Assignment } from "@/interfaces/results";
|
||||
import { getAllAssignersByCorporate } from "@/utils/groups.be";
|
||||
import { Type } from "@/interfaces/user";
|
||||
|
||||
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();
|
||||
@@ -24,39 +24,39 @@ export const getAssignments = async () => {
|
||||
};
|
||||
|
||||
export const getAssignment = async (id: string) => {
|
||||
return await db.collection("assignments").findOne<Assignment>({id});
|
||||
return await db.collection("assignments").findOne<Assignment>({ id });
|
||||
};
|
||||
|
||||
export const getAssignmentsByAssignee = async (id: string, filter?: {[key in keyof Partial<Assignment>]: any}) => {
|
||||
export const getAssignmentsByAssignee = async (id: string, filter?: { [key in keyof Partial<Assignment>]: any }) => {
|
||||
return await db
|
||||
.collection("assignments")
|
||||
.find<Assignment>({assignees: [id], ...(!filter ? {} : filter)})
|
||||
.find<Assignment>({ assignees: id, ...(!filter ? {} : filter) })
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const getAssignmentsByAssignerBetweenDates = async (id: string, startDate: Date, endDate: Date) => {
|
||||
return await db.collection("assignments").find<Assignment>({assigner: id}).toArray();
|
||||
return await db.collection("assignments").find<Assignment>({ assigner: id }).toArray();
|
||||
};
|
||||
|
||||
export const getAssignmentsByAssigners = async (ids: string[], startDate?: Date, endDate?: Date) => {
|
||||
return await db
|
||||
.collection("assignments")
|
||||
.find<Assignment>({
|
||||
assigner: {$in: ids},
|
||||
...(!!startDate ? {startDate: {$gte: startDate.toISOString()}} : {}),
|
||||
...(!!endDate ? {endDate: {$lte: endDate.toISOString()}} : {}),
|
||||
assigner: { $in: ids },
|
||||
...(!!startDate ? { startDate: { $gte: startDate.toISOString() } } : {}),
|
||||
...(!!endDate ? { endDate: { $lte: endDate.toISOString() } } : {}),
|
||||
})
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const getEntityAssignments = async (id: string) => {
|
||||
return await db.collection("assignments").find<Assignment>({entity: id}).toArray();
|
||||
return await db.collection("assignments").find<Assignment>({ entity: id }).toArray();
|
||||
};
|
||||
|
||||
export const getEntitiesAssignments = async (ids: string[]) => {
|
||||
return await db
|
||||
.collection("assignments")
|
||||
.find<Assignment>({entity: {$in: ids}})
|
||||
.find<Assignment>({ entity: { $in: ids } })
|
||||
.toArray();
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user