Updated the assignments active filter to work with the startDate
This commit is contained in:
@@ -18,26 +18,39 @@ import ExamEditor from "@/components/ExamEditor";
|
|||||||
import MultipleAudioUploader from "@/components/ExamEditor/Shared/AudioEdit";
|
import MultipleAudioUploader from "@/components/ExamEditor/Shared/AudioEdit";
|
||||||
import { redirect, serialize } from "@/utils";
|
import { redirect, serialize } from "@/utils";
|
||||||
import { requestUser } from "@/utils/api";
|
import { requestUser } from "@/utils/api";
|
||||||
|
import { Module } from "@/interfaces";
|
||||||
|
import { getExam, getExams } from "@/utils/exams.be";
|
||||||
|
import { Exam } from "@/interfaces/exam";
|
||||||
|
import { useEffect } from "react";
|
||||||
|
|
||||||
export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
|
export const getServerSideProps = withIronSessionSsr(async ({ req, res, query }) => {
|
||||||
const user = await requestUser(req, res)
|
const user = await requestUser(req, res)
|
||||||
if (!user) return redirect("/login")
|
if (!user) return redirect("/login")
|
||||||
|
|
||||||
if (shouldRedirectHome(user) || !checkAccess(user, ["admin", "mastercorporate", "developer", "corporate"]))
|
if (shouldRedirectHome(user) || !checkAccess(user, ["admin", "mastercorporate", "developer", "corporate"]))
|
||||||
return redirect("/")
|
return redirect("/")
|
||||||
|
|
||||||
|
const { id, module } = query as { id?: string, module?: Module }
|
||||||
|
if (!id || !module) return { props: serialize({ user }) };
|
||||||
|
|
||||||
|
const exam = await getExam(module, id)
|
||||||
|
if (!exam) return redirect("/generation")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: serialize({user}),
|
props: serialize({ user, exam }),
|
||||||
};
|
};
|
||||||
}, sessionOptions);
|
}, sessionOptions);
|
||||||
|
|
||||||
export default function Generation({ user }: { user: User; }) {
|
export default function Generation({ user, exam }: { user: User; exam?: Exam }) {
|
||||||
const { title, currentModule, dispatch } = useExamEditorStore();
|
const { title, currentModule, dispatch } = useExamEditorStore();
|
||||||
|
|
||||||
const updateRoot = (updates: Partial<ExamEditorStore>) => {
|
const updateRoot = (updates: Partial<ExamEditorStore>) => {
|
||||||
dispatch({ type: 'UPDATE_ROOT', payload: { updates } });
|
dispatch({ type: 'UPDATE_ROOT', payload: { updates } });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (exam) { }
|
||||||
|
}, [exam])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -60,7 +73,7 @@ export default function Generation({ user }: { user: User; }) {
|
|||||||
placeholder="Insert a title here"
|
placeholder="Insert a title here"
|
||||||
name="title"
|
name="title"
|
||||||
label="Title"
|
label="Title"
|
||||||
onChange={(title) => updateRoot({title})}
|
onChange={(title) => updateRoot({ title })}
|
||||||
roundness="xl"
|
roundness="xl"
|
||||||
defaultValue={title}
|
defaultValue={title}
|
||||||
required
|
required
|
||||||
@@ -69,7 +82,7 @@ export default function Generation({ user }: { user: User; }) {
|
|||||||
<label className="font-normal text-base text-mti-gray-dim">Module</label>
|
<label className="font-normal text-base text-mti-gray-dim">Module</label>
|
||||||
<RadioGroup
|
<RadioGroup
|
||||||
value={currentModule}
|
value={currentModule}
|
||||||
onChange={(currentModule) => updateRoot({currentModule})}
|
onChange={(currentModule) => updateRoot({ currentModule })}
|
||||||
className="flex flex-row -2xl:flex-wrap w-full gap-4 -md:justify-center justify-between">
|
className="flex flex-row -2xl:flex-wrap w-full gap-4 -md:justify-center justify-between">
|
||||||
{[...MODULE_ARRAY].map((x) => (
|
{[...MODULE_ARRAY].map((x) => (
|
||||||
<Radio value={x} key={x}>
|
<Radio value={x} key={x}>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import {Assignment} from "@/interfaces/results";
|
import { Assignment } from "@/interfaces/results";
|
||||||
|
|
||||||
// export const futureAssignmentFilter = (a: Assignment) => {
|
// export const futureAssignmentFilter = (a: Assignment) => {
|
||||||
// if(a.archived) return false;
|
// if(a.archived) return false;
|
||||||
@@ -15,43 +15,38 @@ import {Assignment} from "@/interfaces/results";
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
export const futureAssignmentFilter = (a: Assignment) => {
|
export const futureAssignmentFilter = (a: Assignment) => {
|
||||||
const currentDate = moment();
|
const currentDate = moment();
|
||||||
if(moment(a.endDate).isBefore(currentDate)) return false;
|
if (moment(a.endDate).isBefore(currentDate)) return false;
|
||||||
if(a.archived) return false;
|
if (a.archived) return false;
|
||||||
|
|
||||||
if(a.autoStart && a.autoStartDate && moment(a.autoStartDate).isBefore(currentDate)) return false;
|
if (a.autoStart && a.autoStartDate && moment(a.autoStartDate).isBefore(currentDate)) return false;
|
||||||
|
|
||||||
if(!a.start) {
|
if (!a.start) {
|
||||||
if(moment(a.startDate).isBefore(currentDate)) return false;
|
if (moment(a.startDate).isBefore(currentDate)) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const pastAssignmentFilter = (a: Assignment) => {
|
export const pastAssignmentFilter = (a: Assignment) => {
|
||||||
const currentDate = moment();
|
const currentDate = moment();
|
||||||
if(a.archived) {
|
if (a.archived) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return moment(a.endDate).isBefore(currentDate);
|
return moment(a.endDate).isBefore(currentDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const archivedAssignmentFilter = (a: Assignment) => a.archived;
|
export const archivedAssignmentFilter = (a: Assignment) => a.archived;
|
||||||
|
|
||||||
export const activeAssignmentFilter = (a: Assignment) => {
|
export const activeAssignmentFilter = (a: Assignment) => {
|
||||||
const currentDate = moment();
|
const currentDate = moment();
|
||||||
if(moment(a.endDate).isBefore(currentDate)) return false;
|
if (moment(a.endDate).isBefore(currentDate) || a.archived) return false;
|
||||||
if(a.archived) return false;
|
|
||||||
|
|
||||||
if(a.start) return true;
|
if (a.start) return true;
|
||||||
|
if (a.autoStart && a.autoStartDate) return moment(a.autoStartDate).isBefore(currentDate);
|
||||||
|
|
||||||
if(a.autoStart && a.autoStartDate) {
|
return currentDate.isAfter(moment(a.startDate));
|
||||||
return moment(a.autoStartDate).isBefore(currentDate);
|
|
||||||
}
|
|
||||||
|
|
||||||
// if(currentDate.isAfter(moment(a.startDate))) return true;
|
|
||||||
return false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// export const unstartedAssignmentFilter = (a: Assignment) => {
|
// export const unstartedAssignmentFilter = (a: Assignment) => {
|
||||||
@@ -69,9 +64,9 @@ export const activeAssignmentFilter = (a: Assignment) => {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
export const startHasExpiredAssignmentFilter = (a: Assignment) => {
|
export const startHasExpiredAssignmentFilter = (a: Assignment) => {
|
||||||
const currentDate = moment();
|
const currentDate = moment();
|
||||||
if(a.archived) return false;
|
if (a.archived) return false;
|
||||||
if(a.start) return false;
|
if (a.start) return false;
|
||||||
if(currentDate.isAfter(moment(a.startDate)) && currentDate.isBefore(moment(a.endDate))) return true;
|
if (currentDate.isAfter(moment(a.startDate)) && currentDate.isBefore(moment(a.endDate))) return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
import {collection, getDocs, query, where, setDoc, doc, Firestore, getDoc, and} from "firebase/firestore";
|
import { collection, getDocs, query, where, setDoc, doc, Firestore, getDoc, and } from "firebase/firestore";
|
||||||
import {groupBy, shuffle} from "lodash";
|
import { groupBy, shuffle } from "lodash";
|
||||||
import {Difficulty, Exam, InstructorGender, SpeakingExam, Variant, WritingExam} from "@/interfaces/exam";
|
import { Difficulty, Exam, InstructorGender, SpeakingExam, Variant, WritingExam } from "@/interfaces/exam";
|
||||||
import {DeveloperUser, Stat, StudentUser, User} from "@/interfaces/user";
|
import { DeveloperUser, Stat, StudentUser, User } from "@/interfaces/user";
|
||||||
import {Module} from "@/interfaces";
|
import { Module } from "@/interfaces";
|
||||||
import {getCorporateUser} from "@/resources/user";
|
import { getCorporateUser } from "@/resources/user";
|
||||||
import {getUserCorporate} from "./groups.be";
|
import { getUserCorporate } from "./groups.be";
|
||||||
import {Db, ObjectId} from "mongodb";
|
import { Db, ObjectId } from "mongodb";
|
||||||
import client from "@/lib/mongodb";
|
import client from "@/lib/mongodb";
|
||||||
import {MODULE_ARRAY} from "./moduleUtils";
|
import { MODULE_ARRAY } from "./moduleUtils";
|
||||||
import { mapBy } from ".";
|
import { mapBy } from ".";
|
||||||
|
|
||||||
const db = client.db(process.env.MONGODB_DB);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
@@ -21,7 +21,7 @@ export async function getSpecificExams(ids: string[]) {
|
|||||||
async (module) =>
|
async (module) =>
|
||||||
await db
|
await db
|
||||||
.collection(module)
|
.collection(module)
|
||||||
.find<Exam>({id: {$in: ids}})
|
.find<Exam>({ id: { $in: ids } })
|
||||||
.toArray(),
|
.toArray(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -30,7 +30,11 @@ export async function getSpecificExams(ids: string[]) {
|
|||||||
return exams;
|
return exams;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getExamsByIds = async (ids: {module: Module; id: string}[]) => {
|
export const getExam = async (module: Module, id: string) => {
|
||||||
|
return await db.collection(module).findOne<Exam>({ id }) ?? undefined
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getExamsByIds = async (ids: { module: Module; id: string }[]) => {
|
||||||
const groupedByModule = groupBy(ids, "module");
|
const groupedByModule = groupBy(ids, "module");
|
||||||
const exams: Exam[] = (
|
const exams: Exam[] = (
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
@@ -38,7 +42,7 @@ export const getExamsByIds = async (ids: {module: Module; id: string}[]) => {
|
|||||||
async (m) =>
|
async (m) =>
|
||||||
await db
|
await db
|
||||||
.collection(m)
|
.collection(m)
|
||||||
.find<Exam>({id: {$in: mapBy(groupedByModule[m], 'id')}})
|
.find<Exam>({ id: { $in: mapBy(groupedByModule[m], 'id') } })
|
||||||
.toArray(),
|
.toArray(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -121,7 +125,7 @@ const filterByOwners = async (exams: Exam[], userID?: string) => {
|
|||||||
|
|
||||||
const filterByDifficulty = async (db: Db, exams: Exam[], module: Module, userID?: string) => {
|
const filterByDifficulty = async (db: Db, exams: Exam[], module: Module, userID?: string) => {
|
||||||
if (!userID) return exams;
|
if (!userID) return exams;
|
||||||
const user = await db.collection("users").findOne<User>({id: userID});
|
const user = await db.collection("users").findOne<User>({ id: userID });
|
||||||
if (!user) return exams;
|
if (!user) return exams;
|
||||||
|
|
||||||
const difficulty = user.levels[module] <= 3 ? "easy" : user.levels[module] <= 6 ? "medium" : "hard";
|
const difficulty = user.levels[module] <= 3 ? "easy" : user.levels[module] <= 6 ? "medium" : "hard";
|
||||||
@@ -135,7 +139,7 @@ const filterByPreference = async (db: Db, exams: Exam[], module: Module, userID?
|
|||||||
|
|
||||||
if (!userID) return exams;
|
if (!userID) return exams;
|
||||||
|
|
||||||
const user = await db.collection("users").findOne<StudentUser | DeveloperUser>({id: userID});
|
const user = await db.collection("users").findOne<StudentUser | DeveloperUser>({ id: userID });
|
||||||
if (!user) return exams;
|
if (!user) return exams;
|
||||||
|
|
||||||
if (!["developer", "student"].includes(user.type)) return exams;
|
if (!["developer", "student"].includes(user.type)) return exams;
|
||||||
|
|||||||
Reference in New Issue
Block a user