Updated the assignments active filter to work with the startDate

This commit is contained in:
Tiago Ribeiro
2024-11-07 12:08:28 +00:00
parent 0325bb68f5
commit fae7b729fe
3 changed files with 64 additions and 52 deletions

View File

@@ -1,13 +1,13 @@
import {collection, getDocs, query, where, setDoc, doc, Firestore, getDoc, and} from "firebase/firestore";
import {groupBy, shuffle} from "lodash";
import {Difficulty, 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 { collection, getDocs, query, where, setDoc, doc, Firestore, getDoc, and } from "firebase/firestore";
import { groupBy, shuffle } from "lodash";
import { Difficulty, 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 client from "@/lib/mongodb";
import {MODULE_ARRAY} from "./moduleUtils";
import { MODULE_ARRAY } from "./moduleUtils";
import { mapBy } from ".";
const db = client.db(process.env.MONGODB_DB);
@@ -21,7 +21,7 @@ export async function getSpecificExams(ids: string[]) {
async (module) =>
await db
.collection(module)
.find<Exam>({id: {$in: ids}})
.find<Exam>({ id: { $in: ids } })
.toArray(),
),
)
@@ -30,7 +30,11 @@ export async function getSpecificExams(ids: string[]) {
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 exams: Exam[] = (
await Promise.all(
@@ -38,7 +42,7 @@ export const getExamsByIds = async (ids: {module: Module; id: string}[]) => {
async (m) =>
await db
.collection(m)
.find<Exam>({id: {$in: mapBy(groupedByModule[m], 'id')}})
.find<Exam>({ id: { $in: mapBy(groupedByModule[m], 'id') } })
.toArray(),
),
)
@@ -121,7 +125,7 @@ const filterByOwners = async (exams: Exam[], userID?: string) => {
const filterByDifficulty = async (db: Db, exams: Exam[], module: Module, userID?: string) => {
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;
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;
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 (!["developer", "student"].includes(user.type)) return exams;