Updated the exam selection to get exams related to the user's topic preference
This commit is contained in:
@@ -69,7 +69,7 @@ export interface UserSolution {
|
|||||||
export interface WritingExam {
|
export interface WritingExam {
|
||||||
module: "writing";
|
module: "writing";
|
||||||
id: string;
|
id: string;
|
||||||
exercises: Exercise[];
|
exercises: WritingExercise[];
|
||||||
minTimer: number;
|
minTimer: number;
|
||||||
isDiagnostic: boolean;
|
isDiagnostic: boolean;
|
||||||
variant?: Variant;
|
variant?: Variant;
|
||||||
@@ -84,7 +84,7 @@ interface WordCounter {
|
|||||||
export interface SpeakingExam {
|
export interface SpeakingExam {
|
||||||
id: string;
|
id: string;
|
||||||
module: "speaking";
|
module: "speaking";
|
||||||
exercises: Exercise[];
|
exercises: (SpeakingExercise | InteractiveSpeakingExercise)[];
|
||||||
minTimer: number;
|
minTimer: number;
|
||||||
isDiagnostic: boolean;
|
isDiagnostic: boolean;
|
||||||
variant?: Variant;
|
variant?: Variant;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {collection, getDocs, query, where, setDoc, doc, Firestore, getDoc} from "firebase/firestore";
|
import {collection, getDocs, query, where, setDoc, doc, Firestore, getDoc} from "firebase/firestore";
|
||||||
import {shuffle} from "lodash";
|
import {shuffle} from "lodash";
|
||||||
import {Difficulty, Exam, InstructorGender, Variant} from "@/interfaces/exam";
|
import {Difficulty, Exam, InstructorGender, SpeakingExam, Variant, WritingExam} from "@/interfaces/exam";
|
||||||
import {Stat, User} from "@/interfaces/user";
|
import {DeveloperUser, Stat, StudentUser, User} from "@/interfaces/user";
|
||||||
import {Module} from "@/interfaces";
|
import {Module} from "@/interfaces";
|
||||||
|
|
||||||
export const getExams = async (
|
export const getExams = async (
|
||||||
@@ -28,9 +28,10 @@ export const getExams = async (
|
|||||||
})),
|
})),
|
||||||
) as Exam[];
|
) as Exam[];
|
||||||
|
|
||||||
const variantExams: Exam[] = filterByVariant(allExams, variant);
|
let exams: Exam[] = filterByVariant(allExams, variant);
|
||||||
const genderedExams: Exam[] = filterByInstructorGender(variantExams, instructorGender);
|
exams = filterByInstructorGender(exams, instructorGender);
|
||||||
const difficultyExams: Exam[] = await filterByDifficulty(db, genderedExams, module, userId);
|
exams = await filterByDifficulty(db, exams, module, userId);
|
||||||
|
exams = await filterByPreference(db, exams, module, userId);
|
||||||
|
|
||||||
if (avoidRepeated === "true") {
|
if (avoidRepeated === "true") {
|
||||||
const statsQ = query(collection(db, "stats"), where("user", "==", userId));
|
const statsQ = query(collection(db, "stats"), where("user", "==", userId));
|
||||||
@@ -40,12 +41,12 @@ export const getExams = async (
|
|||||||
id: doc.id,
|
id: doc.id,
|
||||||
...doc.data(),
|
...doc.data(),
|
||||||
})) as unknown as Stat[];
|
})) as unknown as Stat[];
|
||||||
const filteredExams = difficultyExams.filter((x) => !stats.map((s) => s.exam).includes(x.id));
|
const filteredExams = exams.filter((x) => !stats.map((s) => s.exam).includes(x.id));
|
||||||
|
|
||||||
return filteredExams.length > 0 ? filteredExams : difficultyExams;
|
return filteredExams.length > 0 ? filteredExams : exams;
|
||||||
}
|
}
|
||||||
|
|
||||||
return difficultyExams;
|
return exams;
|
||||||
};
|
};
|
||||||
|
|
||||||
const filterByInstructorGender = (exams: Exam[], instructorGender?: InstructorGender) => {
|
const filterByInstructorGender = (exams: Exam[], instructorGender?: InstructorGender) => {
|
||||||
@@ -70,3 +71,25 @@ const filterByDifficulty = async (db: Firestore, exams: Exam[], module: Module,
|
|||||||
const filteredExams = exams.filter((exam) => exam.difficulty === difficulty);
|
const filteredExams = exams.filter((exam) => exam.difficulty === difficulty);
|
||||||
return filteredExams.length === 0 ? exams : filteredExams;
|
return filteredExams.length === 0 ? exams : filteredExams;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const filterByPreference = async (db: Firestore, exams: Exam[], module: Module, userID?: string) => {
|
||||||
|
if (!["speaking", "writing"].includes(module)) return exams;
|
||||||
|
|
||||||
|
if (!userID) return exams;
|
||||||
|
const userRef = await getDoc(doc(db, "users", userID));
|
||||||
|
if (!userRef.exists()) return exams;
|
||||||
|
|
||||||
|
const user = {...userRef.data(), id: userRef.id} as StudentUser | DeveloperUser;
|
||||||
|
if (!["developer", "student"].includes(user.type)) return exams;
|
||||||
|
if (!user.preferredTopics || user.preferredTopics.length === 0) return exams;
|
||||||
|
|
||||||
|
const userTopics = user.preferredTopics;
|
||||||
|
const topicalExams = exams.filter((e) => {
|
||||||
|
const exam = e as WritingExam | SpeakingExam;
|
||||||
|
const topics = exam.exercises.map((x) => x.topic).filter((x) => !!x) as string[];
|
||||||
|
|
||||||
|
return topics.some((topic) => userTopics.map((x) => x.toLowerCase()).includes(topic.toLowerCase()));
|
||||||
|
});
|
||||||
|
|
||||||
|
return topicalExams.length > 0 ? shuffle(topicalExams) : exams;
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user