Solved some race conditions related to the previous commit

This commit is contained in:
Tiago Ribeiro
2024-09-03 17:00:42 +01:00
parent e8c47941d0
commit 67929655f4

View File

@@ -19,16 +19,18 @@ export const getExams = async (
): Promise<Exam[]> => { ): Promise<Exam[]> => {
const moduleRef = collection(db, module); const moduleRef = collection(db, module);
const q = query(moduleRef, and(where("isDiagnostic", "==", false), where("private", "!=", true))); const q = query(moduleRef, where("isDiagnostic", "==", false));
const snapshot = await getDocs(q); const snapshot = await getDocs(q);
const allExams = shuffle( const allExams = (
snapshot.docs.map((doc) => ({ shuffle(
id: doc.id, snapshot.docs.map((doc) => ({
...doc.data(), id: doc.id,
module, ...doc.data(),
})), module,
) as Exam[]; })),
) as Exam[]
).filter((x) => !x.private);
let exams: Exam[] = await filterByOwners(allExams, userId); let exams: Exam[] = await filterByOwners(allExams, userId);
exams = filterByVariant(exams, variant); exams = filterByVariant(exams, variant);
@@ -65,7 +67,7 @@ const filterByVariant = (exams: Exam[], variant?: Variant) => {
const filterByOwners = async (exams: Exam[], userID?: string) => { const filterByOwners = async (exams: Exam[], userID?: string) => {
if (!userID) return exams.filter((x) => !x.owners || x.owners.length === 0); if (!userID) return exams.filter((x) => !x.owners || x.owners.length === 0);
return Promise.all( return await Promise.all(
exams.filter(async (x) => { exams.filter(async (x) => {
if (!x.owners) return true; if (!x.owners) return true;
if (x.owners.length === 0) return true; if (x.owners.length === 0) return true;